Skip to content

Commit

Permalink
Merge pull request #982 from cloudflare/gitlab
Browse files Browse the repository at this point in the history
Add gitlab reporter
  • Loading branch information
prymitive authored May 24, 2024
2 parents 46cc9a3 + 7d4b6f3 commit 2be0d47
Show file tree
Hide file tree
Showing 19 changed files with 1,969 additions and 137 deletions.
4 changes: 3 additions & 1 deletion .github/spellcheck/wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ APIs
automaxprocs
BitBucket
bool
ci
changelog
Changelog
ci
CLI
cloudflare
Cloudflare
Expand All @@ -15,6 +15,8 @@ dir
durations
endraw
github
gitlab
GitLab
GOGC
golang
GOMAXPROCS
Expand Down
14 changes: 0 additions & 14 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ run:
timeout: 5m
tests: true

output:
sort-results: true

linters:
enable:
- depguard
- gofumpt
- goimports
- revive
Expand All @@ -32,16 +28,6 @@ issues:
max-same-issues: 0

linters-settings:
depguard:
rules:
test:
files:
- "$test"
deny:
- pkg: "github.com/stretchr/testify/assert"
desc: "Use github.com/stretchr/testify/require instead of github.com/stretchr/testify/assert"
- pkg: "io/ioutil"
desc: "Use corresponding 'os' or 'io' functions instead."
goimports:
local-prefixes: github.com/cloudflare/pint
gofumpt:
Expand Down
29 changes: 27 additions & 2 deletions cmd/pint/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,28 @@ func actionCI(c *cli.Context) error {
reps = append(reps, br)
}

if meta.cfg.Repository != nil && meta.cfg.Repository.GitLab != nil {
token, ok := os.LookupEnv("GITLAB_AUTH_TOKEN")
if !ok {
return fmt.Errorf("GITLAB_AUTH_TOKEN env variable is required when reporting to GitLab")
}

timeout, _ := time.ParseDuration(meta.cfg.Repository.GitLab.Timeout)
var gl reporter.GitLabReporter
if gl, err = reporter.NewGitLabReporter(
version,
currentBranch,
meta.cfg.Repository.GitLab.URI,
timeout,
token,
meta.cfg.Repository.GitLab.Project,
meta.cfg.Repository.GitLab.MaxComments,
); err != nil {
return err
}
reps = append(reps, gl)
}

