/
pull_request.go
158 lines (147 loc) · 5.01 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package github
import (
"context"
"fmt"
"github.com/int128/ghcp/pkg/git"
"github.com/shurcooL/githubv4"
)
type QueryForPullRequestInput struct {
BaseRepository git.RepositoryID
BaseBranchName git.BranchName
HeadRepository git.RepositoryID
HeadBranchName git.BranchName
ReviewerUser string // optional
}
type QueryForPullRequestOutput struct {
CurrentUserName string
BaseRepositoryNodeID InternalRepositoryNodeID
HeadBranchCommitSHA git.CommitSHA
PullRequestURL string // URL of the pull request associated to the head branch, if exists
ReviewerUserNodeID githubv4.ID // optional
}
// QueryForPullRequest performs the query for creating a pull request.
func (c *GitHub) QueryForPullRequest(ctx context.Context, in QueryForPullRequestInput) (*QueryForPullRequestOutput, error) {
var q struct {
Viewer struct {
Login string
}
BaseRepository struct {
ID githubv4.ID
} `graphql:"baseRepository: repository(owner: $baseOwner, name: $baseRepo)"`
HeadRepository struct {
Ref struct {
Target struct {
OID string
}
AssociatedPullRequests struct {
Nodes []struct {
URL string
}
} `graphql:"associatedPullRequests(baseRefName: $baseRefName, first: 1)"`
} `graphql:"ref(qualifiedName: $headRefName)"`
} `graphql:"headRepository: repository(owner: $headOwner, name: $headRepo)"`
ReviewerUser struct {
ID githubv4.ID
} `graphql:"reviewer: user(login: $reviewerUser) @include(if: $withReviewerUser)"`
}
v := map[string]interface{}{
"baseOwner": githubv4.String(in.BaseRepository.Owner),
"baseRepo": githubv4.String(in.BaseRepository.Name),
"baseRefName": githubv4.String(in.BaseBranchName),
"headOwner": githubv4.String(in.HeadRepository.Owner),
"headRepo": githubv4.String(in.HeadRepository.Name),
"headRefName": githubv4.String(in.HeadBranchName.QualifiedName().String()),
"reviewerUser": githubv4.String(in.ReviewerUser),
"withReviewerUser": githubv4.Boolean(in.ReviewerUser != ""),
}
c.Logger.Debugf("Querying the existing pull request with %+v", v)
if err := c.Client.Query(ctx, &q, v); err != nil {
return nil, fmt.Errorf("GitHub API error: %w", err)
}
c.Logger.Debugf("Got the result: %+v", q)
out := QueryForPullRequestOutput{
CurrentUserName: q.Viewer.Login,
BaseRepositoryNodeID: q.BaseRepository.ID,
HeadBranchCommitSHA: git.CommitSHA(q.HeadRepository.Ref.Target.OID),
ReviewerUserNodeID: q.ReviewerUser.ID,
}
if len(q.HeadRepository.Ref.AssociatedPullRequests.Nodes) > 0 {
out.PullRequestURL = q.HeadRepository.Ref.AssociatedPullRequests.Nodes[0].URL
}
c.Logger.Debugf("Returning the result: %+v", out)
return &out, nil
}
type CreatePullRequestInput struct {
BaseRepository git.RepositoryID
BaseBranchName git.BranchName
BaseRepositoryNodeID InternalRepositoryNodeID
HeadRepository git.RepositoryID
HeadBranchName git.BranchName
Title string
Body string // optional
Draft bool
}
type CreatePullRequestOutput struct {
PullRequestNodeID githubv4.ID
URL string
}
func (c *GitHub) CreatePullRequest(ctx context.Context, in CreatePullRequestInput) (*CreatePullRequestOutput, error) {
c.Logger.Debugf("Creating a pull request %+v", in)
headRefName := string(in.HeadBranchName)
if in.BaseRepository != in.HeadRepository {
// For cross-repository pull requests.
// https://developer.github.com/v4/input_object/createpullrequestinput/
headRefName = fmt.Sprintf("%s:%s", in.HeadRepository.Owner, in.HeadBranchName)
}
v := githubv4.CreatePullRequestInput{
RepositoryID: in.BaseRepositoryNodeID,
BaseRefName: githubv4.String(in.BaseBranchName),
HeadRefName: githubv4.String(headRefName),
Title: githubv4.String(in.Title),
}
if in.Body != "" {
v.Body = githubv4.NewString(githubv4.String(in.Body))
}
if in.Draft {
v.Draft = githubv4.NewBoolean(true)
}
var m struct {
CreatePullRequest struct {
PullRequest struct {
ID githubv4.ID
URL string
}
} `graphql:"createPullRequest(input: $input)"`
}
if err := c.Client.Mutate(ctx, &m, v, nil); err != nil {
return nil, fmt.Errorf("GitHub API error: %w", err)
}
c.Logger.Debugf("Got the result: %+v", m)
return &CreatePullRequestOutput{
PullRequestNodeID: m.CreatePullRequest.PullRequest.ID,
URL: m.CreatePullRequest.PullRequest.URL,
}, nil
}
type RequestPullRequestReviewInput struct {
PullRequest githubv4.ID
User githubv4.ID
}
func (c *GitHub) RequestPullRequestReview(ctx context.Context, in RequestPullRequestReviewInput) error {
c.Logger.Debugf("Requesting a review for the pull request %+v", in)
v := githubv4.RequestReviewsInput{
PullRequestID: in.PullRequest,
UserIDs: &[]githubv4.ID{in.User},
}
var m struct {
RequestReviews struct {
Actor struct {
Login string
}
} `graphql:"requestReviews(input: $input)"`
}
if err := c.Client.Mutate(ctx, &m, v, nil); err != nil {
return fmt.Errorf("GitHub API error: %w", err)
}
c.Logger.Debugf("Got the result: %+v", m)
return nil
}