Remove duplicates

This commit is contained in:
Alisson Santos 2020-03-05 13:55:36 +01:00
parent d4cb2d860f
commit dcedacd4f7
3 changed files with 85 additions and 1 deletions

View file

@ -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 {

View file

@ -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()

50
test/fixtures/prListWithDuplicates.json vendored Normal file
View file

@ -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": ""
}
}
}
}
}