Skip to content

Commit 65301ea

Browse files
committed
Include comparison sha/branch in descriptions
1 parent 70ae4ab commit 65301ea

File tree

3 files changed

+20
-26
lines changed

3 files changed

+20
-26
lines changed

cmd/commitstat/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ var rootCmd = &cobra.Command{
8888
fmt.Printf("No comparison stat found on %s\n", comparisonRef)
8989
}
9090

91-
status, err := status.NewStatus(stat, comparisonStat, goal)
91+
status, err := status.NewStatus(stat, comparisonStat, comparisonRef, goal)
9292
if err != nil {
9393
printErrAndExitFailure(err)
9494
}

internal/github/events.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ func NewGitHubPushEvent(path string) *githubPushEvent {
3535
}
3636

3737
func (event *githubPushEvent) GetComparisonRef() string {
38-
defaultRef := "refs/heads/" + event.Repository.DefaultBranch
39-
if event.Ref == defaultRef {
38+
if event.Ref == "refs/heads/"+event.Repository.DefaultBranch {
4039
// This is a push to master/main, so compare stat to previous commit sha
41-
return event.Before
40+
// (shorten for readability)
41+
return event.Before[:7]
4242
}
4343
// Compare to latest on the default branch
44-
return defaultRef
44+
return event.Repository.DefaultBranch
4545
}

internal/status/main.go

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,26 @@ const (
1616
)
1717

1818
type Status struct {
19-
Stat string
20-
ComparisonStat string
21-
Goal string
22-
State state
23-
Description string
19+
State state
20+
Description string
2421
}
2522

26-
func NewStatus(stat, comparisonStat, goal string) (*Status, error) {
27-
state, description, err := compareStats(stat, comparisonStat, goal)
23+
func NewStatus(stat, comparisonStat, comparisonRef, goal string) (*Status, error) {
24+
state, description, err := compareStats(stat, comparisonStat, comparisonRef, goal)
2825
if err != nil {
2926
return nil, err
3027
}
3128
return &Status{
32-
Stat: stat,
33-
ComparisonStat: comparisonStat,
34-
Goal: goal,
35-
State: state,
36-
Description: description,
29+
State: state,
30+
Description: description,
3731
}, nil
3832
}
3933

40-
func compareStats(stat, comparisonStat, goal string) (state, string, error) {
34+
func compareStats(stat, comparisonStat, comparisonRef, goal string) (state, string, error) {
4135
// IMPORTANT: the current stat needs to be the first thing in the description
4236
// because that's how we will parse it back off
4337
if comparisonStat == "" {
44-
return stateError, fmt.Sprintf("%s - nothing to compare to (stat is either new or being processed simultaneously)", stat), nil
38+
return stateError, fmt.Sprintf("%s - nothing to compare on %s (stat is either new or being processed simultaneously)", comparisonRef, stat), nil
4539
}
4640

4741
statNum, err := parse.ParseStatNumber(stat)
@@ -51,24 +45,24 @@ func compareStats(stat, comparisonStat, goal string) (state, string, error) {
5145

5246
prevStatNum, err := parse.ParseStatNumber(comparisonStat)
5347
if err != nil {
54-
return stateError, "unable to parse comparison stat", err
48+
return stateError, fmt.Sprintf("unable to parse comparison stat from %s", comparisonRef), err
5549
}
5650

5751
if goal == "decrease" {
5852
if statNum < prevStatNum {
59-
return stateSuccess, fmt.Sprintf("%s - less than %s", stat, comparisonStat), nil
53+
return stateSuccess, fmt.Sprintf("%s - less than %s (%s)", stat, comparisonStat, comparisonRef), nil
6054
} else if statNum == prevStatNum {
61-
return stateSuccess, fmt.Sprintf("%s - no change", stat), nil
55+
return stateSuccess, fmt.Sprintf("%s - no change compared to %s", stat, comparisonRef), nil
6256
} else {
63-
return stateFailure, fmt.Sprintf("%s - more than %s", stat, comparisonStat), nil
57+
return stateFailure, fmt.Sprintf("%s - more than %s (%s)", stat, comparisonStat, comparisonRef), nil
6458
}
6559
} else if goal == "increase" {
6660
if statNum > prevStatNum {
67-
return stateSuccess, fmt.Sprintf("%s - more than %s", stat, comparisonStat), nil
61+
return stateSuccess, fmt.Sprintf("%s - more than %s (%s)", stat, comparisonStat, comparisonRef), nil
6862
} else if statNum == prevStatNum {
69-
return stateSuccess, fmt.Sprintf("%s - no change", stat), nil
63+
return stateSuccess, fmt.Sprintf("%s - no change compared to %s", stat, comparisonRef), nil
7064
} else {
71-
return stateFailure, fmt.Sprintf("%s - less than %s", stat, comparisonStat), nil
65+
return stateFailure, fmt.Sprintf("%s - less than %s (%s)", stat, comparisonStat, comparisonRef), nil
7266
}
7367
} else {
7468
return stateError, "unknown goal", errors.New("unknown goal")

0 commit comments

Comments
 (0)