2017-04-13 10:59:05 -04:00
|
|
|
package releaser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2017-09-10 11:14:02 -04:00
|
|
|
"strings"
|
2017-04-13 10:59:05 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-09-10 11:14:02 -04:00
|
|
|
gitHubCommitsAPI = "https://api.github.com/repos/gohugoio/REPO/commits/%s"
|
|
|
|
gitHubRepoAPI = "https://api.github.com/repos/gohugoio/REPO"
|
|
|
|
gitHubContributorsAPI = "https://api.github.com/repos/gohugoio/REPO/contributors"
|
2017-04-13 10:59:05 -04:00
|
|
|
)
|
|
|
|
|
2017-09-10 11:14:02 -04:00
|
|
|
type gitHubAPI struct {
|
|
|
|
commitsAPITemplate string
|
|
|
|
repoAPI string
|
|
|
|
contributorsAPITemplate string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newGitHubAPI(repo string) *gitHubAPI {
|
|
|
|
return &gitHubAPI{
|
|
|
|
commitsAPITemplate: strings.Replace(gitHubCommitsAPI, "REPO", repo, -1),
|
|
|
|
repoAPI: strings.Replace(gitHubRepoAPI, "REPO", repo, -1),
|
|
|
|
contributorsAPITemplate: strings.Replace(gitHubContributorsAPI, "REPO", repo, -1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 10:59:05 -04:00
|
|
|
type gitHubCommit struct {
|
|
|
|
Author gitHubAuthor `json:"author"`
|
2018-09-06 15:50:54 -04:00
|
|
|
HTMLURL string `json:"html_url"`
|
2017-04-13 10:59:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type gitHubAuthor struct {
|
|
|
|
ID int `json:"id"`
|
|
|
|
Login string `json:"login"`
|
2018-09-06 15:50:54 -04:00
|
|
|
HTMLURL string `json:"html_url"`
|
2017-04-13 10:59:05 -04:00
|
|
|
AvatarURL string `json:"avatar_url"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type gitHubRepo struct {
|
|
|
|
ID int `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
2018-09-06 15:50:54 -04:00
|
|
|
HTMLURL string `json:"html_url"`
|
2017-04-13 10:59:05 -04:00
|
|
|
Stars int `json:"stargazers_count"`
|
|
|
|
Contributors []gitHubContributor
|
|
|
|
}
|
|
|
|
|
|
|
|
type gitHubContributor struct {
|
|
|
|
ID int `json:"id"`
|
|
|
|
Login string `json:"login"`
|
2018-09-06 15:50:54 -04:00
|
|
|
HTMLURL string `json:"html_url"`
|
2017-04-13 10:59:05 -04:00
|
|
|
Contributions int `json:"contributions"`
|
|
|
|
}
|
|
|
|
|
2017-09-10 11:14:02 -04:00
|
|
|
func (g *gitHubAPI) fetchCommit(ref string) (gitHubCommit, error) {
|
2017-04-13 10:59:05 -04:00
|
|
|
var commit gitHubCommit
|
|
|
|
|
2017-09-10 11:14:02 -04:00
|
|
|
u := fmt.Sprintf(g.commitsAPITemplate, ref)
|
2017-04-13 10:59:05 -04:00
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", u, nil)
|
|
|
|
if err != nil {
|
|
|
|
return commit, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = doGitHubRequest(req, &commit)
|
|
|
|
|
|
|
|
return commit, err
|
|
|
|
}
|
|
|
|
|
2017-09-10 11:14:02 -04:00
|
|
|
func (g *gitHubAPI) fetchRepo() (gitHubRepo, error) {
|
2017-04-13 10:59:05 -04:00
|
|
|
var repo gitHubRepo
|
|
|
|
|
2017-09-10 11:14:02 -04:00
|
|
|
req, err := http.NewRequest("GET", g.repoAPI, nil)
|
2017-04-13 10:59:05 -04:00
|
|
|
if err != nil {
|
|
|
|
return repo, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = doGitHubRequest(req, &repo)
|
|
|
|
if err != nil {
|
|
|
|
return repo, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var contributors []gitHubContributor
|
|
|
|
page := 0
|
|
|
|
for {
|
|
|
|
page++
|
|
|
|
var currPage []gitHubContributor
|
2017-09-10 11:14:02 -04:00
|
|
|
url := fmt.Sprintf(g.contributorsAPITemplate+"?page=%d", page)
|
2017-04-13 10:59:05 -04:00
|
|
|
|
|
|
|
req, err = http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return repo, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = doGitHubRequest(req, &currPage)
|
|
|
|
if err != nil {
|
|
|
|
return repo, err
|
|
|
|
}
|
|
|
|
if len(currPage) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
contributors = append(contributors, currPage...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
repo.Contributors = contributors
|
|
|
|
|
|
|
|
return repo, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func doGitHubRequest(req *http.Request, v interface{}) error {
|
|
|
|
addGitHubToken(req)
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if isError(resp) {
|
|
|
|
b, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
return fmt.Errorf("GitHub lookup failed: %s", string(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.NewDecoder(resp.Body).Decode(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isError(resp *http.Response) bool {
|
|
|
|
return resp.StatusCode < 200 || resp.StatusCode > 299
|
|
|
|
}
|
|
|
|
|
|
|
|
func addGitHubToken(req *http.Request) {
|
|
|
|
gitHubToken := os.Getenv("GITHUB_TOKEN")
|
|
|
|
if gitHubToken != "" {
|
|
|
|
req.Header.Add("Authorization", "token "+gitHubToken)
|
|
|
|
}
|
|
|
|
}
|