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