blob: f0c66a256ff58cceed4cb1b4ac9f8e6a7bf93f3a [file] [log] [blame]
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"path"
"sort"
"strconv"
"sync"
"time"
"golang.org/x/build/maintner"
"golang.org/x/build/maintner/godata"
)
// A server is an http.Handler that serves content within staticDir at root and
// the dynamically-generated dashboards at their respective endpoints.
type server struct {
mux *http.ServeMux
staticDir string
cMu sync.RWMutex // Used to protect the fields below.
corpus *maintner.Corpus
helpWantedIssues []int32
userMapping map[int]*maintner.GitHubUser // Gerrit Owner ID => GitHub user
activities []activity // All contribution activities
totalPoints int
}
func newServer(mux *http.ServeMux, staticDir string) *server {
s := &server{
mux: mux,
staticDir: staticDir,
userMapping: map[int]*maintner.GitHubUser{},
}
s.mux.Handle("/", http.FileServer(http.Dir(s.staticDir)))
s.mux.HandleFunc("/favicon.ico", s.handleFavicon)
s.mux.HandleFunc("/release", handleRelease)
for _, p := range []string{"/imfeelinghelpful", "/imfeelinglucky"} {
s.mux.HandleFunc(p, s.handleRandomHelpWantedIssue)
}
s.mux.HandleFunc("/_/activities", s.handleActivities)
return s
}
// initCorpus fetches a full maintner corpus, overwriting any existing data.
func (s *server) initCorpus(ctx context.Context) error {
s.cMu.Lock()
defer s.cMu.Unlock()
corpus, err := godata.Get(ctx)
if err != nil {
return fmt.Errorf("godata.Get: %v", err)
}
s.corpus = corpus
return nil
}
// corpusUpdateLoop continuously updates the server’s corpus until ctx’s Done
// channel is closed.
func (s *server) corpusUpdateLoop(ctx context.Context) {
log.Println("Starting corpus update loop ...")
for {
log.Println("Updating help wanted issues ...")
s.updateHelpWantedIssues()
log.Println("Updating activities ...")
s.updateActivities()
err := s.corpus.UpdateWithLocker(ctx, &s.cMu)
if err != nil {
if err == maintner.ErrSplit {
log.Println("Corpus out of sync. Re-fetching corpus.")
s.initCorpus(ctx)
} else {
log.Printf("corpus.Update: %v; sleeping 15s", err)
time.Sleep(15 * time.Second)
continue
}
}
select {
case <-ctx.Done():
return
default:
continue
}
}
}
const (
labelIDHelpWanted = 150880243
issuesURLBase = "https://github.com/golang/go/issues/"
issueNumGerritUserMapping = 20945 // Special sign-up issue.
)
func (s *server) updateHelpWantedIssues() {
s.cMu.Lock()
defer s.cMu.Unlock()
repo := s.corpus.GitHub().Repo("golang", "go")
if repo == nil {
log.Println(`s.corpus.GitHub().Repo("golang", "go") = nil`)
return
}
ids := []int32{}
repo.ForeachIssue(func(i *maintner.GitHubIssue) error {
if i.Closed {
return nil
}
if _, ok := i.Labels[labelIDHelpWanted]; ok {
ids = append(ids, i.Number)
}
return nil
})
s.helpWantedIssues = ids
}
// intFromStr returns the first integer within s, allowing for non-numeric
// characters to be present.
func intFromStr(s string) (int, bool) {
var (
foundNum bool
r int
)
for i := 0; i < len(s); i++ {
if s[i] >= '0' && s[i] <= '9' {
foundNum = true
r = r*10 + int(s[i]-'0')
} else if foundNum {
return r, true
}
}
if foundNum {
return r, true
}
return 0, false
}
// Keep these in sync with the frontend JS.
const (
activityTypeRegister = "REGISTER"
activityTypeCreateChange = "CREATE_CHANGE"
activityTypeAmendChange = "AMEND_CHANGE"
activityTypeMergeChange = "MERGE_CHANGE"
)
var pointsPerActivity = map[string]int{
activityTypeRegister: 1,
activityTypeCreateChange: 2,
activityTypeAmendChange: 2,
activityTypeMergeChange: 3,
}
// An activity represents something a contributor has done. e.g. register on
// the GitHub issue, create a change, amend a change, etc.
type activity struct {
Type string `json:"type"`
Created time.Time `json:"created"`
User string `json:"gitHubUser"`
Points int `json:"points"`
}
func (s *server) updateActivities() {
s.cMu.Lock()
defer s.cMu.Unlock()
repo := s.corpus.GitHub().Repo("golang", "go")
if repo == nil {
log.Println(`s.corpus.GitHub().Repo("golang", "go") = nil`)
return
}
issue := repo.Issue(issueNumGerritUserMapping)
if issue == nil {
log.Printf("repo.Issue(%d) = nil", issueNumGerritUserMapping)
return
}
latest := issue.Created
if len(s.activities) > 0 {
latest = s.activities[len(s.activities)-1].Created
}
var newActivities []activity
issue.ForeachComment(func(c *maintner.GitHubComment) error {
if !c.Created.After(latest) {
return nil
}
id, ok := intFromStr(c.Body)
if !ok {
return fmt.Errorf("intFromStr(%q) = %v", c.Body, ok)
}
s.userMapping[id] = c.User
newActivities = append(newActivities, activity{
Type: activityTypeRegister,
Created: c.Created,
User: c.User.Login,
Points: pointsPerActivity[activityTypeRegister],
})
s.totalPoints += pointsPerActivity[activityTypeRegister]
return nil
})
s.corpus.Gerrit().ForeachProjectUnsorted(func(p *maintner.GerritProject) error {
p.ForeachCLUnsorted(func(cl *maintner.GerritCL) error {
if !cl.Commit.CommitTime.After(latest) {
return nil
}
user := s.userMapping[cl.OwnerID()]
if user == nil {
return nil
}
newActivities = append(newActivities, activity{
Type: activityTypeCreateChange,
Created: cl.Created,
User: user.Login,
Points: pointsPerActivity[activityTypeCreateChange],
})
s.totalPoints += pointsPerActivity[activityTypeCreateChange]
if cl.Version > 1 {
newActivities = append(newActivities, activity{
Type: activityTypeAmendChange,
Created: cl.Commit.CommitTime,
User: user.Login,
Points: pointsPerActivity[activityTypeAmendChange],
})
s.totalPoints += pointsPerActivity[activityTypeAmendChange]
}
if cl.Status == "merged" {
newActivities = append(newActivities, activity{
Type: activityTypeMergeChange,
Created: cl.Commit.CommitTime,
User: user.Login,
Points: pointsPerActivity[activityTypeMergeChange],
})
s.totalPoints += pointsPerActivity[activityTypeMergeChange]
}
return nil
})
return nil
})
sort.Sort(byCreated(newActivities))
s.activities = append(s.activities, newActivities...)
}
type byCreated []activity
func (a byCreated) Len() int { return len(a) }
func (a byCreated) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byCreated) Less(i, j int) bool { return a[i].Created.Before(a[j].Created) }
func (s *server) handleActivities(w http.ResponseWriter, r *http.Request) {
i, _ := strconv.Atoi(r.FormValue("since"))
since := time.Unix(int64(i)/1000, 0)
recentActivity := []activity{}
for _, a := range s.activities {
if a.Created.After(since) {
recentActivity = append(recentActivity, a)
}
}
s.cMu.RLock()
defer s.cMu.RUnlock()
w.Header().Set("Content-Type", "application/json")
result := struct {
Activities []activity `json:"activities"`
TotalPoints int `json:"totalPoints"`
}{
Activities: recentActivity,
TotalPoints: s.totalPoints,
}
if err := json.NewEncoder(w).Encode(result); err != nil {
log.Printf("Encode(%+v) = %v", result, err)
return
}
}
func (s *server) handleRandomHelpWantedIssue(w http.ResponseWriter, r *http.Request) {
s.cMu.RLock()
defer s.cMu.RUnlock()
if len(s.helpWantedIssues) == 0 {
http.Redirect(w, r, issuesURLBase, http.StatusSeeOther)
return
}
rid := s.helpWantedIssues[rand.Intn(len(s.helpWantedIssues))]
http.Redirect(w, r, issuesURLBase+strconv.Itoa(int(rid)), http.StatusSeeOther)
}
func (s *server) handleFavicon(w http.ResponseWriter, r *http.Request) {
// Need to specify content type for consistent tests, without this it's
// determined from mime.types on the box the test is running on
w.Header().Set("Content-Type", "image/x-icon")
http.ServeFile(w, r, path.Join(s.staticDir, "/favicon.ico"))
}
// ServeHTTP satisfies the http.Handler interface.
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.TLS != nil {
w.Header().Set("Strict-Transport-Security", "max-age=31536000; preload")
}
s.mux.ServeHTTP(w, r)
}
var (
pageStoreMu sync.Mutex
pageStore = map[string][]byte{}
)
func getPage(name string) ([]byte, error) {
pageStoreMu.Lock()
defer pageStoreMu.Unlock()
p, ok := pageStore[name]
if ok {
return p, nil
}
return nil, fmt.Errorf("page key %s not found", name)
}
func writePage(key string, content []byte) error {
pageStoreMu.Lock()
defer pageStoreMu.Unlock()
pageStore[key] = content
return nil
}
func servePage(w http.ResponseWriter, r *http.Request, key string) {
b, err := getPage(key)
if err != nil {
log.Printf("getPage(%q) = %v", key, err)
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(b)
}
func handleRelease(w http.ResponseWriter, r *http.Request) {
servePage(w, r, "release")
}