Add GetIssues to go/github

Also rename GetIssueUrlBase to GetPullRequestUrlBase
and add a more appropriate GetIssueUrlBase

Bug: skia:10783
Change-Id: Ic9e58ebae66626a0f408b5b9da5222e2153370e3
Reviewed-on: https://skia-review.googlesource.com/c/buildbot/+/328979
Reviewed-by: Joe Gregorio <jcgregorio@google.com>
diff --git a/autoroll/go/codereview/codereview.go b/autoroll/go/codereview/codereview.go
index 5501fd8..eda2953 100644
--- a/autoroll/go/codereview/codereview.go
+++ b/autoroll/go/codereview/codereview.go
@@ -121,7 +121,7 @@
 	}
 	return &githubCodeReview{
 		cfg:            cfg,
-		issueUrlBase:   githubClient.GetIssueUrlBase(),
+		issueUrlBase:   githubClient.GetPullRequestUrlBase(),
 		fullHistoryUrl: githubClient.GetFullHistoryUrl(userName),
 		githubClient:   githubClient,
 		userEmail:      userEmail,
diff --git a/go/github/github.go b/go/github/github.go
index 921651b..168b7b5 100644
--- a/go/github/github.go
+++ b/go/github/github.go
@@ -262,6 +262,28 @@
 	return labelNames
 }
 
+// See https://developer.github.com/v3/issues/#list-repository-issues
+// for the API documentation.
+func (g *GitHub) GetIssues(open bool, labels []string, maxResults int) ([]*github.Issue, error) {
+	opts := &github.IssueListByRepoOptions{
+		Labels: labels,
+		ListOptions: github.ListOptions{
+			PerPage: maxResults, // Default seems to be 30 per page.
+		},
+	}
+	if open {
+		opts.State = "open"
+	}
+	issues, resp, err := g.client.Issues.ListByRepo(g.ctx, g.RepoOwner, g.RepoName, opts)
+	if err != nil {
+		return nil, fmt.Errorf("Failed doing issues.list: %s", err)
+	}
+	if resp.StatusCode != http.StatusOK {
+		return nil, fmt.Errorf("Unexpected status code %d from issues.list.", resp.StatusCode)
+	}
+	return issues, nil
+}
+
 // See https://developer.github.com/v3/issues/#get-a-single-issue
 // for the API documentation.
 func (g *GitHub) GetLabels(pullRequestNum int) ([]string, error) {
@@ -479,6 +501,10 @@
 	return fmt.Sprintf("https://github.com/%s/%s/pulls/%s", g.RepoOwner, g.RepoName, user)
 }
 
-func (g *GitHub) GetIssueUrlBase() string {
+func (g *GitHub) GetPullRequestUrlBase() string {
 	return fmt.Sprintf("https://github.com/%s/%s/pull/", g.RepoOwner, g.RepoName)
 }
+
+func (g *GitHub) GetIssueUrlBase() string {
+	return fmt.Sprintf("https://github.com/%s/%s/issues/", g.RepoOwner, g.RepoName)
+}
diff --git a/go/github/github_test.go b/go/github/github_test.go
index 5d7f7f0..c2a4d2f 100644
--- a/go/github/github_test.go
+++ b/go/github/github_test.go
@@ -193,6 +193,27 @@
 	require.Equal(t, CLOSED_STATE, *pr.State)
 }
 
+func TestGetIssues(t *testing.T) {
+	unittest.SmallTest(t)
+	id1 := int64(11)
+	id2 := int64(22)
+	issue1 := github.Issue{ID: &id1}
+	issue2 := github.Issue{ID: &id2}
+	respBody := []byte(testutils.MarshalJSON(t, []*github.Issue{&issue1, &issue2}))
+	r := mux.NewRouter()
+	md := mockhttpclient.MockGetDialogue(respBody)
+	r.Schemes("https").Host("api.github.com").Methods("GET").Path("/repos/kryptonians/krypton/issues").Queries("labels", "label1,label2", "per_page", "123", "state", "open").Handler(md)
+	httpClient := mockhttpclient.NewMuxClient(r)
+
+	githubClient, err := NewGitHub(context.Background(), "kryptonians", "krypton", httpClient)
+	require.NoError(t, err)
+	issues, getIssuesErr := githubClient.GetIssues(true, []string{"label1", "label2"}, 123)
+	require.NoError(t, getIssuesErr)
+	require.Equal(t, 2, len(issues))
+	require.Equal(t, id1, issues[0].GetID())
+	require.Equal(t, id2, issues[1].GetID())
+}
+
 func TestGetLabelsRequest(t *testing.T) {
 	unittest.SmallTest(t)
 	label1Name := "test1"
@@ -389,11 +410,20 @@
 	require.Equal(t, "https://github.com/kryptonians/krypton/pulls/superman", fullHistoryUrl)
 }
 
+func TestGetPullRequestUrlBase(t *testing.T) {
+	unittest.SmallTest(t)
+	httpClient := mockhttpclient.NewMuxClient(mux.NewRouter())
+	githubClient, err := NewGitHub(context.Background(), "kryptonians", "krypton", httpClient)
+	require.NoError(t, err)
+	pullRequestUrlBase := githubClient.GetPullRequestUrlBase()
+	require.Equal(t, "https://github.com/kryptonians/krypton/pull/", pullRequestUrlBase)
+}
+
 func TestGetIssueUrlBase(t *testing.T) {
 	unittest.SmallTest(t)
 	httpClient := mockhttpclient.NewMuxClient(mux.NewRouter())
 	githubClient, err := NewGitHub(context.Background(), "kryptonians", "krypton", httpClient)
 	require.NoError(t, err)
 	issueUrlBase := githubClient.GetIssueUrlBase()
-	require.Equal(t, "https://github.com/kryptonians/krypton/pull/", issueUrlBase)
+	require.Equal(t, "https://github.com/kryptonians/krypton/issues/", issueUrlBase)
 }