generated from actions/container-action
-
Notifications
You must be signed in to change notification settings - Fork 10
/
pull_request.go
75 lines (60 loc) · 1.88 KB
/
pull_request.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
/*
Package pullrequest checks pull requests for specified labels
*/
package pullrequest
import (
"context"
"github.com/agilepathway/label-checker/internal/error/panic"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)
// PullRequest encapsulates a GitHub Pull Request
type PullRequest struct {
repositoryOwner string
repository string
number int
Labels Labels
}
// New creates a new PullRequest
func New(repoOwner string, repo string, prNumber int, ghToken string, enterpriseEndpoint string) *PullRequest {
pr := new(PullRequest)
pr.repositoryOwner = repoOwner
pr.repository = repo
pr.number = prNumber
pr.Labels = pr.initLabels(apiClient(ghToken, enterpriseEndpoint))
return pr
}
func (pr PullRequest) initLabels(apiClient *githubv4.Client) Labels {
variables := map[string]interface{}{
"owner": githubv4.String(pr.repositoryOwner),
"name": githubv4.String(pr.repository),
"pullRequestNumber": githubv4.Int(pr.number),
}
var query struct {
Repository struct {
PullRequest struct {
Labels struct {
Nodes []struct {
Name string
}
} `graphql:"labels(first: 100)"`
} `graphql:"pullRequest(number: $pullRequestNumber)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
err := apiClient.Query(context.Background(), &query, variables)
panic.IfError(err)
labelNodes := query.Repository.PullRequest.Labels.Nodes
var labels Labels
for i := 0; i < len(labelNodes); i++ {
labels = append(labels, labelNodes[i].Name)
}
return labels
}
func apiClient(token string, enterpriseEndpoint string) *githubv4.Client {
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
httpClient := oauth2.NewClient(context.Background(), tokenSource)
if enterpriseEndpoint != "" {
return githubv4.NewEnterpriseClient(enterpriseEndpoint, httpClient)
}
return githubv4.NewClient(httpClient)
}