-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
71 lines (62 loc) · 1.34 KB
/
main.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
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"fmt"
"log"
"regexp"
)
const (
// TicketRegex is the regular expression used to match a JIRA ticket.
// Understood to be a sequence of capital letters, followed by a hyphen,
// followed by a sequence of numbers.
TicketRegex = "[A-Z]+-[\\d]+"
)
var (
regex = regexp.MustCompile(TicketRegex)
)
func currentBranch(creds credentials) {
branchName, err := getCurrentBranch()
if err != nil {
log.Fatal(err)
}
if !regex.MatchString(branchName) {
log.Fatalf("Branch name must match the JIRA format. This branch: %v\n", branchName)
}
issue, err := getTicket(branchName, creds)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s: %+v\n", issue.Key, issue.Fields.Summary)
}
func allBranches(creds credentials) {
branches, err := getAllBranches()
if err != nil {
log.Fatal(err)
}
for i, b := range branches {
if !regex.MatchString(b) {
branches = append(branches[:i], branches[i+1:]...)
}
}
if len(branches) == 0 {
log.Fatal("no branches match JIRA format")
}
issues, err := getTickets(branches, creds)
if err != nil {
log.Fatal(err)
}
for _, issue := range issues {
fmt.Printf("%s: %+v\n", issue.Key, issue.Fields.Summary)
}
}
func main() {
parseFlags()
creds, err := loadCredentials()
if err != nil {
log.Fatal(err)
}
if opts.AllBranches {
allBranches(creds)
} else {
currentBranch(creds)
}
}