-
Notifications
You must be signed in to change notification settings - Fork 0
/
label_check.go
58 lines (47 loc) · 1.52 KB
/
label_check.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package cmd
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/antham/versem/github"
"github.com/spf13/cobra"
)
var labelCheckCmd = &cobra.Command{
Use: "check [commitSha|pullRequestId]",
Short: "Ensure a pull request contains a semver label",
Run: setupLabelCheckCmdFunc(labelCheck),
}
func init() {
labelCmd.AddCommand(labelCheckCmd)
}
func setupLabelCheckCmdFunc(f func(messageHandler, semverService, *cobra.Command, []string)) func(*cobra.Command, []string) {
return func(cmd *cobra.Command, args []string) {
msgHandler := newMessageHandler()
f(msgHandler, getSemverLabelService(), cmd, args)
}
}
func labelCheck(msgHandler messageHandler, semverService semverService, cmd *cobra.Command, args []string) {
if len(args) != 1 {
msgHandler.errorFatalStr("provide a pull request number or full commit sha as first argument")
}
var version github.Version
var err error
var successMsg = "%s semver version found"
switch {
case regexp.MustCompile("[0-9a-f]{40}").MatchString(args[0]):
version, err = semverService.GetFromCommit(args[0])
if version == github.NORELEASE {
successMsg += fmt.Sprintf(" or commit %s is not tied to a pull request", args[0])
}
case regexp.MustCompile("[0-9]+").MatchString(args[0]):
n, _ := strconv.Atoi(args[0])
version, err = semverService.GetFromPullRequest(n)
default:
err = fmt.Errorf("%s is not a valid number, nor a valid commit sha", args[0])
}
if err != nil {
msgHandler.errorFatal(err)
}
msgHandler.success(successMsg, strings.ToLower(version.String()))
}