-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.go
85 lines (72 loc) · 2.07 KB
/
github.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
package main
import (
"context"
"fmt"
"github.com/google/go-github/v51/github"
"golang.org/x/oauth2"
"io"
"strconv"
"strings"
)
// postComment posts c as a comment on PR prNumber which is extracted from ref or returns an error
func postComment(c, repoOwner, repo, ref string) error {
x := strings.Split(ref, "/")
if len(x) < 3 {
return fmt.Errorf("unable to extract PR number from ref %q", ref)
}
prNumber, err := strconv.Atoi(x[2])
if err != nil {
return err
}
ctx := context.Background()
client := github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: githubToken},
)))
comment := &github.IssueComment{
Body: github.String(c),
}
comment, _, err = client.Issues.CreateComment(ctx, repoOwner, repo, prNumber, comment)
if err != nil {
return err
}
return nil
}
func createAndSubmitReview(c, repoOwner, repo, ref string, comments []*github.DraftReviewComment) error {
x := strings.Split(ref, "/")
if len(x) < 3 {
return fmt.Errorf("unable to extract PR number from ref %q", ref)
}
prNumber, err := strconv.Atoi(x[2])
if err != nil {
return err
}
ctx := context.Background()
client := github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: githubToken},
)))
reviewReq := &github.PullRequestReviewRequest{
Body: &c,
Event: github.String("REQUEST_CHANGES"),
Comments: comments,
}
review, resp, err := client.PullRequests.CreateReview(ctx, repoOwner, repo, prNumber, reviewReq)
if err != nil {
body, exx := io.ReadAll(resp.Body)
if exx != nil {
fmt.Printf("problem reading body: %s", err)
}
fmt.Printf("problem creating code review: %s\nBody: %s", err, body)
return err
}
submittedReview, resp, err := client.PullRequests.SubmitReview(ctx, repoOwner, repo, prNumber, *review.ID, reviewReq)
if err != nil {
body, exx := io.ReadAll(resp.Body)
if exx != nil {
fmt.Printf("problem reading body: %s", err)
}
fmt.Printf("problem submitting code review: %s\nBody: %s", err, body)
return err
}
fmt.Printf("%#v submitted", *submittedReview)
return nil
}