From 80036180104e44c8f7b48bbdab79e7f12298e7b8 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 23 Jun 2023 16:08:24 +0200 Subject: [PATCH] refactor --- service/github.go | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/service/github.go b/service/github.go index 39c2ffc..e3aba33 100644 --- a/service/github.go +++ b/service/github.go @@ -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) }