Respect --token argument on github too (#79)

Reviewed-on: https://gitea.com/gitea/changelog/pulls/79
Reviewed-by: John Olheiser <john+gitea@jolheiser.com>
Reviewed-by: delvh <dev.lh@web.de>
This commit is contained in:
6543 2023-06-23 17:20:48 +00:00
parent 07c0fc1414
commit 482c085290
1 changed files with 16 additions and 10 deletions

View File

@ -22,14 +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) {
tagURL := fmt.Sprintf("## [%s](https://github.com/%s/releases/tag/%s) - %s", gh.Milestone, gh.Repo, gh.GitTag, time.Now().Format("2006-01-02"))
client := github.NewClient(httpClient())
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)
@ -42,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,
@ -84,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,
@ -123,14 +124,19 @@ 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 {
ctx := context.Background()
if token != "" {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
cl = oauth2.NewClient(ctx, ts)
}
return cl
gh.client = github.NewClient(cl)
}