meta.cfg.Repository = detectRepository(meta.cfg.Repository)
if meta.cfg.Repository != nil && meta.cfg.Repository.GitHub != nil {
token, ok := os.LookupEnv("GITHUB_AUTH_TOKEN")
Expand Down Expand Up @@ -204,8 +226,11 @@ func actionCI(c *cli.Context) error {
slog.Info("Problems found", logSeverityCounters(bySeverity)...)
}

if err := submitReports(reps, summary); err != nil {
return fmt.Errorf("submitting reports: %w", err)
for _, rep := range reps {
err = rep.Submit(summary)
if err != nil {
return fmt.Errorf("submitting reports: %w", err)
}
}

if problemsFound {
Expand Down
10 changes: 0 additions & 10 deletions cmd/pint/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,3 @@ This usually means that it's missing some required fields.`,
checkIterationChecksDone.Inc()
}
}

func submitReports(reps []reporter.Reporter, summary reporter.Summary) (err error) {
for _, rep := range reps {
err = rep.Submit(summary)
if err != nil {
return err
}
}
return nil
}
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## v0.59.0

### Added

- Added GitLab support when running `pint ci`.

### Changed

- Reduced severity of problems reported by [promql/counter](checks/promql/counter.md) due
Expand Down
42 changes: 41 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,21 +140,40 @@ ci {

Configure supported code hosting repository, used for reporting PR checks from CI
back to the repository, to be displayed in the PR UI.
Currently it only supports [BitBucket](https://bitbucket.org/) and [GitHub](https://github.com/).
Currently supported platforms are:

- [BitBucket](https://bitbucket.org)
- [GitHub](https://github.com)
- [GitLab](https://gitlab.com)

**NOTE**: BitBucket integration requires `BITBUCKET_AUTH_TOKEN` environment variable
to be set. It should contain a personal access token used to authenticate with the API.

**NOTE**: GitHub integration requires `GITHUB_AUTH_TOKEN` environment variable
to be set to a personal access key that can access your repository.

**NOTE**: GitLab integration requires `GITLAB_AUTH_TOKEN` environment variable
to be set to a personal access key that can access your repository.

**NOTE** The 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, the `GITHUB_PULL_REQUEST_NUMBER` environment variable must be set
with the pull request number.

Syntax:

```js
repository {
bitbucket { ... }
github { ... }
gitlab { ... }
}
```

### BitBucket options

Syntax:

```js
repository {
bitbucket {
Expand All @@ -175,6 +194,8 @@ repository {
- `bitbucket:maxComments` - the maximum number of comments pint can create on a single
pull request. Default is 50.

### GitHub options

```js
repository {
github {
Expand Down Expand Up @@ -206,6 +227,25 @@ Most GitHub settings can be detected from environment variables that are set ins
environment. The only exception is `GITHUB_AUTH_TOKEN` environment variable that must be set
manually.

### GitLab options

```js
repository {
gitlab {
uri = "https://..."
timeout = "1m"
project = "..."
maxComments = 50
}
}
```

- `gitlab:uri` - optional base URI for GitLab API calls when using self hosted setup.
You don't need to set it if you use repositories hosted on [gitlab.com](https://gitlab.com/).
- `gitlab:timeout` - timeout to be used for API requests, defaults to 1 minute.
- `gitlab:project` - ID of the GitLab repository.
- `gitlab:maxComments` - the maximum number of comments pint can create on a single pull request. Default is 50.

## Prometheus servers

Some checks work by querying a running Prometheus instance to verify if
Expand Down
8 changes: 5 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ Running `pint ci` doesn't require any configuration but it's recommended to add
with `ci` section containing at least the `include` option. This will ensure that pint validates
only Prometheus rules and ignores other files.

Results can optionally be reported using
[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.
Results can optionally be reported as comments on a pull request when using one of supported platforms:

- [BitBucket API](https://developer.atlassian.com/server/bitbucket/rest/)
- [GitHub API](https://docs.github.com/en/rest)
- [GitLab API](https://docs.gitlab.com/ee/api/rest/)

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
5 changes: 4 additions & 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.17.0
github.com/gkampitakis/go-snaps v0.5.4
github.com/google/go-cmp v0.6.0
github.com/google/go-github/v60 v60.0.0
github.com/google/go-github/v62 v62.0.0
github.com/hashicorp/hcl/v2 v2.20.1
github.com/klauspost/compress v1.17.8
github.com/neilotoole/slogt v1.1.0
Expand All @@ -19,6 +19,7 @@ require (
github.com/rogpeppe/go-internal v1.12.0
github.com/stretchr/testify v1.9.0
github.com/urfave/cli/v2 v2.27.2
github.com/xanzy/go-gitlab v0.105.0
github.com/zclconf/go-cty v1.14.4
go.uber.org/atomic v1.11.0
go.uber.org/automaxprocs v1.5.3
Expand Down Expand Up @@ -57,6 +58,8 @@ require (
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grafana/regexp v0.0.0-20221122212121-6b5c0a4cb7fd // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.4 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github/v60 v60.0.0 h1:oLG98PsLauFvvu4D/YPxq374jhSxFYdzQGNCyONLfn8=
github.com/google/go-github/v60 v60.0.0/go.mod h1:ByhX2dP9XT9o/ll2yXAu2VD8l5eNVg8hD4Cr0S/LmQk=
github.com/google/go-github/v62 v62.0.0 h1:/6mGCaRywZz9MuHyw9gD1CwsbmBX8GWsbFkwMmHdhl4=
github.com/google/go-github/v62 v62.0.0/go.mod h1:EMxeUqGJq2xRu9DYBMwel/mr7kZrzUOfQmmpYrZn2a4=
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 @@ -247,6 +247,7 @@ github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-hclog v1.5.0 h1:bI2ocEMgcVlz55Oj1xZNBsVi900c7II+fWDyV9o+13c=
github.com/hashicorp/go-hclog v1.5.0/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
github.com/hashicorp/go-immutable-radix v1.3.1 h1:DKHmCUm2hRBK510BaiZlwvpD40f8bJFeZnpfm2KLowc=
Expand Down Expand Up @@ -432,6 +433,8 @@ github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI=
github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM=
github.com/vultr/govultr/v2 v2.17.2 h1:gej/rwr91Puc/tgh+j33p/BLR16UrIPnSr+AIwYWZQs=
github.com/vultr/govultr/v2 v2.17.2/go.mod h1:ZFOKGWmgjytfyjeyAdhQlSWwTjh2ig+X49cAp50dzXI=
github.com/xanzy/go-gitlab v0.105.0 h1:3nyLq0ESez0crcaM19o5S//SvezOQguuIHZ3wgX64hM=
github.com/xanzy/go-gitlab v0.105.0/go.mod h1:ETg8tcj4OhrB84UEgeE8dSuV/0h4BBL1uOV/qK0vlyI=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913/go.mod h1:4aEEwZQutDLsQv2Deui4iYQ6DWTxR14g6m8Wv88+Xqk=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
22 changes: 2 additions & 20 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,26 +258,8 @@ func Load(path string, failOnMissing bool) (cfg Config, err error) {
}
}

if cfg.Repository != nil && cfg.Repository.BitBucket != nil {
if cfg.Repository.BitBucket.Timeout == "" {
cfg.Repository.BitBucket.Timeout = time.Minute.String()
}
if cfg.Repository.BitBucket.MaxComments == 0 {
cfg.Repository.BitBucket.MaxComments = 50
}
if err = cfg.Repository.BitBucket.validate(); err != nil {
return cfg, err
}
}

if cfg.Repository != nil && cfg.Repository.GitHub != nil {
if cfg.Repository.GitHub.Timeout == "" {
cfg.Repository.GitHub.Timeout = time.Minute.String()
}
if cfg.Repository.GitHub.MaxComments == 0 {
cfg.Repository.GitHub.MaxComments = 50
}
if err = cfg.Repository.GitHub.validate(); err != nil {
if cfg.Repository != nil {
if err = cfg.Repository.validate(); err != nil {
return cfg, err
}
}
Expand Down
59 changes: 59 additions & 0 deletions internal/config/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/url"
"os"
"strings"
"time"
)

type BitBucket struct {
Expand Down Expand Up @@ -84,7 +85,65 @@ func (gh GitHub) validate() error {
return nil
}

type GitLab struct {
URI string `hcl:"uri,optional"`
Timeout string `hcl:"timeout,optional"`
Project int `hcl:"project"`
MaxComments int `hcl:"maxComments,optional"`
}

func (gl GitLab) validate() error {
if gl.Project <= 0 {
return fmt.Errorf("project must be set")
}
if gl.MaxComments < 0 {
return fmt.Errorf("maxComments cannot be negative")
}
return nil
}

type Repository struct {
BitBucket *BitBucket `hcl:"bitbucket,block" json:"bitbucket,omitempty"`
GitHub *GitHub `hcl:"github,block" json:"github,omitempty"`
GitLab *GitLab `hcl:"gitlab,block" json:"gitlab,omitempty"`
}

func (r *Repository) validate() (err error) {
if r.BitBucket != nil {
if r.BitBucket.Timeout == "" {
r.BitBucket.Timeout = time.Minute.String()
}
if r.BitBucket.MaxComments == 0 {
r.BitBucket.MaxComments = 50
}
if err = r.BitBucket.validate(); err != nil {
return err
}
}

if r.GitHub != nil {
if r.GitHub.Timeout == "" {
r.GitHub.Timeout = time.Minute.String()
}
if r.GitHub.MaxComments == 0 {
r.GitHub.MaxComments = 50
}
if err = r.GitHub.validate(); err != nil {
return err
}
}

if r.GitLab != nil {
if r.GitLab.Timeout == "" {
r.GitLab.Timeout = time.Minute.String()
}
if r.GitLab.MaxComments == 0 {
r.GitLab.MaxComments = 50
}
if err = r.GitLab.validate(); err != nil {
return err
}
}

return nil
}
Loading

0 comments on commit 2be0d47

Please sign in to comment.