forked from jsantell/fx-devtools-bot
/
utils.go
145 lines (116 loc) · 3.79 KB
/
utils.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"strings"
"github.com/google/go-github/github"
)
import "net/http"
import "github.com/bitly/go-simplejson"
import "regexp"
// MAX_LENGTH determines how long a commit message can be after trimmed,
// while leaving room for the URL and trailing ellipses to remain
// under 140 characters.
const MAX_LENGTH = 112
var compMap = map[string]string{
"3D View": "Tilt",
"Canvas Debugger": "Canvas",
"Graphic Commandline and Toolbar": "gcli",
"Object Inspector": "Inspector", // Alias for brevity and clarity to end users
"Responsive Mode": "Responsive",
"Web Audio Editor": "Audio",
"WebGL Shader Editor": "Shader",
}
var cleanRegExp = regexp.MustCompile("(?i)[\\s]*bug [\\d]{4,9}[\\s]*[\\:\\-]+[\\s]*(.*\\w)\\W*\\s*(a|r)=")
var stripBugRegExp = regexp.MustCompile("(?i)[\\s]*bug [\\d]{4,9}[\\s]*[\\:\\-]+(.*)")
var bugNumberRegExp = regexp.MustCompile("^(?i)[\\s]*bug ([\\d]{1,9})")
var changesetRegExp = regexp.MustCompile("^(?i)Backed out changeset")
var mergeRegExp = regexp.MustCompile("^(?i)merge ")
var bumpRegExp = regexp.MustCompile("^(?i)Bumping (gaia|mani)")
var subcomponentRegExp = regexp.MustCompile("^(?i)Developer Tools: (.*)$")
// Returns a boolean indicating whether or not this commit message
// is useful, ignoring merges, backouts and automated commits.
func IsValidCommit(message string) bool {
return !changesetRegExp.MatchString(message) &&
!mergeRegExp.MatchString(message) &&
!bumpRegExp.MatchString(message)
}
// Takes a DB file store path and a slice of github.RepositoryCommits
// and returns a slice of commits that are valid based off of:
//
// * Have not yet been tweeted
// * Is not a changeset, merge or bump commit
func FilterCommits(dbName string, commits []github.RepositoryCommit) []github.RepositoryCommit {
var filtered []github.RepositoryCommit
for _, commit := range commits {
if IsValidCommit(*commit.Commit.Message) &&
GetSHA(dbName, *commit.SHA) == false {
filtered = append(filtered, commit)
}
}
return filtered
}
func GetJson(url string) (*simplejson.Json, error) {
res, err := http.Get(url)
if err != nil {
return nil, err
}
defer res.Body.Close()
return simplejson.NewFromReader(res.Body)
}
func CreateMessage(message string, bugNumber string, subcomponent string) string {
url := GetBugzillaURL(bugNumber)
result := cleanRegExp.FindStringSubmatch(message)
// Clean up the bug number and everything after the message, like the `r=jsantell` comments and after
if len(result) > 1 {
message = result[1]
// If couldn't parse, just remove the bug number and leave everything else
} else {
result = stripBugRegExp.FindStringSubmatch(message)
// If still not cleaned, who cares, let Twitter deal with it
if len(result) > 1 {
message = result[1]
}
}
message = strings.Trim(message, " ")
if subcomponent != "" {
message = "[" + subcomponent + "]" + " " + message
}
if len(message) > MAX_LENGTH {
message = message[0:MAX_LENGTH] + "..."
}
return message + " " + url
}
func GetBugzillaURL(bugNum string) string {
if bugNum != "" {
return "http://bugzil.la/" + bugNum
} else {
return ""
}
}
func GetBugData(bugNum string) (*simplejson.Json, error) {
j, err := GetJson("https://bugzilla.mozilla.org/rest/bug/" + bugNum)
if err != nil {
return nil, err
}
return j.Get("bugs").GetIndex(0), nil
}
// Get bug number from a commit message
func GetBugNumber(str string) string {
result := bugNumberRegExp.FindStringSubmatch(str)
if len(result) == 2 {
return result[1]
} else {
return ""
}
}
func GetSubComponent(c string) string {
result := subcomponentRegExp.FindStringSubmatch(c)
if len(result) > 1 {
mapping := compMap[result[1]]
if mapping != "" {
return mapping
} else {
return result[1]
}
}
return ""
}