-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpull_request.go
More file actions
159 lines (147 loc) · 4.99 KB
/
pull_request.go
File metadata and controls
159 lines (147 loc) · 4.99 KB
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
159
package github
import (
"context"
"fmt"
"log/slog"
"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
ExistingPullRequests []ExistingPullRequest
ReviewerUserNodeID githubv4.ID // optional
}
type ExistingPullRequest struct {
URL string
}
// 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 []ExistingPullRequest
} `graphql:"associatedPullRequests(baseRefName: $baseRefName, states: [OPEN], 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 != ""),
}
slog.Debug("Querying the existing pull request", "params", v)
if err := c.Client.Query(ctx, &q, v); err != nil {
return nil, fmt.Errorf("GitHub API error: %w", err)
}
slog.Debug("Got the response", "response", q)
out := QueryForPullRequestOutput{
CurrentUserName: q.Viewer.Login,
BaseRepositoryNodeID: q.BaseRepository.ID,
HeadBranchCommitSHA: git.CommitSHA(q.HeadRepository.Ref.Target.OID),
ExistingPullRequests: q.HeadRepository.Ref.AssociatedPullRequests.Nodes,
ReviewerUserNodeID: q.ReviewerUser.ID,
}
slog.Debug("Returning the result", "result", 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) {
slog.Debug("Creating a pull request", "input", 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)
}
slog.Debug("Got the response", "response", 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 {
slog.Debug("Requesting a review for the pull request", "pullRequest", in.PullRequest, "user", in.User)
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)
}
slog.Debug("Got the response", "response", m)
return nil
}