Skip to content

Commit 6f3bbe6

Browse files
committed
Use github action event json to find comparison ref
1 parent 7efaecf commit 6f3bbe6

File tree

3 files changed

+59
-25
lines changed

3 files changed

+59
-25
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ $ curl https://rg.gosu.cc/dropseed/commitstat/master/install.sh |
2020

2121
## GitHub Action
2222

23+
You can run commitstat right after your tests and once you have some sort of stat to parse.
24+
2325
```yml
24-
name: commitstat
26+
name: test
2527

26-
on:
27-
pull_request:
28-
types: [synchronize]
28+
on: [push]
2929

3030
jobs:
31-
commitstat:
31+
test:
3232
runs-on: ubuntu-latest
3333
steps:
3434
- name: Checkout

cmd/commitstat/main.go

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"io/ioutil"
77
"os"
8-
"os/exec"
98
"strings"
109

1110
"github.com/dropseed/commitstat/internal/github"
@@ -59,26 +58,25 @@ var rootCmd = &cobra.Command{
5958
fmt.Printf("Parsed stat value: %s\n", stat)
6059

6160
if os.Getenv("GITHUB_ACTIONS") != "" {
61+
if event := os.Getenv("GITHUB_EVENT_NAME"); event != "push" {
62+
println("Stat comparisons are only made for \"push\" events")
63+
return
64+
}
65+
6266
githubToken := os.Getenv("GITHUB_TOKEN")
6367
if githubToken == "" {
6468
printErrAndExitFailure(errors.New("GITHUB_TOKEN is required to submit stats"))
6569
}
6670

67-
comparisonRef := ""
68-
69-
if branch := os.Getenv("GITHUB_BASE_REF"); branch != "" {
70-
fmt.Printf("Fetching comparision stat from %s\n", branch)
71-
comparisonRef = branch
72-
} else {
73-
println("Fetching comparison stat from previous commit")
74-
comparisonRef = gitPreviousSHA()
75-
}
76-
7771
repo := os.Getenv("GITHUB_REPOSITORY")
7872
if repo == "" {
7973
printErrAndExitFailure(errors.New("GITHUB_REPOSITORY is required"))
8074
}
8175

76+
pushEvent := github.NewGitHubPushEvent(os.Getenv("GITHUB_EVENT_PATH"))
77+
comparisonRef := pushEvent.GetComparisonRef()
78+
fmt.Printf("Fetching comparision stat from %s\n", comparisonRef)
79+
8280
comparisonStat, err := github.FetchRefStat(repo, comparisonRef, statName, githubToken)
8381
if err != nil {
8482
printErrAndExitFailure(err)
@@ -107,15 +105,6 @@ var rootCmd = &cobra.Command{
107105
},
108106
}
109107

110-
func gitPreviousSHA() string {
111-
cmd := exec.Command("git", "rev-parse", "HEAD^1")
112-
out, err := cmd.CombinedOutput()
113-
if err != nil {
114-
return ""
115-
}
116-
return string(out)
117-
}
118-
119108
func init() {
120109
rootCmd.Flags().StringVar(&regex, "regex", "", "regex to parse the stat (optional)")
121110
rootCmd.Flags().StringVar(&statName, "name", "", "name for the stat")

internal/github/events.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package github
2+
3+
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
"os"
7+
)
8+
9+
type githubRepository struct {
10+
DefaultBranch string `json:"default_branch"`
11+
}
12+
13+
type githubPushEvent struct {
14+
Before string `json:"before"`
15+
Ref string `json:"ref"`
16+
Repository *githubRepository `json:"repository"`
17+
}
18+
19+
func NewGitHubPushEvent(path string) *githubPushEvent {
20+
jsonFile, err := os.Open(path)
21+
if err != nil {
22+
panic(err)
23+
}
24+
25+
defer jsonFile.Close()
26+
27+
byteValue, _ := ioutil.ReadAll(jsonFile)
28+
29+
var event githubPushEvent
30+
if err := json.Unmarshal([]byte(byteValue), &event); err != nil {
31+
panic(err)
32+
}
33+
34+
return &event
35+
}
36+
37+
func (event *githubPushEvent) GetComparisonRef() string {
38+
defaultRef := "refs/heads/" + event.Repository.DefaultBranch
39+
if event.Ref == defaultRef {
40+
// This is a push to master/main, so compare stat to previous commit sha
41+
return event.Before
42+
}
43+
// Compare to latest on the default branch
44+
return defaultRef
45+
}

0 commit comments

Comments
 (0)