Skip to content

Commit

Permalink
Correctly report file parse errors in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed Apr 6, 2022
1 parent 1560729 commit 6f0aa8a
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 8 deletions.
8 changes: 4 additions & 4 deletions cmd/pint/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ func tryDecodingYamlError(e string) (int, string) {
return 1, e
}

func scanProblem(path string, err error) reporter.Report {
func scanProblem(path, owner string, err error) reporter.Report {
line, e := tryDecodingYamlError(err.Error())
return reporter.Report{
Path: path,
Path: path,
Owner: owner,
Problem: checks.Problem{
Lines: []int{line},
Reporter: yamlParseReporter,
Expand Down Expand Up @@ -127,7 +128,7 @@ func scanWorker(ctx context.Context, jobs <-chan scanJob, results chan<- reporte
return
default:
if job.entry.PathError != nil {
results <- scanProblem(job.entry.Path, job.entry.PathError)
results <- scanProblem(job.entry.Path, job.entry.Owner, job.entry.PathError)
} else if job.entry.Rule.Error.Err != nil {
results <- reporter.Report{
Path: job.entry.Path,
Expand All @@ -142,7 +143,6 @@ func scanWorker(ctx context.Context, jobs <-chan scanJob, results chan<- reporte
},
Owner: job.entry.Owner,
}
continue
} else {
start := time.Now()
probles := job.check.Check(ctx, job.entry.Rule, job.allEntries)
Expand Down
112 changes: 112 additions & 0 deletions cmd/pint/tests/0070_bitbucket_string.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
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 GIT_AUTHOR_EMAIL=pint@example.com
env GIT_COMMITTER_NAME=pint
env GIT_COMMITTER_EMAIL=pint@example.com
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 debug --no-color ci
! stdout .
stderr 'result\\":\\"FAIL\\"'
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'
exec sh -c 'cat ../server.pid | xargs kill'

-- src/v1.yml --
- alert: rule1
expr: sum(foo) by(job)
- alert: rule2
expr: sum(foo) by(job)
for: 0s

-- src/v2.yml --
- alert: rule1
expr: sum(foo) by(job)
for: 0s
- alert: rule2
expr: sum(foo) by(job)
for: 0s

-- src/.pint.hcl --
ci {
baseBranch = "main"
}
repository {
bitbucket {
uri = "http://127.0.0.1:6070"
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:6070")
if err != nil {
log.Fatal(err)
}

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

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
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v0.17.2

### Fixed

- File parse errors were not reported correctly when running `pint ci`.

## v0.17.1

### Fixed
Expand Down
8 changes: 6 additions & 2 deletions internal/discovery/git_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,12 @@ func (f GitBranchFinder) Find() (entries []Entry, err error) {
return nil, err
}
for _, e := range els {
e.ModifiedLines = alloweLines
if isOverlap(alloweLines, e.Rule.Lines()) {
if len(e.ModifiedLines) == 0 {
e.ModifiedLines = alloweLines
if isOverlap(alloweLines, e.Rule.Lines()) {
entries = append(entries, e)
}
} else if isOverlap(alloweLines, e.ModifiedLines) {
entries = append(entries, e)
}
}
Expand Down
5 changes: 3 additions & 2 deletions internal/discovery/git_branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ R090 foo/c2c.yml c2c.yml
0,
nil,
),
rules: map[string][]string{},
rules: map[string][]string{"foo.yml": {""}},
},
{
files: map[string]string{
Expand Down Expand Up @@ -451,7 +451,8 @@ R090 foo/c2c.yml c2c.yml
var name string
if e.Rule.AlertingRule != nil {
name = e.Rule.AlertingRule.Alert.Value.Value
} else {
}
if e.Rule.RecordingRule != nil {
name = e.Rule.RecordingRule.Record.Value.Value
}
m[e.Path] = append(m[e.Path], name)
Expand Down

0 comments on commit 6f0aa8a

Please sign in to comment.