Skip to content

Commit

Permalink
set label on PR based on check result
Browse files Browse the repository at this point in the history
  • Loading branch information
fakeboboliu committed Dec 22, 2024
1 parent 9e89e8b commit 8c668fc
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 9 deletions.
8 changes: 3 additions & 5 deletions tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ require (
github.com/creachadair/command v0.1.13
github.com/creachadair/flax v0.0.0-20240525192034-44db93b3a8ad
github.com/creachadair/mds v0.15.2
github.com/google/go-github/v63 v63.0.0
github.com/creachadair/taskgroup v0.9.0
github.com/google/go-github/v67 v67.0.0
github.com/natefinch/atomic v1.0.1
)

require (
github.com/creachadair/taskgroup v0.9.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
)
require github.com/google/go-querystring v1.1.0 // indirect
6 changes: 4 additions & 2 deletions tools/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ github.com/creachadair/mds v0.15.2 h1:es1qGKgRGSaztpvrSQcZ0B9I6NsHYJ1Sa9naD/3OfC
github.com/creachadair/mds v0.15.2/go.mod h1:4vrFYUzTXMJpMBU+OA292I6IUxKWCCfZkgXg+/kBZMo=
github.com/creachadair/taskgroup v0.9.0 h1:kzXSea5C7R5DtnKFBOTEW3hvmCkiVnRkODMVDMgSS6k=
github.com/creachadair/taskgroup v0.9.0/go.mod h1:+1hJc8zL1rQkxcMVqEYJ0UPGtwl6Iz1+fd4zcOLtt+A=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/google/go-cmp v0.5.2/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/v63 v63.0.0 h1:13xwK/wk9alSokujB9lJkuzdmQuVn2QCPeck76wR3nE=
github.com/google/go-github/v63 v63.0.0/go.mod h1:IqbcrgUmIcEaioWrGYei/09o+ge5vhffGOcxrO0AfmA=
github.com/google/go-github/v67 v67.0.0 h1:g11NDAmfaBaCO8qYdI9fsmbaRipHNWRIU/2YGvlh4rg=
github.com/google/go-github/v67 v67.0.0/go.mod h1:zH3K7BxjFndr9QSeFibx4lTKkYS3K9nDanoI1NjaOtY=
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/natefinch/atomic v1.0.1 h1:ZPYKxkqQOx3KZ+RsbnP/YsgvxWQPGxjC0oBt2AhwV0A=
Expand Down
10 changes: 9 additions & 1 deletion tools/internal/github/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"os"
"time"

"github.com/google/go-github/v63/github"
"github.com/google/go-github/v67/github"
)

// Client is a GitHub API client that performs PSL-specific
Expand Down Expand Up @@ -185,3 +185,11 @@ func (c *Repo) PSLForHash(ctx context.Context, hash string) ([]byte, error) {
}
return []byte(ret), nil
}

func (c *Repo) LabelPullRequest(ctx context.Context, prNum int, labels []string) error {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

_, _, err := c.apiClient().Issues.ReplaceLabelsForIssue(ctx, c.owner(), c.repo(), prNum, labels)
return err
}
100 changes: 100 additions & 0 deletions tools/psltool/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package main

import (
"errors"
"reflect"

"github.com/publicsuffix/list/tools/internal/parser"
)

const (
invalidTag = "❌invalid"
dnsFailTag = "❌FAIL - DNS VALIDATION"
sortFailTag = "❌FAIL - FIX SORTING ⏬"
)

var errToLabel = map[error]string{
// all parser errors, sort in alphabetical order
//parser.ErrCommentPreventsSectionSort{}: "",
parser.ErrConflictingSuffixAndException{}: invalidTag,
//parser.ErrCommentPreventsSuffixSort{}: "",
parser.ErrDuplicateSection{}: invalidTag,
parser.ErrDuplicateSuffix{}: invalidTag,
parser.ErrInvalidEncoding{}: invalidTag,
parser.ErrInvalidSuffix{}: invalidTag,
parser.ErrInvalidUnicode{}: invalidTag,
parser.ErrMissingEntityEmail{}: invalidTag,
parser.ErrMissingEntityName{}: invalidTag,
parser.ErrMissingSection{}: invalidTag,
parser.ErrMissingTXTRecord{}: dnsFailTag,
parser.ErrMismatchedSection{}: invalidTag,
parser.ErrNestedSection{}: invalidTag,
parser.ErrSectionInSuffixBlock{}: invalidTag,
parser.ErrTXTCheckFailure{}: dnsFailTag,
parser.ErrTXTRecordMismatch{}: dnsFailTag,
parser.ErrUnclosedSection{}: invalidTag,
parser.ErrUnknownSection{}: invalidTag,
parser.ErrUnknownSectionMarker{}: invalidTag,
parser.ErrUnknownSectionMarker{}: invalidTag,
parser.ErrUnclosedSection{}: invalidTag,

// all other errors
ErrReformat: sortFailTag,
}

var (
ErrReformat = errors.New("file needs reformatting, run 'psltool fmt' to fix")
)

func errorsToLabels(errs []error) []string {
labels := make([]string, 0, len(errs))

var (
sortSuccess = true
dnsSuccess = true
)
setLabel := func(label string) {
switch label {
case sortFailTag:
sortSuccess = false
case dnsFailTag:
dnsSuccess = false
}
labels = append(labels, label)
}

for _, err := range errs {
if label, ok := errToLabel[err]; ok {
setLabel(label)
continue
}
for tpl, label := range errToLabel {
if isType(err, tpl) {
setLabel(label)
break
}
}
}

if sortSuccess {
labels = append(labels, "✔️Sorting Validated")
}
if dnsSuccess {
labels = append(labels, "✔️DNS _psl Validated")
}

return labels
}

func isType(err error, tpl error) bool {
if errors.Is(err, tpl) {
return true
}
if reflect.TypeOf(err) == reflect.TypeOf(tpl) {
return true
}
if wraped, ok := err.(interface{ Unwrap() error }); ok {
return isType(wraped.Unwrap(), tpl)
}
return false
}
10 changes: 9 additions & 1 deletion tools/psltool/psltool.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,15 @@ func runCheckPR(env *command.Env, prStr string) error {

clean := after.MarshalPSL()
if !bytes.Equal(withPR, clean) {
errs = append(errs, errors.New("file needs reformatting, run 'psltool fmt' to fix"))
errs = append(errs, ErrReformat)
}

// Label the PR base on our errors
if len(errs) > 0 {
labels := errorsToLabels(errs)
if err := client.LabelPullRequest(env.Context(), pr, labels); err != nil {
return fmt.Errorf("failed to set labels on PR: %w", err)
}
}

// Print the blocks marked changed, so a human can check that
Expand Down

0 comments on commit 8c668fc

Please sign in to comment.