Skip to content

Commit

Permalink
Merge pull request #501 from cloudflare/gh
Browse files Browse the repository at this point in the history
Use GitHub review API
  • Loading branch information
prymitive authored Jan 12, 2023
2 parents 92fbc6c + b81378b commit bd7d94c
Show file tree
Hide file tree
Showing 16 changed files with 352 additions and 108 deletions.
5 changes: 4 additions & 1 deletion .github/pint/rules/1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ groups:
rules:
- alert: Service Is Down
expr: up == 0
for: 0m
for: 0s

- alert: Service Is Down
expr: up{job="abc"} == 0
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- main

permissions:
checks: write
pull-requests: write

jobs:
ci:
Expand Down
28 changes: 26 additions & 2 deletions cmd/pint/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -114,16 +115,31 @@ func actionCI(c *cli.Context) error {
return fmt.Errorf("GITHUB_AUTH_TOKEN env variable is required when reporting to GitHub")
}

prVal, ok := os.LookupEnv("GITHUB_PULL_REQUEST_NUMBER")
if !ok {
return fmt.Errorf("GITHUB_PULL_REQUEST_NUMBER env variable is required when reporting to GitHub")
}

prNum, err := strconv.Atoi(prVal)
if err != nil {
return fmt.Errorf("got not a valid number via GITHUB_PULL_REQUEST_NUMBER: %w", err)
}

timeout, _ := time.ParseDuration(meta.cfg.Repository.GitHub.Timeout)
gr := reporter.NewGithubReporter(
gr, err := reporter.NewGithubReporter(
version,
meta.cfg.Repository.GitHub.BaseURI,
meta.cfg.Repository.GitHub.UploadURI,
timeout,
token,
meta.cfg.Repository.GitHub.Owner,
meta.cfg.Repository.GitHub.Repo,
prNum,
git.RunGit,
)
if err != nil {
return err
}
reps = append(reps, gr)
}

Expand Down Expand Up @@ -190,7 +206,15 @@ func detectRepository(cfg *config.Repository) *config.Repository {
}

