@@ -9,60 +9,102 @@ import (
99 "context"
1010 "fmt"
1111 "os"
12+ "strconv"
1213 "strings"
1314
1415 "github.com/cockroachdb/errors"
1516 "github.com/google/go-github/v61/github"
1617 "golang.org/x/oauth2"
1718)
1819
19- // CommentTag is used to identify an existing comment.
20- const CommentTag = "<!-- microbench-ci -->"
20+ type GitHubConfig struct {
21+ client * github.Client
22+ Owner string
23+ Repo string
24+ PrNum int
25+ }
26+
27+ const (
28+ CommentTag = "<!-- microbench-ci -->"
29+ PerfLabel = "X-perf-check"
30+ )
2131
22- // post posts or updates a comment on a GitHub PR, with the given summary text,
23- // that has the CommentTag marker in it.
24- func post (summaryText , githubRepository string , prNumber int ) error {
32+ // NewGithubConfig creates a new GitHubConfig from the environment variables.
33+ func NewGithubConfig () (* GitHubConfig , error ) {
2534 token := os .Getenv ("GITHUB_TOKEN" )
2635 if token == "" {
27- return errors .New ("GITHUB_TOKEN is not set, this command is meant to be run in a GitHub Action " )
36+ return nil , errors .New ("GITHUB_TOKEN is not set" )
2837 }
2938
30- repoInfo := strings .Split (githubRepository , "/" )
31- if len (repoInfo ) != 2 {
32- return errors .New ("invalid GitHub repository flag" )
33- }
34- owner := repoInfo [0 ]
35- repo := repoInfo [1 ]
36-
3739 ctx := context .Background ()
3840 ts := oauth2 .StaticTokenSource (& oauth2.Token {AccessToken : token })
3941 tc := oauth2 .NewClient (ctx , ts )
4042 client := github .NewClient (tc )
4143
42- // Check for an existing comment
43- comments , _ , err := client .Issues .ListComments (ctx , owner , repo , prNumber , nil )
44+ githubRepository := os .Getenv ("GITHUB_REPO" )
45+ if githubRepository == "" {
46+ return nil , errors .New ("GITHUB_REPO is not set" )
47+ }
48+ prNumber := os .Getenv ("GITHUB_PR_NUMBER" )
49+ if prNumber == "" {
50+ return nil , errors .New ("GITHUB_PR_NUMBER is not set" )
51+ }
52+ prNumberInt , err := strconv .Atoi (prNumber )
4453 if err != nil {
45- return err
54+ return nil , errors . Newf ( "invalid GITHUB_PR_NUMBER format %s" , prNumber )
4655 }
47- var existingComment * github.IssueComment
48- for _ , comment := range comments {
49- if strings .Contains (comment .GetBody (), CommentTag ) {
50- existingComment = comment
51- break
52- }
56+ repoInfo := strings .Split (githubRepository , "/" )
57+ if len (repoInfo ) != 2 {
58+ return nil , errors .Newf ("invalid GitHub repository flag, %s" , githubRepository )
5359 }
60+ owner := repoInfo [0 ]
61+ repo := repoInfo [1 ]
62+
63+ return & GitHubConfig {
64+ client : client ,
65+ Owner : owner ,
66+ Repo : repo ,
67+ PrNum : prNumberInt ,
68+ }, nil
69+ }
5470
71+ // postComment posts a comment on a GitHub PR, with the given summary text, that
72+ // has the CommentTag marker in it.
73+ func (c * GitHubConfig ) postComment (summaryText string ) error {
74+ ctx := context .Background ()
5575 commentBody := github .String (fmt .Sprintf ("%s\n %s" , summaryText , CommentTag ))
56- if existingComment != nil {
57- // Update the existing comment
58- existingComment .Body = commentBody
59- _ , _ , err = client .Issues .EditComment (ctx , owner , repo , existingComment .GetID (), existingComment )
60- return err
61- }
76+ _ , _ , err := c .client .Issues .CreateComment (
77+ ctx , c .Owner , c .Repo , c .PrNum ,
78+ & github.IssueComment {
79+ Body : commentBody ,
80+ },
81+ )
82+ return err
83+ }
6284
63- // Create a new comment
64- _ , _ , err = client .Issues .CreateComment (ctx , owner , repo , prNumber , & github.IssueComment {
65- Body : commentBody ,
66- })
85+ // addLabel adds a label to a GitHub PR.
86+ func (c * GitHubConfig ) addLabel (label string ) error {
87+ ctx := context .Background ()
88+ _ , _ , err := c .client .Issues .AddLabelsToIssue (
89+ ctx , c .Owner , c .Repo , c .PrNum ,
90+ []string {label },
91+ )
6792 return err
6893}
94+
95+ // hasLabel checks if a GitHub PR has a label.
96+ func (c * GitHubConfig ) hasLabel (label string ) (bool , error ) {
97+ ctx := context .Background ()
98+ labels , _ , err := c .client .Issues .ListLabelsByIssue (
99+ ctx , c .Owner , c .Repo , c .PrNum , & github.ListOptions {PerPage : 100 },
100+ )
101+ if err != nil {
102+ return false , err
103+ }
104+ for _ , l := range labels {
105+ if * l .Name == label {
106+ return true , nil
107+ }
108+ }
109+ return false , nil
110+ }
0 commit comments