-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPostCommitHookCommand.swift
More file actions
56 lines (41 loc) · 1.59 KB
/
PostCommitHookCommand.swift
File metadata and controls
56 lines (41 loc) · 1.59 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
import Foundation
import Commander
import PathKit
import SwiftCLIToolbox
public class PostCommitHookCommand: CommandProtocol {
public static func make() -> CommandType {
let postCommitHookCommand = command {
let action = PostCommitHookCommandAction()
action.doAction()
}
return postCommitHookCommand
}
}
class PostCommitHookCommandAction {
lazy var system: SystemProtocol = System()
lazy var git: GitProtocol.Type = Git.self
lazy var request = CommentRequest.defaultRequest()
func doAction() {
guard let commitLog = git.commitMessage("") else {
return
}
let authorName = git.authorName("") ?? "No author name"
let repoName = git.repoName()
let branchName = git.branchName()
let issuesNumber = processIssuesNumber()
issuesNumber?.forEach({ issueNumber in
let context = CommentContext(content: commitLog,
authorName: authorName,
gitRepoName: repoName,
gitBranchName: branchName)
request.send(to: issueNumber, wtih: context)
})
}
private func processIssuesNumber() -> [Int]? {
guard let issuesNumber = git.commitTitle("")?.findIssuesNumber() else {
return nil
}
// Remove issue string's `#`, i.g. #12345 -> 12345
return issuesNumber.map { Int($0.dropFirst())! }
}
}