Skip to content

Commit 5d89e8e

Browse files
committed
Add checks for driving GitHub Checks via Actions
1 parent b1aa297 commit 5d89e8e

File tree

12 files changed

+419
-33
lines changed

12 files changed

+419
-33
lines changed

Makefile

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

4+
build/checks:
5+
go build -o bin/checks ./cmd/checks
6+
47
build:
58
go build -o bin/actions ./cmd
69

README.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# go-actions
22

3-
[![](https://img.shields.io/docker/automated/variantdev/actions.svg)](https://hub.docker.com/r/variantdev/actions)
4-
[![](https://img.shields.io/docker/pulls/variantdev/actions.svg)](https://hub.docker.com/r/variantdev/actions)
5-
[![](https://img.shields.io/docker/stars/variantdev/actions.svg)](https://hub.docker.com/r/variantdev/actions)
3+
[![dockeri.co](https://dockeri.co/image/variantdev/actions)](https://hub.docker.com/r/variantdev/actions)
64

75
A collection of usable commands for GitHub v2 Actions, written in Go.
86

@@ -12,6 +10,7 @@ Use for as-is, or reference and inspiration of your own command.
1210

1311
- [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.
1412
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
1514

1615
## Usage
1716

@@ -28,10 +27,31 @@ Usage:
2827
actions [command]
2928
Available Commands:
3029
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
3131
3232
Use "actions [command] --help" for more information about a command
3333
```
3434

35+
### GitHub Actions
36+
37+
Provide `GITHUB_TOKEN` as you usually do on GitHub Actions:
38+
39+
```
40+
name: pullvet
41+
on:
42+
pull_request:
43+
types: [opened, reopened, edited, milestoned, demilestoned, labeled, unlabeled, synchronize ]
44+
jobs:
45+
pullvet:
46+
runs-on: ubuntu-latest
47+
steps:
48+
- uses: docker://variantdev/actions:latest
49+
with:
50+
args: pullvet -require-any -label releasenote/none -note releasenote
51+
env:
52+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
53+
```
54+
3555
## Developing
3656

3757
Run `make build` to build `bin/actions`:

cmd/checks/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# checks
2+
3+
`checks` drives GitHub Checks by creating CheckSuite and CheckRun, running and updating CheckRun
4+
5+
## Rationale
6+
7+
I'm too lazy to subscribe any commercial SaaS or host my own GitHub app just to run [GitHub Checks](https://developer.github.com/v3/checks/).
8+
9+
Just give me a simple Golang program that reads GitHub Actions v2 event json file and do anything other than the business logic(=some shell snippet for tasks like building, testing, etc.)
10+
11+
## Usage
12+
13+
```
14+
$ bin/checks -h
15+
Usage of bin/checks:
16+
-create-run (re)requested
17+
Name of CheckRun to be created on CheckSuite (re)requested event. Specify multiple times to create two or more runs
18+
-github-base-url string
19+
20+
-github-upload-url string
21+
22+
-run string
23+
CheckRun's name to be updated after the command in run
24+
```
25+
26+
## Running locally
27+
28+
Capture actual webhook payloads for `pull_request`, `check_suite`, `check_run` events by running `cat /github/workflow/event.json` on GitHub Actions.
29+
30+
Then build and run `checks` against json files containing captured events:
31+
32+
```
33+
$ make build/checks
34+
35+
# Typically this is run on Actions with `on: pull_requqest`
36+
$ GITHUB_EVENT_PATH=$(pwd)/pull_request_event.json bin/checks
37+
38+
# `on: check_suite`
39+
$ GITHUB_EVENT_PATH=$(pwd)/check_suite_event.json bin/checks -create-run foo -create-run bar
40+
41+
# `on: check_run`
42+
$ GITHUB_EVENT_PATH=$(pwd)/check_run_event.json bin/checks -run foo -- actions pullvet ...
43+
```

cmd/checks/checks.go

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

cmd/main.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package main
33
import (
44
"flag"
55
"fmt"
6-
"github.com/variantdev/go-actions"
7-
"github.com/variantdev/go-actions/pkg/pullvet"
86
"os"
7+
8+
"github.com/variantdev/go-actions/pkg/checks"
9+
"github.com/variantdev/go-actions/pkg/pullvet"
910
)
1011

1112
func flagUsage() {
@@ -15,6 +16,7 @@ Usage:
1516
actions [command]
1617
Available Commands:
1718
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
1820
1921
Use "actions [command] --help" for more information about a command
2022
`
@@ -27,11 +29,14 @@ func fatal(format string, args ...interface{}) {
2729
os.Exit(1)
2830
}
2931

32+
const (
33+
CmdPullvet = "pullvet"
34+
CmdChecks = "checks"
35+
)
36+
3037
func main() {
3138
flag.Usage = flagUsage
3239

33-
CmdPullvet := "pullvet"
34-
3540
if len(os.Args) == 1 {
3641
flag.Usage()
3742
return
@@ -40,18 +45,22 @@ func main() {
4045
switch os.Args[1] {
4146
case CmdPullvet:
4247
fs := flag.NewFlagSet(CmdPullvet, flag.ExitOnError)
43-
cmd := pullvet.NewCommand()
48+
cmd := pullvet.New()
4449
cmd.AddFlags(fs)
4550

4651
fs.Parse(os.Args[2:])
4752

48-
pr, err := actions.PullRequest()
49-
if err != nil {
50-
fmt.Fprintf(os.Stderr, "%v\n", err)
51-
os.Exit(1)
53+
if err := cmd.Run(); err != nil {
54+
fatal("%v\n", err)
5255
}
56+
case CmdChecks:
57+
fs := flag.NewFlagSet(CmdChecks, flag.ExitOnError)
58+
cmd := checks.New()
59+
cmd.AddFlags(fs)
60+
61+
fs.Parse(os.Args[2:])
5362

54-
if err := cmd.Run(pr); err != nil {
63+
if err := cmd.Run(os.Args); err != nil {
5564
fatal("%v\n", err)
5665
}
5766
default:

cmd/pullvet/pullvet.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package main
33
import (
44
"flag"
55
"fmt"
6-
"github.com/variantdev/go-actions"
7-
"github.com/variantdev/go-actions/pkg/pullvet"
86
"os"
7+
8+
"github.com/variantdev/go-actions/pkg/pullvet"
99
)
1010

1111
// pullvet checks for the existence of the specified pull request label(s) exists with a non-zero status
@@ -14,21 +14,15 @@ import (
1414
// This should be useful for compliance purpose. that is, it will help preventing any pr from being merged when it misses required labels.
1515
// when run on GitHub Actions v2
1616
func main() {
17-
cmd := pullvet.NewCommand()
17+
cmd := pullvet.New()
1818

1919
fs := flag.CommandLine
2020
fs.Init("pullvet", flag.ExitOnError)
2121
cmd.AddFlags(fs)
2222

2323
flag.Parse()
2424

25-
pr, err := actions.PullRequest()
26-
if err != nil {
27-
fmt.Fprintf(os.Stderr, "%v\n", err)
28-
os.Exit(1)
29-
}
30-
31-
if err := cmd.Run(pr); err != nil {
25+
if err := cmd.Run(); err != nil {
3226
fmt.Fprintf(os.Stderr, "%v\n", err)
3327
os.Exit(1)
3428
}

event.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package actions
22

33
import (
44
"fmt"
5-
"github.com/google/go-github/github"
5+
"github.com/google/go-github/v28/github"
66
"io/ioutil"
77
"os"
88
)
99

1010
func EventPath() string {
11+
// See https://help.github.com/en/articles/virtual-environments-for-github-actions#default-environment-variables
1112
path := os.Getenv("GITHUB_EVENT_PATH")
1213
if path == "" {
1314
fmt.Fprintf(os.Stderr, "GITHUB_EVENT_PATH not set. Please run this command on GitHub Actions")
@@ -16,6 +17,16 @@ func EventPath() string {
1617
return path
1718
}
1819

20+
func EventName() string {
21+
// See https://help.github.com/en/articles/virtual-environments-for-github-actions#default-environment-variables
22+
name := os.Getenv("GITHUB_EVENT_NAME")
23+
if name == "" {
24+
fmt.Fprintf(os.Stderr, "GITHUB_EVENT_NAME not set. Please run this command on GitHub Actions")
25+
os.Exit(1)
26+
}
27+
return name
28+
}
29+
1930
func Event() []byte {
2031
payload, err := ioutil.ReadFile(EventPath())
2132
if err != nil {
@@ -24,6 +35,10 @@ func Event() []byte {
2435
return payload
2536
}
2637

38+
func ParseEvent() (interface{}, error) {
39+
return github.ParseWebHook(EventName(), Event())
40+
}
41+
2742
func PullRequestEvent() (*github.PullRequestEvent, error) {
2843
evt, err := github.ParseWebHook("pull_request", Event())
2944
if err != nil {

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ module github.com/variantdev/go-actions
33
go 1.12
44

55
require (
6-
github.com/google/go-github v17.0.0+incompatible
7-
github.com/google/go-querystring v1.0.0 // indirect
6+
github.com/google/go-github/v28 v28.1.1
7+
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
88
)

go.sum

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
1-
github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY=
2-
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
1+
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
2+
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
3+
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
4+
github.com/google/go-github/v28 v28.1.1 h1:kORf5ekX5qwXO2mGzXXOjMe/g6ap8ahVe0sBEulhSxo=
5+
github.com/google/go-github/v28 v28.1.1/go.mod h1:bsqJWQX05omyWVmc00nEUql9mhQyv38lDZ8kPZcQVoM=
36
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
47
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
8+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
9+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
10+
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
11+
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e h1:bRhVy7zSSasaqNksaRZiA5EEI+Ei4I1nO5Jh72wfHlg=
12+
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
13+
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
14+
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
15+
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
16+
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0=
17+
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
18+
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
19+
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6 h1:bjcUS9ztw9kFmmIxJInhon/0Is3p+EHBKNgquIzo1OI=
20+
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
21+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
22+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
23+
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
24+
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
25+
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=

0 commit comments

Comments
 (0)