cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/reporter/github.go
106lines · modecode
| 1 | package reporter |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "sort" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/google/go-github/v37/github" |
| 10 | "github.com/rs/zerolog/log" |
| 11 | "golang.org/x/oauth2" |
| 12 | |
| 13 | "github.com/cloudflare/pint/internal/git" |
| 14 | ) |
| 15 | |
| 16 | type GithubReporter struct { |
| 17 | baseURL string |
| 18 | uploadURL string |
| 19 | timeout time.Duration |
| 20 | authToken string |
| 21 | owner string |
| 22 | repo string |
| 23 | prNum int |
| 24 | gitCmd git.CommandRunner |
| 25 | } |
| 26 | |
| 27 | // NewGithubReporter creates a new GitHub reporter that reports |
| 28 | // problems via comments on a given pull request number (integer). |
| 29 | func NewGithubReporter(baseURL, uploadURL string, timeout time.Duration, token, owner, repo string, prNum int, gitCmd git.CommandRunner) GithubReporter { |
| 30 | return GithubReporter{ |
| 31 | baseURL: baseURL, |
| 32 | uploadURL: uploadURL, |
| 33 | timeout: timeout, |
| 34 | authToken: token, |
| 35 | owner: owner, |
| 36 | repo: repo, |
| 37 | prNum: prNum, |
| 38 | gitCmd: gitCmd, |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // Submit submits the summary to GitHub. |
| 43 | func (gr GithubReporter) Submit(summary Summary) error { |
| 44 | ctx, cancel := context.WithTimeout(context.Background(), gr.timeout) |
| 45 | defer cancel() |
| 46 | |
| 47 | ts := oauth2.StaticTokenSource( |
| 48 | &oauth2.Token{AccessToken: gr.authToken}, |
| 49 | ) |
| 50 | tc := oauth2.NewClient(ctx, ts) |
| 51 | |
| 52 | var client *github.Client |
| 53 | |
| 54 | if gr.uploadURL != "" && gr.baseURL != "" { |
| 55 | ec, err := github.NewEnterpriseClient(gr.baseURL, gr.uploadURL, tc) |
| 56 | if err != nil { |
| 57 | return fmt.Errorf("creating new GitHub client: %w", err) |
| 58 | } |
| 59 | client = ec |
| 60 | } else { |
| 61 | client = github.NewClient(tc) |
| 62 | } |
| 63 | |
| 64 | comments := []*github.DraftReviewComment{} |
| 65 | for _, rep := range summary.Reports() { |
| 66 | rep := rep |
| 67 | |
| 68 | if len(rep.ModifiedLines) == 0 { |
| 69 | continue |
| 70 | } |
| 71 | |
| 72 | var comment *github.DraftReviewComment |
| 73 | |
| 74 | if len(rep.ModifiedLines) == 1 { |
| 75 | comment = &github.DraftReviewComment{ |
| 76 | Path: github.String(rep.ReportedPath), |
| 77 | Body: github.String(rep.Problem.Text), |
| 78 | Line: github.Int(rep.ModifiedLines[0]), |
| 79 | } |
| 80 | } else if len(rep.ModifiedLines) > 1 { |
| 81 | sort.Ints(rep.ModifiedLines) |
| 82 | start, end := rep.ModifiedLines[0], rep.ModifiedLines[len(rep.ModifiedLines)-1] |
| 83 | comment = &github.DraftReviewComment{ |
| 84 | Path: github.String(rep.ReportedPath), |
| 85 | Body: github.String(rep.Problem.Text), |
| 86 | Line: github.Int(end), |
| 87 | StartLine: github.Int(start), |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | comments = append(comments, comment) |
| 92 | } |
| 93 | |
| 94 | if len(comments) > 0 { |
| 95 | _, resp, err := client.PullRequests.CreateReview(ctx, gr.owner, gr.repo, gr.prNum, &github.PullRequestReviewRequest{ |
| 96 | Event: github.String("COMMENT"), |
| 97 | Comments: comments, |
| 98 | }) |
| 99 | if err != nil { |
| 100 | return fmt.Errorf("creating review: %w", err) |
| 101 | } |
| 102 | log.Info().Str("status", resp.Status).Msg("Report submitted") |
| 103 | } |
| 104 | |
| 105 | return nil |
| 106 | } |
| 107 | |