Repository: jenkins-x/lighthouse
Pull request #1062 (2020-09-16 11:15:49)

fix: make welcome plugin work
This PR makes the welcome plugin work.

The `FindIssues` method in the scm provider client is not implemented, i created a `FindPullRequestsByAuthor` method to be used by the welcome plugin.

jenkins-x-bot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by:
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **eddycharly**
You can assign the PR to them by writing `/assign @eddycharly` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=jenkins-x%2Flighthouse).

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/jenkins-x/lighthouse/blob/master/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["eddycharly"]} -->
abayer: /lgtm
jenkins-x-bot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *[abayer](https://github.com/jenkins-x/lighthouse/pull/1062#issuecomment-693388392 "LGTM")*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=jenkins-x%2Flighthouse).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com#issuecomment-693388392/jenkins-x/lighthouse/blob/master/OWNERS)~~ [abayer]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/pkg/plugins/welcome/welcome.go b/pkg/plugins/welcome/welcome.go
index 816e97ba..41997427 100644
--- a/pkg/plugins/welcome/welcome.go
+++ b/pkg/plugins/welcome/welcome.go
@@ -76,7 +76,7 @@ func configHelp(config *plugins.Configuration, enabledRepos []string) (map[strin
 
 type scmProviderClient interface {
 	CreateComment(owner, repo string, number int, pr bool, comment string) error
-	FindIssues(query, sort string, asc bool) ([]scm.Issue, error)
+	FindPullRequestsByAuthor(owner, repo string, author string) ([]*scm.PullRequest, error)
 }
 
 type client struct {
@@ -105,8 +105,7 @@ func handlePR(c client, pre scm.PullRequestHook, welcomeTemplate string) error {
 	org := pre.PullRequest.Base.Repo.Namespace
 	repo := pre.PullRequest.Base.Repo.Name
 	user := pre.PullRequest.Author.Login
-	query := fmt.Sprintf("is:pr repo:%s/%s author:%s", org, repo, user)
-	issues, err := c.SCMProviderClient.FindIssues(query, "", false)
+	issues, err := c.SCMProviderClient.FindPullRequestsByAuthor(org, repo, user)
 	if err != nil {
 		return err
 	}
diff --git a/pkg/scmprovider/client.go b/pkg/scmprovider/client.go
index cb0fd823..a693bcfe 100644
--- a/pkg/scmprovider/client.go
+++ b/pkg/scmprovider/client.go
@@ -67,6 +67,7 @@ type SCMClient interface {
 	ReopenPR(string, string, int) error
 	ClosePR(string, string, int) error
 	ListAllPullRequestsForFullNameRepo(string, scm.PullRequestListOptions) ([]*scm.PullRequest, error)
+	FindPullRequestsByAuthor(string, string, string) ([]*scm.PullRequest, error)
 
 	// Functions implemented in repositories.go
 	GetRepoLabels(string, string) ([]*scm.Label, error)
diff --git a/pkg/scmprovider/pull_requests.go b/pkg/scmprovider/pull_requests.go
index 1c99156f..37d83c5d 100644
--- a/pkg/scmprovider/pull_requests.go
+++ b/pkg/scmprovider/pull_requests.go
@@ -189,3 +189,31 @@ func (c *Client) ClosePR(owner, repo string, number int) error {
 	_, err := c.client.PullRequests.Close(ctx, fullName, number)
 	return err
 }
+
+// FindPullRequestsByAuthor finds all pull requests for a given author
+func (c *Client) FindPullRequestsByAuthor(owner, repo string, author string) ([]*scm.PullRequest, error) {
+	ctx := context.Background()
+	fullName := c.repositoryName(owner, repo)
+	var allPullRequests []*scm.PullRequest
+	var resp *scm.Response
+	var pullRequests []*scm.PullRequest
+	var err error
+	firstRun := false
+	opts := scm.PullRequestListOptions{
+		Page: 1,
+	}
+	for !firstRun || (resp != nil && opts.Page <= resp.Page.Last) {
+		pullRequests, resp, err = c.client.PullRequests.List(ctx, fullName, opts)
+		if err != nil {
+			return nil, err
+		}
+		firstRun = true
+		for _, pullRequest := range pullRequests {
+			if pullRequest.Author.Login == author {
+				allPullRequests = append(allPullRequests, pullRequest)
+			}
+		}
+		opts.Page++
+	}
+	return allPullRequests, err
+}


Test patch:
diff --git a/pkg/plugins/welcome/welcome_test.go b/pkg/plugins/welcome/welcome_test.go
index 78562f5d..23b07e99 100644
--- a/pkg/plugins/welcome/welcome_test.go
+++ b/pkg/plugins/welcome/welcome_test.go
@@ -19,7 +19,6 @@ package welcome
 import (
 	"fmt"
 	"io/ioutil"
-	"regexp"
 	"strings"
 	"testing"
 
@@ -68,10 +67,6 @@ func (fc *fakeClient) NumComments() int {
 	return n
 }
 
-var (
-	expectedQueryRegex = regexp.MustCompile(`is:pr repo:(.+)/(.+) author:(.+)`)
-)
-
 // AddPR records an PR in the client
 func (fc *fakeClient) AddPR(owner, repo, author string, number int) {
 	key := fmt.Sprintf("%s,%s,%s", owner, repo, author)
@@ -88,18 +83,13 @@ func (fc *fakeClient) ClearPRs() {
 
 // FindIssues fails if the query does not match the expected query regex and
 // looks up issues based on parsing the expected query format
-func (fc *fakeClient) FindIssues(query, sort string, asc bool) ([]scm.Issue, error) {
-	fields := expectedQueryRegex.FindStringSubmatch(query)
-	if fields == nil || len(fields) != 4 {
-		return nil, fmt.Errorf("invalid query: `%s` does not match expected regex `%s`", query, expectedQueryRegex.String())
-	}
+func (fc *fakeClient) FindPullRequestsByAuthor(owner, repo string, author string) ([]*scm.PullRequest, error) {
 	// "find" results
-	owner, repo, author := fields[1], fields[2], fields[3]
 	key := fmt.Sprintf("%s,%s,%s", owner, repo, author)
 
-	issues := []scm.Issue{}
+	issues := []*scm.PullRequest{}
 	for _, number := range fc.prs[key].List() {
-		issues = append(issues, scm.Issue{
+		issues = append(issues, &scm.PullRequest{
 			Number: number,
 		})
 	}