Skip to content

Commit

Permalink
Merge pull request #14 from Adirio/workflows
Browse files Browse the repository at this point in the history
Improve debugging capabilities
  • Loading branch information
Adirio committed Nov 19, 2020
2 parents 52659fd + 05e5c5e commit bc677cf
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ inputs:
github_token:
description: "the github_token provided by the actions runner"
required: true
suffix:
description: "suffix for Check Run names"
required: false
default: ""
runs:
using: docker
image: '../Dockerfile'
image: '../../../Dockerfile'
32 changes: 24 additions & 8 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
name: PR Verifier

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

jobs:
verify:
name: Verify PR contents (${{ github.repository }}:${{ github.base_ref }})
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Verifier action
uses: ./.github/actions/verifier
with:
github_token: ${{ secrets.GITHUB_TOKEN }}

verify-pr:
name: Verify PR contents (${{ github.event.pull_request.head.repo.full_name }}:${{ github.head_ref }})
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
with:
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.head_ref }}
- name: Verifier action
uses: ./.github/actions/verifier
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
suffix: (${{ github.event.pull_request.head.repo.full_name }}:${{ github.head_ref }})
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ The code that actually runs lives in [verify/cmd](/verify/cmd), while
[/verify](/verify) contains a framework for running PR description checks
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).
This repo itself uses two "live" version of the action that always rebuilds
from the local code (master branch) and from the PR branch, which lives in
[.github/actions/verifier](/.github/actions/verifier).

### Updating the action

Expand Down
6 changes: 6 additions & 0 deletions verify/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type ActionsEnv struct {
Repo string
Event *github.PullRequestEvent
Client *github.Client
Suffix string
}

func setupEnv() (*ActionsEnv, error) {
Expand Down Expand Up @@ -79,6 +80,7 @@ func setupEnv() (*ActionsEnv, error) {
Repo: ownerAndRepo[1],
Event: &event,
Client: client,
Suffix: os.Getenv("INPUT_SUFFIX"),
}, nil
}

Expand All @@ -104,6 +106,10 @@ func RunPlugins(plugins ...PRPlugin) ActionsCallback {
var done sync.WaitGroup

for _, plugin := range plugins {
if env.Suffix != "" {
plugin.Name += " " + env.Suffix
}

done.Add(1)
go func(plugin PRPlugin) {
defer done.Done()
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 bc677cf

Please sign in to comment.