Skip to content

Commit 8561588

Browse files
authored
Merge pull request #547 from danger/parse_commit_verification
Parse GitHub commit verification
2 parents 649db46 + 764bb8f commit 8561588

File tree

4 files changed

+137
-7
lines changed

4 files changed

+137
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
## Master
1515

16+
- Parse GitHub commit verification [@f-meloni][] - [#547](https://github.com/danger/swift/pull/547)
1617
- Bump Docker image base version to swift 5.7 [@mxsc][] - [#542](https://github.com/danger/swift/pull/542)
1718

1819
## 3.14.2

Sources/Danger/GitHubDSL.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,53 @@ public extension GitHub.Commit {
389389

390390
/// The URL for the commit.
391391
public let url: String
392+
393+
public let verification: Verification
394+
}
395+
}
396+
397+
public extension GitHub.Commit.CommitData {
398+
enum Verification: Equatable, Decodable {
399+
case verified(signature: String, payload: String)
400+
case unverified(UnverifiedReason)
401+
402+
enum CodingKeys: String, CodingKey {
403+
case payload
404+
case reason
405+
case signature
406+
case verified
407+
}
408+
409+
public init(from decoder: Decoder) throws {
410+
let container = try decoder.container(keyedBy: CodingKeys.self)
411+
let verified = try container.decode(Bool.self, forKey: .verified)
412+
413+
if verified {
414+
let signature = try container.decode(String.self, forKey: .signature)
415+
let payload = try container.decode(String.self, forKey: .payload)
416+
self = .verified(signature: signature, payload: payload)
417+
} else {
418+
let reason = try container.decode(UnverifiedReason.self, forKey: .reason)
419+
self = .unverified(reason)
420+
}
421+
}
422+
}
423+
}
424+
425+
public extension GitHub.Commit.CommitData.Verification {
426+
enum UnverifiedReason: String, Decodable {
427+
case expiredKey = "expired_key"
428+
case notSigningKey = "not_signing_key"
429+
case gpgVerifyError = "gpgverify_error"
430+
case gpgVerifyUnavailable = "gpgverify_unavailable"
431+
case unsigned
432+
case unknownSignatureType = "unknown_signature_type"
433+
case noUser = "no_user"
434+
case unverifiedEmail = "unverified_email"
435+
case badEmail = "bad_email"
436+
case unknownKey = "unknown_key"
437+
case malformedSignature = "malformed_signature"
438+
case invalid
392439
}
393440
}
394441

Tests/DangerTests/GitHubTestResources/GitHubCommit.swift

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public let GitHubCommitWithEmptyAuthorJSON = """
2222
"comment_count": 0,
2323
"verification": {
2424
"verified": false,
25-
"reason": "unsigned",
25+
"reason": "unknown_signature_type",
2626
"signature": null,
2727
"payload": null
2828
}
@@ -55,3 +55,61 @@ public let GitHubCommitWithEmptyAuthorJSON = """
5555
]
5656
}
5757
"""
58+
59+
public let GitHubCommitVerifiedJSON = """
60+
{
61+
"sha": "cad494648f773cd4fad5a9ea948c1bfabf36032a",
62+
"node_id": "MDY6Q29tbWl0MTAxMzM5MjEyOmNhZDQ5NDY0OGY3NzNjZDRmYWQ1YTllYTk0OGMxYmZhYmYzNjAzMmE=",
63+
"commit": {
64+
"author": {
65+
"name": "Franco Meloni",
66+
"email": "[email protected]",
67+
"date": "2019-04-20T17:46:50Z"
68+
},
69+
"committer": {
70+
"name": "Franco Meloni",
71+
"email": "[email protected]",
72+
"date": "2019-04-20T17:46:50Z"
73+
},
74+
"message": "Re use the same executor on the runner",
75+
"tree": {
76+
"sha": "96b7fdf0ab04926cc9eee844a15d66e097922777",
77+
"url": "https://api.github.com/repos/danger/swift/git/trees/96b7fdf0ab04926cc9eee844a15d66e097922777"
78+
},
79+
"url": "https://api.github.com/repos/danger/swift/git/commits/cad494648f773cd4fad5a9ea948c1bfabf36032a",
80+
"comment_count": 0,
81+
"verification": {
82+
"verified": true,
83+
"reason": "valid",
84+
"signature": "Test Signature",
85+
"payload": "Test Payload"
86+
}
87+
},
88+
"url": "https://api.github.com/repos/danger/swift/commits/cad494648f773cd4fad5a9ea948c1bfabf36032a",
89+
"html_url": "https://github.com/danger/swift/commit/cad494648f773cd4fad5a9ea948c1bfabf36032a",
90+
"comments_url": "https://api.github.com/repos/danger/swift/commits/cad494648f773cd4fad5a9ea948c1bfabf36032a/comments",
91+
"author": {},
92+
"committer": {
93+
"login": "f-meloni",
94+
"id": 17830956,
95+
"node_id": "MDQ6VXNlcjE3ODMwOTU2",
96+
"avatar_url": "https://avatars1.githubusercontent.com/u/17830956?v=4",
97+
"gravatar_id": "",
98+
"url": "https://api.github.com/users/f-meloni",
99+
"html_url": "https://github.com/f-meloni",
100+
"followers_url": "https://api.github.com/users/f-meloni/followers",
101+
"following_url": "https://api.github.com/users/f-meloni/following{/other_user}",
102+
"gists_url": "https://api.github.com/users/f-meloni/gists{/gist_id}",
103+
"starred_url": "https://api.github.com/users/f-meloni/starred{/owner}{/repo}",
104+
"subscriptions_url": "https://api.github.com/users/f-meloni/subscriptions",
105+
"organizations_url": "https://api.github.com/users/f-meloni/orgs",
106+
"repos_url": "https://api.github.com/users/f-meloni/repos",
107+
"events_url": "https://api.github.com/users/f-meloni/events{/privacy}",
108+
"received_events_url": "https://api.github.com/users/f-meloni/received_events",
109+
"type": "User",
110+
"site_admin": false
111+
},
112+
"parents": [
113+
]
114+
}
115+
"""

Tests/DangerTests/GitHubTests.swift

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,36 @@ final class GitHubTests: XCTestCase {
254254
XCTAssertNil(testCommit.author)
255255
XCTAssertEqual(testCommit.sha, "cad494648f773cd4fad5a9ea948c1bfabf36032a")
256256
XCTAssertEqual(testCommit.url, "https://api.github.com/repos/danger/swift/commits/cad494648f773cd4fad5a9ea948c1bfabf36032a")
257-
XCTAssertEqual(testCommit.commit, GitHub.Commit.CommitData(sha: nil,
258-
author: expectedAuthor,
259-
committer: expectedAuthor,
260-
message: "Re use the same executor on the runner",
261-
parents: nil,
262-
url: "https://api.github.com/repos/danger/swift/git/commits/cad494648f773cd4fad5a9ea948c1bfabf36032a"))
257+
XCTAssertEqual(testCommit.commit, GitHub.Commit.CommitData(
258+
sha: nil,
259+
author: expectedAuthor,
260+
committer: expectedAuthor,
261+
message: "Re use the same executor on the runner",
262+
parents: nil,
263+
url: "https://api.github.com/repos/danger/swift/git/commits/cad494648f773cd4fad5a9ea948c1bfabf36032a",
264+
verification: .unverified(.unknownSignatureType)
265+
))
266+
XCTAssertEqual(testCommit.committer, GitHub.User(id: 17_830_956, login: "f-meloni", userType: .user))
267+
}
268+
269+
func test_GitHubCommit_decodesJSONWithVerifiedCommit() throws {
270+
let data = Data(GitHubCommitVerifiedJSON.utf8)
271+
let expectedAuthor = Git.Commit.Author(name: "Franco Meloni", email: "[email protected]", date: "2019-04-20T17:46:50Z")
272+
273+
let testCommit = try decoder.decode(GitHub.Commit.self, from: data)
274+
275+
XCTAssertNil(testCommit.author)
276+
XCTAssertEqual(testCommit.sha, "cad494648f773cd4fad5a9ea948c1bfabf36032a")
277+
XCTAssertEqual(testCommit.url, "https://api.github.com/repos/danger/swift/commits/cad494648f773cd4fad5a9ea948c1bfabf36032a")
278+
XCTAssertEqual(testCommit.commit, GitHub.Commit.CommitData(
279+
sha: nil,
280+
author: expectedAuthor,
281+
committer: expectedAuthor,
282+
message: "Re use the same executor on the runner",
283+
parents: nil,
284+
url: "https://api.github.com/repos/danger/swift/git/commits/cad494648f773cd4fad5a9ea948c1bfabf36032a",
285+
verification: .verified(signature: "Test Signature", payload: "Test Payload")
286+
))
263287
XCTAssertEqual(testCommit.committer, GitHub.User(id: 17_830_956, login: "f-meloni", userType: .user))
264288
}
265289

0 commit comments

Comments
 (0)