Skip to content

Commit

Permalink
Retrieve commit sha from payload on pull_request event
Browse files Browse the repository at this point in the history
  • Loading branch information
norio-nomura committed Sep 18, 2019
1 parent 1331588 commit bdc8535
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
50 changes: 42 additions & 8 deletions Sources/Lib/GitHub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,28 +86,62 @@ extension GitHub.Repository.CheckRun {
}
}

extension GitHub {
public enum EventPayload {}
}

extension GitHub.EventPayload {
public struct PullRequestEvent: Decodable {
public var pull_request: PullRequest
}

public struct PullRequest: Decodable {
public var head: Head
}

public struct Head: Decodable {
public var sha: String
}
}

extension GitHub.Repository {
public func currentCheckRun() -> CheckRun? {
guard let sha = environment("GITHUB_SHA") else { return nil }
guard let checkRun = findCheckRun(for: sha) else {
print("Current Action not found!")
switch environment("GITHUB_EVENT_NAME") {
case "push":
return environment("GITHUB_SHA").flatMap(findCheckRun)
case "pull_request":
return (pullRequestEventPayload()?.pull_request.head.sha).flatMap(findCheckRun)
default:
return nil
}
return checkRun
}

public func pullRequestEventPayload() -> GitHub.EventPayload.PullRequestEvent? {
return environment("GITHUB_EVENT_PATH").flatMap {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: $0))
return try JSONDecoder().decode(GitHub.EventPayload.PullRequestEvent.self, from: data)
} catch {
print("\(error)")
return nil
}
}
}

public func findCheckRun(for ref: String) -> CheckRun? {
return checkRuns(for: ref).first { checkRun -> Bool in
checkRun.app.name == "GitHub Actions"
guard let checkRun = checkRuns(for: ref).first(where: { $0.app.name == "GitHub Actions" }) else {
print("Current Action not found!")
return nil
}
return checkRun
}

public func checkRuns(for ref: String) -> [CheckRun] {
public func checkRuns(for sha: String) -> [CheckRun] {
struct Response: Decodable {
var check_runs: [CheckRun]
}

let response1: Response? = request(url(with: "/repos/\(repo)/commits/\(ref)/check-runs"))
let response1: Response? = request(url(with: "/repos/\(repo)/commits/\(sha)/check-runs"))
guard let suite_id = response1?.check_runs.first?.check_suite.id else { return [] }
let response2: Response? = request(url(with: "/repos/\(repo)/check-suites/\(suite_id)/check-runs"))
return response2?.check_runs ?? []
Expand Down
8 changes: 4 additions & 4 deletions Tests/action-swiftlintTests/action_swiftlintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ final class action_swiftlintTests: XCTestCase {

XCTAssertEqual(output, """
Sources/Lib/GitHub.swift:45:16: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)
Sources/Lib/GitHub.swift:106:9: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)
Sources/Lib/GitHub.swift:118:9: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)
Sources/Lib/GitHub.swift:119:13: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)
Sources/Lib/GitHub.swift:119:13: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)
Sources/Lib/GitHub.swift:140:9: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)
Sources/Lib/GitHub.swift:152:9: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)
Sources/Lib/GitHub.swift:153:13: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)
Sources/Lib/GitHub.swift:153:13: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)
Sources/Lib/SwiftLint.swift:6:16: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)
Sources/Lib/execute().swift:4:68: warning: Large Tuple Violation: Tuples should have at most 2 members. (large_tuple)
\(warningMessage)
Expand Down

0 comments on commit bdc8535

Please sign in to comment.