This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
79 lines (66 loc) · 1.52 KB
/
main.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
72
73
74
75
76
77
78
79
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/url"
"os"
"github.com/tomhjp/gh-action-jira/config"
"github.com/tomhjp/gh-action-jira/gha"
"github.com/tomhjp/gh-action-jira/jira"
)
func main() {
err := search()
if err != nil {
log.Fatal(err)
}
}
func search() error {
jql := os.Getenv("INPUT_JQL")
if jql == "" {
return errors.New("no jql query provided as input")
}
config, err := config.ReadConfig()
if err != nil {
return err
}
issueKeys, err := findIssueKeys(config, jql)
if err != nil {
return err
}
if len(issueKeys) == 0 {
fmt.Println("Successfully queried API but did not find any issues")
return nil
} else if len(issueKeys) > 1 {
return errors.New("jql does not uniquely identify an issue")
}
key := issueKeys[0]
fmt.Printf("Found issue %s\n", key)
if err := gha.SetOutput("key", key); err != nil {
return err
}
return nil
}
type searchResponse struct {
Issues []struct {
Key string `json:"key"`
} `json:"issues"`
}
func findIssueKeys(config config.JiraConfig, jql string) ([]string, error) {
query := url.Values{
"jql": {jql},
"fields": {"summary"}, // Specify fields summary purely to minimise the size of all the unused fields in the response.
}
respBody, err := jira.DoRequest(config, "GET", "/rest/api/3/search", query, nil)
if err != nil {
return nil, err
}
var response searchResponse
json.Unmarshal(respBody, &response)
result := []string{}
for _, issue := range response.Issues {
result = append(result, issue.Key)
}
return result, nil
}