Use issue list API instead of search (#78)
This PR changes to using the GH issue list API rather than search, as for some reason the search results started missing several PRs... Reviewed-on: https://gitea.com/gitea/changelog/pulls/78 Co-authored-by: jolheiser <john.olheiser@gmail.com> Co-committed-by: jolheiser <john.olheiser@gmail.com>
This commit is contained in:
parent
482c085290
commit
3d93c3a0cd
|
@ -6,9 +6,12 @@ package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/go-github/v50/github"
|
"github.com/google/go-github/v50/github"
|
||||||
|
@ -25,8 +28,18 @@ type GitHub struct {
|
||||||
client *github.Client
|
client *github.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OwnerRepo splits owner/repo
|
||||||
|
func (gh *GitHub) OwnerRepo() (string, string) {
|
||||||
|
parts := strings.Split(gh.Repo, "/")
|
||||||
|
if len(parts) < 2 {
|
||||||
|
return parts[0], ""
|
||||||
|
}
|
||||||
|
return parts[0], parts[1]
|
||||||
|
}
|
||||||
|
|
||||||
// Generate returns a GitHub changelog
|
// Generate returns a GitHub changelog
|
||||||
func (gh *GitHub) Generate() (string, []Entry, error) {
|
func (gh *GitHub) Generate() (string, []Entry, error) {
|
||||||
|
owner, repo := gh.OwnerRepo()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
gh.initClient(ctx)
|
gh.initClient(ctx)
|
||||||
|
|
||||||
|
@ -34,16 +47,17 @@ func (gh *GitHub) Generate() (string, []Entry, error) {
|
||||||
|
|
||||||
prs := make([]Entry, 0)
|
prs := make([]Entry, 0)
|
||||||
|
|
||||||
state := "merged"
|
milestoneNum, err := gh.milestoneNum(ctx)
|
||||||
if gh.Issues {
|
if err != nil {
|
||||||
state = "closed"
|
return "", nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
query := fmt.Sprintf(`repo:%s is:%s milestone:"%s"`, gh.Repo, state, gh.Milestone)
|
|
||||||
p := 1
|
p := 1
|
||||||
perPage := 100
|
perPage := 100
|
||||||
for {
|
for {
|
||||||
result, _, err := gh.client.Search.Issues(ctx, query, &github.SearchOptions{
|
result, _, err := gh.client.Issues.ListByRepo(ctx, owner, repo, &github.IssueListByRepoOptions{
|
||||||
|
Milestone: strconv.Itoa(milestoneNum),
|
||||||
|
State: "closed",
|
||||||
ListOptions: github.ListOptions{
|
ListOptions: github.ListOptions{
|
||||||
Page: p,
|
Page: p,
|
||||||
PerPage: perPage,
|
PerPage: perPage,
|
||||||
|
@ -56,7 +70,7 @@ func (gh *GitHub) Generate() (string, []Entry, error) {
|
||||||
|
|
||||||
isPull := !(gh.Issues)
|
isPull := !(gh.Issues)
|
||||||
|
|
||||||
for _, pr := range result.Issues {
|
for _, pr := range result {
|
||||||
if pr.IsPullRequest() == isPull {
|
if pr.IsPullRequest() == isPull {
|
||||||
p := Entry{
|
p := Entry{
|
||||||
Title: CleanTitle(pr.GetTitle()),
|
Title: CleanTitle(pr.GetTitle()),
|
||||||
|
@ -75,7 +89,7 @@ func (gh *GitHub) Generate() (string, []Entry, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(result.Issues) != perPage {
|
if len(result) != perPage {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -86,14 +100,20 @@ 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) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
owner, repo := gh.OwnerRepo()
|
||||||
gh.initClient(ctx)
|
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)
|
milestoneNum, err := gh.milestoneNum(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
p := 1
|
p := 1
|
||||||
perPage := 100
|
perPage := 100
|
||||||
for {
|
for {
|
||||||
result, _, err := gh.client.Search.Issues(ctx, query, &github.SearchOptions{
|
result, _, err := gh.client.Issues.ListByRepo(ctx, owner, repo, &github.IssueListByRepoOptions{
|
||||||
|
Milestone: strconv.Itoa(milestoneNum),
|
||||||
|
State: "closed",
|
||||||
ListOptions: github.ListOptions{
|
ListOptions: github.ListOptions{
|
||||||
Page: p,
|
Page: p,
|
||||||
PerPage: perPage,
|
PerPage: perPage,
|
||||||
|
@ -104,11 +124,11 @@ func (gh *GitHub) Contributors() (ContributorList, error) {
|
||||||
}
|
}
|
||||||
p++
|
p++
|
||||||
|
|
||||||
for _, pr := range result.Issues {
|
for _, pr := range result {
|
||||||
contributorsMap[pr.GetUser().GetLogin()] = true
|
contributorsMap[pr.GetUser().GetLogin()] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(result.Issues) != perPage {
|
if len(result) != perPage {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -129,7 +149,6 @@ func (gh *GitHub) initClient(ctx context.Context) {
|
||||||
if envToken, ok := os.LookupEnv("CHANGELOG_GITHUB_TOKEN"); ok && token == "" {
|
if envToken, ok := os.LookupEnv("CHANGELOG_GITHUB_TOKEN"); ok && token == "" {
|
||||||
token = envToken
|
token = envToken
|
||||||
}
|
}
|
||||||
|
|
||||||
cl := http.DefaultClient
|
cl := http.DefaultClient
|
||||||
if token != "" {
|
if token != "" {
|
||||||
ts := oauth2.StaticTokenSource(
|
ts := oauth2.StaticTokenSource(
|
||||||
|
@ -140,3 +159,36 @@ func (gh *GitHub) initClient(ctx context.Context) {
|
||||||
|
|
||||||
gh.client = github.NewClient(cl)
|
gh.client = github.NewClient(cl)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (gh *GitHub) milestoneNum(ctx context.Context) (int, error) {
|
||||||
|
owner, repo := gh.OwnerRepo()
|
||||||
|
p := 1
|
||||||
|
perPage := 100
|
||||||
|
for {
|
||||||
|
milestones, _, err := gh.client.Issues.ListMilestones(ctx, owner, repo, &github.MilestoneListOptions{
|
||||||
|
State: "all",
|
||||||
|
ListOptions: github.ListOptions{
|
||||||
|
Page: p,
|
||||||
|
PerPage: perPage,
|
||||||
|
},
|
||||||
|
Sort: "due_on",
|
||||||
|
Direction: "desc",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
p++
|
||||||
|
|
||||||
|
for _, milestone := range milestones {
|
||||||
|
if strings.EqualFold(milestone.GetTitle(), gh.Milestone) {
|
||||||
|
return milestone.GetNumber(), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(milestones) != perPage {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0, errors.New("no milestone found")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue