Skip to content

Commit

Permalink
Merge pull request #547 from danger/parse_commit_verification
Browse files Browse the repository at this point in the history
Parse GitHub commit verification
  • Loading branch information
f-meloni authored Nov 22, 2022
2 parents 649db46 + 764bb8f commit 8561588
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

## Master

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

## 3.14.2
Expand Down
47 changes: 47 additions & 0 deletions Sources/Danger/GitHubDSL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,53 @@ public extension GitHub.Commit {

/// The URL for the commit.
public let url: String

public let verification: Verification
}
}

public extension GitHub.Commit.CommitData {
enum Verification: Equatable, Decodable {
case verified(signature: String, payload: String)
case unverified(UnverifiedReason)

enum CodingKeys: String, CodingKey {
case payload
case reason
case signature
case verified
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let verified = try container.decode(Bool.self, forKey: .verified)

if verified {
let signature = try container.decode(String.self, forKey: .signature)
let payload = try container.decode(String.self, forKey: .payload)
self = .verified(signature: signature, payload: payload)
} else {
let reason = try container.decode(UnverifiedReason.self, forKey: .reason)
self = .unverified(reason)
}
}
}
}

public extension GitHub.Commit.CommitData.Verification {
enum UnverifiedReason: String, Decodable {
case expiredKey = "expired_key"
case notSigningKey = "not_signing_key"
case gpgVerifyError = "gpgverify_error"
case gpgVerifyUnavailable = "gpgverify_unavailable"
case unsigned
case unknownSignatureType = "unknown_signature_type"
case noUser = "no_user"
case unverifiedEmail = "unverified_email"
case badEmail = "bad_email"
case unknownKey = "unknown_key"
case malformedSignature = "malformed_signature"
case invalid
}
}

Expand Down
60 changes: 59 additions & 1 deletion Tests/DangerTests/GitHubTestResources/GitHubCommit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public let GitHubCommitWithEmptyAuthorJSON = """
"comment_count": 0,
"verification": {
"verified": false,
"reason": "unsigned",
"reason": "unknown_signature_type",
"signature": null,
"payload": null
}
Expand Down Expand Up @@ -55,3 +55,61 @@ public let GitHubCommitWithEmptyAuthorJSON = """
]
}
"""

public let GitHubCommitVerifiedJSON = """
{
"sha": "cad494648f773cd4fad5a9ea948c1bfabf36032a",
"node_id": "MDY6Q29tbWl0MTAxMzM5MjEyOmNhZDQ5NDY0OGY3NzNjZDRmYWQ1YTllYTk0OGMxYmZhYmYzNjAzMmE=",
"commit": {
"author": {
"name": "Franco Meloni",
"email": "[email protected]",
"date": "2019-04-20T17:46:50Z"
},
"committer": {
"name": "Franco Meloni",
"email": "[email protected]",
"date": "2019-04-20T17:46:50Z"
},
"message": "Re use the same executor on the runner",
"tree": {
"sha": "96b7fdf0ab04926cc9eee844a15d66e097922777",
"url": "https://api.github.com/repos/danger/swift/git/trees/96b7fdf0ab04926cc9eee844a15d66e097922777"
},
"url": "https://api.github.com/repos/danger/swift/git/commits/cad494648f773cd4fad5a9ea948c1bfabf36032a",
"comment_count": 0,
"verification": {
"verified": true,
"reason": "valid",
"signature": "Test Signature",
"payload": "Test Payload"
}
},
"url": "https://api.github.com/repos/danger/swift/commits/cad494648f773cd4fad5a9ea948c1bfabf36032a",
"html_url": "https://github.com/danger/swift/commit/cad494648f773cd4fad5a9ea948c1bfabf36032a",
"comments_url": "https://api.github.com/repos/danger/swift/commits/cad494648f773cd4fad5a9ea948c1bfabf36032a/comments",
"author": {},
"committer": {
"login": "f-meloni",
"id": 17830956,
"node_id": "MDQ6VXNlcjE3ODMwOTU2",
"avatar_url": "https://avatars1.githubusercontent.com/u/17830956?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/f-meloni",
"html_url": "https://github.com/f-meloni",
"followers_url": "https://api.github.com/users/f-meloni/followers",
"following_url": "https://api.github.com/users/f-meloni/following{/other_user}",
"gists_url": "https://api.github.com/users/f-meloni/gists{/gist_id}",
"starred_url": "https://api.github.com/users/f-meloni/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/f-meloni/subscriptions",
"organizations_url": "https://api.github.com/users/f-meloni/orgs",
"repos_url": "https://api.github.com/users/f-meloni/repos",
"events_url": "https://api.github.com/users/f-meloni/events{/privacy}",
"received_events_url": "https://api.github.com/users/f-meloni/received_events",
"type": "User",
"site_admin": false
},
"parents": [
]
}
"""
36 changes: 30 additions & 6 deletions Tests/DangerTests/GitHubTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,36 @@ final class GitHubTests: XCTestCase {
XCTAssertNil(testCommit.author)
XCTAssertEqual(testCommit.sha, "cad494648f773cd4fad5a9ea948c1bfabf36032a")
XCTAssertEqual(testCommit.url, "https://api.github.com/repos/danger/swift/commits/cad494648f773cd4fad5a9ea948c1bfabf36032a")
XCTAssertEqual(testCommit.commit, GitHub.Commit.CommitData(sha: nil,
author: expectedAuthor,
committer: expectedAuthor,
message: "Re use the same executor on the runner",
parents: nil,
url: "https://api.github.com/repos/danger/swift/git/commits/cad494648f773cd4fad5a9ea948c1bfabf36032a"))
XCTAssertEqual(testCommit.commit, GitHub.Commit.CommitData(
sha: nil,
author: expectedAuthor,
committer: expectedAuthor,
message: "Re use the same executor on the runner",
parents: nil,
url: "https://api.github.com/repos/danger/swift/git/commits/cad494648f773cd4fad5a9ea948c1bfabf36032a",
verification: .unverified(.unknownSignatureType)
))
XCTAssertEqual(testCommit.committer, GitHub.User(id: 17_830_956, login: "f-meloni", userType: .user))
}

func test_GitHubCommit_decodesJSONWithVerifiedCommit() throws {
let data = Data(GitHubCommitVerifiedJSON.utf8)
let expectedAuthor = Git.Commit.Author(name: "Franco Meloni", email: "[email protected]", date: "2019-04-20T17:46:50Z")

let testCommit = try decoder.decode(GitHub.Commit.self, from: data)

XCTAssertNil(testCommit.author)
XCTAssertEqual(testCommit.sha, "cad494648f773cd4fad5a9ea948c1bfabf36032a")
XCTAssertEqual(testCommit.url, "https://api.github.com/repos/danger/swift/commits/cad494648f773cd4fad5a9ea948c1bfabf36032a")
XCTAssertEqual(testCommit.commit, GitHub.Commit.CommitData(
sha: nil,
author: expectedAuthor,
committer: expectedAuthor,
message: "Re use the same executor on the runner",
parents: nil,
url: "https://api.github.com/repos/danger/swift/git/commits/cad494648f773cd4fad5a9ea948c1bfabf36032a",
verification: .verified(signature: "Test Signature", payload: "Test Payload")
))
XCTAssertEqual(testCommit.committer, GitHub.User(id: 17_830_956, login: "f-meloni", userType: .user))
}

Expand Down

0 comments on commit 8561588

Please sign in to comment.