diff --git a/LICENSE b/LICENSE index 6136b0f..5631af5 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,16 @@ -MIT License - -Copyright (c) 2020 WeTransfer - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +Copyright 2020 WeTransfer + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +* No Harm: The software may not be used by anyone for systems or activities that actively and knowingly endanger, harm, or otherwise threaten the physical, mental, economic, or general well-being of other individuals or groups, in violation of the United Nations Universal Declaration of Human Rights (https://www.un.org/en/universal-declaration-human-rights/). + +* Services: If the Software is used to provide a service to others, the licensee shall, as a condition of use, require those others not to use the service in any way that violates the No Harm clause above. + +* Enforceability: If any portion or provision of this License shall to any extent be declared illegal or unenforceable by a court of competent jurisdiction, then the remainder of this License, or the application of such portion or provision in circumstances other than those as to which it is so declared illegal or unenforceable, shall not be affected thereby, and each portion and provision of this Agreement shall be valid and enforceable to the fullest extent permitted by law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +This Hippocratic License is an Ethical Source license (https://ethicalsource.dev) derived from the MIT License, amended to limit the impact of the unethical use of open source software. + diff --git a/Sources/GitBuddyCore/Changelog/ChangelogProducer.swift b/Sources/GitBuddyCore/Changelog/ChangelogProducer.swift index 79c58ab..6e33eb2 100644 --- a/Sources/GitBuddyCore/Changelog/ChangelogProducer.swift +++ b/Sources/GitBuddyCore/Changelog/ChangelogProducer.swift @@ -11,16 +11,15 @@ import OctoKit import SPMUtility struct ChangelogCommand: Command { - let command = "changelog" - let description = "Create a changelog for GitHub repositories" + + static let command = "changelog" + static let description = "Create a changelog for GitHub repositories" let sinceTag: OptionArgument let baseBranch: OptionArgument - init(parser: ArgumentParser) { - let subparser = parser.add(subparser: command, overview: description) - sinceTag = subparser.add(option: "--sinceTag", shortName: "-s", kind: String.self, usage: "The tag to use as a base. Defaults to the latest tag.") - baseBranch = subparser.add(option: "--baseBranch", shortName: "-b", kind: String.self, usage: "The base branch to compare with. Defaults to master.") - _ = subparser.add(option: "--verbose", kind: Bool.self, usage: "Show extra logging for debugging purposes") + init(subparser: ArgumentParser) { + sinceTag = subparser.add(option: "--since-tag", shortName: "-s", kind: String.self, usage: "The tag to use as a base. Defaults to the latest tag.") + baseBranch = subparser.add(option: "--base-branch", shortName: "-b", kind: String.self, usage: "The base branch to compare with. Defaults to master.") } @discardableResult func run(using arguments: ArgumentParser.Result) throws -> String { diff --git a/Sources/GitBuddyCore/GitBuddy.swift b/Sources/GitBuddyCore/GitBuddy.swift index f9c8ff7..23d1700 100644 --- a/Sources/GitBuddyCore/GitBuddy.swift +++ b/Sources/GitBuddyCore/GitBuddy.swift @@ -3,6 +3,7 @@ // GitBuddyCore // // Created by Antoine van der Lee on 03/02/2020. +// Copyright © 2020 WeTransfer. All rights reserved. // import Foundation @@ -17,6 +18,8 @@ public enum GitBuddy { var debugDescription: String { "GitHub Access Token is missing. Add an environment variable: GITBUDDY_ACCESS_TOKEN='username:access_token'" } } + public static let version = "2.0.0" + @discardableResult public static func run(arguments: [String] = ProcessInfo.processInfo.arguments, environment: [String: String] = ProcessInfo.processInfo.environment, configuration: URLSessionConfiguration? = nil) throws -> String? { let configuration = try configuration ?? sessionConfiguration(using: environment) URLSessionInjector.urlSession = URLSession(configuration: configuration, delegate: nil, delegateQueue: nil) @@ -27,11 +30,27 @@ public enum GitBuddy { environment: environment) try commandRegistry.register(commandType: ChangelogCommand.self) try commandRegistry.register(commandType: ReleaseCommand.self) + + addVersionArgument(using: arguments, parser: commandRegistry.parser) + let output = commandRegistry.run() Log.message(output) return output } + private static func addVersionArgument(using arguments: [String], parser: ArgumentParser) { + guard !arguments.contains("--version") else { + print(""" + HL GitBuddy \(Self.version) + Copyright © 2020 WeTransfer. All rights reserved. + This Hippocratic License (HL) is an Ethical Source license (https://ethicalsource.dev) derived from the MIT License. + It's amended to limit the impact of the unethical use of open source software. + """) + exit(0) + } + _ = parser.add(option: "--version", kind: String.self, usage: "Prints the current GitBuddy version") + } + private static func sessionConfiguration(using environment: [String: String]) throws -> URLSessionConfiguration { guard let gitHubAccessToken = environment["GITBUDDY_ACCESS_TOKEN"] else { throw Error.missingAccessToken diff --git a/Sources/GitBuddyCore/GitHub/Commenter.swift b/Sources/GitBuddyCore/GitHub/Commenter.swift index 6435ab5..3920059 100644 --- a/Sources/GitBuddyCore/GitHub/Commenter.swift +++ b/Sources/GitBuddyCore/GitHub/Commenter.swift @@ -3,6 +3,7 @@ // GitBuddyCore // // Created by Antoine van der Lee on 06/02/2020. +// Copyright © 2020 WeTransfer. All rights reserved. // import Foundation @@ -25,7 +26,7 @@ enum Comment { } /// Responsible for posting comments on issues at GitHub. -struct Commenter { +enum Commenter { static var urlSession: URLSession { URLSessionInjector.urlSession } /// Posts a given comment on the issue from the given project. diff --git a/Sources/GitBuddyCore/Helpers/Command.swift b/Sources/GitBuddyCore/Helpers/Command.swift index beb13ec..f44e52a 100644 --- a/Sources/GitBuddyCore/Helpers/Command.swift +++ b/Sources/GitBuddyCore/Helpers/Command.swift @@ -3,6 +3,7 @@ // GitBuddyCore // // Created by Antoine van der Lee on 03/02/2020. +// Copyright © 2020 WeTransfer. All rights reserved. // import Foundation @@ -11,14 +12,14 @@ import SPMUtility /// Defines a command that can be executed. protocol Command { /// The name of the command. - var command: String { get } + static var command: String { get } /// The command description explaining what it does. - var description: String { get } + static var description: String { get } /// Creates a new instance of this `Command`. - /// - Parameter parser: The main parser that is being used for execution. - init(parser: ArgumentParser) throws + /// - Parameter subparser: The subparser that is being used for this execution. + init(subparser: ArgumentParser) throws /// Runs the command with the given arguments and environment. /// - Parameters: diff --git a/Sources/GitBuddyCore/Helpers/CommandRegistry.swift b/Sources/GitBuddyCore/Helpers/CommandRegistry.swift index 8b3fa50..68b0255 100644 --- a/Sources/GitBuddyCore/Helpers/CommandRegistry.swift +++ b/Sources/GitBuddyCore/Helpers/CommandRegistry.swift @@ -3,6 +3,7 @@ // GitBuddyCore // // Created by Antoine van der Lee on 03/02/2020. +// Copyright © 2020 WeTransfer. All rights reserved. // import Foundation @@ -12,7 +13,7 @@ import Basic /// Allows to register subcommands that can be run. struct CommandRegistry { - private let parser: ArgumentParser + let parser: ArgumentParser private var commands: [Command] = [] private let arguments: [String] private let environment: [String: String] @@ -24,7 +25,10 @@ struct CommandRegistry { } mutating func register(commandType: Command.Type) throws { - let command = try commandType.init(parser: parser) + let subparser = parser.add(subparser: commandType.command, overview: commandType.description) + let command = try commandType.init(subparser: subparser) + subparser.addHelpArgument() + subparser.addVerboseArgument() commands.append(command) } @@ -36,12 +40,13 @@ struct CommandRegistry { @discardableResult func run() -> String { do { let arguments = try processArguments() - + guard let subparser = arguments.subparser(parser), - let command = commands.first(where: { $0.command == subparser }) else { + let command = commands.first(where: { type(of: $0).command == subparser }) else { parser.printUsage(on: stdoutStream) return "" } + return try command.run(using: arguments) } catch let error as ArgumentParserError { return error.description @@ -51,3 +56,13 @@ struct CommandRegistry { } } + +private extension ArgumentParser { + func addHelpArgument() { + _ = add(option: "--help", kind: Bool.self, usage: "Display available options") + } + + func addVerboseArgument() { + _ = add(option: "--verbose", kind: Bool.self, usage: "Show extra logging for debugging purposes") + } +} diff --git a/Sources/GitBuddyCore/Helpers/DependencyInjectors.swift b/Sources/GitBuddyCore/Helpers/DependencyInjectors.swift index f0be22a..c065750 100644 --- a/Sources/GitBuddyCore/Helpers/DependencyInjectors.swift +++ b/Sources/GitBuddyCore/Helpers/DependencyInjectors.swift @@ -3,6 +3,7 @@ // GitBuddyCore // // Created by Antoine van der Lee on 03/02/2020. +// Copyright © 2020 WeTransfer. All rights reserved. // import Foundation diff --git a/Sources/GitBuddyCore/Models/Changelog.swift b/Sources/GitBuddyCore/Models/Changelog.swift index 32764da..bb0c9c8 100644 --- a/Sources/GitBuddyCore/Models/Changelog.swift +++ b/Sources/GitBuddyCore/Models/Changelog.swift @@ -3,6 +3,7 @@ // GitBuddyCore // // Created by Antoine van der Lee on 05/02/2020. +// Copyright © 2020 WeTransfer. All rights reserved. // import Foundation @@ -35,21 +36,21 @@ struct Changelog: CustomStringConvertible { } extension PullRequest: Hashable { + public func hash(into hasher: inout Hasher) { + hasher.combine(id) + } + public static func == (lhs: PullRequest, rhs: PullRequest) -> Bool { return lhs.id == rhs.id } +} +extension Issue: Hashable { public func hash(into hasher: inout Hasher) { hasher.combine(id) } -} -extension Issue: Hashable { public static func == (lhs: Issue, rhs: Issue) -> Bool { return lhs.id == rhs.id } - - public func hash(into hasher: inout Hasher) { - hasher.combine(id) - } } diff --git a/Sources/GitBuddyCore/Models/Release.swift b/Sources/GitBuddyCore/Models/Release.swift index 371273f..4a84262 100644 --- a/Sources/GitBuddyCore/Models/Release.swift +++ b/Sources/GitBuddyCore/Models/Release.swift @@ -3,6 +3,7 @@ // GitBuddyCore // // Created by Antoine van der Lee on 05/02/2020. +// Copyright © 2020 WeTransfer. All rights reserved. // import Foundation diff --git a/Sources/GitBuddyCore/Release/ReleaseProducer.swift b/Sources/GitBuddyCore/Release/ReleaseProducer.swift index 03f8949..2fa0099 100644 --- a/Sources/GitBuddyCore/Release/ReleaseProducer.swift +++ b/Sources/GitBuddyCore/Release/ReleaseProducer.swift @@ -3,6 +3,7 @@ // GitBuddy // // Created by Antoine van der Lee on 04/02/2020. +// Copyright © 2020 WeTransfer. All rights reserved. // import Foundation @@ -10,16 +11,14 @@ import OctoKit import SPMUtility struct ReleaseCommand: Command { - let command = "release" - let description = "Create a new release including a changelog and publish comments on related issues" + static let command = "release" + static let description = "Create a new release including a changelog and publish comments on related issues" let changelogPath: OptionArgument let skipComments: OptionArgument - init(parser: ArgumentParser) { - let subparser = parser.add(subparser: command, overview: description) - changelogPath = subparser.add(option: "--changelogPath", shortName: "-c", kind: String.self, usage: "The path to the Changelog to update it with the latest changes") - skipComments = subparser.add(option: "--skipComments", shortName: "-s", kind: Bool.self, usage: "Disable commenting on issues and PRs about the new release") - _ = subparser.add(option: "--verbose", kind: Bool.self, usage: "Show extra logging for debugging purposes") + init(subparser: ArgumentParser) { + changelogPath = subparser.add(option: "--changelog-path", shortName: "-c", kind: String.self, usage: "The path to the Changelog to update it with the latest changes") + skipComments = subparser.add(option: "--skip-comments", shortName: "-s", kind: Bool.self, usage: "Disable commenting on issues and PRs about the new release") } @discardableResult func run(using arguments: ArgumentParser.Result) throws -> String { diff --git a/Tests/GitBuddyTests/GitBuddyTests.swift b/Tests/GitBuddyTests/GitBuddyTests.swift index d87b3ee..224174f 100644 --- a/Tests/GitBuddyTests/GitBuddyTests.swift +++ b/Tests/GitBuddyTests/GitBuddyTests.swift @@ -3,6 +3,7 @@ // GitBuddyTests // // Created by Antoine van der Lee on 04/02/2020. +// Copyright © 2020 WeTransfer. All rights reserved. // import XCTest diff --git a/Tests/GitBuddyTests/Models/ChangelogTests.swift b/Tests/GitBuddyTests/Models/ChangelogTests.swift index dad6f76..7401786 100644 --- a/Tests/GitBuddyTests/Models/ChangelogTests.swift +++ b/Tests/GitBuddyTests/Models/ChangelogTests.swift @@ -3,6 +3,7 @@ // GitBuddyTests // // Created by Antoine van der Lee on 05/02/2020. +// Copyright © 2020 WeTransfer. All rights reserved. // import Foundation diff --git a/Tests/GitBuddyTests/Release/ReleaseProducerTests.swift b/Tests/GitBuddyTests/Release/ReleaseProducerTests.swift index a186a9f..6f8bbcc 100644 --- a/Tests/GitBuddyTests/Release/ReleaseProducerTests.swift +++ b/Tests/GitBuddyTests/Release/ReleaseProducerTests.swift @@ -3,6 +3,7 @@ // GitBuddyTests // // Created by Antoine van der Lee on 05/02/2020. +// Copyright © 2020 WeTransfer. All rights reserved. // import XCTest diff --git a/Tests/GitBuddyTests/TestHelpers/CommentJSON.swift b/Tests/GitBuddyTests/TestHelpers/CommentJSON.swift index 107e430..78f3548 100644 --- a/Tests/GitBuddyTests/TestHelpers/CommentJSON.swift +++ b/Tests/GitBuddyTests/TestHelpers/CommentJSON.swift @@ -3,6 +3,7 @@ // GitBuddyTests // // Created by Antoine van der Lee on 05/02/2020. +// Copyright © 2020 WeTransfer. All rights reserved. // import Foundation diff --git a/Tests/GitBuddyTests/TestHelpers/ReleaseJSON.swift b/Tests/GitBuddyTests/TestHelpers/ReleaseJSON.swift index 40d3f9d..4321c3a 100644 --- a/Tests/GitBuddyTests/TestHelpers/ReleaseJSON.swift +++ b/Tests/GitBuddyTests/TestHelpers/ReleaseJSON.swift @@ -3,6 +3,7 @@ // GitBuddyTests // // Created by Antoine van der Lee on 05/02/2020. +// Copyright © 2020 WeTransfer. All rights reserved. // import Foundation