Skip to content

Commit

Permalink
Merge pull request #228 from cloudflare/owner
Browse files Browse the repository at this point in the history
Add require owner flag to ci command
  • Loading branch information
prymitive authored Apr 12, 2022
2 parents cc968ab + c3fd53f commit 7459ba5
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cmd/pint/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ var ciCmd = &cli.Command{
Name: "ci",
Usage: "Lint CI changes",
Action: actionCI,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: requireOwnerFlag,
Aliases: []string{"r"},
Value: false,
Usage: "Require all rules to have an owner set via comment",
},
},
}

func actionCI(c *cli.Context) error {
Expand Down Expand Up @@ -56,6 +64,10 @@ func actionCI(c *cli.Context) error {
ctx := context.WithValue(context.Background(), config.CommandKey, config.CICommand)
summary := checkRules(ctx, meta.workers, meta.cfg, entries)

if c.Bool(requireOwnerFlag) {
summary.Reports = append(summary.Reports, verifyOwners(entries)...)
}

reps := []reporter.Reporter{
reporter.NewConsoleReporter(os.Stderr),
}
Expand Down
118 changes: 118 additions & 0 deletions cmd/pint/tests/0071_ci_owner.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
exec bash -x ./webserver.sh &
exec bash -c 'I=0 ; while [ ! -f server.pid ] && [ $I -lt 30 ]; do sleep 1; I=$((I+1)); done'

mkdir testrepo
cd testrepo
exec git init --initial-branch=main .

cp ../src/v1.yml rules.yml
cp ../src/.pint.hcl .
env GIT_AUTHOR_NAME=pint
env [email protected]
env GIT_COMMITTER_NAME=pint
env [email protected]
exec git add .
exec git commit -am 'import rules and config'

exec git checkout -b v2
cp ../src/v2.yml rules.yml
exec git commit -am 'v2'

env BITBUCKET_AUTH_TOKEN="12345"
pint.error -l error --no-color ci --require-owner
! stdout .
cd ..
cmp stderr stderr.txt
exec sh -c 'cat server.pid | xargs kill'

-- stderr.txt --
rules.yml:1: rule/owner comments are required in all files, please add a "# pint file/owner $owner" somewhere in this file and/or "# pint rule/owner $owner" on top of each rule (rule/owner)
groups:

rules.yml:5: alert query doesn't have any condition, it will always fire if the metric exists (alerts/comparison)
expr: sum(foo) by(job)

level=fatal msg="Fatal error" error="problems found"
-- src/v1.yml --
- alert: rule1
expr: sum(foo) by(job)
- alert: rule2
expr: sum(foo) by(job)
for: 0s

-- src/v2.yml --
groups:
- name: foo
rules:
- alert: rule1
expr: sum(foo) by(job)
- alert: rule2
expr: sum(foo) by(job) > 0

-- src/.pint.hcl --
ci {
baseBranch = "main"
}
repository {
bitbucket {
uri = "http://127.0.0.1:6071"
timeout = "10s"
project = "prometheus"
repository = "rules"
}
}

-- webserver.go --
package main

import (
"context"
"io"
"log"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"time"
)

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "OK")
})

listener, err := net.Listen("tcp", "127.0.0.1:6071")
if err != nil {
log.Fatal(err)
}

server := &http.Server{
Addr: "127.0.0.1:6071",
}

go func() {
_ = server.Serve(listener)
}()

pid := os.Getpid()
err = os.WriteFile("server.pid", []byte(strconv.Itoa(pid)), 0644)
if err != nil {
log.Fatal(err)
}

stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
time.Sleep(time.Minute*2)
stop <- syscall.SIGTERM
}()
<-stop
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
server.Shutdown(ctx)
}

-- webserver.sh --
env GOCACHE=$TMPDIR go run webserver.go
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Added

- Added `pint_last_run_duration_seconds` metric.
- Added `--require-owner` flag support to `pint ci` command.

### Fixed

Expand Down

0 comments on commit 7459ba5

Please sign in to comment.