diff --git a/api/queries_pr.go b/api/queries_pr.go index 265378b1e..092c7c12b 100644 --- a/api/queries_pr.go +++ b/api/queries_pr.go @@ -590,7 +590,21 @@ loop: } } - return prs, nil + return removeDuplicates(prs), nil +} + +func removeDuplicates(prs []PullRequest) []PullRequest { + var check = make(map[int]struct{}) + var uniqPRs []PullRequest + + for _, pr := range prs { + if _, ok := check[pr.Number]; !ok { + check[pr.Number] = struct{}{} + uniqPRs = append(uniqPRs, pr) + } + } + + return uniqPRs } func min(a, b int) int { diff --git a/command/pr_test.go b/command/pr_test.go index 51469342a..395dd4097 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -240,6 +240,26 @@ No pull requests match your search eq(t, reqBody.Variables.Labels, []string{"one", "two", "three"}) } +func TestPRList_filteringRemoveDuplicate(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + jsonFile, _ := os.Open("../test/fixtures/prListWithDuplicates.json") + defer jsonFile.Close() + http.StubResponse(200, jsonFile) + + output, err := RunCommand(prListCmd, "pr list -l one,two") + if err != nil { + t.Fatal(err) + } + + eq(t, output.String(), `32 New feature feature +29 Fixed bad bug hubot:bug-fix +28 Improve documentation docs +`) +} + func TestPRList_filteringClosed(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() diff --git a/test/fixtures/prListWithDuplicates.json b/test/fixtures/prListWithDuplicates.json new file mode 100644 index 000000000..a9cf19638 --- /dev/null +++ b/test/fixtures/prListWithDuplicates.json @@ -0,0 +1,50 @@ +{ + "data": { + "repository": { + "pullRequests": { + "edges": [ + { + "node": { + "number": 32, + "title": "New feature", + "url": "https://github.com/monalisa/hello/pull/32", + "headRefName": "feature" + } + }, + { + "node": { + "number": 32, + "title": "New feature", + "url": "https://github.com/monalisa/hello/pull/32", + "headRefName": "feature" + } + }, + { + "node": { + "number": 29, + "title": "Fixed bad bug", + "url": "https://github.com/monalisa/hello/pull/29", + "headRefName": "bug-fix", + "isCrossRepository": true, + "headRepositoryOwner": { + "login": "hubot" + } + } + }, + { + "node": { + "number": 28, + "title": "Improve documentation", + "url": "https://github.com/monalisa/hello/pull/28", + "headRefName": "docs" + } + } + ], + "pageInfo": { + "hasNextPage": false, + "endCursor": "" + } + } + } + } +}