forked from lunny/changelog
ff5c3f7b47
My guess is that GitHub changed something here recently, which is why it no longer worked without the explicit arguments
150 lines
3.1 KiB
Go
150 lines
3.1 KiB
Go
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/google/go-github/v50/github"
|
|
"golang.org/x/oauth2"
|
|
)
|
|
|
|
// GitHub defines a GitHub service
|
|
type GitHub struct {
|
|
Milestone string
|
|
GitTag string
|
|
Token string
|
|
Repo string
|
|
Issues bool
|
|
client *github.Client
|
|
}
|
|
|
|
// Generate returns a GitHub changelog
|
|
func (gh *GitHub) Generate() (string, []Entry, error) {
|
|
ctx := context.Background()
|
|
gh.initClient(ctx)
|
|
|
|
tagURL := fmt.Sprintf("## [%s](https://github.com/%s/releases/tag/%s) - %s", gh.Milestone, gh.Repo, gh.GitTag, time.Now().Format("2006-01-02"))
|
|
|
|
prs := make([]Entry, 0)
|
|
|
|
state := "merged"
|
|
isString := "is:pull-request"
|
|
if gh.Issues {
|
|
state = "closed"
|
|
isString = "is:issue"
|
|
}
|
|
|
|
query := fmt.Sprintf(`repo:%s %s is:%s milestone:"%s"`, gh.Repo, isString, state, gh.Milestone)
|
|
p := 1
|
|
perPage := 100
|
|
for {
|
|
result, _, err := gh.client.Search.Issues(ctx, query, &github.SearchOptions{
|
|
ListOptions: github.ListOptions{
|
|
Page: p,
|
|
PerPage: perPage,
|
|
},
|
|
})
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
p++
|
|
|
|
isPull := !(gh.Issues)
|
|
|
|
for _, pr := range result.Issues {
|
|
if pr.IsPullRequest() == isPull {
|
|
p := Entry{
|
|
Title: CleanTitle(pr.GetTitle()),
|
|
Index: int64(pr.GetNumber()),
|
|
}
|
|
|
|
labels := make([]Label, len(pr.Labels))
|
|
for idx, lbl := range pr.Labels {
|
|
labels[idx] = Label{
|
|
Name: lbl.GetName(),
|
|
}
|
|
}
|
|
p.Labels = labels
|
|
|
|
prs = append(prs, p)
|
|
}
|
|
}
|
|
|
|
if len(result.Issues) != perPage {
|
|
break
|
|
}
|
|
}
|
|
|
|
return tagURL, prs, nil
|
|
}
|
|
|
|
// Contributors returns a list of contributors from GitHub
|
|
func (gh *GitHub) Contributors() (ContributorList, error) {
|
|
ctx := context.Background()
|
|
gh.initClient(ctx)
|
|
|
|
contributorsMap := make(map[string]bool)
|
|
isString := "is:pull-request"
|
|
if gh.Issues {
|
|
isString = "is:issue"
|
|
}
|
|
|
|
query := fmt.Sprintf(`repo:%s %s is:merged milestone:"%s"`, gh.Repo, isString, gh.Milestone)
|
|
p := 1
|
|
perPage := 100
|
|
for {
|
|
result, _, err := gh.client.Search.Issues(ctx, query, &github.SearchOptions{
|
|
ListOptions: github.ListOptions{
|
|
Page: p,
|
|
PerPage: perPage,
|
|
},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
p++
|
|
|
|
for _, pr := range result.Issues {
|
|
contributorsMap[pr.GetUser().GetLogin()] = true
|
|
}
|
|
|
|
if len(result.Issues) != perPage {
|
|
break
|
|
}
|
|
}
|
|
|
|
contributors := make(ContributorList, 0, len(contributorsMap))
|
|
for contributor := range contributorsMap {
|
|
contributors = append(contributors, Contributor{
|
|
Name: contributor,
|
|
Profile: fmt.Sprintf("https://github.com/%s", contributor),
|
|
})
|
|
}
|
|
|
|
return contributors, nil
|
|
}
|
|
|
|
func (gh *GitHub) initClient(ctx context.Context) {
|
|
token := gh.Token
|
|
if envToken, ok := os.LookupEnv("CHANGELOG_GITHUB_TOKEN"); ok && token == "" {
|
|
token = envToken
|
|
}
|
|
|
|
cl := http.DefaultClient
|
|
if token != "" {
|
|
ts := oauth2.StaticTokenSource(
|
|
&oauth2.Token{AccessToken: token},
|
|
)
|
|
cl = oauth2.NewClient(ctx, ts)
|
|
}
|
|
|
|
gh.client = github.NewClient(cl)
|
|
}
|