This commit is contained in:
6543 2023-06-23 16:08:24 +02:00
parent ae9006322a
commit 8003618010
No known key found for this signature in database
GPG Key ID: B8BE6D610E61C862
1 changed files with 14 additions and 13 deletions

View File

@ -22,20 +22,15 @@ type GitHub struct {
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"))
var client *github.Client
if gh.Token != "" {
client = github.NewTokenClient(ctx, gh.Token)
} else {
client = github.NewClient(httpClient())
}
prs := make([]Entry, 0)
@ -48,7 +43,7 @@ func (gh *GitHub) Generate() (string, []Entry, error) {
p := 1
perPage := 100
for {
result, _, err := client.Search.Issues(ctx, query, &github.SearchOptions{
result, _, err := gh.client.Search.Issues(ctx, query, &github.SearchOptions{
ListOptions: github.ListOptions{
Page: p,
PerPage: perPage,
@ -90,15 +85,15 @@ func (gh *GitHub) Generate() (string, []Entry, error) {
// Contributors returns a list of contributors from GitHub
func (gh *GitHub) Contributors() (ContributorList, error) {
client := github.NewClient(httpClient())
ctx := context.Background()
gh.initClient(ctx)
contributorsMap := make(map[string]bool)
query := fmt.Sprintf(`repo:%s is:merged milestone:"%s"`, gh.Repo, gh.Milestone)
p := 1
perPage := 100
for {
result, _, err := client.Search.Issues(ctx, query, &github.SearchOptions{
result, _, err := gh.client.Search.Issues(ctx, query, &github.SearchOptions{
ListOptions: github.ListOptions{
Page: p,
PerPage: perPage,
@ -129,14 +124,20 @@ func (gh *GitHub) Contributors() (ContributorList, error) {
return contributors, nil
}
func httpClient() *http.Client {
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, ok := os.LookupEnv("CHANGELOG_GITHUB_TOKEN"); ok {
if token != "" {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
cl = oauth2.NewClient(ctx, ts)
}
return cl
gh.client = github.NewClient(cl)
}