github.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. package service
  2. import (
  3. "context"
  4. "log"
  5. "net/http"
  6. "strings"
  7. "sync"
  8. "time"
  9. "github.com/shurcooL/githubv4"
  10. )
  11. type Label struct {
  12. Name string `json:"name"`
  13. Color string `json:"color"`
  14. }
  15. type User struct {
  16. Login string `json:"login"`
  17. AvatarUrl string `json:"avatar_url"`
  18. }
  19. // Issue represents a GitHub issue with minimal fields.
  20. type Issue struct {
  21. ID string `json:"id"`
  22. Title string `json:"title"`
  23. Body string `json:"-"`
  24. Url string `json:"url"`
  25. Labels []Label `json:"labels"`
  26. CommentCount int `json:"comment_count"`
  27. ThumbsUpCount int `json:"thumbs_up"`
  28. Author User `json:"author"`
  29. CreatedAt int64 `json:"created_at"`
  30. }
  31. // Discussion represents a GitHub discussion.
  32. type Discussion struct {
  33. ID string `json:"id"`
  34. Url string `json:"url"`
  35. Title string `json:"title"`
  36. BodyText string `json:"-"`
  37. Labels []Label `json:"labels"`
  38. ThumbsUpCount int `json:"thumbs_up"`
  39. CommentCount int `json:"comment_count"`
  40. UpvoteCount int `json:"upvote_count"`
  41. Author User `json:"author"`
  42. CommentUsers []User `json:"comment_users"`
  43. CreatedAt int64 `json:"created_at"`
  44. IsAnswered bool `json:"is_answered"`
  45. Category Category `json:"category"`
  46. }
  47. type Category struct {
  48. ID string `json:"id"`
  49. Name string `json:"name"`
  50. Emoji string `json:"emoji"`
  51. EmojiHTML string `json:"emoji_html" graphql:"emojiHTML"`
  52. }
  53. type GitHubAPI interface {
  54. Query(ctx context.Context, q interface{}, variables map[string]interface{}) error
  55. }
  56. // GitHubService provides methods to interact with the GitHub API.
  57. type GitHubService struct {
  58. token string
  59. cache sync.Map
  60. cacheTTL time.Duration
  61. owner string
  62. repo string
  63. }
  64. type GithubConfig struct {
  65. Token string
  66. Owner string
  67. Repo string
  68. CacheTTL time.Duration
  69. }
  70. // headerTransport is custom Transport used to add header information to the request
  71. type headerTransport struct {
  72. transport *http.Transport
  73. headers map[string]string
  74. }
  75. // RoundTrip implements the http.RoundTripper interface
  76. func (h *headerTransport) RoundTrip(req *http.Request) (*http.Response, error) {
  77. for key, value := range h.headers {
  78. req.Header.Add(key, value)
  79. }
  80. return h.transport.RoundTrip(req)
  81. }
  82. // NewGitHubService creates a new instance of GitHubService with authorized client.
  83. func NewGitHubService(cfg *GithubConfig) *GitHubService {
  84. s := &GitHubService{
  85. token: cfg.Token,
  86. cache: sync.Map{},
  87. cacheTTL: cfg.CacheTTL,
  88. owner: cfg.Owner,
  89. repo: cfg.Repo,
  90. }
  91. go s.loop()
  92. return s
  93. }
  94. func (s *GitHubService) loop() {
  95. s.refreshCache()
  96. t := time.NewTicker(s.cacheTTL * time.Minute)
  97. defer t.Stop()
  98. for range t.C {
  99. s.refreshCache()
  100. }
  101. }
  102. func (s *GitHubService) client(proxy bool) GitHubAPI {
  103. httpClient := &http.Client{
  104. Transport: &headerTransport{
  105. transport: &http.Transport{},
  106. headers: map[string]string{"Authorization": "Bearer " + s.token},
  107. },
  108. }
  109. if proxy {
  110. httpClient.Transport.(*headerTransport).transport = &http.Transport{
  111. Proxy: http.ProxyFromEnvironment,
  112. }
  113. }
  114. return githubv4.NewClient(httpClient)
  115. }
  116. func (s *GitHubService) request(ctx context.Context, q interface{}, variables map[string]interface{}) (err error) {
  117. err = s.client(true).Query(ctx, q, variables)
  118. if err != nil {
  119. log.Printf("request using proxy fails and falls back to non-proxy mode: %#v", err)
  120. err = s.client(false).Query(ctx, q, variables)
  121. }
  122. return
  123. }
  124. func (s *GitHubService) refreshCache() {
  125. issues, err := s.fetchIssues(context.Background(), nil)
  126. if err != nil {
  127. log.Printf("failed to fetch issues %v", err)
  128. return
  129. }
  130. s.cache.Store("issues", issues)
  131. discussions, err := s.fetchDiscussions(context.Background(), nil)
  132. if err != nil {
  133. log.Printf("failed to fetch discussions %v", err)
  134. return
  135. }
  136. s.cache.Store("discussions", discussions)
  137. }
  138. // GetIssues tries to get the issues from cache; if not available, fetches from GitHub API.
  139. func (s *GitHubService) GetIssues(ctx context.Context, filter string) (issues []*Issue, err error) {
  140. cachedIssues, found := s.cache.Load("issues")
  141. if found {
  142. return s.filterIssues(cachedIssues.([]*Issue), filter)
  143. }
  144. issues, err = s.fetchIssues(ctx, nil)
  145. if err != nil {
  146. return nil, err
  147. }
  148. return s.filterIssues(issues, filter)
  149. }
  150. func (s *GitHubService) filterIssues(issues []*Issue, filter string) ([]*Issue, error) {
  151. if filter != "" {
  152. filteredIssues := make([]*Issue, 0)
  153. for _, issue := range issues {
  154. if strings.Contains(issue.Title, filter) || strings.Contains(issue.Body, filter) {
  155. filteredIssues = append(filteredIssues, issue)
  156. }
  157. }
  158. return filteredIssues, nil
  159. }
  160. return issues, nil
  161. }
  162. // GetRepositoryIssues queries GitHub for issues of a repository.
  163. func (s *GitHubService) fetchIssues(ctx context.Context, afterCursor *githubv4.String) ([]*Issue, error) {
  164. var query struct {
  165. Repository struct {
  166. Issues struct {
  167. Nodes []struct {
  168. ID string
  169. Title string
  170. Body string
  171. Url string
  172. CreatedAt githubv4.DateTime
  173. Author User
  174. Labels struct {
  175. Nodes []struct {
  176. Color string
  177. Name string
  178. }
  179. } `graphql:"labels(first: 10)"`
  180. Comments struct {
  181. TotalCount int
  182. }
  183. Reactions struct {
  184. TotalCount int
  185. } `graphql:"reactions(content: THUMBS_UP)"`
  186. }
  187. PageInfo struct {
  188. EndCursor githubv4.String
  189. HasNextPage bool
  190. }
  191. } `graphql:"issues(first: 100, after: $afterCursor, states: OPEN, orderBy: {field: CREATED_AT, direction: DESC})"`
  192. } `graphql:"repository(owner: $owner, name: $name)"`
  193. }
  194. variables := map[string]interface{}{
  195. "owner": githubv4.String(s.owner),
  196. "name": githubv4.String(s.repo),
  197. "afterCursor": afterCursor,
  198. }
  199. err := s.request(ctx, &query, variables)
  200. if err != nil {
  201. return nil, err
  202. }
  203. issues := make([]*Issue, 0)
  204. for _, node := range query.Repository.Issues.Nodes {
  205. issue := &Issue{
  206. ID: node.ID,
  207. Title: node.Title,
  208. Body: node.Body,
  209. Url: node.Url,
  210. }
  211. issue.Labels = make([]Label, len(node.Labels.Nodes))
  212. for i, label := range node.Labels.Nodes {
  213. issue.Labels[i] = Label{Name: label.Name, Color: label.Color}
  214. }
  215. issue.CommentCount = node.Comments.TotalCount
  216. issue.ThumbsUpCount = node.Reactions.TotalCount
  217. issue.Author = node.Author
  218. issue.CreatedAt = node.CreatedAt.Unix()
  219. issues = append(issues, issue)
  220. }
  221. if query.Repository.Issues.PageInfo.HasNextPage {
  222. moreIssues, err := s.fetchIssues(ctx, &query.Repository.Issues.PageInfo.EndCursor)
  223. if err != nil {
  224. return nil, err
  225. }
  226. issues = append(issues, moreIssues...)
  227. }
  228. return issues, nil
  229. }
  230. // GetDiscussions tries to get the discussions from cache; if not available, fetches from GitHub API.
  231. func (s *GitHubService) GetDiscussions(ctx context.Context, filter string) ([]*Discussion, error) {
  232. if cachedData, found := s.cache.Load("discussions"); found {
  233. return s.filterDiscussions(cachedData.([]*Discussion), filter)
  234. }
  235. discussions, err := s.fetchDiscussions(ctx, nil)
  236. if err != nil {
  237. return nil, err
  238. }
  239. return s.filterDiscussions(discussions, filter)
  240. }
  241. func (s *GitHubService) filterDiscussions(discussions []*Discussion, filter string) ([]*Discussion, error) {
  242. if filter != "" {
  243. filteredDiscussions := make([]*Discussion, 0)
  244. for _, discussion := range discussions {
  245. if strings.Contains(discussion.Title, filter) || strings.Contains(discussion.BodyText, filter) {
  246. filteredDiscussions = append(filteredDiscussions, discussion)
  247. }
  248. }
  249. return filteredDiscussions, nil
  250. }
  251. return discussions, nil
  252. }
  253. // fetchDiscussionsFromGitHub queries GitHub for discussions of a repository.
  254. func (s *GitHubService) fetchDiscussions(ctx context.Context, afterCursor *githubv4.String) ([]*Discussion, error) {
  255. var query struct {
  256. Repository struct {
  257. Discussions struct {
  258. Nodes []struct {
  259. ID string
  260. Url string
  261. UpvoteCount int
  262. Title string
  263. BodyText string
  264. Author User
  265. CreatedAt githubv4.DateTime
  266. IsAnswered bool
  267. Labels struct {
  268. Nodes []struct {
  269. Color string
  270. Name string
  271. }
  272. } `graphql:"labels(first: 10)"`
  273. Reactions struct {
  274. TotalCount int
  275. } `graphql:"reactions(content: THUMBS_UP)"`
  276. Comments struct {
  277. Nodes []struct {
  278. Author User
  279. }
  280. } `graphql:"comments(first: 10)"`
  281. Category Category
  282. }
  283. PageInfo struct {
  284. EndCursor githubv4.String
  285. HasNextPage bool
  286. }
  287. } `graphql:"discussions(first: 100, after: $afterCursor, orderBy: {field: CREATED_AT, direction: DESC})"`
  288. } `graphql:"repository(owner: $owner, name: $name)"`
  289. }
  290. variables := map[string]interface{}{
  291. "owner": githubv4.String(s.owner),
  292. "name": githubv4.String(s.repo),
  293. "afterCursor": afterCursor,
  294. }
  295. err := s.request(ctx, &query, variables)
  296. if err != nil {
  297. return nil, err
  298. }
  299. discussions := make([]*Discussion, 0)
  300. for _, node := range query.Repository.Discussions.Nodes {
  301. discussion := &Discussion{
  302. ID: node.ID,
  303. Url: node.Url,
  304. Title: node.Title,
  305. BodyText: node.BodyText,
  306. }
  307. discussion.Labels = make([]Label, len(node.Labels.Nodes))
  308. for i, label := range node.Labels.Nodes {
  309. discussion.Labels[i] = Label{Name: label.Name, Color: label.Color}
  310. }
  311. exist := make(map[string]struct{})
  312. discussion.CommentUsers = make([]User, 0, len(node.Comments.Nodes))
  313. discussion.CommentUsers = append(discussion.CommentUsers, node.Author)
  314. exist[node.Author.Login] = struct{}{}
  315. for _, comment := range node.Comments.Nodes {
  316. if _, ok := exist[comment.Author.Login]; ok {
  317. continue
  318. }
  319. exist[comment.Author.Login] = struct{}{}
  320. discussion.CommentUsers = append(discussion.CommentUsers, comment.Author)
  321. }
  322. discussion.ThumbsUpCount = node.Reactions.TotalCount
  323. discussion.CommentCount = len(node.Comments.Nodes)
  324. discussion.UpvoteCount = node.UpvoteCount
  325. discussion.Author = node.Author
  326. discussion.CreatedAt = node.CreatedAt.Unix()
  327. discussion.IsAnswered = node.IsAnswered
  328. discussion.Category = node.Category
  329. discussions = append(discussions, discussion)
  330. }
  331. if query.Repository.Discussions.PageInfo.HasNextPage {
  332. moreDiscussions, err := s.fetchDiscussions(ctx, &query.Repository.Discussions.PageInfo.EndCursor)
  333. if err != nil {
  334. return nil, err
  335. }
  336. discussions = append(discussions, moreDiscussions...)
  337. }
  338. return discussions, nil
  339. }