Skip to content

Commit

Permalink
Improve debugging output from the action execution
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Orive <adrian.orive.oneca@gmail.com>
  • Loading branch information
Adirio committed Nov 20, 2020
1 parent 52659fd commit d573ec9
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ inputs:
required: true
runs:
using: docker
image: '../Dockerfile'
image: '../../../Dockerfile'
17 changes: 9 additions & 8 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
name: PR Verifier

on:
pull_request_target:
types: [opened, edited, reopened, synchronize]

jobs:
verify:
name: Verify PR contents
runs-on: ubuntu-latest
name: verify PR contents
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Verifier action
id: verifier
uses: ./action-nightly
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
- name: Checkout
uses: actions/checkout@v2
- name: Verifier action
uses: ./.github/actions/verifier
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ The code that actually runs lives in [verify/cmd](/verify/cmd), while
from GitHub actions & uploading the result via the GitHub checks API.

This repo itself uses a "live" version of the action that always rebuilds
from the local code, which lives in [action-nightly](/action-nightly).
from the local code (master branch), which lives in
[.github/actions/verifier](/.github/actions/verifier).

### Updating the action

Expand Down
25 changes: 19 additions & 6 deletions verify/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,31 @@ package verify

import (
"fmt"
"strings"
)

const (
errorPrefix = "::error::"
debugPrefix = "::debug::"
warningPrefix = "::debug::"
)

type logger struct{}

func (logger) errorf(format string, args ...interface{}) {
fmt.Printf("::error::"+format+"\n", args...)
func (logger) log(prefix, content string) {
for _, s := range strings.Split(content, "\n") {
fmt.Println(prefix + s)
}
}

func (l logger) errorf(format string, args ...interface{}) {
l.log(errorPrefix, fmt.Sprintf(format, args...))
}

func (logger) debugf(format string, args ...interface{}) {
fmt.Printf("::debug::"+format+"\n", args...)
func (l logger) debugf(format string, args ...interface{}) {
l.log(debugPrefix, fmt.Sprintf(format, args...))
}

func (logger) warningf(format string, args ...interface{}) {
fmt.Printf("::warning::"+format+"\n", args...)
func (l logger) warningf(format string, args ...interface{}) {
l.log(warningPrefix, fmt.Sprintf(format, args...))
}
55 changes: 35 additions & 20 deletions verify/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ type PRPlugin struct {
logger
}

func (p PRPlugin) errorf(format string, args ...interface{}) {
p.logger.errorf(fmt.Sprintf("[%s]", p.Name) + format, args...)
}

func (p PRPlugin) debugf(format string, args ...interface{}) {
p.logger.debugf(fmt.Sprintf("[%s]", p.Name) + format, args...)
}

func (p PRPlugin) warningf(format string, args ...interface{}) {
p.logger.warningf(fmt.Sprintf("[%s]", p.Name) + format, args...)
}

// processPR executes the provided ProcessPR and parses the result
func (p PRPlugin) processPR(pr *github.PullRequest) (conclusion, summary, text string, err error) {
text, err = p.ProcessPR(pr)
Expand Down Expand Up @@ -101,13 +113,13 @@ func (p PRPlugin) createCheckRun(client *github.Client, owner, repo, headSHA str
Status: Started.StringP(),
},
)
if err != nil {
return nil, fmt.Errorf("unable to submit check result: %w", err)
}

p.debugf("create check API response: %+v", res)
p.debugf("created run: %+v", checkRun)

if err != nil {
return nil, fmt.Errorf("unable to create check run: %w", err)
}
return checkRun, nil
}

Expand All @@ -125,22 +137,25 @@ func (p PRPlugin) getCheckRun(client *github.Client, owner, repo, headSHA string
CheckName: github.String(p.Name),
},
)
if err != nil {
return nil, err
}

p.debugf("list check API response: %+v", res)
p.debugf("listed runs: %+v", checkRunList)

if err != nil {
return nil, fmt.Errorf("unable to get check run: %w", err)
}

switch n := *checkRunList.Total; {
case n == 0:
return p.createCheckRun(client, owner, repo, headSHA)
case n == 1:
return checkRunList.CheckRuns[0], nil
case n > 1:
return nil, fmt.Errorf("multiple instances of `%s` check run found on %s/%s @ %s", p.Name, owner, repo, headSHA)
return nil, fmt.Errorf("multiple instances of `%s` check run found on %s/%s @ %s",
p.Name, owner, repo, headSHA)
default: // Should never happen
return nil, fmt.Errorf("negative number of instances (%d) of `%s` check run found on %s/%s @ %s", n, p.Name, owner, repo, headSHA)
return nil, fmt.Errorf("negative number of instances (%d) of `%s` check run found on %s/%s @ %s",
n, p.Name, owner, repo, headSHA)
}
}

