-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommentRequest.swift
More file actions
82 lines (61 loc) Β· 2.36 KB
/
CommentRequest.swift
File metadata and controls
82 lines (61 loc) Β· 2.36 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
import Foundation
import SwiftyRequest
import Stencil
import PathKit
import HTMLEntities
import SwiftCLIToolbox
struct CommentContext {
let content: String
let authorName: String
var gitRepoName: String?
var gitBranchName: String?
}
class CommentRequest: NSObject {
lazy var system: SystemProtocol = System()
var config: Config
var runner = SwiftScriptRunner()
static func defaultRequest() -> CommentRequest {
let config = Config.readFromPath(Path.redmineConfig())
return CommentRequest(config)
}
init(_ config: Config) {
self.config = config
}
public func send(to issueNumber: Int, wtih context: CommentContext) {
runner.lock()
let htmlContext = context.content.htmlEscape().replacingOccurrences(of: "\n", with: "<br>")
var templateContext = [
"content": htmlContext,
"authorName": context.authorName
]
if let branchName = context.gitBranchName {
templateContext["branchName"] = branchName
}
if let repoName = context.gitRepoName {
templateContext["repoName"] = repoName
}
let template = Template(templateString: DefaultTemplate.comment())
let rendered = try? template.render(templateContext)
guard let commentInfo = rendered else {
system.printFatalError("Rendered error. π")
}
let url = config.redmineUrl + "/issues/\(issueNumber).json"
let parameters = ["issue": ["notes": commentInfo]]
guard let data = try? JSONSerialization.data(withJSONObject: parameters, options: []) else {
return
}
let request = RestRequest(method: .put, url: url)
request.messageBody = data
request.responseString(queryItems: [URLQueryItem(name: "key", value: config.apiAccessKey)]) { response in
switch response.result {
case .success:
self.system.printSuccess("Comment send success!")
case .failure(let error):
self.system.printWarning("Got some network error. π")
self.system.printWarning("Error: \n\(error)")
}
self.runner.unlock()
}
runner.wait()
}
}