/
story_issue.go
212 lines (173 loc) · 4.67 KB
/
story_issue.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package issues
import (
// Stdlib
"bytes"
"fmt"
"regexp"
// Vendor
"github.com/google/go-github/github"
)
// StoryReviewIssue represents the review issue associated with a story.
type StoryReviewIssue struct {
StoryId string
StoryURL string
StorySummary string
TrackerName string
StoryKey string
*ReviewIssueCommonBody
}
func NewStoryReviewIssue(
storyId string,
storyURL string,
storySummary string,
issueTracker string,
storyKey string,
) *StoryReviewIssue {
return &StoryReviewIssue{
StoryId: storyId,
StoryURL: storyURL,
StorySummary: storySummary,
TrackerName: issueTracker,
StoryKey: storyKey,
ReviewIssueCommonBody: newReviewIssueCommonBody(),
}
}
// Formatting ------------------------------------------------------------------
func (issue *StoryReviewIssue) FormatTitle() string {
return fmt.Sprintf("Review story %v: %v", issue.StoryId, issue.StorySummary)
}
const (
TagIssueTracker = "SF-Issue-Tracker"
TagStoryKey = "SF-Story-Key"
)
var storyReviewIssueBodyTemplate = fmt.Sprintf(`Story being reviewed: [{{.StoryId}}]({{.StoryURL}})
%v: {{.TrackerName}}
%v: {{.StoryKey}}
`, TagIssueTracker, TagStoryKey)
func (issue *StoryReviewIssue) FormatBody() string {
var buffer bytes.Buffer
execTemplate(&buffer, "story review issue body", storyReviewIssueBodyTemplate, issue)
issue.ReviewIssueCommonBody.execTemplate(&buffer)
return buffer.String()
}
// Parsing ---------------------------------------------------------------------
var (
storyIssueTitleRegexp = regexp.MustCompile(`^Review story ([^:]+): (.+)$`)
storyIssueIntroLineRegexp = regexp.MustCompile(`^Story being reviewed: \[([^\]]+)\]\(([^ ]+)\)`)
storyIssueTrackerNameRegexp = regexp.MustCompile(fmt.Sprintf("^%v: (.+)$", TagIssueTracker))
storyIssueStoryKeyRegexp = regexp.MustCompile(fmt.Sprintf("^%v: (.+)$", TagStoryKey))
)
func parseStoryReviewIssue(issue *github.Issue) (*StoryReviewIssue, error) {
var (
title = *issue.Title
body = *issue.Body
)
// Parse the title.
match := storyIssueTitleRegexp.FindStringSubmatch(title)
if len(match) == 0 {
return nil, &ErrInvalidTitle{issue}
}
// match[1] is the story ID, which we don't need right now.
// It is returned from parseStoryReviewIssueIntro.
storySummary := match[2]
// Parse the body.
var err error
scanner := newBodyScanner(issue, body)
// Parse the intro line.
storyId, storyURL := parseStoryReviewIssueIntro(&err, scanner)
// An empty line follows.
readEmptyLine(&err, scanner)
// Parse the metadata.
issueTracker, storyKey := parseStoryReviewIssueMetadata(&err, scanner)
// An empty line follows.
readEmptyLine(&err, scanner)
// Parse the common body.
commonBody := parseRemainingIssueBody(&err, scanner)
// Check the error, at last.
if err != nil {
return nil, err
}
// Return the context.
return &StoryReviewIssue{
StoryId: storyId,
StoryURL: storyURL,
StorySummary: storySummary,
TrackerName: issueTracker,
StoryKey: storyKey,
ReviewIssueCommonBody: commonBody,
}, nil
}
func parseStoryReviewIssueIntro(
err *error,
scanner *bodyScanner,
) (storyId, storySummary string) {
if *err != nil {
return "", ""
}
// Read the intro line.
line, _, ex := scanner.ReadLine()
if ex != nil {
*err = ex
return
}
// Parse the line.
match := storyIssueIntroLineRegexp.FindStringSubmatch(line)
if len(match) != 3 {
*err = scanner.CurrentLineInvalid()
return "", ""
}
storyId, storySummary = match[1], match[2]
// Return the results.
return storyId, storySummary
}
func readEmptyLine(err *error, scanner *bodyScanner) {
if *err != nil {
return
}
// Read the next line.
line, _, ex := scanner.ReadLine()
if ex != nil {
*err = ex
return
}
// Make sure that it is empty.
if line != "" {
*err = scanner.CurrentLineInvalid()
}
}
func parseStoryReviewIssueMetadata(
err *error,
scanner *bodyScanner,
) (issueTracker, storyKey string) {
if *err != nil {
return "", ""
}
// Read the tracker name line.
line, _, ex := scanner.ReadLine()
if ex != nil {
*err = ex
return "", ""
}
// Parse the tracker name tag.
match := storyIssueTrackerNameRegexp.FindStringSubmatch(line)
if len(match) != 2 {
*err = scanner.TagNotFound(TagIssueTracker)
return "", ""
}
issueTracker = match[1]
// Read the story key line.
line, _, ex = scanner.ReadLine()
if ex != nil {
*err = ex
return "", ""
}
// Parse the story key tag.
match = storyIssueStoryKeyRegexp.FindStringSubmatch(line)
if len(match) != 2 {
*err = scanner.TagNotFound(TagStoryKey)
return "", ""
}
storyKey = match[1]
// Return the results.
return issueTracker, storyKey
}