Skip to content

Commit

Permalink
Refactor bitbucket reporter to use comments
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed Nov 3, 2023
1 parent 2f1956a commit 657679d
Show file tree
Hide file tree
Showing 23 changed files with 2,237 additions and 461 deletions.
17 changes: 9 additions & 8 deletions cmd/pint/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"net/http"
"os"
"path"
"regexp"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -84,7 +85,7 @@ func httpServer(ts *testscript.TestScript, _ bool, args []string) {
ts.Fatalf("! http response command requires '$NAME $PATH $CODE $BODY' args, got [%s]", strings.Join(args, " "))
}
name := args[1]
path := args[2]
path := regexp.MustCompile(args[2])
code, err := strconv.Atoi(args[3])
ts.Check(err)
body := strings.Join(args[4:], " ")
Expand All @@ -99,7 +100,7 @@ func httpServer(ts *testscript.TestScript, _ bool, args []string) {
}
name := args[1]
meth := args[2]
path := args[3]
path := regexp.MustCompile(args[3])
code, err := strconv.Atoi(args[4])
ts.Check(err)
body := strings.Join(args[5:], " ")
Expand All @@ -114,7 +115,7 @@ func httpServer(ts *testscript.TestScript, _ bool, args []string) {
ts.Fatalf("! http response command requires '$NAME $PATH $USER $PASS $CODE $BODY' args, got [%s]", strings.Join(args, " "))
}
name := args[1]
path := args[2]
path := regexp.MustCompile(args[2])
user := args[3]
pass := args[4]
code, err := strconv.Atoi(args[5])
Expand All @@ -136,7 +137,7 @@ func httpServer(ts *testscript.TestScript, _ bool, args []string) {
ts.Fatalf("! http response command requires '$NAME $PATH $DELAY $CODE $BODY' args, got [%s]", strings.Join(args, " "))
}
name := args[1]
path := args[2]
path := regexp.MustCompile(args[2])
delay, err := time.ParseDuration(args[3])
ts.Check(err)
code, err := strconv.Atoi(args[4])
Expand All @@ -154,7 +155,7 @@ func httpServer(ts *testscript.TestScript, _ bool, args []string) {
ts.Fatalf("! http redirect command requires '$NAME $SRCPATH $DSTPATH' args, got [%s]", strings.Join(args, " "))
}
name := args[1]
srcpath := args[2]
srcpath := regexp.MustCompile(args[2])
dstpath := args[3]
mocks.add(name, httpMock{pattern: srcpath, handler: func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Location", dstpath)
Expand All @@ -181,10 +182,10 @@ func httpServer(ts *testscript.TestScript, _ bool, args []string) {
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)) {
if mock.method != "" && mock.method != r.Method {
continue
}
if mock.method != "" && mock.method != r.Method {
if !mock.pattern.MatchString(r.URL.Path) {
continue
}
mock.handler(w, r)
Expand Down Expand Up @@ -226,7 +227,7 @@ func httpServer(ts *testscript.TestScript, _ bool, args []string) {
}

type httpMock struct {
pattern string
pattern *regexp.Regexp
method string
handler func(http.ResponseWriter, *http.Request)
}
Expand Down
11 changes: 6 additions & 5 deletions cmd/pint/tests/0031_ci_bitbucket.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
http response bitbucket / 200 OK
http response bitbucket /rest/insights/1.0/projects/prometheus/repos/rules/commits/.*/reports/pint 200 OK
http response bitbucket /rest/api/1.0/projects/prometheus/repos/rules/commits/.*/pull-requests 200 {}
http start bitbucket 127.0.0.1:6031

mkdir testrepo
Expand All @@ -21,10 +22,10 @@ exec git commit -am 'v2'
env BITBUCKET_AUTH_TOKEN="12345"
pint.ok -l debug --no-color ci
! stdout .
stderr 'level=DEBUG msg="Sending a request to BitBucket" method=PUT'
stderr 'level=DEBUG msg="BitBucket request completed" status=200'
stderr 'level=DEBUG msg="Sending a request to BitBucket" method=DELETE'
stderr 'level=DEBUG msg="BitBucket request completed" status=200'
stderr 'msg="Sending a request to BitBucket" method=PUT'
stderr 'msg="BitBucket request completed" status=200'
stderr 'msg="Sending a request to BitBucket" method=DELETE'
stderr 'msg="BitBucket request completed" status=200'

-- src/v1.yml --
- alert: rule1
Expand Down
11 changes: 6 additions & 5 deletions cmd/pint/tests/0068_skip_ci.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
http response bitbucket / 200 OK
http response bitbucket /rest/insights/1.0/projects/prometheus/repos/rules/commits/.*/reports/pint 200 OK
http response bitbucket /rest/api/1.0/projects/prometheus/repos/rules/commits/.*/pull-requests 200 {}
http start bitbucket 127.0.0.1:6068

mkdir testrepo
Expand All @@ -22,10 +23,10 @@ env BITBUCKET_AUTH_TOKEN="12345"
pint.ok -l debug --no-color ci
! stdout .
stderr 'level=INFO msg="Found a commit with ''\[skip ci\]'', skipping all checks" commit=.*'
stderr 'level=DEBUG msg="Sending a request to BitBucket" method=PUT'
stderr 'level=DEBUG msg="BitBucket request completed" status=200'
stderr 'level=DEBUG msg="Sending a request to BitBucket" method=DELETE'
stderr 'level=DEBUG msg="BitBucket request completed" status=200'
stderr 'msg="Sending a request to BitBucket" method=PUT'
stderr 'msg="BitBucket request completed" status=200'
stderr 'msg="Sending a request to BitBucket" method=DELETE'
stderr 'msg="BitBucket request completed" status=200'

-- src/v1.yml --
- alert: rule1
Expand Down
3 changes: 2 additions & 1 deletion cmd/pint/tests/0069_bitbucket_skip_unmodified.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
http response bitbucket / 200 OK
http response bitbucket /rest/insights/1.0/projects/prometheus/repos/rules/commits/.*/reports/pint 200 OK
http response bitbucket /rest/api/1.0/projects/prometheus/repos/rules/commits/.*/pull-requests 200 {}
http start bitbucket 127.0.0.1:6069

mkdir testrepo
Expand Down
11 changes: 6 additions & 5 deletions cmd/pint/tests/0070_bitbucket_strict.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
http response bitbucket / 200 OK
http response bitbucket /rest/insights/1.0/projects/prometheus/repos/rules/commits/.*/reports/pint 200 OK
http response bitbucket /rest/api/1.0/projects/prometheus/repos/rules/commits/.*/pull-requests 200 {}
http start bitbucket 127.0.0.1:6070

mkdir testrepo
Expand All @@ -23,10 +24,10 @@ pint.error -l debug --no-color ci
! stdout .
stderr 'result":"FAIL"'
stderr ',"line":3,'
stderr 'level=DEBUG msg="Sending a request to BitBucket" method=PUT'
stderr 'level=DEBUG msg="BitBucket request completed" status=200'
stderr 'level=DEBUG msg="Sending a request to BitBucket" method=DELETE'
stderr 'level=DEBUG msg="BitBucket request completed" status=200'
stderr 'msg="Sending a request to BitBucket" method=PUT'
stderr 'msg="BitBucket request completed" status=200'
stderr 'msg="Sending a request to BitBucket" method=DELETE'
stderr 'msg="BitBucket request completed" status=200'

-- src/v1.yml --
- alert: rule1
Expand Down
3 changes: 2 additions & 1 deletion cmd/pint/tests/0071_ci_owner.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
http response bitbucket / 200 OK
http response bitbucket /rest/insights/1.0/projects/prometheus/repos/rules/commits/.*/reports/pint 200 OK
http response bitbucket /rest/api/1.0/projects/prometheus/repos/rules/commits/.*/pull-requests 200 {}
http start bitbucket 127.0.0.1:6071

mkdir testrepo
Expand Down
11 changes: 6 additions & 5 deletions cmd/pint/tests/0072_bitbucket_move_bug_to_modified.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
http response bitbucket / 200 OK
http response bitbucket /rest/insights/1.0/projects/prometheus/repos/rules/commits/.*/reports/pint 200 OK
http response bitbucket /rest/api/1.0/projects/prometheus/repos/rules/commits/.*/pull-requests 200 {}
http start bitbucket 127.0.0.1:6072

mkdir testrepo
Expand All @@ -24,10 +25,10 @@ pint.error --no-color -l debug ci
stderr 'level=INFO msg="Problems found" Fatal=1'
stderr 'result":"FAIL"'
stderr ',"line":3,'
stderr 'level=DEBUG msg="Sending a request to BitBucket" method=PUT'
stderr 'level=DEBUG msg="BitBucket request completed" status=200'
stderr 'level=DEBUG msg="Sending a request to BitBucket" method=DELETE'
stderr 'level=DEBUG msg="BitBucket request completed" status=200'
stderr 'msg="Sending a request to BitBucket" method=PUT'
stderr 'msg="BitBucket request completed" status=200'
stderr 'msg="Sending a request to BitBucket" method=DELETE'
stderr 'msg="BitBucket request completed" status=200'

-- src/v1.yml --
- alert: rule1
Expand Down
11 changes: 6 additions & 5 deletions cmd/pint/tests/0075_ci_strict.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
http response bitbucket / 200 OK
http response bitbucket /rest/insights/1.0/projects/prometheus/repos/rules/commits/.*/reports/pint 200 OK
http response bitbucket /rest/api/1.0/projects/prometheus/repos/rules/commits/.*/pull-requests 200 {}
http start bitbucket 127.0.0.1:6075

mkdir testrepo
Expand All @@ -21,10 +22,10 @@ exec git commit -am 'v2'
env BITBUCKET_AUTH_TOKEN="12345"
pint.error -l debug --no-color ci --require-owner
! stdout .
stderr 'level=DEBUG msg="Sending a request to BitBucket" method=PUT'
stderr 'level=DEBUG msg="BitBucket request completed" status=200'
stderr 'level=DEBUG msg="Sending a request to BitBucket" method=DELETE'
stderr 'level=DEBUG msg="BitBucket request completed" status=200'
stderr 'msg="Sending a request to BitBucket" method=PUT'
stderr 'msg="BitBucket request completed" status=200'
stderr 'msg="Sending a request to BitBucket" method=DELETE'
stderr 'msg="BitBucket request completed" status=200'
stderr 'level=INFO msg="Problems found" Fatal=1 Bug=1'
! stderr 'parse error: unclosed left parenthesis'

Expand Down
5 changes: 3 additions & 2 deletions cmd/pint/tests/0076_ci_group_errors.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
http response prometheus / 200 OK
http start prometheus 127.0.0.1:6076
http response bitbucket /rest/insights/1.0/projects/prometheus/repos/rules/commits/.*/reports/pint 200 OK
http response bitbucket /rest/api/1.0/projects/prometheus/repos/rules/commits/.*/pull-requests 200 {}
http start bitbucket 127.0.0.1:6076

mkdir testrepo
cd testrepo
Expand Down
11 changes: 6 additions & 5 deletions cmd/pint/tests/0093_ci_bitbucket_ignore_file.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
http response bitbucket / 200 OK
http response bitbucket /rest/insights/1.0/projects/prometheus/repos/rules/commits/.*/reports/pint 200 OK
http response bitbucket /rest/api/1.0/projects/prometheus/repos/rules/commits/.*/pull-requests 200 {}
http start bitbucket 127.0.0.1:6093

mkdir testrepo
Expand All @@ -25,10 +26,10 @@ stderr 'level=INFO msg="Problems found" Information=1'
stderr 'result":"PASS"'
stderr ',"line":7,'
stderr '"message":"ignore/file:'
stderr 'level=DEBUG msg="Sending a request to BitBucket" method=PUT'
stderr 'level=DEBUG msg="BitBucket request completed" status=200'
stderr 'level=DEBUG msg="Sending a request to BitBucket" method=DELETE'
stderr 'level=DEBUG msg="BitBucket request completed" status=200'
stderr 'msg="Sending a request to BitBucket" method=PUT'
stderr 'msg="BitBucket request completed" status=200'
stderr 'msg="Sending a request to BitBucket" method=DELETE'
stderr 'msg="BitBucket request completed" status=200'

-- src/v1.yml --
# pint ignore/file
Expand Down
3 changes: 2 additions & 1 deletion cmd/pint/tests/0094_rule_file_symlink_bb.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ http response prometheus5m /api/v1/query_range 200 {"status":"success","data":{"
http response prometheus5m /api/v1/query 200 {"status":"success","data":{"resultType":"vector","result":[]}}
http start prometheus5m 127.0.0.1:2094

http response bitbucket / 200 OK
http response bitbucket /rest/insights/1.0/projects/prometheus/repos/rules/commits/.*/reports/pint 200 OK
http response bitbucket /rest/api/1.0/projects/prometheus/repos/rules/commits/.*/pull-requests 200 {}
http start bitbucket 127.0.0.1:6094

mkdir testrepo
Expand Down
3 changes: 2 additions & 1 deletion cmd/pint/tests/0100_ci_alerts_count.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ http response prometheus /api/v1/query_range 200 {"status":"success","data":{"re
http response prometheus /api/v1/query 200 {"status":"success","data":{"resultType":"vector","result":[]}}
http start prometheus 127.0.0.1:2100

http response bitbucket / 200 OK
http response bitbucket /rest/insights/1.0/projects/prometheus/repos/rules/commits/.*/reports/pint 200 OK
http response bitbucket /rest/api/1.0/projects/prometheus/repos/rules/commits/.*/pull-requests 200 {}
http start bitbucket 127.0.0.1:6100

mkdir testrepo
Expand Down
3 changes: 2 additions & 1 deletion cmd/pint/tests/0123_ci_owner_allowed.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
http response bitbucket / 200 OK
http response bitbucket /rest/insights/1.0/projects/prometheus/repos/rules/commits/.*/reports/pint 200 OK
http response bitbucket /rest/api/1.0/projects/prometheus/repos/rules/commits/.*/pull-requests 200 {}
http start bitbucket 127.0.0.1:6123

mkdir testrepo
Expand Down
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
and add that information to the report.
This allows pint to flag rules that are most likely deployed to the wrong servers,
using missing scrape jobs.
- Reporting problems to BitBucket will now use comments instead of annotations.
This is only if there is an open pull request for tested branch, if there is no
open pull request problems will be reported using code insight annotations.

## v0.48.2

Expand Down
4 changes: 1 addition & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ It currently supports git for which it will find all commits on the current bran
present in the parent branch and scan all modified files included in those changes.

Results can optionally be reported using
[BitBucket API](https://docs.atlassian.com/bitbucket-server/rest/7.8.0/bitbucket-code-insights-rest.html)
[BitBucket API](https://developer.atlassian.com/server/bitbucket/rest/)
or [GitHub API](https://docs.github.com/en/rest) to generate a report with any found issues.
If you are using BitBucket API then each issue will create an inline annotation in BitBucket with a description of
the issue. If you are using GitHub API then each issue will appear as a comment on your pull request.

Exit code will be one (1) if any issues were detected with severity `Bug` or higher. This permits running
`pint` in your CI system whilst at the same you will get detailed reports on your source control system.
Expand Down
53 changes: 24 additions & 29 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ require (
github.com/google/go-github/v55 v55.0.0
github.com/hashicorp/hcl/v2 v2.19.1
github.com/klauspost/compress v1.17.2
github.com/neilotoole/slogt v1.1.0
github.com/prometheus/client_golang v1.17.0
github.com/prometheus/client_model v0.5.0
github.com/prometheus/common v0.45.0
github.com/prometheus/prometheus v0.47.2
github.com/prometheus/prometheus v0.48.0-rc.2
github.com/prymitive/current v0.1.0
github.com/rogpeppe/go-internal v1.11.0
github.com/stretchr/testify v1.8.4
Expand All @@ -22,40 +23,40 @@ require (
go.uber.org/atomic v1.11.0
go.uber.org/automaxprocs v1.5.3
go.uber.org/ratelimit v0.3.0
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
golang.org/x/oauth2 v0.13.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.7.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.3.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.0.0 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.8.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.4.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.0 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230923063757-afb1ddc0824c // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/aws/aws-sdk-go v1.44.302 // indirect
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/aws/aws-sdk-go v1.47.2 // indirect
github.com/benbjohnson/clock v1.3.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/cloudflare/circl v1.3.6 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dennwc/varint v1.0.0 // indirect
github.com/edsrzf/mmap-go v1.1.0 // indirect
github.com/gkampitakis/ciinfo v0.2.5 // indirect
github.com/gkampitakis/go-diff v1.3.2 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang-jwt/jwt/v5 v5.0.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/grafana/regexp v0.0.0-20221122212121-6b5c0a4cb7fd // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
Expand All @@ -64,7 +65,7 @@ require (
github.com/kr/text v0.2.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand All @@ -75,30 +76,24 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/common/sigv4 v0.1.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/tidwall/gjson v1.16.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
go.opentelemetry.io/otel v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
go.uber.org/goleak v1.2.1 // indirect
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
go.uber.org/goleak v1.3.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.11.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
golang.org/x/tools v0.14.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

exclude (
cloud.google.com/go v0.34.0
cloud.google.com/go v0.65.0
)
Loading

0 comments on commit 657679d

Please sign in to comment.