github.com/decred/politeia@v1.4.0/politeiawww/legacy/codetracker/github/api/pullrequests.go (about) 1 // Copyright (c) 2020 The Decred developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package api 6 7 import ( 8 "encoding/json" 9 "fmt" 10 "net/http" 11 ) 12 13 const ( 14 apiPullsRequestURL = `https://api.github.com/repos/%s/%s/pulls?per_page=250&page=%d&state=all&sort=updated&direction=desc` 15 apiPullRequestURL = `https://api.github.com/repos/%s/%s/pulls/%d` 16 ) 17 18 // FetchPullRequest requests information about a given pull request based on 19 // organization, repo and pull request number. 20 func (a *Client) FetchPullRequest(org, repo string, prNum int) (*PullRequest, error) { 21 url := fmt.Sprintf(apiPullRequestURL, org, repo, prNum) 22 req, err := http.NewRequest("GET", url, nil) 23 if err != nil { 24 return nil, err 25 } 26 27 body, err := a.sendGithubRequest(req) 28 if err != nil { 29 return nil, err 30 } 31 32 var pullRequest PullRequest 33 err = json.Unmarshal(body, &pullRequest) 34 if err != nil { 35 return nil, err 36 } 37 38 return &pullRequest, nil 39 } 40 41 // FetchPullsRequest requests all of the pull requests from a given repo 42 // under an organization. 43 func (a *Client) FetchPullsRequest(org, repo string) ([]PullsRequest, error) { 44 var totalPullsRequests []PullsRequest 45 page := 1 46 for { 47 url := fmt.Sprintf(apiPullsRequestURL, org, repo, page) 48 req, err := http.NewRequest("GET", url, nil) 49 if err != nil { 50 return totalPullsRequests, err 51 } 52 body, err := a.sendGithubRequest(req) 53 if err != nil { 54 return totalPullsRequests, err 55 } 56 57 var pullsRequests []PullsRequest 58 err = json.Unmarshal(body, &pullsRequests) 59 if err != nil { 60 return totalPullsRequests, err 61 } 62 63 // no more left 64 if len(pullsRequests) == 0 { 65 break 66 } 67 68 totalPullsRequests = append(totalPullsRequests, pullsRequests...) 69 page++ 70 } 71 return totalPullsRequests, nil 72 }