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