Expand All @@ -154,7 +169,7 @@ func (p PRPlugin) resetCheckRun(client *github.Client, owner, repo string, headS
return checkRun, err
}

p.debugf("updating check run %q on %s/%s...", p.Name, owner, repo)
p.debugf("resetting check run %q on %s/%s...", p.Name, owner, repo)

checkRun, updateResp, err := client.Checks.UpdateCheckRun(
context.TODO(),
Expand All @@ -166,20 +181,20 @@ func (p PRPlugin) resetCheckRun(client *github.Client, owner, repo string, headS
Status: github.String("in-progress"),
},
)
if err != nil {
return checkRun, fmt.Errorf("unable to update check result: %w", err)
}

p.debugf("update check API response: %+v", updateResp)
p.debugf("updated run: %+v", checkRun)

if err != nil {
return checkRun, fmt.Errorf("unable to reset check run: %w", err)
}
return checkRun, nil
}

// finishCheckRun updates the Check-Run with id checkRunID setting its output.
// It returns an error in case it couldn't be updated.
func (p PRPlugin) finishCheckRun(client *github.Client, owner, repo string, checkRunID int64, conclusion, summary, text string) error {
p.debugf("updating check run %q on %s/%s...", p.Name, owner, repo)
p.debugf("adding results to check run %q on %s/%s...", p.Name, owner, repo)

checkRun, updateResp, err := client.Checks.UpdateCheckRun(context.TODO(), owner, repo, checkRunID, github.UpdateCheckRunOptions{
Name: p.Name,
Expand All @@ -191,19 +206,19 @@ func (p PRPlugin) finishCheckRun(client *github.Client, owner, repo string, chec
Text: github.String(text),
},
})
if err != nil {
return fmt.Errorf("unable to update check result: %w", err)
}

p.debugf("update check API response: %+v", updateResp)
p.debugf("updated run: %+v", checkRun)

if err != nil {
return fmt.Errorf("unable to update check run with results: %w", err)
}
return nil
}

// duplicateCheckRun creates a new Check-Run with the same info as the provided one but for a new headSHA
func (p PRPlugin) duplicateCheckRun(client *github.Client, owner, repo, headSHA string, checkRun *github.CheckRun) (*github.CheckRun, error) {
p.debugf("creating check run %q on %s/%s @ %s...", p.Name, owner, repo, headSHA)
p.debugf("duplicating check run %q on %s/%s @ %s...", p.Name, owner, repo, headSHA)

checkRun, res, err := client.Checks.CreateCheckRun(
context.TODO(),
Expand All @@ -221,13 +236,13 @@ func (p PRPlugin) duplicateCheckRun(client *github.Client, owner, repo, headSHA
Output: checkRun.Output,
},
)
if err != nil {
return nil, fmt.Errorf("unable to submit check result: %w", err)
}

p.debugf("create check API response: %+v", res)
p.debugf("created run: %+v", checkRun)

if err != nil {
return checkRun, fmt.Errorf("unable to duplicate check run: %w", err)
}
return checkRun, nil
}

Expand Down

0 comments on commit d573ec9

Please sign in to comment.