Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds pagination to Github request. #4

Merged
merged 1 commit into from
Jan 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
"context"
"strings"
"os"
"path"
"strings"

"github.com/golang/glog"
"golang.org/x/oauth2"
Expand All @@ -14,13 +14,13 @@ import (
)

func contains(slice []string, item string) bool {
set := make(map[string]struct{}, len(slice))
for _, s := range slice {
set[s] = struct{}{}
}
set := make(map[string]struct{}, len(slice))
Copy link
Contributor Author

@potiuk potiuk Jan 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I run gofmt with the project and it formatted it this way. I hope it's OK for you in one PR.

for _, s := range slice {
set[s] = struct{}{}
}

_, ok := set[item]
return ok
_, ok := set[item]
return ok
}

func getCurrentLabels(pr *github.PullRequest) []string {
Expand All @@ -33,7 +33,7 @@ func getCurrentLabels(pr *github.PullRequest) []string {

func containsLabels(expected []string, current []string) bool {
for _, e := range expected {
if ! contains(current, e) {
if !contains(current, e) {
return false
}
}
Expand Down Expand Up @@ -97,18 +97,25 @@ func main() {
}

opt := &github.PullRequestListOptions{State: "open", Sort: "updated"}
pulls, _, err := client.PullRequests.List(context.Background(), owner, repo, opt)
if err != nil {
glog.Fatal(err)
}
for _, pull := range pulls {
files, _, err := client.PullRequests.ListFiles(context.Background(), owner, repo, *pull.Number, nil)
// get all pages of results
for {
pulls, resp, err := client.PullRequests.List(context.Background(), owner, repo, opt)
if err != nil {
glog.Error(err)
glog.Fatal(err)
}
for _, pull := range pulls {
files, _, err := client.PullRequests.ListFiles(context.Background(), owner, repo, *pull.Number, nil)
if err != nil {
glog.Error(err)
}
expectedLabels := matchFiles(labelMatchers, files)
if !containsLabels(expectedLabels, getCurrentLabels(pull)) {
client.Issues.AddLabelsToIssue(context.Background(), owner, repo, *pull.Number, expectedLabels)
}
}
expectedLabels := matchFiles(labelMatchers, files)
if ! containsLabels(expectedLabels, getCurrentLabels(pull)) {
client.Issues.AddLabelsToIssue(context.Background(), owner, repo, *pull.Number, expectedLabels)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
}