Repository navigation
Expand file tree
/
Copy pathgithub.go
More file actions
121 lines (106 loc) · 3.05 KB
/
Copy pathgithub.go
File metadata and controls
121 lines (106 loc) · 3.05 KB
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
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
type PRInfo struct {
Number int `json:"number"`
Title string `json:"title"`
Author string `json:"author"`
AvatarURL string `json:"avatar_url"`
Branch string `json:"branch"`
CloneURL string `json:"clone_url"`
UpdatedAt string `json:"updated_at"`
Labels []string `json:"labels"`
}
// FetchPulls returns open PRs, caching for 60 seconds.
// The fetch happens outside the lock so slow GitHub responses
// don't block concurrent readers.
func (s *Server) FetchPulls() ([]PRInfo, error) {
s.prCache.mu.Lock()
if time.Since(s.prCache.at) < 60*time.Second && s.prCache.pulls != nil {
pulls := s.prCache.pulls
s.prCache.mu.Unlock()
return pulls, nil
}
// Mark as fetching so other goroutines wait on the singleflight
// rather than all hitting GitHub concurrently.
if s.prCache.fetching {
// Another goroutine is already fetching — return stale data if available
pulls := s.prCache.pulls
s.prCache.mu.Unlock()
if pulls != nil {
return pulls, nil
}
return nil, fmt.Errorf("PR data loading, try again")
}
s.prCache.fetching = true
s.prCache.mu.Unlock()
pulls, err := fetchPullsFromGitHub(s.config.GitHubRepo)
s.prCache.mu.Lock()
s.prCache.fetching = false
if err == nil {
s.prCache.pulls = pulls
s.prCache.at = time.Now()
}
s.prCache.mu.Unlock()
return pulls, err
}
func fetchPullsFromGitHub(repo string) ([]PRInfo, error) {
url := fmt.Sprintf("https://api.github.com/repos/%s/pulls?state=open&per_page=100&sort=updated&direction=desc", repo)
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("Accept", "application/vnd.github.v3+json")
req.Header.Set("User-Agent", "thelounge-preview-service")
if token := envOr("GITHUB_TOKEN", ""); token != "" {
req.Header.Set("Authorization", "token "+token)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("github API: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("github API returned %d", resp.StatusCode)
}
var ghPRs []struct {
Number int `json:"number"`
Title string `json:"title"`
User struct {
Login string `json:"login"`
AvatarURL string `json:"avatar_url"`
} `json:"user"`
Head struct {
Ref string `json:"ref"`
Repo struct {
CloneURL string `json:"clone_url"`
} `json:"repo"`
} `json:"head"`
UpdatedAt string `json:"updated_at"`
Labels []struct {
Name string `json:"name"`
} `json:"labels"`
}
if err := json.NewDecoder(resp.Body).Decode(&ghPRs); err != nil {
return nil, fmt.Errorf("decode: %w", err)
}
pulls := make([]PRInfo, 0, len(ghPRs))
for _, pr := range ghPRs {
labels := make([]string, len(pr.Labels))
for i, l := range pr.Labels {
labels[i] = l.Name
}
pulls = append(pulls, PRInfo{
Number: pr.Number,
Title: pr.Title,
Author: pr.User.Login,
AvatarURL: pr.User.AvatarURL,
Branch: pr.Head.Ref,
CloneURL: pr.Head.Repo.CloneURL,
UpdatedAt: pr.UpdatedAt,
Labels: labels,
})
}
return pulls, nil
}