diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-04-13 16:59:05 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-04-24 11:08:56 +0200 |
commit | 7f6430d84d68765536b8758b1331a4b84db04c84 (patch) | |
tree | 7c2b1450c2d2789fbbc0323fc6027dac586ef6df /releaser/github.go | |
parent | 0e87b18b66d2c8ba9e2abc429630cb03f5b093d6 (diff) |
Automate the Hugo release process
This commit adds a work flow aroung GoReleaser to get the Hugo release process automated and more uniform:
* It can be run fully automated or in two steps to allow for manual edits of the relase notes.
* It supports both patch and full releases.
* It fetches author, issue, repo info. etc. for the release notes from GitHub.
* The file names produced are mainly the same as before, but we no use tar.gz as archive for all Unix versions.
* There isn't a fully automated CI setup in place yet, but the release tag is marked in the commit message with "[ci deploy]"
Fixes #3358
Diffstat (limited to 'releaser/github.go')
-rw-r--r-- | releaser/github.go | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/releaser/github.go b/releaser/github.go new file mode 100644 index 000000000..0dbb1bca1 --- /dev/null +++ b/releaser/github.go @@ -0,0 +1,129 @@ +package releaser + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "os" +) + +var ( + gitHubCommitsApi = "https://api.github.com/repos/spf13/hugo/commits/%s" + gitHubRepoApi = "https://api.github.com/repos/spf13/hugo" + gitHubContributorsApi = "https://api.github.com/repos/spf13/hugo/contributors" +) + +type gitHubCommit struct { + Author gitHubAuthor `json:"author"` + HtmlURL string `json:"html_url"` +} + +type gitHubAuthor struct { + ID int `json:"id"` + Login string `json:"login"` + HtmlURL string `json:"html_url"` + AvatarURL string `json:"avatar_url"` +} + +type gitHubRepo struct { + ID int `json:"id"` + Name string `json:"name"` + Description string `json:"description"` + HtmlURL string `json:"html_url"` + Stars int `json:"stargazers_count"` + Contributors []gitHubContributor +} + +type gitHubContributor struct { + ID int `json:"id"` + Login string `json:"login"` + HtmlURL string `json:"html_url"` + Contributions int `json:"contributions"` +} + +func fetchCommit(ref string) (gitHubCommit, error) { + var commit gitHubCommit + + u := fmt.Sprintf(gitHubCommitsApi, ref) + + req, err := http.NewRequest("GET", u, nil) + if err != nil { + return commit, err + } + + err = doGitHubRequest(req, &commit) + + return commit, err +} + +func fetchRepo() (gitHubRepo, error) { + var repo gitHubRepo + + req, err := http.NewRequest("GET", gitHubRepoApi, nil) + 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 + url := fmt.Sprintf(gitHubContributorsApi+"?page=%d", page) + + 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) + } +} |