github.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package feed
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. "sync"
  7. "time"
  8. )
  9. type githubReleaseLatestResponseJson struct {
  10. TagName string `json:"tag_name"`
  11. PublishedAt string `json:"published_at"`
  12. HtmlUrl string `json:"html_url"`
  13. Reactions struct {
  14. Downvotes int `json:"-1"`
  15. } `json:"reactions"`
  16. }
  17. func fetchLatestGithubRelease(request *ReleaseRequest) (*AppRelease, error) {
  18. httpRequest, err := http.NewRequest(
  19. "GET",
  20. fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", request.Repository),
  21. nil,
  22. )
  23. if err != nil {
  24. return nil, err
  25. }
  26. if request.Token != nil {
  27. httpRequest.Header.Add("Authorization", "Bearer "+(*request.Token))
  28. }
  29. response, err := decodeJsonFromRequest[githubReleaseLatestResponseJson](defaultClient, httpRequest)
  30. if err != nil {
  31. return nil, err
  32. }
  33. return &AppRelease{
  34. Source: ReleaseSourceGithub,
  35. Name: request.Repository,
  36. Version: normalizeVersionFormat(response.TagName),
  37. NotesUrl: response.HtmlUrl,
  38. TimeReleased: parseRFC3339Time(response.PublishedAt),
  39. Downvotes: response.Reactions.Downvotes,
  40. }, nil
  41. }
  42. type GithubTicket struct {
  43. Number int
  44. CreatedAt time.Time
  45. Title string
  46. }
  47. type RepositoryDetails struct {
  48. Name string
  49. Stars int
  50. Forks int
  51. OpenPullRequests int
  52. PullRequests []GithubTicket
  53. OpenIssues int
  54. Issues []GithubTicket
  55. LastCommits int
  56. Commits []CommitDetails
  57. }
  58. type githubRepositoryDetailsResponseJson struct {
  59. Name string `json:"full_name"`
  60. Stars int `json:"stargazers_count"`
  61. Forks int `json:"forks_count"`
  62. }
  63. type githubTicketResponseJson struct {
  64. Count int `json:"total_count"`
  65. Tickets []struct {
  66. Number int `json:"number"`
  67. CreatedAt string `json:"created_at"`
  68. Title string `json:"title"`
  69. } `json:"items"`
  70. }
  71. type CommitDetails struct {
  72. Sha string
  73. Author string
  74. CreatedAt time.Time
  75. Message string
  76. }
  77. type gitHubCommitResponseJson struct {
  78. Sha string `json:"sha"`
  79. Commit struct {
  80. Author struct {
  81. Name string `json:"name"`
  82. Date string `json:"date"`
  83. } `json:"author"`
  84. Message string `json:"message"`
  85. } `json:"commit"`
  86. }
  87. func FetchRepositoryDetailsFromGithub(repository string, token string, maxPRs int, maxIssues int, maxCommits int) (RepositoryDetails, error) {
  88. repositoryRequest, err := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/repos/%s", repository), nil)
  89. if err != nil {
  90. return RepositoryDetails{}, fmt.Errorf("%w: could not create request with repository: %v", ErrNoContent, err)
  91. }
  92. PRsRequest, _ := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/search/issues?q=is:pr+is:open+repo:%s&per_page=%d", repository, maxPRs), nil)
  93. issuesRequest, _ := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/search/issues?q=is:issue+is:open+repo:%s&per_page=%d", repository, maxIssues), nil)
  94. CommitsRequest, _ := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/repos/%s/commits?per_page=%d", repository, maxCommits), nil)
  95. if token != "" {
  96. token = fmt.Sprintf("Bearer %s", token)
  97. repositoryRequest.Header.Add("Authorization", token)
  98. PRsRequest.Header.Add("Authorization", token)
  99. issuesRequest.Header.Add("Authorization", token)
  100. CommitsRequest.Header.Add("Authorization", token)
  101. }
  102. var detailsResponse githubRepositoryDetailsResponseJson
  103. var detailsErr error
  104. var PRsResponse githubTicketResponseJson
  105. var PRsErr error
  106. var issuesResponse githubTicketResponseJson
  107. var issuesErr error
  108. var commitsResponse []gitHubCommitResponseJson
  109. var CommitsErr error
  110. var wg sync.WaitGroup
  111. wg.Add(1)
  112. go (func() {
  113. defer wg.Done()
  114. detailsResponse, detailsErr = decodeJsonFromRequest[githubRepositoryDetailsResponseJson](defaultClient, repositoryRequest)
  115. })()
  116. if maxPRs > 0 {
  117. wg.Add(1)
  118. go (func() {
  119. defer wg.Done()
  120. PRsResponse, PRsErr = decodeJsonFromRequest[githubTicketResponseJson](defaultClient, PRsRequest)
  121. })()
  122. }
  123. if maxIssues > 0 {
  124. wg.Add(1)
  125. go (func() {
  126. defer wg.Done()
  127. issuesResponse, issuesErr = decodeJsonFromRequest[githubTicketResponseJson](defaultClient, issuesRequest)
  128. })()
  129. }
  130. if maxCommits > 0 {
  131. wg.Add(1)
  132. go (func() {
  133. defer wg.Done()
  134. commitsResponse, CommitsErr = decodeJsonFromRequest[[]gitHubCommitResponseJson](defaultClient, CommitsRequest)
  135. })()
  136. }
  137. wg.Wait()
  138. if detailsErr != nil {
  139. return RepositoryDetails{}, fmt.Errorf("%w: could not get repository details: %s", ErrNoContent, detailsErr)
  140. }
  141. details := RepositoryDetails{
  142. Name: detailsResponse.Name,
  143. Stars: detailsResponse.Stars,
  144. Forks: detailsResponse.Forks,
  145. PullRequests: make([]GithubTicket, 0, len(PRsResponse.Tickets)),
  146. Issues: make([]GithubTicket, 0, len(issuesResponse.Tickets)),
  147. Commits: make([]CommitDetails, 0, len(commitsResponse)),
  148. }
  149. err = nil
  150. if maxPRs > 0 {
  151. if PRsErr != nil {
  152. err = fmt.Errorf("%w: could not get PRs: %s", ErrPartialContent, PRsErr)
  153. } else {
  154. details.OpenPullRequests = PRsResponse.Count
  155. for i := range PRsResponse.Tickets {
  156. details.PullRequests = append(details.PullRequests, GithubTicket{
  157. Number: PRsResponse.Tickets[i].Number,
  158. CreatedAt: parseRFC3339Time(PRsResponse.Tickets[i].CreatedAt),
  159. Title: PRsResponse.Tickets[i].Title,
  160. })
  161. }
  162. }
  163. }
  164. if maxIssues > 0 {
  165. if issuesErr != nil {
  166. // TODO: fix, overwriting the previous error
  167. err = fmt.Errorf("%w: could not get issues: %s", ErrPartialContent, issuesErr)
  168. } else {
  169. details.OpenIssues = issuesResponse.Count
  170. for i := range issuesResponse.Tickets {
  171. details.Issues = append(details.Issues, GithubTicket{
  172. Number: issuesResponse.Tickets[i].Number,
  173. CreatedAt: parseRFC3339Time(issuesResponse.Tickets[i].CreatedAt),
  174. Title: issuesResponse.Tickets[i].Title,
  175. })
  176. }
  177. }
  178. }
  179. if maxCommits > 0 {
  180. if CommitsErr != nil {
  181. err = fmt.Errorf("%w: could not get issues: %s", ErrPartialContent, CommitsErr)
  182. } else {
  183. for i := range commitsResponse {
  184. details.Commits = append(details.Commits, CommitDetails{
  185. Sha: commitsResponse[i].Sha,
  186. Author: commitsResponse[i].Commit.Author.Name,
  187. CreatedAt: parseRFC3339Time(commitsResponse[i].Commit.Author.Date),
  188. Message: strings.SplitN(commitsResponse[i].Commit.Message, "\n\n", 2)[0],
  189. })
  190. }
  191. }
  192. }
  193. return details, err
  194. }