github.com/decred/politeia@v1.4.0/politeiawww/legacy/codetracker/github/api/reviews.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  	apiPullRequestReviewsURL = `https://api.github.com/repos/%s/%s/pulls/%d/reviews?per_page=250&page=1`
    15  )
    16  
    17  // FetchPullRequestReviews requests all of the reviews from a given pull request
    18  // based on organization, repository, pull request number and time.
    19  func (a *Client) FetchPullRequestReviews(org, repo string, prNum int) ([]PullRequestReview, error) {
    20  	var totalPullRequestReviews []PullRequestReview
    21  	url := fmt.Sprintf(apiPullRequestReviewsURL, org, repo, prNum)
    22  	req, err := http.NewRequest("GET", url, nil)
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  	body, err := a.sendGithubRequest(req)
    27  	if err != nil {
    28  		return totalPullRequestReviews, err
    29  	}
    30  	if len(body) == 0 {
    31  		return totalPullRequestReviews, nil
    32  	}
    33  
    34  	var pullRequestReviews []PullRequestReview
    35  	err = json.Unmarshal(body, &pullRequestReviews)
    36  	if err != nil {
    37  		return totalPullRequestReviews, err
    38  	}
    39  	totalPullRequestReviews = append(totalPullRequestReviews, pullRequestReviews...)
    40  
    41  	return totalPullRequestReviews, nil
    42  }