Skip to content

Commit 29b2301

Browse files
authored
Merge pull request #6 from norio-nomura/update-to-latest
Update to latest
2 parents 64846bf + fe83461 commit 29b2301

File tree

7 files changed

+85
-43
lines changed

7 files changed

+85
-43
lines changed

.github/workflows/push.yml .github/workflows/ci.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
on: push
2-
name: New workflow
1+
name: CI
2+
on: [push]
33
jobs:
44
dockerLint:
55
name: Docker Lint
@@ -11,7 +11,7 @@ jobs:
1111
with:
1212
args: Dockerfile
1313
- name: swift test
14-
uses: docker://norionomura/swiftlint:swift-4.2
14+
uses: docker://norionomura/swiftlint:swift-5
1515
env:
1616
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1717
with:

Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
FROM norionomura/swiftlint:swift-4.2
2-
LABEL version="0.1.0"
1+
FROM norionomura/swiftlint:swift-5
2+
LABEL version="2.0.0"
33
LABEL repository="https://github.com/norio-nomura/action-swiftlint"
44
LABEL homepage="https://github.com/norio-nomura/action-swiftlint"
55
LABEL maintainer="Norio Nomura <[email protected]>"

README.md

+16-10
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,22 @@ This Action executes [SwiftLint](https://github.com/realm/SwiftLint) and generat
66

77
An example workflow to executing SwiftLint follows:
88

9-
```hcl
10-
workflow "Execute SwiftLint" {
11-
on = "push"
12-
resolves = ["swiftlint"]
13-
}
14-
15-
action "swiftlint" {
16-
uses = "norio-nomura/action-swiftlint@master"
17-
secrets = ["GITHUB_TOKEN"]
18-
}
9+
```yaml
10+
name: CI
11+
12+
on: [push]
13+
14+
jobs:
15+
build:
16+
17+
runs-on: ubuntu-latest
18+
19+
steps:
20+
- uses: actions/checkout@v1
21+
- name: GitHub Action for SwiftLint
22+
uses: norio-nomura/[email protected]
23+
env:
24+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1925
```
2026
2127
## Secrets

Sources/Lib/GitHub.swift

+24-14
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@ extension GitHub.Repository {
2929
public var url: URL
3030
public var output: Output?
3131
public var app: App
32+
public var check_suite: CheckSuite
3233
}
3334
}
3435

3536
extension GitHub.Repository.CheckRun {
3637
public struct Output: Decodable {
37-
public var title: String
38-
public var summary: String
38+
public var title: String?
39+
public var summary: String?
3940
public var text: String?
4041
public var annotations: [Annotation]?
4142
}
@@ -79,22 +80,25 @@ extension GitHub.Repository.CheckRun {
7980
public var id: Int
8081
public var name: String
8182
}
83+
84+
public struct CheckSuite: Decodable {
85+
public var id: Int
86+
}
8287
}
8388

8489
extension GitHub.Repository {
8590
public func currentCheckRun() -> CheckRun? {
8691
guard let sha = environment("GITHUB_SHA") else { return nil }
87-
guard let name = environment("GITHUB_ACTION") else { return nil }
88-
guard let checkRun = findCheckRun(for: sha, with: name) else {
89-
print("Current Action `\(name)` not found!")
92+
guard let checkRun = findCheckRun(for: sha) else {
93+
print("Current Action not found!")
9094
return nil
9195
}
9296
return checkRun
9397
}
9498

95-
public func findCheckRun(for ref: String, with name: String) -> CheckRun? {
99+
public func findCheckRun(for ref: String) -> CheckRun? {
96100
return checkRuns(for: ref).first { checkRun -> Bool in
97-
checkRun.name == name && checkRun.app.name == "GitHub Actions"
101+
checkRun.app.name == "GitHub Actions"
98102
}
99103
}
100104

@@ -103,8 +107,10 @@ extension GitHub.Repository {
103107
var check_runs: [CheckRun]
104108
}
105109

106-
let response: Response? = request(url(with: "/repos/\(repo)/commits/\(ref)/check-runs"))
107-
return response?.check_runs ?? []
110+
let response1: Response? = request(url(with: "/repos/\(repo)/commits/\(ref)/check-runs"))
111+
guard let suite_id = response1?.check_runs.first?.check_suite.id else { return [] }
112+
let response2: Response? = request(url(with: "/repos/\(repo)/check-suites/\(suite_id)/check-runs"))
113+
return response2?.check_runs ?? []
108114
}
109115

110116
@discardableResult
@@ -118,16 +124,17 @@ extension GitHub.Repository {
118124
var output: Output
119125
}
120126

121-
guard let output = checkRun.output else {
122-
print("\(checkRun) has no output.")
123-
return false
124-
}
127+
let title = "SwiftLint Violations"
128+
let warnings = annotations.filter { $0.annotation_level == .warning }.count
129+
let failure = annotations.filter { $0.annotation_level == .failure }.count
130+
let summary = "warnings: \(warnings), failures: \(failure)"
131+
125132
let batchSize = 50
126133
var rest = ArraySlice(annotations)
127134
while !rest.isEmpty {
128135
let annotations = Array(rest.prefix(batchSize))
129136
rest = rest.dropFirst(batchSize)
130-
let output = Request.Output(title: output.title, summary: output.summary, annotations: annotations)
137+
let output = Request.Output(title: title, summary: summary, annotations: annotations)
131138
let response: CheckRun? = request(checkRun.url, method: "PATCH", with: Request(output: output))
132139
if response == nil {
133140
return false
@@ -174,6 +181,9 @@ extension GitHub.Repository {
174181
}
175182
guard (200...299).contains(httpURLResponse.statusCode) else {
176183
print("server error status: \(httpURLResponse.statusCode)")
184+
if let data = data, let output = String(data: data, encoding: .utf8) {
185+
print(output)
186+
}
177187
return
178188
}
179189
guard let data = data else {

Tests/action-swiftlintTests/TestHelper.swift

+29-9
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,36 @@ import Foundation
33
let projectRootURL = URL(fileURLWithPath: #file).deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()
44
let projectRootPath = projectRootURL.appendingPathComponent("/").path
55

6-
extension String {
6+
private struct StaticStringImitator {
7+
let string: String
8+
79
func withStaticString(_ closure: (StaticString) -> Void) {
8-
withCString {
9-
let rawPointer = $0._rawValue
10-
let byteSize = lengthOfBytes(using: .utf8)._builtinWordValue
11-
let isASCII = true._getBuiltinLogicValue()
12-
let staticString = StaticString(_builtinStringLiteral: rawPointer,
13-
utf8CodeUnitCount: byteSize,
14-
isASCII: isASCII)
15-
closure(staticString)
10+
let isASCII = string.utf8.allSatisfy { $0 < 0x7f }
11+
string.utf8CString.dropLast().withUnsafeBytes {
12+
let imitator = Imitator(startPtr: $0.baseAddress!, utf8CodeUnitCount: $0.count, isASCII: isASCII)
13+
closure(imitator.staticString)
1614
}
1715
}
16+
17+
struct Imitator {
18+
let startPtr: UnsafeRawPointer
19+
let utf8CodeUnitCount: Int
20+
let flags: Int8
21+
22+
init(startPtr: UnsafeRawPointer, utf8CodeUnitCount: Int, isASCII: Bool) {
23+
self.startPtr = startPtr
24+
self.utf8CodeUnitCount = utf8CodeUnitCount
25+
flags = isASCII ? 0x2 : 0x0
26+
}
27+
28+
var staticString: StaticString {
29+
return unsafeBitCast(self, to: StaticString.self)
30+
}
31+
}
32+
}
33+
34+
extension String {
35+
func withStaticString(_ closure: (StaticString) -> Void) {
36+
StaticStringImitator(string: self).withStaticString(closure)
37+
}
1838
}

Tests/action-swiftlintTests/action_swiftlintTests.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ final class action_swiftlintTests: XCTestCase {
1212
"Can not find `GITHUB_TOKEN` environment variable.\n" : ""
1313

1414
XCTAssertEqual(output, """
15-
Sources/Lib/GitHub.swift:44:16: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)
16-
Sources/Lib/GitHub.swift:102:9: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)
17-
Sources/Lib/GitHub.swift:112:9: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)
18-
Sources/Lib/GitHub.swift:113:13: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)
19-
Sources/Lib/GitHub.swift:113:13: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)
15+
Sources/Lib/GitHub.swift:45:16: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)
16+
Sources/Lib/GitHub.swift:106:9: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)
17+
Sources/Lib/GitHub.swift:118:9: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)
18+
Sources/Lib/GitHub.swift:119:13: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)
19+
Sources/Lib/GitHub.swift:119:13: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)
2020
Sources/Lib/SwiftLint.swift:6:16: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)
2121
Sources/Lib/execute().swift:4:68: warning: Large Tuple Violation: Tuples should have at most 2 members. (large_tuple)
2222
\(warningMessage)

action.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: 'GitHub Action for SwiftLint'
2+
description: 'A tool to enforce Swift style and conventions'
3+
author: 'Norio Nomura <[email protected]>'
4+
runs:
5+
using: 'docker'
6+
image: 'Dockerfile'

0 commit comments

Comments
 (0)