|
5 | 5 | "fmt"
|
6 | 6 | "io/ioutil"
|
7 | 7 | "os"
|
8 |
| - "os/exec" |
9 | 8 | "strings"
|
10 | 9 |
|
11 | 10 | "github.com/dropseed/commitstat/internal/github"
|
@@ -59,26 +58,25 @@ var rootCmd = &cobra.Command{
|
59 | 58 | fmt.Printf("Parsed stat value: %s\n", stat)
|
60 | 59 |
|
61 | 60 | 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 | + |
62 | 66 | githubToken := os.Getenv("GITHUB_TOKEN")
|
63 | 67 | if githubToken == "" {
|
64 | 68 | printErrAndExitFailure(errors.New("GITHUB_TOKEN is required to submit stats"))
|
65 | 69 | }
|
66 | 70 |
|
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 |
| - |
77 | 71 | repo := os.Getenv("GITHUB_REPOSITORY")
|
78 | 72 | if repo == "" {
|
79 | 73 | printErrAndExitFailure(errors.New("GITHUB_REPOSITORY is required"))
|
80 | 74 | }
|
81 | 75 |
|
| 76 | + pushEvent := github.NewGitHubPushEvent(os.Getenv("GITHUB_EVENT_PATH")) |
| 77 | + comparisonRef := pushEvent.GetComparisonRef() |
| 78 | + fmt.Printf("Fetching comparision stat from %s\n", comparisonRef) |
| 79 | + |
82 | 80 | comparisonStat, err := github.FetchRefStat(repo, comparisonRef, statName, githubToken)
|
83 | 81 | if err != nil {
|
84 | 82 | printErrAndExitFailure(err)
|
@@ -107,15 +105,6 @@ var rootCmd = &cobra.Command{
|
107 | 105 | },
|
108 | 106 | }
|
109 | 107 |
|
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 |
| - |
119 | 108 | func init() {
|
120 | 109 | rootCmd.Flags().StringVar(®ex, "regex", "", "regex to parse the stat (optional)")
|
121 | 110 | rootCmd.Flags().StringVar(&statName, "name", "", "name for the stat")
|
|
0 commit comments