-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgql.go
137 lines (116 loc) · 2.77 KB
/
gql.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"net/http"
"strings"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
func githubPRs(repo string) ([]PullRequest, error) {
flag.Parse()
repoParts := strings.Split(repo, "/")
orgName := repoParts[0]
repoName := repoParts[1]
changelogQuery := fmt.Sprintf(queryFmt, orgName, repoName)
var changelog ChangelogResponse
if err := queryGithubGQL(changelogQuery, &changelog); err != nil {
return nil, errors.Wrap(err, "failed to query Github GQL")
}
return changelog.PullRequests(), nil
}
const queryFmt = `{
"query": "query {
repository(owner: \"%s\", name: \"%s\") {
pullRequests(states: MERGED, last: 100) {
nodes {
number,
title,
labels(first: 10) {
nodes {
name
}
}
author {
login
}
}
}
}
}"
}`
type PullRequestType int
const (
UNKNOWN PullRequestType = iota
FEATURE
FIX
)
type PullRequest struct {
Number int `json:"number,omitempty"`
Title string `json:"title,omitempty"`
Labels struct {
Nodes []struct {
Name string `json:"name,omitempty"`
} `json:"nodes,omitempty"`
} `json:"labels,omitempty"`
Author struct {
Login string `json:"login,omitempty"`
} `json:"author,omitempty"`
}
func (pr PullRequest) LabelNames() []string {
var labels []string
for _, lbl := range pr.Labels.Nodes {
labels = append(labels, lbl.Name)
}
return labels
}
func (pr PullRequest) Type() PullRequestType {
for _, lbl := range pr.LabelNames() {
if lbl == "feature" {
return FEATURE
}
if lbl == "fix" || lbl == "bug" {
return FIX
}
}
return UNKNOWN
}
type ChangelogResponse struct {
Data struct {
Repository struct {
PullRequests struct {
Nodes []PullRequest `json:"nodes,omitempty"`
} `json:"pullRequests,omitempty"`
} `json:"repository,omitempty"`
} `json:"data,omitempty"`
}
func (cr ChangelogResponse) PullRequests() []PullRequest {
return cr.Data.Repository.PullRequests.Nodes
}
func queryGithubGQL(query string, response interface{}) error {
bearer := fmt.Sprintf("bearer %s", viper.GetString("git-token"))
var queryBytes = []byte(toOneLine(query))
req, _ := http.NewRequest("POST", "https://api.github.com/graphql", bytes.NewBuffer(queryBytes))
req.Header.Set("Authorization", bearer)
c := http.Client{}
resp, err := c.Do(req)
if err != nil {
return errors.Wrap(err, "failed to query Github GQL")
}
if resp.StatusCode == http.StatusUnauthorized {
return errors.New("unauthorized")
}
if err = json.NewDecoder(resp.Body).Decode(&response); err != nil {
return errors.Wrap(err, "failed to decode Github GQL response")
}
return nil
}
func toOneLine(q string) string {
parts := strings.Split(q, "\n")
for i := range parts {
parts[i] = strings.TrimSpace(parts[i])
}
return strings.Join(parts, " ")
}