Skip to content

Commit 1d793c7

Browse files
committed
Make goal optional and show a success status if not defined
1 parent 09298d4 commit 1d793c7

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,18 @@ $ curl https://raw.githubusercontent.com/dropseed/commitstat/master/install.sh |
2828

2929
## Options
3030

31-
### `--goal`
32-
33-
Either "increase" or "decrease". This is the direction you *want* the stat to go. For example, test coverage should "increase" and if it actually decreases, then a failling status will be reported. If the stat is new or doesn't change, it is considered successful.
34-
3531
### `--name`
3632

3733
The name of the stat.
3834
Will show up as `commitstat/{name}` when submitted as a GitHub commit status.
3935
Changing the name for an existing stat will break the pass/fail comparison until the new name shows up on your main/master branch.
4036

37+
### `--goal` (optional)
38+
39+
Either "increase" or "decrease". This is the direction you *want* the stat to go. For example, test coverage should "increase" and if it actually decreases, then a failling status will be reported. If the stat is new or doesn't change, it is considered successful.
40+
41+
If you don't specify a goal, then you'll get a successful status and the stat/change will be purely informational.
42+
4143
### `--regex` (optional)
4244

4345
A regular expression to parse the file/stdin for a specific value.

cmd/commitstat/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,9 @@ var rootCmd = &cobra.Command{
107107

108108
func init() {
109109
rootCmd.Flags().StringVar(&regex, "regex", "", "regex to parse the stat (optional)")
110+
rootCmd.Flags().StringVar(&goal, "goal", "", "goal for the stat (\"increase\" or \"decrease\", optional)")
110111
rootCmd.Flags().StringVar(&statName, "name", "", "name for the stat")
111112
rootCmd.MarkFlagRequired("name")
112-
rootCmd.Flags().StringVar(&goal, "goal", "", "goal for the stat (\"increase\" or \"decrease\")")
113-
rootCmd.MarkFlagRequired("goal")
114113
}
115114

116115
func printErrAndExitFailure(err error) {

internal/status/main.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,37 @@ func compareStats(stat, comparisonStat, comparisonRef, goal string) (state, stri
5353
return stateError, fmt.Sprintf("Stats are not the same units: %s vs %s", statUnits, prevStatUnits), nil
5454
}
5555

56+
description := ""
57+
58+
if statNum > prevStatNum {
59+
diff := strings.TrimRight(fmt.Sprintf("%f", statNum-prevStatNum), ".0")
60+
description = fmt.Sprintf("%s - increased by %s%s (%s on %s)", stat, diff, statUnits, comparisonStat, comparisonRef)
61+
} else if statNum == prevStatNum {
62+
description = fmt.Sprintf("%s - same as %s", stat, comparisonRef)
63+
} else {
64+
diff := strings.TrimRight(fmt.Sprintf("%f", prevStatNum-statNum), ".0")
65+
description = fmt.Sprintf("%s - decreased by %s%s (%s on %s)", stat, diff, statUnits, comparisonStat, comparisonRef)
66+
}
67+
5668
if goal == "decrease" {
5769
if statNum < prevStatNum {
58-
diff := strings.TrimRight(fmt.Sprintf("%f", prevStatNum-statNum), ".0")
59-
return stateSuccess, fmt.Sprintf("%s - decreased by %s%s (%s on %s)", stat, diff, statUnits, comparisonStat, comparisonRef), nil
70+
return stateSuccess, description, nil
6071
} else if statNum == prevStatNum {
61-
return stateSuccess, fmt.Sprintf("%s - same as %s", stat, comparisonRef), nil
72+
return stateSuccess, description, nil
6273
} else {
63-
diff := strings.TrimRight(fmt.Sprintf("%f", statNum-prevStatNum), ".0")
64-
return stateFailure, fmt.Sprintf("%s - increased by %s%s (%s on %s)", stat, diff, statUnits, comparisonStat, comparisonRef), nil
74+
return stateFailure, description, nil
6575
}
6676
} else if goal == "increase" {
6777
if statNum > prevStatNum {
68-
diff := strings.TrimRight(fmt.Sprintf("%f", statNum-prevStatNum), ".0")
69-
return stateSuccess, fmt.Sprintf("%s - increased by %s%s (%s on %s)", stat, diff, statUnits, comparisonStat, comparisonRef), nil
78+
return stateSuccess, description, nil
7079
} else if statNum == prevStatNum {
71-
return stateSuccess, fmt.Sprintf("%s - same as %s", stat, comparisonRef), nil
80+
return stateSuccess, description, nil
7281
} else {
73-
diff := strings.TrimRight(fmt.Sprintf("%f", prevStatNum-statNum), ".0")
74-
return stateFailure, fmt.Sprintf("%s - decreased by %s%s (%s on %s)", stat, diff, statUnits, comparisonStat, comparisonRef), nil
82+
return stateFailure, description, nil
7583
}
84+
} else if goal == "" {
85+
// Always success if there was no stated goal
86+
return stateSuccess, description, nil
7687
} else {
7788
return stateError, "unknown goal", errors.New("unknown goal")
7889
}

0 commit comments

Comments
 (0)