Skip to content

Commit ce4c4e9

Browse files
committed
rename checks to exec
1 parent 22baf07 commit ce4c4e9

File tree

12 files changed

+440
-39
lines changed

12 files changed

+440
-39
lines changed

.github/workflows/dev.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
make build
4949
- name: dev1
5050
run: |
51-
bin/actions checks -run dev1 -- bin/actions pullvet -require-any -label dev1
51+
bin/actions exec -check-run-name dev1 -- bin/actions pullvet -require-any -label dev1
5252
env:
5353
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
5454
ensure-dev2:
@@ -63,7 +63,7 @@ jobs:
6363
make build
6464
- name: dev2
6565
run: |
66-
bin/actions checks -run dev2 -- bin/actions pullvet -require-any -label dev2 -label dev3
66+
bin/actions exec -check-run-name dev2 -- bin/actions pullvet -require-any -label dev2 -label dev3
6767
env:
6868
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
6969
ensure-dev3:
@@ -78,7 +78,7 @@ jobs:
7878
make build
7979
- name: dev3
8080
run: |
81-
bin/actions checks -status-context checks/dev3 -- bin/actions pullvet -require-any -label dev3
81+
bin/actions exec -status-context checks/dev3 -- bin/actions pullvet -require-any -label dev3
8282
env:
8383
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
8484
ensure-changelog:
@@ -93,7 +93,7 @@ jobs:
9393
make build
9494
- name: changelog
9595
run: |
96-
bin/actions checks -status-context checks/changelog -- bin/actions pullvet -require-any -note changelog1 -note changelog2
96+
bin/actions exec -status-context checks/changelog -- bin/actions pullvet -require-any -note changelog1 -note changelog2
9797
env:
9898
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
9999
ensure-releasenote:
@@ -104,6 +104,6 @@ jobs:
104104
with:
105105
entrypoint: sh
106106
args: |
107-
-c "actions checks -status-context checks/releasenote -- actions pullvet -require-any -label releasenote/none -note releasenote"
107+
-c "actions exec -status-context checks/releasenote -- actions pullvet -require-any -label releasenote/none -note releasenote"
108108
env:
109109
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
build/pullvet:
22
go build -o bin/pullvet ./cmd/pullvet
33

4-
build/checks:
5-
go build -o bin/checks ./cmd/checks
4+
build/exec:
5+
go build -o bin/exec ./cmd/exec
66

77
build:
88
go build -o bin/actions ./cmd

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Use for as-is, or reference and inspiration of your own command.
1010

