2020-01-24 16:30:16 +00:00
|
|
|
// 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"
|
|
|
|
"time"
|
|
|
|
|
2020-08-07 11:55:11 +00:00
|
|
|
"github.com/google/go-github/v32/github"
|
2020-01-24 16:30:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GitHub defines a GitHub service
|
|
|
|
type GitHub struct {
|
|
|
|
Milestone string
|
2022-03-13 02:33:03 +00:00
|
|
|
GitTag string
|
2020-01-24 16:30:16 +00:00
|
|
|
Token string
|
|
|
|
Repo string
|
2020-08-04 13:30:19 +00:00
|
|
|
Issues bool
|
2020-01-24 16:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate returns a GitHub changelog
|
2020-08-04 13:30:19 +00:00
|
|
|
func (gh *GitHub) Generate() (string, []Entry, error) {
|
2022-03-13 02:33:03 +00:00
|
|
|
tagURL := fmt.Sprintf("## [%s](https://github.com/%s/releases/tag/%s) - %s", gh.Milestone, gh.Repo, gh.GitTag, time.Now().Format("2006-01-02"))
|
2020-01-24 16:30:16 +00:00
|
|
|
|
|
|
|
client := github.NewClient(nil)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2020-08-04 13:30:19 +00:00
|
|
|
prs := make([]Entry, 0)
|
2020-01-24 16:30:16 +00:00
|
|
|
|
2020-08-04 13:30:19 +00:00
|
|
|
state := "merged"
|
|
|
|
if gh.Issues {
|
|
|
|
state = "closed"
|
|
|
|
}
|
|
|
|
|
|
|
|
query := fmt.Sprintf(`repo:%s is:%s milestone:"%s"`, gh.Repo, state, gh.Milestone)
|
2020-01-24 16:30:16 +00:00
|
|
|
p := 1
|
|
|
|
perPage := 100
|
|
|
|
for {
|
|
|
|
result, _, err := client.Search.Issues(ctx, query, &github.SearchOptions{
|
|
|
|
ListOptions: github.ListOptions{
|
|
|
|
Page: p,
|
|
|
|
PerPage: perPage,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
p++
|
|
|
|
|
2020-08-04 13:30:19 +00:00
|
|
|
isPull := !(gh.Issues)
|
|
|
|
|
2020-01-24 16:30:16 +00:00
|
|
|
for _, pr := range result.Issues {
|
2020-08-04 13:30:19 +00:00
|
|
|
if pr.IsPullRequest() == isPull {
|
|
|
|
p := Entry{
|
2020-01-24 16:30:16 +00:00
|
|
|
Title: 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) {
|
|
|
|
client := github.NewClient(nil)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
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{
|
|
|
|
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))
|
2020-01-26 03:43:05 +00:00
|
|
|
for contributor := range contributorsMap {
|
2020-01-24 16:30:16 +00:00
|
|
|
contributors = append(contributors, Contributor{
|
|
|
|
Name: contributor,
|
|
|
|
Profile: fmt.Sprintf("https://github.com/%s", contributor),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return contributors, nil
|
|
|
|
}
|