Skip to content

Commit d4bc8ed

Browse files
urldandybons
authored andcommitted
x/build/devapp: support title prefix filtering in HelpWanted handler
A query '/imfeelinghelpful?pkg=net,cmd/go' only redirects to random issues with the prefix 'net' or 'cmd/go'. This allows individuals to browse random issues for packages they have specialized in. Fixes golang/go#20955 Change-Id: I786975fc9f6ea87269176039a3e9233a3500cf06 Reviewed-on: https://go-review.googlesource.com/95275 Reviewed-by: Andrew Bonventre <[email protected]>
1 parent cc2a632 commit d4bc8ed

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed

devapp/server.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type server struct {
3131
cMu sync.RWMutex // Used to protect the fields below.
3232
corpus *maintner.Corpus
3333
repo *maintner.GitHubRepo
34-
helpWantedIssues []int32
34+
helpWantedIssues []issueData
3535
data pageData
3636

3737
// GopherCon-specific fields. Must still hold cMu when reading/writing these.
@@ -40,6 +40,11 @@ type server struct {
4040
totalPoints int
4141
}
4242

43+
type issueData struct {
44+
id int32
45+
titlePrefix string
46+
}
47+
4348
type pageData struct {
4449
release releaseData
4550
reviews reviewsData
@@ -129,17 +134,18 @@ func (s *server) updateHelpWantedIssues() {
129134
s.cMu.Lock()
130135
defer s.cMu.Unlock()
131136

132-
var ids []int32
137+
var issues []issueData
133138
s.repo.ForeachIssue(func(i *maintner.GitHubIssue) error {
134139
if i.Closed {
135140
return nil
136141
}
137142
if i.HasLabelID(labelHelpWantedID) {
138-
ids = append(ids, i.Number)
143+
prefix := strings.SplitN(i.Title, ":", 2)[0]
144+
issues = append(issues, issueData{id: i.Number, titlePrefix: prefix})
139145
}
140146
return nil
141147
})
142-
s.helpWantedIssues = ids
148+
s.helpWantedIssues = issues
143149
}
144150

145151
func (s *server) handleRandomHelpWantedIssue(w http.ResponseWriter, r *http.Request) {
@@ -149,10 +155,34 @@ func (s *server) handleRandomHelpWantedIssue(w http.ResponseWriter, r *http.Requ
149155
http.Redirect(w, r, issuesURLBase, http.StatusSeeOther)
150156
return
151157
}
152-
rid := s.helpWantedIssues[rand.Intn(len(s.helpWantedIssues))]
158+
pkgs := r.URL.Query().Get("pkg")
159+
var rid int32
160+
if pkgs == "" {
161+
rid = s.helpWantedIssues[rand.Intn(len(s.helpWantedIssues))].id
162+
} else {
163+
filtered := s.filteredHelpWantedIssues(strings.Split(pkgs, ",")...)
164+
if len(filtered) == 0 {
165+
http.Redirect(w, r, issuesURLBase, http.StatusSeeOther)
166+
return
167+
}
168+
rid = filtered[rand.Intn(len(filtered))].id
169+
}
153170
http.Redirect(w, r, issuesURLBase+strconv.Itoa(int(rid)), http.StatusSeeOther)
154171
}
155172

173+
func (s *server) filteredHelpWantedIssues(pkgs ...string) []issueData {
174+
var issues []issueData
175+
for _, i := range s.helpWantedIssues {
176+
for _, p := range pkgs {
177+
if strings.HasPrefix(i.titlePrefix, p) {
178+
issues = append(issues, i)
179+
break
180+
}
181+
}
182+
}
183+
return issues
184+
}
185+
156186
func (s *server) handleFavicon(w http.ResponseWriter, r *http.Request) {
157187
// Need to specify content type for consistent tests, without this it's
158188
// determined from mime.types on the box the test is running on

devapp/server_test.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,34 @@ func TestRandomHelpWantedIssue(t *testing.T) {
5959
}
6060

6161
testServer.cMu.Lock()
62-
testServer.helpWantedIssues = []int32{42}
62+
testServer.helpWantedIssues = []issueData{{id: 42}}
63+
testServer.cMu.Unlock()
64+
w = httptest.NewRecorder()
65+
testServer.ServeHTTP(w, req)
66+
if w.Code != http.StatusSeeOther {
67+
t.Errorf("w.Code = %d; want %d", w.Code, http.StatusSeeOther)
68+
}
69+
if g, w := w.Header().Get("Location"), issuesURLBase+"42"; g != w {
70+
t.Errorf("Location header = %q; want %q", g, w)
71+
}
72+
}
73+
74+
func TestRandomFilteredHelpWantedIssue(t *testing.T) {
75+
req := httptest.NewRequest("GET", "/imfeelinglucky?pkg=foo", nil)
76+
w := httptest.NewRecorder()
77+
testServer.ServeHTTP(w, req)
78+
if w.Code != http.StatusSeeOther {
79+
t.Errorf("w.Code = %d; want %d", w.Code, http.StatusSeeOther)
80+
}
81+
if g, w := w.Header().Get("Location"), issuesURLBase; g != w {
82+
t.Errorf("Location header = %q; want %q", g, w)
83+
}
84+
85+
testServer.cMu.Lock()
86+
testServer.helpWantedIssues = []issueData{
87+
{id: 41, titlePrefix: "not/foo: bar"},
88+
{id: 42, titlePrefix: "foo/bar: baz"},
89+
}
6390
testServer.cMu.Unlock()
6491
w = httptest.NewRecorder()
6592
testServer.ServeHTTP(w, req)

0 commit comments

Comments
 (0)