1111
- [pullvet](https://github.com/variantdev/go-actions/tree/master/cmd/pullvet) checks labels and milestones associated to each pull request for project management and compliance.
1212
A pullvet rule looks like `accept only PR that does have at least one of these labels and one or more release notes in the description`.
13-
- [checks](https://github.com/variantdev/go-actions/tree/master/cmd/checks) checks drives GitHub Checks by creating CheckSuite and CheckRun, running and updating CheckRun
13+
- [exec](https://github.com/variantdev/go-actions/tree/master/cmd/xec) checks drives GitHub Checks by creating CheckSuite and CheckRun, running and updating CheckRun
1414

1515
## Usage
1616

@@ -27,7 +27,7 @@ Usage:
2727
actions [command]
2828
Available Commands:
2929
pullvet checks labels and milestones associated to each pull request for project management and compliance
30-
checks checks drives GitHub Checks by creating CheckSuite and CheckRun, running and updating CheckRun
30+
exec runs an arbitrary command and updates GitHub "Check Run" and/or "Status" accordingly
3131
3232
Use "actions [command] --help" for more information about a command
3333
```

cmd/checks/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Usage of bin/checks:
3232
> and there's currently no way to obtain an installation token locally,
3333
> you can't test this locally.
3434
35-
Capture actual webhook payloads for `pull_request`, `check_suite`, `check_run` events by running `cat /github/workflow/event.json` on GitHub Actions.
35+
Capture actual webhook payloads for `pull_request`, `check_suite`, `check_run` events by running `cat $GITHUB_EVENT_PATH` on GitHub Actions.
3636

3737
Install this workflow onto your test repository:
3838

cmd/checks/checks.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,8 @@ import (
88
"github.com/variantdev/go-actions/pkg/checks"
99
)
1010

11-
// pullvet checks for the existence of the specified pull request label(s) exists with a non-zero status
12-
// whenever one ore more required labels are missing in the pull request
13-
//
14-
// This should be useful for compliance purpose. that is, it will help preventing any pr from being merged when it misses required labels.
15-
// when run on GitHub Actions v2
1611
func main() {
17-
cmd := checks.New()
12+
cmd := exec.New()
1813

1914
fs := flag.CommandLine
2015
fs.Init("checks", flag.ExitOnError)

cmd/exec/README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# checks
2+
3+
`exec` runs an arbitrary command and updates GitHub "Check Run" and/or "Status" accordingly.
4+
5+
## Rationale
6+
7+
GitHub Actions v2 has a known issue that prevents the "Required Status Checks" branch protection from working due to
8+
duplicate pull request statuses created by multiple workflow runs against a specific Git commit.
9+
10+
Give me a simple CLI program that updates Check Run and/or pull request Status without duplication so that the branch protection works.
11+
12+
## Usage
13+
14+
```
15+
$ bin/actions exec -help
16+
Usage of exec:
17+
-check-run-name string
18+
CheckRun's name to be updated after the command in run
19+
-github-base-url string
20+
21+
-github-upload-url string
22+
23+
-status-context exec
24+
Commit status' context. If not empty, exec creates a status with this context
25+
-status-description exec
26+
Commit status' description. exec creates a status with this description
27+
```
28+
29+
## Running locally
30+
31+
Capture actual webhook payloads for `pull_request`by running `cat $GITHUB_EVENT_PATH` on GitHub Actions.
32+
33+
Install this workflow onto your test repository:
34+
35+
https://github.com/variantdev/variant-github-actions-demo/blob/master/.github/workflows/dump-event.yml
36+
37+
Open a PR against the repo:
38+
39+
```
40+
git checkout -b test
41+
git commit --allow-empty
42+
git push
43+
hub pull-request
44+
```
45+
46+
Browse to the Actions tab in your repo:
47+
48+
```
49+
open https://github.com/USER/REPO/actions
50+
```
51+
52+
Nagivate to the workflow run that corresponds to the pull request and browse `View raw logs` in the popup menu.
53+
54+
Copy the dump of the event.json and save it as a json file:
55+
56+
```
57+
$ pbpaste | awk '{$1=""; print $0;}' > pull_request_event.json
58+
```
59+
60+
Then build and run `exec` against json files containing captured events:
61+
62+
```
63+
$ make build/exec
64+
65+
# Typically this is run on Actions with `on: pull_requqest`
66+
$ GITHUB_TOKEN_TYPE=bearer GITHUB_EVENT_PATH=$(pwd)/pull_request_event.json GITHUB_EVENT_NAME=pull_request bin/exec -status-context ci/test
67+
```

cmd/exec/exec.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"os"
7+
8+
"github.com/variantdev/go-actions/pkg/exec"
9+
)
10+
11+
func main() {
12+
cmd := exec.New()
13+
14+
fs := flag.CommandLine
15+
fs.Init("exec", flag.ExitOnError)
16+
cmd.AddFlags(fs)
17+
18+
flag.Parse()
19+
20+
if err := cmd.Run(fs.Args()); err != nil {
21+
fmt.Fprintf(os.Stderr, "%v\n", err)
22+
os.Exit(1)
23+
}
24+
}

cmd/main.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"os"
77

8-
"github.com/variantdev/go-actions/pkg/checks"
8+
"github.com/variantdev/go-actions/pkg/exec"
99
"github.com/variantdev/go-actions/pkg/pullvet"
1010
)
1111

@@ -16,7 +16,7 @@ Usage:
1616
actions [command]
1717
Available Commands:
1818
pullvet checks labels and milestones associated to each pull request for project management and compliance
19-
checks drives GitHub Checks by creating CheckSuite and CheckRun, running and updating CheckRun
19+
exec runs an arbitrary command and updates GitHub "Check Run" and/or "Status" accordingly.
2020
2121
Use "actions [command] --help" for more information about a command
2222
`
@@ -31,7 +31,7 @@ func fatal(format string, args ...interface{}) {
3131

3232
const (
3333
CmdPullvet = "pullvet"
34-
CmdChecks = "checks"
34+
CmdExec = "exec"
3535
)
3636

3737
func main() {
@@ -53,9 +53,9 @@ func main() {
5353
if err := cmd.Run(); err != nil {
5454
fatal("%v\n", err)
5555
}
56-
case CmdChecks:
57-
fs := flag.NewFlagSet(CmdChecks, flag.ExitOnError)
58-
cmd := checks.New()
56+
case CmdExec:
57+
fs := flag.NewFlagSet(CmdExec, flag.ExitOnError)
58+
cmd := exec.New()
5959
cmd.AddFlags(fs)
6060

6161
fs.Parse(os.Args[2:])

pkg/checks/checks.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ type Command struct {
2222
BaseURL, UploadURL string
2323
createRuns cmd.StringSlice
2424

25-
checkName string
25+
checkRunName string
2626

27-
statusContext string
27+
statusContext string
28+
statusDescription string
2829

2930
cmd string
3031
args []string
@@ -41,8 +42,9 @@ func (c *Command) AddFlags(fs *flag.FlagSet) {
4142
fs.StringVar(&c.BaseURL, "github-base-url", "", "")
4243
fs.StringVar(&c.UploadURL, "github-upload-url", "", "")
4344
fs.Var(&c.createRuns, "create-run", "Name of CheckRun to be created on CheckSuite `(re)requested` event. Specify multiple times to create two or more runs")
44-
fs.StringVar(&c.checkName, "run", "", "CheckRun's name to be updated after the command in run")
45-
fs.StringVar(&c.statusContext, "status-context", "", "Commit status' context. If not empty, `checks` creates a status with this contecxt")
45+
fs.StringVar(&c.checkRunName, "check-run-name", "", "CheckRun's name to be updated after the command in run")
46+
fs.StringVar(&c.statusContext, "status-context", "", "Commit status' context. If not empty, `exec` creates a status with this context")
47+
fs.StringVar(&c.statusDescription, "status-description", "", "Commit status' description. `exec` creates a status with this description")
4648
}
4749

4850
func (c *Command) Run(args []string) error {
@@ -178,21 +180,21 @@ func (c *Command) EnsureCheckRun(pre *github.PullRequestEvent) error {
178180
owner := pre.Repo.Owner.GetLogin()
179181
repo := pre.Repo.GetName()
180182

181-
if c.checkName != "" {
183+
if c.checkRunName != "" {
182184
suite, err := c.EnsureCheckSuite(pre)
183185
if err != nil {
184186
return err
185187
}
186188

187189
cr := Run{
188-
name: c.checkName,
190+
name: c.checkRunName,
189191
owner: owner,
190192
repo: repo,
191193
suiteId: suite.GetID(),
192194
}
193195

194196
checkRunsList, _, err := client.Checks.ListCheckRunsCheckSuite(context.Background(), cr.owner, cr.repo, cr.suiteId, &github.ListCheckRunsOptions{
195-
CheckName: github.String(c.checkName),
197+
CheckName: github.String(c.checkRunName),
196198
// TODO
197199
//ListOptions: github.ListOptions{},
198200
})
@@ -286,8 +288,8 @@ func (c *Command) ExecCheckRun(e *github.CheckRunEvent) error {
286288
}
287289

288290
func (c *Command) UpdateCheckRun(owner, repo string, checkRun *github.CheckRun, summary, text string, runErr error) error {
289-
if checkRun.GetName() != c.checkName {
290-
return fmt.Errorf("unexpected run name: expected %q, got %q", c.checkName, checkRun.GetName())
291+
if checkRun.GetName() != c.checkRunName {
292+
return fmt.Errorf("unexpected run name: expected %q, got %q", c.checkRunName, checkRun.GetName())
291293
}
292294

293295
client, err := c.instTokenClient()
@@ -327,7 +329,7 @@ func (c *Command) UpdateCheckRun(owner, repo string, checkRun *github.CheckRun,
327329
}
328330

329331
func (c *Command) runIt() (string, string, error) {
330-
return runCmd(c.cmd, c.args)
332+
return actions.RunCmd(c.cmd, c.args)
331333
}
332334

333335
func (c *Command) logResponseAndError(suites *github.ListCheckSuiteResults, res *github.Response, err error) error {
@@ -374,10 +376,10 @@ func (c *Command) getSuite(pre *github.PullRequestEvent) (*github.CheckSuite, er
374376

375377
c.logResponseAndError(suites, res, err)
376378

377-
log.Printf("Listing relevant suites for check name %q...", c.checkName)
379+
log.Printf("Listing relevant suites for check name %q...", c.checkRunName)
378380

379381
suites, res, err = client.Checks.ListCheckSuitesForRef(context.Background(), owner, repo, sha, &github.ListCheckSuiteOptions{
380-
CheckName: github.String(c.checkName),
382+
CheckName: github.String(c.checkRunName),
381383
})
382384

383385
if err := c.logResponseAndError(suites, res, err); err != nil {
@@ -468,4 +470,3 @@ func (c *Command) CreateCheckSuite(pre *github.PullRequestEvent) (*github.CheckS
468470
func (c *Command) instTokenClient() (*github.Client, error) {
469471
return actions.CreateInstallationTokenClient(os.Getenv("GITHUB_TOKEN"), c.BaseURL, c.UploadURL)
470472
}
471-

0 commit comments

Comments
 (0)