Skip to content

Commit 77ec8be

Browse files
authored
Add FXIOS [Danger] Adding fun rules with Danger (#29452)
* Fun stuff with Danger πŸ˜‹ * New rules * Swiftlint * Clean up
1 parent ed19e0e commit 77ec8be

File tree

1 file changed

+97
-15
lines changed

1 file changed

+97
-15
lines changed

β€ŽDangerfile.swiftβ€Ž

Lines changed: 97 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,71 @@ import Foundation
1010
let danger = Danger()
1111
let standardImageIdentifiersPath = "./BrowserKit/Sources/Common/Constants/StandardImageIdentifiers.swift"
1212

13+
checkForFunMetrics()
1314
checkAlphabeticalOrder(inFile: standardImageIdentifiersPath)
1415
checkBigPullRequest()
1516
checkCodeCoverage()
1617
failOnNewFilesWithoutCoverage()
17-
checkForPRDescription()
1818
checkForWebEngineFileChange()
1919
checkForCodeUsage()
20-
changedFiles()
2120
checkStringsFile()
2221

23-
func changedFiles() {
24-
message("Edited \(danger.git.modifiedFiles.count) files")
25-
message("Created \(danger.git.createdFiles.count) files")
22+
// Add some fun comments in Danger to have positive feedback on PRs
23+
func checkForFunMetrics() {
24+
let edited = danger.git.modifiedFiles + danger.git.createdFiles
25+
let testFiles = edited.filter { path in
26+
path.localizedCaseInsensitiveContains("Tests.swift")
27+
}
28+
29+
if !testFiles.isEmpty {
30+
markdown("""
31+
### πŸ’ͺ **Quality guardian**
32+
**\(testFiles.count)** tests files modified. You're a champion of test coverage! πŸš€
33+
""")
34+
}
35+
36+
let additions = danger.github?.pullRequest.additions ?? 0
37+
let deletions = danger.github?.pullRequest.deletions ?? 0
38+
if deletions > additions && (additions + deletions) > 50 {
39+
markdown("""
40+
### πŸ—‘οΈ **Tossing out clutter**
41+
**\(deletions - additions)** line(s) removed. Fewer lines, fewer bugs πŸ›!
42+
""")
43+
}
44+
45+
let filesChanged = danger.github?.pullRequest.changedFiles ?? 0
46+
if filesChanged > 0 && filesChanged <= 5 {
47+
markdown("""
48+
### 🧹 **Tidy commit**
49+
Just **\(filesChanged)** file(s) touched. Thanks for keeping it clean and review-friendly!
50+
""")
51+
}
52+
53+
let totalLines = deletions + additions
54+
if totalLines < 50 {
55+
markdown("""
56+
### 🌱 **Tiny but mighty**
57+
Only **\(totalLines)** line(s) changed. Fast to review, faster to land! πŸš€
58+
""")
59+
}
60+
61+
let weekday = Calendar(identifier: .gregorian).component(.weekday, from: Date()) // 6 = Friday
62+
if weekday == 6 {
63+
markdown("""
64+
### πŸ™Œ **Friday high-five**
65+
Thanks for pushing us across the finish line this week! πŸ™Œ
66+
""")
67+
}
68+
69+
let docTouched = edited.contains { $0.contains(".md") }
70+
if docTouched {
71+
markdown("""
72+
### 🌟 **Documentation star**
73+
Great documentation touches. Future you says thank you! πŸ“š
74+
""")
75+
}
76+
77+
checkDescriptionSection()
2678
}
2779

2880
func checkCodeCoverage() {
@@ -97,15 +149,6 @@ func checkBigPullRequest() {
97149
}
98150
}
99151

100-
// Encourage writing up some reasoning about the PR, rather than just leaving a title.
101-
func checkForPRDescription() {
102-
let body = danger.github.pullRequest.body?.count ?? 0
103-
let linesOfCode = danger.github.pullRequest.additions ?? 0
104-
if body < 3 && linesOfCode > 10 {
105-
warn("Please provide a summary of your changes in the Pull Request description. This helps reviewers to understand your code and technical decisions. Please also include the JIRA issue number and the GitHub ticket number (if available).")
106-
}
107-
}
108-
109152
// Detect and warn about some changes related to WebView management to ensure we port changes to the WebEngine project
110153
func checkForWebEngineFileChange() {
111154
let webEngineFiles = ["Tab.swift", "BrowserViewController+WebViewDelegates.swift"]
@@ -290,6 +333,45 @@ func checkStringsFile() {
290333
let touchedStrings = edited.contains(where: { $0 == "firefox-ios/Shared/Strings.swift" })
291334

292335
if touchedStrings {
293-
danger.message("✍️ Please ask a member of [@mozilla-mobile/firefox-ios-l10n](https://github.com/orgs/mozilla-mobile/teams/firefox-ios-l10n) team for Strings review ✍️")
336+
markdown("""
337+
### ✍️ **Strings file changed**
338+
"Please ask a member of [@mozilla-mobile/firefox-ios-l10n](https://github.com/orgs/mozilla-mobile/teams/firefox-ios-l10n) team for Strings review ✍️"
339+
""")
340+
}
341+
}
342+
343+
func checkDescriptionSection() {
344+
guard let body = danger.github.pullRequest.body else { return }
345+
346+
// Regex to capture everything between "## :bulb: Description" and "## :pencil: Checklist"
347+
guard let regex = try? NSRegularExpression(
348+
pattern: #"(?s)## :bulb: Description\s*(.*?)## :pencil: Checklist"#,
349+
options: []
350+
) else { return }
351+
352+
if let match = regex.firstMatch(in: body, options: [], range: NSRange(location: 0, length: body.utf16.count)),
353+
let range = Range(match.range(at: 1), in: body) {
354+
// extract description content
355+
var desc = String(body[range])
356+
// strip out HTML comments so `<!--- ... -->` placeholders don't count
357+
desc = desc.replacingOccurrences(of: #"<!--.*?-->"#, with: "", options: .regularExpression)
358+
359+
let count = desc.trimmingCharacters(in: .whitespacesAndNewlines).count
360+
if count == 0 {
361+
warn("""
362+
πŸ’‘ **More details help!**
363+
Your description section is empty. Adding a bit more context will make reviews smoother. πŸ™Œ
364+
""")
365+
} else if count < 10 {
366+
warn("""
367+
πŸ’‘ **More details help!**
368+
Your description section is a bit short (\(count) characters). Adding a bit more context will make reviews smoother. πŸ™Œ
369+
""")
370+
} else if count >= 350 {
371+
markdown("""
372+
### πŸ’¬ **Description craftsman**
373+
Great PR description! Reviewers salute you 🫑
374+
""")
375+
}
294376
}
295377
}

0 commit comments

Comments
Β (0)