Last active
February 10, 2021 07:48
-
-
Save RamonGilabert/79b7d150bff0974fbb3f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env xcrun swift | |
import Foundation | |
let firstStaticRegex = "\\[.*?\\)\\)" | |
let secondStaticRegex = "(?<=\\*\\*Merged pull requests:\\*\\*\\s\\s)(.|\\n)*?(?=\\s##)" | |
let token = "cf14a306ab918d348fdab2b1824bfd79b51b3172" | |
let publishRelease = "https://api.github.com/repos/" | |
let editRelease = "https://api.github.com/repos/" | |
let error = "There was an error, type CTRL+C to exit. 👽" | |
let success = "Release notes updated! 🤗" | |
var headers: [String : String] = [ | |
"Content-Type" : "application/json", | |
"Authorization" : "token \(token)", | |
] | |
var data = [String : String]() | |
struct System { | |
static func execute(command: String, _ arguments: String = "") -> String? { | |
guard let command = which(command) else { return nil } | |
return task(command, arguments) | |
} | |
private static func which(command: String, _ arguments: String? = nil) -> String? { | |
let task = NSTask() | |
task.launchPath = "/usr/bin/which" | |
task.arguments = [command] | |
let pipe = NSPipe() | |
task.standardOutput = pipe | |
task.launch() | |
let data = pipe.fileHandleForReading.readDataToEndOfFile() | |
let output = NSString(data: data, encoding: NSUTF8StringEncoding) as String? | |
return output?.componentsSeparatedByString("\n").first | |
} | |
private static func task(command: String, _ arguments: String? = nil) -> String? { | |
let task = NSTask() | |
task.launchPath = command | |
if let arguments = arguments where !arguments.isEmpty { | |
task.arguments = arguments.componentsSeparatedByString(" ") | |
} | |
let stdOut = NSPipe() | |
task.standardOutput = stdOut | |
let stdErr = NSPipe() | |
task.standardError = stdErr | |
let handler = { (file: NSFileHandle!) -> Void in | |
let data = file.availableData | |
guard let output = NSString(data: data, encoding: NSUTF8StringEncoding) | |
else { return} | |
print(output.componentsSeparatedByString("\n").first!) | |
} | |
stdErr.fileHandleForReading.readabilityHandler = handler | |
stdOut.fileHandleForReading.readabilityHandler = handler | |
task.terminationHandler = { (task: NSTask?) -> () in | |
stdErr.fileHandleForReading.readabilityHandler = nil | |
stdOut.fileHandleForReading.readabilityHandler = nil | |
} | |
task.launch() | |
task.waitUntilExit() | |
return "" | |
} | |
} | |
func changelogLocation() -> String { | |
let directory = NSFileManager.defaultManager().currentDirectoryPath | |
return directory + "/CHANGELOG.md" | |
} | |
func manageContent() -> String { | |
var changelog = "" | |
do { | |
let content = try NSMutableString(contentsOfFile: changelogLocation(), encoding: NSUTF8StringEncoding) | |
let firstRegex = try NSRegularExpression(pattern: firstStaticRegex, options: []) | |
let secondRegex = try NSRegularExpression(pattern: secondStaticRegex, options: []) | |
firstRegex.replaceMatchesInString(content, options: [], range: NSMakeRange(0, content.length), withTemplate: "") | |
if let key = secondRegex.firstMatchInString(content as String, | |
options: [], range: NSMakeRange(0, content.length)) { | |
changelog = content.substringWithRange(key.rangeAtIndex(0)) | |
} | |
} catch { | |
print(error) | |
} | |
return changelog | |
} | |
func handleReleaseNotes() { | |
do { | |
NSFileManager().changeCurrentDirectoryPath("cd ~/Documents/mystore-ios") | |
System.execute("git", "checkout master") | |
System.execute("git", "pull --all") | |
guard let publishURL = NSURL(string: publishRelease) else { return } | |
data["tag_name"] = Process.arguments[1] | |
data["name"] = Process.arguments[1] | |
data["body"] = Process.arguments[1] | |
let session = NSURLSession.sharedSession() | |
let request = NSMutableURLRequest(URL: publishURL) | |
request.HTTPMethod = "POST" | |
request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(data, options: NSJSONWritingOptions.PrettyPrinted) | |
for (key, value) in headers { | |
request.addValue(value, forHTTPHeaderField: key) | |
} | |
var hasError = true | |
var alive = true | |
session.dataTaskWithRequest(request, completionHandler: { dataRequest, response, error in | |
if let responseHTTP = response as? NSHTTPURLResponse where !(responseHTTP.statusCode == 200 || responseHTTP.statusCode == 201) { alive = false } | |
guard let dataRequest = dataRequest else { return } | |
do { | |
let releaseOutput = try NSJSONSerialization.JSONObjectWithData(dataRequest, options: NSJSONReadingOptions.AllowFragments) | |
System.execute("github_changelog_generator", "-t $token --max-issues 100") | |
guard let id = releaseOutput["id"] as? Int, editURL = NSURL(string: "\(editRelease)/\(id)") else { return } | |
data["body"] = manageContent() | |
let request = NSMutableURLRequest(URL: editURL) | |
request.HTTPMethod = "PATCH" | |
request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(data, options: NSJSONWritingOptions.PrettyPrinted) | |
for (key, value) in headers { | |
request.addValue(value, forHTTPHeaderField: key) | |
} | |
session.dataTaskWithRequest(request, completionHandler: { data, response, error in | |
if let responseHTTP = response as? NSHTTPURLResponse where !(responseHTTP.statusCode == 200 || responseHTTP.statusCode == 201) { alive = false } | |
System.execute("git", "add .") | |
System.execute("git", "commit -m \"Release \(Process.arguments[1])\"") | |
System.execute("git", "push origin master") | |
hasError = false | |
alive = false | |
}).resume() | |
} catch { | |
print(error) | |
} | |
}).resume() | |
while alive && NSRunLoop.currentRunLoop().runMode(NSDefaultRunLoopMode, beforeDate: NSDate(timeIntervalSinceNow: 0.1)) { } | |
hasError ? print(error) : print(success) | |
} catch { | |
print(error) | |
} | |
} | |
handleReleaseNotes() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment