cloudflare/pint

Public

mirrored from https://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.39.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

internal/reporter/github.go

118lines · modecode

1package reporter
2
3import (
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/checks"
14 "github.com/cloudflare/pint/internal/git"
15)
16
17type GithubReporter struct {
18 baseURL string
19 uploadURL string
20 timeout time.Duration
21 authToken string
22 owner string
23 repo string
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).
29func NewGithubReporter(baseURL, uploadURL string, timeout time.Duration, token, owner, repo string, 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 gitCmd: gitCmd,
38 }
39}
40
41// Submit submits the summary to GitHub.
42func (gr GithubReporter) Submit(summary Summary) error {
43 headCommit, err := git.HeadCommit(gr.gitCmd)
44 if err != nil {
45 return fmt.Errorf("failed to get HEAD commit: %w", err)
46 }
47 log.Info().Str("commit", headCommit).Msg("Got HEAD commit from git")
48
49 ctx, cancel := context.WithTimeout(context.Background(), gr.timeout)
50 defer cancel()
51
52 ts := oauth2.StaticTokenSource(
53 &oauth2.Token{AccessToken: gr.authToken},
54 )
55 tc := oauth2.NewClient(ctx, ts)
56
57 var client *github.Client
58
59 if gr.uploadURL != "" && gr.baseURL != "" {
60 client, err = github.NewEnterpriseClient(gr.baseURL, gr.uploadURL, tc)
61 if err != nil {
62 return fmt.Errorf("failed to create a new GitHub client: %w", err)
63 }
64 } else {
65 client = github.NewClient(tc)
66 }
67
68 conclusion := "success"
69 comments := []*github.CheckRunAnnotation{}
70 for _, rep := range summary.Reports() {
71 rep := rep
72
73 var level string
74 switch rep.Problem.Severity {
75 case checks.Fatal, checks.Bug:
76 level = "failure"
77 conclusion = "failure"
78 case checks.Warning:
79 level = "warning"
80 case checks.Information:
81 level = "notice"
82 }
83
84 var comment *github.CheckRunAnnotation
85 sort.Ints(rep.ModifiedLines)
86 start, end := rep.Problem.LineRange()
87 comment = &github.CheckRunAnnotation{
88 Path: github.String(rep.ReportedPath),
89 StartLine: github.Int(start),
90 EndLine: github.Int(end),
91 AnnotationLevel: github.String(level),
92 Message: github.String(rep.Problem.Text),
93 Title: github.String(rep.Problem.Fragment),
94 }
95
96 comments = append(comments, comment)
97 }
98
99 _, resp, err := client.Checks.CreateCheckRun(ctx, gr.owner, gr.repo, github.CreateCheckRunOptions{
100 Name: "pint",
101 HeadSHA: headCommit,
102 DetailsURL: github.String("https://cloudflare.github.io/pint/"),
103 Conclusion: github.String(conclusion),
104 CompletedAt: &github.Timestamp{Time: time.Now()},
105 Output: &github.CheckRunOutput{
106 Title: github.String("pint"),
107 Summary: github.String(BitBucketDescription),
108 AnnotationsCount: github.Int(len(comments)),
109 Annotations: comments,
110 },
111 })
112 if err != nil {
113 return fmt.Errorf("failed to create a new check run: %w", err)
114 }
115 log.Info().Str("status", resp.Status).Msg("Report submitted")
116
117 return nil
118}
119