github.go 10 KB

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