Label PRs that fail the security.txt or DNS validation checks
diff --git a/.github/workflows/psl-validation.yml b/.github/workflows/psl-validation.yml index 5dc6abe..4a857dd 100644 --- a/.github/workflows/psl-validation.yml +++ b/.github/workflows/psl-validation.yml
@@ -27,6 +27,9 @@ name: PSL validation runs-on: ubuntu-latest timeout-minutes: 2 + outputs: + security_txt: ${{ steps.check-pr.outputs.security_txt }} + dns_validation: ${{ steps.check-pr.outputs.dns_validation }} steps: - name: Check out code uses: actions/checkout@v7.0.1 @@ -41,6 +44,7 @@ go-version: 'stable' - name: Run validations + id: check-pr env: GITHUB_TOKEN: ${{ github.token }} OWNER: ${{ github.event.repository.owner.login }} @@ -48,10 +52,56 @@ PR: ${{ github.event.pull_request.number }} run: | cd tools + # psltool prints text taken from the pull request, such as suffixes + # and server responses. Stop the runner from interpreting any of it + # as workflow commands until psltool is done. Its stderr is merged + # into stdout so that all of its output is read before the line + # that resumes command processing. + stop_token="$(openssl rand -hex 16)" + echo "::stop-commands::$stop_token" + status=0 echo "Running PSL validations" && \ go run ./psltool check-pr \ --gh-owner "$OWNER" \ --gh-repo "$REPO" \ --gh-local-clone .. \ --online-checks \ - "$PR" + "$PR" 2>&1 || status=$? + echo "::$stop_token::" + exit "$status" + + # Labelling runs as its own job so that only this job, which handles no + # pull request data beyond the check results, holds a token that can modify + # pull requests. + # + # !cancelled(): the validation job fails when a check fails, which would + # otherwise skip this job — but that failing case is exactly when a label + # must be added. A label is only changed when the validation job got far + # enough to report a result for its check. + label: + name: Label PR + needs: psl-validation + if: ${{ !cancelled() }} + runs-on: ubuntu-latest + timeout-minutes: 2 + permissions: + pull-requests: write + steps: + - name: Label PRs that fail online checks + env: + GH_TOKEN: ${{ github.token }} + REPO: ${{ github.repository }} + PR: ${{ github.event.pull_request.number }} + SECURITY_TXT: ${{ needs.psl-validation.outputs.security_txt }} + DNS_VALIDATION: ${{ needs.psl-validation.outputs.dns_validation }} + run: | + # label <check result> <label name> + label() { + if [ "$1" = "fail" ]; then + gh pr edit "$PR" --repo "$REPO" --add-label "$2" + elif [ "$1" = "pass" ]; then + gh pr edit "$PR" --repo "$REPO" --remove-label "$2" 2>/dev/null || true + fi + } + label "$SECURITY_TXT" "Missing security.txt" + label "$DNS_VALIDATION" "Missing DNS validation"
diff --git a/tools/psltool/psltool.go b/tools/psltool/psltool.go index 8f28b02..6d07ac9 100644 --- a/tools/psltool/psltool.go +++ b/tools/psltool/psltool.go
@@ -253,7 +253,11 @@ ctx, cancel := context.WithTimeout(env.Context(), 300*time.Second) defer cancel() - errs = append(errs, parser.ValidateOnline(ctx, after, &client, prHistory, pr)...) + onlineErrs := parser.ValidateOnline(ctx, after, &client, prHistory, pr) + if err := exportCheckResultsToGHA(onlineErrs); err != nil { + return err + } + errs = append(errs, onlineErrs...) } clean := after.MarshalPSL() @@ -296,6 +300,39 @@ } } +// exportCheckResultsToGHA makes the security.txt and DNS +// validation results available to later steps of a GitHub Actions +// workflow, as the security_txt and dns_validation outputs of the +// current step. Each is "fail" if errs contains a failure of that +// check, and "pass" otherwise. It does nothing when not running in +// GitHub Actions. +func exportCheckResultsToGHA(errs []error) error { + path := os.Getenv("GITHUB_OUTPUT") + if path == "" { + return nil + } + + securityTxt, dnsValidation := "pass", "pass" + for _, err := range errs { + switch err.(type) { + case parser.ErrMissingSecurityTxt, parser.ErrInvalidSecurityTxt: + securityTxt = "fail" + case parser.ErrMissingTXTRecord, parser.ErrTXTRecordMismatch, parser.ErrTXTRecordMultiplePRs, parser.ErrTXTCheckFailure: + dnsValidation = "fail" + } + } + + f, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o644) + if err == nil { + _, err = fmt.Fprintf(f, "security_txt=%s\ndns_validation=%s\n", securityTxt, dnsValidation) + err = errors.Join(err, f.Close()) + } + if err != nil { + return fmt.Errorf("failed to export check results: %w", err) + } + return nil +} + var debugDumpArgs struct { Clean bool `flag:"c,Clean AST before dumping"` Format string `flag:"f,default=ast,Format to dump in, one of 'ast' or 'psl'"`
diff --git a/tools/psltool/psltool_test.go b/tools/psltool/psltool_test.go new file mode 100644 index 0000000..fd4ec7d --- /dev/null +++ b/tools/psltool/psltool_test.go
@@ -0,0 +1,107 @@ +package main + +import ( + "errors" + "os" + "path/filepath" + "testing" + + "github.com/publicsuffix/list/tools/internal/parser" +) + +func TestExportCheckResultsToGHA(t *testing.T) { + tests := []struct { + name string + errs []error + want string + }{ + { + name: "no_errors", + errs: nil, + want: "security_txt=pass\ndns_validation=pass\n", + }, + { + name: "unrelated_error", + errs: []error{errors.New("file needs reformatting")}, + want: "security_txt=pass\ndns_validation=pass\n", + }, + { + name: "missing_security_txt", + errs: []error{errors.New("file needs reformatting"), parser.ErrMissingSecurityTxt{}}, + want: "security_txt=fail\ndns_validation=pass\n", + }, + { + name: "invalid_security_txt", + errs: []error{parser.ErrInvalidSecurityTxt{}, errors.New("file needs reformatting")}, + want: "security_txt=fail\ndns_validation=pass\n", + }, + { + name: "missing_txt_record", + errs: []error{errors.New("file needs reformatting"), parser.ErrMissingTXTRecord{}}, + want: "security_txt=pass\ndns_validation=fail\n", + }, + { + name: "txt_record_mismatch", + errs: []error{parser.ErrTXTRecordMismatch{}}, + want: "security_txt=pass\ndns_validation=fail\n", + }, + { + name: "txt_record_multiple_prs", + errs: []error{parser.ErrTXTRecordMultiplePRs{}}, + want: "security_txt=pass\ndns_validation=fail\n", + }, + { + name: "txt_check_failure", + errs: []error{parser.ErrTXTCheckFailure{}}, + want: "security_txt=pass\ndns_validation=fail\n", + }, + { + name: "missing_security_txt_and_txt_record", + errs: []error{parser.ErrMissingTXTRecord{}, parser.ErrMissingSecurityTxt{}}, + want: "security_txt=fail\ndns_validation=fail\n", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // GitHub Actions creates the file, and every step + // output of the job is appended to it. + const earlierOutput = "earlier=value\n" + path := filepath.Join(t.TempDir(), "github_output") + if err := os.WriteFile(path, []byte(earlierOutput), 0o644); err != nil { + t.Fatal(err) + } + t.Setenv("GITHUB_OUTPUT", path) + + if err := exportCheckResultsToGHA(tc.errs); err != nil { + t.Fatalf("exportCheckResultsToGHA() = %v, want no error", err) + } + + got, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + if want := earlierOutput + tc.want; string(got) != want { + t.Errorf("GITHUB_OUTPUT contains %q, want %q", got, want) + } + }) + } +} + +func TestExportCheckResultsToGHAOutsideGitHubActions(t *testing.T) { + t.Setenv("GITHUB_OUTPUT", "") + // A relative path would be created here if anything was written. + t.Chdir(t.TempDir()) + + if err := exportCheckResultsToGHA([]error{parser.ErrMissingSecurityTxt{}}); err != nil { + t.Errorf("exportCheckResultsToGHA() = %v, want no error", err) + } + + files, err := os.ReadDir(".") + if err != nil { + t.Fatal(err) + } + if len(files) != 0 { + t.Errorf("exportCheckResultsToGHA() created %d files, want none", len(files)) + } +}