/
done.go
71 lines (60 loc) · 1.57 KB
/
done.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package done
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/030/jops/internal/pkg/httprequest"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
)
func (j *Jira) transitionDoneID() (string, error) {
url := j.FQDN + "/rest/api/2/issue/" + j.TicketNumber + "/transitions"
log.Info(url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
req.SetBasicAuth(j.User, j.Pass)
req.Header.Set("Accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
log.Info(resp.StatusCode)
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
bodyString := string(bodyBytes)
log.Info(bodyString)
value := gjson.Get(bodyString, "transitions.#(name==\"Done\").id")
id := value.String()
if id == "" {
return "", fmt.Errorf("transition id Done should not be empty")
}
return id, nil
}
func (j *Jira) Done() error {
tdi, err := j.transitionDoneID()
if err != nil {
return err
}
log.Infof("ticketnumber: '%s' and transitionID: '%s'", j.TicketNumber, tdi)
add := Add{Body: j.Comment}
comment := []Comment{{add}}
update := Update{Comment: comment}
fields := Fields{Resolution: Resolution{"Done"}}
data := Payload{
Fields: fields,
Transition: Transition{tdi},
Update: update,
}
uri := "/" + j.TicketNumber + "/transitions"
htj := httprequest.Jira{URI: uri, APIVersion: "2", Data: data, Method: "POST", FQDN: j.FQDN, User: j.User, Pass: j.Pass}
_, err = htj.ConstructAndInitiate()
if err != nil {
return err
}
return nil
}