|
| 1 | +import Foundation |
| 2 | +import Danger |
| 3 | + |
| 4 | +// MARK: Helper methods |
| 5 | + |
| 6 | +func validateTitleAllowsMerging(for pullRequest: GitHub.PullRequest) throws { |
| 7 | + let range = NSRange(pullRequest.title.startIndex..., in: pullRequest.title) |
| 8 | + |
| 9 | + let wipRegex = try NSRegularExpression(pattern: "\\bWIP\\b", options: .caseInsensitive) |
| 10 | + if wipRegex.firstMatch(in: pullRequest.title, range: range) != nil { |
| 11 | + print("Pull request title contains “WIP”.") |
| 12 | + } |
| 13 | + |
| 14 | + let doNotMergeRegex = try NSRegularExpression(pattern: "\\bdo not merge\\b", options: .caseInsensitive) |
| 15 | + if doNotMergeRegex.firstMatch(in: pullRequest.title, range: range) != nil { |
| 16 | + print("Pull request title contains “do not merge”.") |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +func validateMessage(for ghCommit: GitHub.Commit, doesNotHavePrefixes prefixes: [String]) { |
| 21 | + let commitMessage = ghCommit.commit.message.lowercased() |
| 22 | + for prefix in prefixes where commitMessage.hasPrefix(prefix.lowercased()) { |
| 23 | + fail("Commit message \(ghCommit.sha) begins with `\(prefix)`. Squash before merging.") |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func messageIsGenerated(for ghCommit: GitHub.Commit) -> Bool { |
| 28 | + guard let firstWord = ghCommit.commit.message.components(separatedBy: " ").first else { |
| 29 | + return false |
| 30 | + } |
| 31 | + return firstWord == "Merge" || firstWord == "Revert" || firstWord == "fixup!" || firstWord == "squash!" |
| 32 | +} |
| 33 | + |
| 34 | +/// Fails if the commit’s message does not satisfy Chris Beams’s |
| 35 | +/// [“The seven rules of a great Git commit message”](https://chris.beams.io/posts/git-commit/). |
| 36 | +func validateMessageIsGreat(for ghCommit: GitHub.Commit) { |
| 37 | + guard !messageIsGenerated(for: ghCommit) else { return } |
| 38 | + |
| 39 | + let messageComponents = ghCommit.commit.message.components(separatedBy: "\n") |
| 40 | + let title = messageComponents[0] |
| 41 | + |
| 42 | + // Rule 1 |
| 43 | + if messageComponents.count > 1 { |
| 44 | + let separator = messageComponents[1] |
| 45 | + if !separator.isEmpty { |
| 46 | + fail("Commit message \(ghCommit.sha) is missing a blank line between title and body.") |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // Rule 2 |
| 51 | + if title.count > 50 { |
| 52 | + fail("Commit title \(ghCommit.sha) is \(title.count) characters long (title should be limited to 50 characters).") |
| 53 | + } |
| 54 | + |
| 55 | + // Rule 3 |
| 56 | + if title.first != title.capitalized.first { |
| 57 | + fail("Commit title \(ghCommit.sha) is not capitalized.") |
| 58 | + } |
| 59 | + |
| 60 | + // Rule 4 |
| 61 | + if title.hasSuffix(".") { |
| 62 | + fail("Commit title \(ghCommit.sha) ends with a period.") |
| 63 | + } |
| 64 | + |
| 65 | + // Rule 5 |
| 66 | + // Use the imperative mood in the subject line |
| 67 | + // Not validated here |
| 68 | + |
| 69 | + // Rule 6 |
| 70 | + let body = messageComponents.dropFirst() |
| 71 | + let lineNumbers = body.indices.map({ $0 + 1 }) |
| 72 | + for (lineNumber, line) in zip(lineNumbers, body) where line.count > 72 { |
| 73 | + fail("Commit message \(ghCommit.sha) line \(lineNumber) is \(line.count) characters long (body should be wrapped at 72 characters).") |
| 74 | + } |
| 75 | + |
| 76 | + // Rule 7 |
| 77 | + // Use the body to explain what and why vs. how |
| 78 | + // Not validated here |
| 79 | +} |
| 80 | + |
| 81 | +// MARK: - Validations |
| 82 | + |
| 83 | +let danger = Danger() |
| 84 | +let pullRequest = danger.github.pullRequest |
| 85 | + |
| 86 | +try validateTitleAllowsMerging(for: pullRequest) |
| 87 | + |
| 88 | +let unmergeablePrefixes = ["fixup!", "squash!", "WIP"] |
| 89 | +danger.github.commits.forEach { |
| 90 | + validateMessage(for: $0, doesNotHavePrefixes: unmergeablePrefixes) |
| 91 | + validateMessageIsGreat(for: $0) |
| 92 | +} |
| 93 | + |
| 94 | +if let additions = pullRequest.additions, let deletions = pullRequest.deletions, additions < deletions { |
| 95 | + message("🎉 This PR removes more code than it adds! (\(additions - deletions) net lines)") |
| 96 | +} |
| 97 | + |
| 98 | +SwiftLint.lint(inline: true, directory: "Session") |
0 commit comments