andygrunwald / go-jira

Go client library for Atlassian Jira
https://pkg.go.dev/github.com/andygrunwald/go-jira?tab=doc
MIT License
1.44k stars 458 forks source link

Cannot read jira.Response body #468

Open mike-lloyd03 opened 1 year ago

mike-lloyd03 commented 1 year ago

Describe the bug

I am abstracting the process of transitioning a Jira issue to a different state. When attempting to transition an issue from Backlog to Ready, the Jira web app raises an error telling me I need to define the story points. I'd like to handle this error when automating the process. I have the following function:

func (j *JiraIssue) UpdateStatus(statusId JiraStatus) error {
    possibleTransitions, _, _ := jiraClient.Issue.GetTransitions(j.JiraKey)
    for _, v := range possibleTransitions {
        log.Debugf("Available transition: %s %s \n", v.To.ID, v.Name)
        if v.To.ID == string(statusId) {
            resp, err := jiraClient.Issue.DoTransition(j.JiraKey, string(statusId))
            body, read_err := io.ReadAll(resp.Body)
            if read_err != nil {
                return read_err
            }
            if err != nil {
                return fmt.Errorf("%s: %v", err, string(body))
            }
            return nil
        }
    }
    return fmt.Errorf("requested status change %v is not available for this issue", statusId)
}

I am trying to read the response body if there is an error transitioning the issue. But I get the following error when trying to do so:

http: read on closed response body

How can I read the error response from the API?

Expected behavior

The jira.Response returned from DoTransition should be readable.

Your Environment

Include as many relevant details about the environment you experienced the problem in

Additional context

I'd like to be able to dynamically handle error responses that come from the API. Or at least be able to print a more helpful error message when a request to transition the issue fails.

Thanks!

vladimir-ch commented 1 year ago

In case of an error, go-jira reads and decodes the response body and returns it as an error, see the code for DoTransitionWithPayloadWithContext and NewJiraError.

andygrunwald commented 1 year ago

Hey,

I am very sorry that this issue has been open for a long time with no final solution. We work on this project in our spare time, and sometimes, other priorities take over. This is the typical open source dilemma.

However, there is news: We are kicking off v2 of this library 🚀

To provide visibility, we created the Road to v2 Milestone and calling for your feedback in https://github.com/andygrunwald/go-jira/issues/489

The development will take some time; however, I hope you can benefit from the changes. If you seek priority development for your issue + you like to sponsor it, please contact me.

What does this mean for my issue?

We will work on this issue indirectly. This means that during the development phase, we aim to tackle it. Maybe in a different way like it is currently handled. Please understand that this will take a while because we are running this in our spare time.

Final words

Thanks for using this library. If there is anything else you would like to tell us, let us know!