Skip to content

Commit

Permalink
Add version command and change to doner-case (#19)
Browse files Browse the repository at this point in the history
* Renaming entries to GitBuddy

* Command registry is set up

* Setup the new structure for having multiple commands

* Fix swiftlint warning

* Fix SwiftLint warnings

* Changing the authentication method to Basic HTTP

* Improve folder structure

* Start implementing the release command

* Output is now printed by GitBuddy

* Printing errors is now more clean

* Cleaned up the code, added folders in the tests, added strongly typed for shell commands to make it easier to mock

* Handle verbose globally

* Cleaned up the code a bit more inside the release producer

* Add tests for the release producer

* Adding an option to change the local changelog file

* Added tests

* Related issues and pull requests are now commented

* Changelog is now tested

* Add tests for commenting

* Improve the structure for posting comments

* Fix Octokit reference

* Update the readme to reflect the current changes

* Add verbose and help options for --help command. Convert to doner-case

* Adds a version command and adds all license headers

* Update the License

* Fix SwiftLint warnings
  • Loading branch information
AvdLee authored Feb 7, 2020
1 parent 83f6c3b commit 63346b3
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 50 deletions.
37 changes: 16 additions & 21 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.

13 changes: 6 additions & 7 deletions Sources/GitBuddyCore/Changelog/ChangelogProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>
let baseBranch: OptionArgument<String>

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 {
Expand Down
19 changes: 19 additions & 0 deletions Sources/GitBuddyCore/GitBuddy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// GitBuddyCore
//
// Created by Antoine van der Lee on 03/02/2020.
// Copyright © 2020 WeTransfer. All rights reserved.
//

import Foundation
Expand All @@ -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)
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Sources/GitBuddyCore/GitHub/Commenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// GitBuddyCore
//
// Created by Antoine van der Lee on 06/02/2020.
// Copyright © 2020 WeTransfer. All rights reserved.
//

import Foundation
Expand All @@ -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.
Expand Down
9 changes: 5 additions & 4 deletions Sources/GitBuddyCore/Helpers/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// GitBuddyCore
//
// Created by Antoine van der Lee on 03/02/2020.
// Copyright © 2020 WeTransfer. All rights reserved.
//

import Foundation
Expand All @@ -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:
Expand Down
23 changes: 19 additions & 4 deletions Sources/GitBuddyCore/Helpers/CommandRegistry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// GitBuddyCore
//
// Created by Antoine van der Lee on 03/02/2020.
// Copyright © 2020 WeTransfer. All rights reserved.
//

import Foundation
Expand All @@ -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]
Expand All @@ -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)
}

Expand All @@ -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
Expand All @@ -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")
}
}
1 change: 1 addition & 0 deletions Sources/GitBuddyCore/Helpers/DependencyInjectors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// GitBuddyCore
//
// Created by Antoine van der Lee on 03/02/2020.
// Copyright © 2020 WeTransfer. All rights reserved.
//

import Foundation
Expand Down
13 changes: 7 additions & 6 deletions Sources/GitBuddyCore/Models/Changelog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// GitBuddyCore
//
// Created by Antoine van der Lee on 05/02/2020.
// Copyright © 2020 WeTransfer. All rights reserved.
//

import Foundation
Expand Down Expand Up @@ -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)
}
}
1 change: 1 addition & 0 deletions Sources/GitBuddyCore/Models/Release.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// GitBuddyCore
//
// Created by Antoine van der Lee on 05/02/2020.
// Copyright © 2020 WeTransfer. All rights reserved.
//

import Foundation
Expand Down
13 changes: 6 additions & 7 deletions Sources/GitBuddyCore/Release/ReleaseProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
// GitBuddy
//
// Created by Antoine van der Lee on 04/02/2020.
// Copyright © 2020 WeTransfer. All rights reserved.
//

import Foundation
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<String>
let skipComments: OptionArgument<Bool>

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 {
Expand Down
1 change: 1 addition & 0 deletions Tests/GitBuddyTests/GitBuddyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// GitBuddyTests
//
// Created by Antoine van der Lee on 04/02/2020.
// Copyright © 2020 WeTransfer. All rights reserved.
//

import XCTest
Expand Down
1 change: 1 addition & 0 deletions Tests/GitBuddyTests/Models/ChangelogTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// GitBuddyTests
//
// Created by Antoine van der Lee on 05/02/2020.
// Copyright © 2020 WeTransfer. All rights reserved.
//

import Foundation
Expand Down
1 change: 1 addition & 0 deletions Tests/GitBuddyTests/Release/ReleaseProducerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// GitBuddyTests
//
// Created by Antoine van der Lee on 05/02/2020.
// Copyright © 2020 WeTransfer. All rights reserved.
//

import XCTest
Expand Down
1 change: 1 addition & 0 deletions Tests/GitBuddyTests/TestHelpers/CommentJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// GitBuddyTests
//
// Created by Antoine van der Lee on 05/02/2020.
// Copyright © 2020 WeTransfer. All rights reserved.
//

import Foundation
Expand Down
1 change: 1 addition & 0 deletions Tests/GitBuddyTests/TestHelpers/ReleaseJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// GitBuddyTests
//
// Created by Antoine van der Lee on 05/02/2020.
// Copyright © 2020 WeTransfer. All rights reserved.
//

import Foundation
Expand Down

0 comments on commit 63346b3

Please sign in to comment.