|
| 1 | +import Foundation |
| 2 | +import SwiftCLI |
| 3 | + |
| 4 | +private let ticketPrefix = "Ticket: " |
| 5 | + |
| 6 | +struct AnnotatorError: Error { |
| 7 | + let message: String |
| 8 | +} |
| 9 | + |
| 10 | +extension AnnotatorError: CustomStringConvertible { |
| 11 | + var description: String { |
| 12 | + return self.message |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | + |
| 17 | +func messageHasTicket(_ message: String) -> Bool { |
| 18 | + return message.split(separator: "\n").first(where: { $0.hasPrefix(ticketPrefix) }) != nil |
| 19 | +} |
| 20 | + |
| 21 | +func readBranch() throws -> String { |
| 22 | + return try capture("git", arguments: ["rev-parse", "--abbrev-ref", "HEAD"]).stdout |
| 23 | +} |
| 24 | + |
| 25 | +func extractFirstMatch(of regexp: NSRegularExpression, in string: String) -> String? { |
| 26 | + let fullNSRange = NSRange(location: 0, length: string.utf16.count) |
| 27 | + guard |
| 28 | + let result = regexp.firstMatch(in: string, options: [], range: fullNSRange), |
| 29 | + result.numberOfRanges == 2, |
| 30 | + let range = Range(result.range(at: 1), in: string) |
| 31 | + else { |
| 32 | + return nil |
| 33 | + } |
| 34 | + return String(string[range]) |
| 35 | +} |
| 36 | + |
| 37 | +func makeTicketReader(branchReader: @escaping () throws -> String) -> (NSRegularExpression) throws -> String? { |
| 38 | + return { regexp in |
| 39 | + let branch = try branchReader() |
| 40 | + guard let ticket = extractFirstMatch(of: regexp, in: branch) else { |
| 41 | + throw AnnotatorError(message: "Couldn't find ticket in branch '\(branch)' with regexp '\(regexp.pattern)'") |
| 42 | + } |
| 43 | + return ticket |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func makeMessageUpdater( |
| 48 | + regexp: NSRegularExpression, |
| 49 | + ticketReader: @escaping (NSRegularExpression) throws -> String?) -> (String) throws -> String |
| 50 | +{ |
| 51 | + return { message in |
| 52 | + guard !messageHasTicket(message) else { return message } |
| 53 | + guard let ticket = try ticketReader(regexp) else { |
| 54 | + throw AnnotatorError(message: "Couldn't match ticket name in branch") |
| 55 | + } |
| 56 | + |
| 57 | + let messageWithTicket = message + (message.hasSuffix("\n\n") ? "" : "\n") + ticketPrefix + ticket + "\n" |
| 58 | + return messageWithTicket |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func updateFile(at url: URL, with updater: (String) throws -> String) throws { |
| 63 | + let messageData = try Data(contentsOf: url) |
| 64 | + guard let message = String(bytes: messageData, encoding: .utf8) else { |
| 65 | + throw AnnotatorError(message: "Couldn't parse commit message in \(url)") |
| 66 | + } |
| 67 | + let updatedMessage = try updater(message) |
| 68 | + try Data(updatedMessage.utf8).write(to: url, options: .atomic) |
| 69 | +} |
| 70 | + |
| 71 | +func parseRegexp(raw: String) throws -> NSRegularExpression { |
| 72 | + let regexp = try NSRegularExpression(pattern: raw, options: []) |
| 73 | + guard regexp.numberOfCaptureGroups == 1 else { |
| 74 | + throw AnnotatorError(message: "Regexp must have one capture group for matching the ticket name") |
| 75 | + } |
| 76 | + return regexp |
| 77 | +} |
| 78 | + |
| 79 | +class AddTicket: Command { |
| 80 | + let name = "add-ticket" |
| 81 | + let rawRegexp = Parameter() |
| 82 | + let file = Parameter() |
| 83 | + |
| 84 | + func execute() throws { |
| 85 | + try updateFile( |
| 86 | + at: URL(fileURLWithPath: file.value, isDirectory: false), |
| 87 | + with: makeMessageUpdater( |
| 88 | + regexp: try parseRegexp(raw: rawRegexp.value), |
| 89 | + ticketReader: makeTicketReader(branchReader: readBranch))) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +public func runAnnotator() { |
| 94 | + let argv = ProcessInfo.processInfo.arguments |
| 95 | + let annotator = CLI(name: argv[0]) |
| 96 | + annotator.commands = [AddTicket()] |
| 97 | + exit(annotator.go()) |
| 98 | +} |
0 commit comments