Skip to content

Commit

Permalink
Filter statuses in the github driver
Browse files Browse the repository at this point in the history
GitHub keeps multiple versions of a check with the same label.
The API returns the list in reverse chronological order:
https://developer.github.com/v3/repos/statuses/#list-statuses-for-a-specific-ref.

The status resource in go-scm does not expose any information
that might help distinguish between versions of checks with
the same label.

When a client of go-scm receives a list of statuses that contain
duplicate labels, it has no way of finding out which one is the
most recent one.

Change the github driver to filter the statuses and only return
one (the most recent) for each label.

Fixes drone#79
  • Loading branch information
afrittoli committed Mar 10, 2020
1 parent 838e28f commit 5e8cfa7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
12 changes: 11 additions & 1 deletion scm/driver/github/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,19 @@ func convertCombinedStatus(from *combinedStatus) *scm.CombinedStatus {
}

func convertStatusList(from []*status) []*scm.Status {
// The GitHub API may return multiple statuses with the same Context, in
// reverse chronological order:
// https://developer.github.com/v3/repos/statuses/#list-statuses-for-a-specific-ref.
// We only expose the most recent one to consumers.
to := []*scm.Status{}
unique := make(map[string]interface{})
for _, v := range from {
to = append(to, convertStatus(v))
convertedStatus := convertStatus(v)
if _, ok := unique[convertedStatus.Label]; ok {
continue
}
to = append(to, convertedStatus)
unique[convertedStatus.Label] = nil
}
return to
}
Expand Down
31 changes: 30 additions & 1 deletion scm/driver/github/testdata/statuses.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,34 @@
"type": "User",
"site_admin": false
}
},
{
"created_at": "2012-06-20T01:19:13Z",
"updated_at": "2012-06-20T01:19:13Z",
"state": "failure",
"target_url": "https://ci.example.com/1000/output",
"description": "Build failed",
"id": 2,
"url": "https://api.github.com/repos/octocat/Hello-World/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e",
"context": "continuous-integration/drone",
"creator": {
"login": "octocat",
"id": 1,
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "",
"url": "https://api.github.com/users/octocat",
"html_url": "https://github.com/octocat",
"followers_url": "https://api.github.com/users/octocat/followers",
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
"organizations_url": "https://api.github.com/users/octocat/orgs",
"repos_url": "https://api.github.com/users/octocat/repos",
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/octocat/received_events",
"type": "User",
"site_admin": false
}
}
]
]

0 comments on commit 5e8cfa7

Please sign in to comment.