github.go 6.3 KB

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