func detectGithubActions(gh *config.GitHub) *config.GitHub {
log.Debug().Msg("GitHub actions environment detected")
if os.Getenv("GITHUB_PULL_REQUEST_NUMBER") == "" &&
os.Getenv("GITHUB_EVENT_NAME") == "pull_request" &&
os.Getenv("GITHUB_REF") != "" {
parts := strings.Split(os.Getenv("GITHUB_REF"), "/")
if len(parts) >= 4 {
log.Info().Str("pr", parts[2]).Msg("Setting GITHUB_PULL_REQUEST_NUMBER from GITHUB_REF env variable")
os.Setenv("GITHUB_PULL_REQUEST_NUMBER", parts[2])
}
}

var isDirty, isNil bool

Expand Down
42 changes: 35 additions & 7 deletions cmd/pint/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ func httpServer(ts *testscript.TestScript, neg bool, args []string) {
_, err := w.Write([]byte(body))
ts.Check(err)
}})
case "method":
if len(args) < 6 {
ts.Fatalf("! http response command requires '$NAME $METHOD $PATH $CODE $BODY' args, got [%s]", strings.Join(args, " "))
}
name := args[1]
meth := args[2]
path := args[3]
code, err := strconv.Atoi(args[4])
ts.Check(err)
body := strings.Join(args[5:], " ")
mocks.add(name, httpMock{pattern: path, method: meth, handler: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(code)
_, err := w.Write([]byte(body))
ts.Check(err)
}})
// http auth-response name /200 user password 200 OK
case "auth-response":
if len(args) < 7 {
Expand Down Expand Up @@ -145,15 +160,27 @@ func httpServer(ts *testscript.TestScript, neg bool, args []string) {
listen := args[2]

mux := http.NewServeMux()
for n, mockList := range mocks.responses {
if n == name {
for _, mock := range mockList {
mock := mock
mux.HandleFunc(mock.pattern, mock.handler)
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var done bool
for n, mockList := range mocks.responses {
if n == name {
for _, mock := range mockList {
if mock.pattern != "/" && (r.URL.Path != mock.pattern || !strings.HasPrefix(r.URL.Path, mock.pattern)) {
continue
}
if mock.method != "" && mock.method != r.Method {
continue
}
mock.handler(w, r)
done = true
}
break
}
break
}
}
if !done {
w.WriteHeader(http.StatusNotFound)
}
}))

listener, err := net.Listen("tcp", listen)
ts.Check(err)
Expand All @@ -175,6 +202,7 @@ func httpServer(ts *testscript.TestScript, neg bool, args []string) {

type httpMock struct {
pattern string
method string
handler func(http.ResponseWriter, *http.Request)
}

Expand Down
8 changes: 6 additions & 2 deletions cmd/pint/tests/0032_ci_github.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
http response github / 200 {}
http method github GET /api/v3/repos/cloudflare/pint/pulls/1/reviews 200 []
http method github POST /api/v3/repos/cloudflare/pint/pulls/1/reviews 200 {}
http method github GET /api/v3/repos/cloudflare/pint/pulls/1/comments 200 []
http method github POST /api/v3/repos/cloudflare/pint/pulls/1/comments 200 {}
http start github 127.0.0.1:6032

mkdir testrepo
Expand All @@ -19,9 +22,10 @@ cp ../src/v2.yml rules.yml
exec git commit -am 'v2'

env GITHUB_AUTH_TOKEN=12345
env GITHUB_PULL_REQUEST_NUMBER=1
pint.ok -l debug --offline --no-color ci
! stdout .
stderr 'level=info msg="Report submitted" status="200 OK"'
stderr 'level=info msg="Pull request review created" status="200 OK"'

-- src/v1.yml --
- alert: rule1
Expand Down
8 changes: 6 additions & 2 deletions cmd/pint/tests/0033_ci_github_multi.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
http response github / 200 {}
http method github GET /api/v3/repos/cloudflare/pint/pulls/1/reviews 200 []
http method github POST /api/v3/repos/cloudflare/pint/pulls/1/reviews 200 {}
http method github GET /api/v3/repos/cloudflare/pint/pulls/1/comments 200 []
http method github POST /api/v3/repos/cloudflare/pint/pulls/1/comments 200 {}
http start github 127.0.0.1:6033

mkdir testrepo
Expand All @@ -19,9 +22,10 @@ cp ../src/v2.yml rules.yml
exec git commit -am 'v2'

env GITHUB_AUTH_TOKEN=12345
env GITHUB_PULL_REQUEST_NUMBER=1
pint.error -l debug --no-color ci
! stdout .
stderr 'level=info msg="Report submitted" status="200 OK"'
stderr 'level=info msg="Pull request review created" status="200 OK"'

-- src/v1.yml --
- alert: rule1
Expand Down
9 changes: 7 additions & 2 deletions cmd/pint/tests/0083_github_action.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
http response github / 200 {}
http method github GET /api/v3/repos/foo/bar/pulls/123/reviews 200 []
http method github POST /api/v3/repos/foo/bar/pulls/123/reviews 200 {}
http method github GET /api/v3/repos/foo/bar/pulls/123/comments 200 []
http method github POST /api/v3/repos/foo/bar/pulls/123/comments 200 {}
http start github 127.0.0.1:6083

mkdir testrepo
Expand All @@ -21,12 +24,14 @@ exec git commit -am 'v2'
env GITHUB_AUTH_TOKEN=12345
env GITHUB_ACTION=YES
env GITHUB_EVENT_NAME=pull_request
env GITHUB_REF=refs/pull/123/merge
env GITHUB_BASE_REF=main
env GITHUB_REPOSITORY=foo/bar
env GITHUB_API_URL=http://127.0.0.1:6083
pint.ok -l debug --offline --no-color ci
! stdout .
stderr 'level=info msg="Report submitted" status="200 OK"'
stderr 'level=info msg="Pull request review created" status="200 OK"'
stderr 'level=info msg="Setting GITHUB_PULL_REQUEST_NUMBER from GITHUB_REF env variable" pr=123'
stderr 'level=info msg="Setting repository owner from GITHUB_REPOSITORY env variable" owner=foo'
stderr 'level=info msg="Setting repository name from GITHUB_REPOSITORY env variable" repo=bar'
stderr 'level=info msg="Setting repository base URI from GITHUB_API_URL env variable" baseuri=http://127.0.0.1:6083'
Expand Down
9 changes: 7 additions & 2 deletions cmd/pint/tests/0084_github_action_override.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
http response github / 200 {}
http method github GET /api/v3/repos/cloudflare/pint/pulls/123/reviews 200 []
http method github POST /api/v3/repos/cloudflare/pint/pulls/123/reviews 200 {}
http method github GET /api/v3/repos/cloudflare/pint/pulls/123/comments 200 []
http method github POST /api/v3/repos/cloudflare/pint/pulls/123/comments 200 {}
http start github 127.0.0.1:6084

mkdir testrepo
Expand All @@ -21,12 +24,14 @@ exec git commit -am 'v2'
env GITHUB_AUTH_TOKEN=12345
env GITHUB_ACTION=YES
env GITHUB_EVENT_NAME=pull_request
env GITHUB_REF=refs/pull/123/merge
env GITHUB_BASE_REF=main
env GITHUB_REPOSITORY=foo/bar
env GITHUB_API_URL=http://127.0.0.1:6084
pint.ok -l debug --offline --no-color ci
! stdout .
stderr 'level=info msg="Report submitted" status="200 OK"'
stderr 'level=info msg="Pull request review created" status="200 OK"'
stderr 'level=info msg="Setting GITHUB_PULL_REQUEST_NUMBER from GITHUB_REF env variable" pr=123'
stderr 'level=info msg="Setting repository base URI from GITHUB_API_URL env variable" baseuri=http://127.0.0.1:6084'

-- src/v1.yml --
Expand Down
8 changes: 6 additions & 2 deletions cmd/pint/tests/0085_github_no_envs.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
http response github / 200 {}
http method github GET /api/v3/repos/cloudflare/pint/pulls/123/reviews 200 []
http method github POST /api/v3/repos/cloudflare/pint/pulls/123/reviews 200 {}
http method github GET /api/v3/repos/cloudflare/pint/pulls/123/comments 200 []
http method github POST /api/v3/repos/cloudflare/pint/pulls/123/comments 200 {}
http start github 127.0.0.1:6085

mkdir testrepo
Expand All @@ -20,9 +23,10 @@ exec git commit -am 'v2'

env GITHUB_AUTH_TOKEN=12345
env GITHUB_ACTION=YES
env GITHUB_PULL_REQUEST_NUMBER=123
pint.ok -l debug --offline --no-color ci
! stdout .
stderr 'level=info msg="Report submitted" status="200 OK"'
stderr 'level=info msg="Pull request review created" status="200 OK"'

-- src/v1.yml --
groups:
Expand Down
8 changes: 6 additions & 2 deletions cmd/pint/tests/0098_rule_file_symlink_gh.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
http response github / 200 {}
http method github GET /api/v3/repos/cloudflare/pint/pulls/1/reviews 200 []
http method github POST /api/v3/repos/cloudflare/pint/pulls/1/reviews 200 {}
http method github GET /api/v3/repos/cloudflare/pint/pulls/1/comments 200 []
http method github POST /api/v3/repos/cloudflare/pint/pulls/1/comments 200 {}
http start github 127.0.0.1:6098

mkdir testrepo
Expand All @@ -20,9 +23,10 @@ cp ../src/v2.yml rules.yml
exec git commit -am 'v2'

env GITHUB_AUTH_TOKEN=12345
env GITHUB_PULL_REQUEST_NUMBER=1
pint.ok -l debug -d promql/series --no-color ci
! stdout .
stderr 'level=info msg="Report submitted" status="200 OK"'
stderr 'level=info msg="Pull request review created" status="200 OK"'

-- src/v1.yml --
groups:
Expand Down
5 changes: 5 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
- Added `lookbackRange` and `lookbackStep` configration option to the
[promql/series](checks/promql/series.md) check - #493.

### Changed

- Reverted GitHub integration to use [Pull Request Review](https://docs.github.com/en/rest/pulls/reviews)
API - #490.

## v0.39.0

### Changed
Expand Down
5 changes: 5 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ to be set. It should contain a personal access token used to authenticate with t
**NOTE**: GitHub integration requires `GITHUB_AUTH_TOKEN` environment variable
to be set to a personal access key that can access your repository.

**NOTE** Pull request number must be known to pint so it can add comments if it detects any problems.
If pint is run as part of GitHub actions workflow then this number will be detected from `GITHUB_REF`
environment variable. For other use cases `GITHUB_PULL_REQUEST_NUMBER` environment variable must be set
with the pull request number.

Syntax:

```js
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/fatih/color v1.13.0
github.com/gkampitakis/go-snaps v0.4.2
github.com/google/go-cmp v0.5.9
github.com/google/go-github/v37 v37.0.0
github.com/google/go-github/v49 v49.1.0
github.com/hashicorp/hcl/v2 v2.15.0
github.com/klauspost/compress v1.15.14
github.com/prometheus/client_golang v1.14.0
Expand Down
9 changes: 2 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,10 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github/v37 v37.0.0 h1:rCspN8/6kB1BAJWZfuafvHhyfIo5fkAulaP/3bOQ/tM=
github.com/google/go-github/v37 v37.0.0/go.mod h1:LM7in3NmXDrX58GbEHy7FtNLbI2JijX93RnMKvWG3m4=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/go-github/v49 v49.1.0 h1:LFkMgawGQ8dfzWLH/rNE0b3u1D3n6/dw7ZmrN3b+YFY=
github.com/google/go-github/v49 v49.1.0/go.mod h1:MUUzHPrhGniB6vUKa27y37likpipzG+BXXJbG04J334=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
Expand Down Expand Up @@ -320,7 +318,6 @@ golang.org/x/mod v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand All @@ -336,7 +333,6 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw=
golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.4.0 h1:NF0gk8LVPg1Ml7SSbGyySuoxdsXitj7TvgvuRxIMc/M=
Expand Down Expand Up @@ -398,7 +394,6 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.6.6/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
Expand Down
Loading

0 comments on commit bd7d94c

Please sign in to comment.