-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpullrequest.go
120 lines (103 loc) · 2.88 KB
/
pullrequest.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
package main
import (
"context"
"fmt"
"strings"
"github.com/shurcooL/githubv4"
)
type Github struct {
client *githubv4.Client
}
func (g *Github) CommentPR(ctx context.Context, prnumber int, owner, name, body, signature string) error {
body = body + "\n <sub>Sign: " + signature + "</sub>"
type Comment struct {
ID githubv4.ID
Body string
}
var prComments struct {
Repository struct {
PullRequest struct {
ID githubv4.ID
Comments struct {
Nodes []Comment
PageInfo struct {
EndCursor githubv4.String
HasNextPage bool
}
} `graphql:"comments(first: 100, after: $commentsCursor)"`
} `graphql:"pullRequest(number: $prnumber)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
variables := map[string]interface{}{
"owner": githubv4.String(owner),
"name": githubv4.String(name),
"prnumber": githubv4.Int(prnumber),
"commentsCursor": (*githubv4.String)(nil),
}
var comment *Comment
LOOP:
for {
err := g.client.Query(ctx, &prComments, variables)
if err != nil {
return fmt.Errorf("unable to query PR: %v", err)
}
for _, c := range prComments.Repository.PullRequest.Comments.Nodes {
if strings.Contains(c.Body, signature) {
comment = &c
break LOOP
}
}
if !prComments.Repository.PullRequest.Comments.PageInfo.HasNextPage {
break
}
variables["commentsCursor"] = githubv4.NewString(prComments.Repository.PullRequest.Comments.PageInfo.EndCursor)
}
if comment == nil {
if err := g.addComment(ctx, prComments.Repository.PullRequest.ID, body); err != nil {
return fmt.Errorf("unable to generate new comment on PR: %v", err)
}
return nil
}
if err := g.updateComment(ctx, body, comment.ID); err != nil {
return fmt.Errorf("unable to update comment on PR: %v", err)
}
return nil
}
func (g *Github) updateComment(ctx context.Context, body string, commentID githubv4.ID) error {
fmt.Println("Updating comment")
var updateIssueComment struct {
UpdateIssueComment struct {
IssueComment struct {
ID githubv4.ID
}
} `graphql:"updateIssueComment(input: $input)"`
}
updateIssueCommentInput := githubv4.UpdateIssueCommentInput{
ID: commentID,
Body: githubv4.String(body),
}
err := g.client.Mutate(ctx, &updateIssueComment, updateIssueCommentInput, nil)
if err != nil {
return fmt.Errorf("unable to update comment in PR: %v", err)
}
return nil
}
func (g *Github) addComment(ctx context.Context, prID githubv4.ID, body string) error {
fmt.Println("Creating new comment")
addCommentInput := githubv4.AddCommentInput{
SubjectID: prID,
Body: githubv4.String(body),
}
var addComment struct {
AddComment struct {
Subject struct {
ID githubv4.ID
}
} `graphql:"addComment(input: $input)"`
}
err := g.client.Mutate(ctx, &addComment, addCommentInput, nil)
if err != nil {
return fmt.Errorf("unable to comment in PR: %v", err)
}
return nil
}