Skip to content

Commit

Permalink
Merge pull request #82 from WeTransfer/feature/filter-emojis
Browse files Browse the repository at this point in the history
Filter emojis out of changelog
  • Loading branch information
AvdLee authored May 5, 2022
2 parents a4c547c + 0c1e6df commit e694195
Show file tree
Hide file tree
Showing 40 changed files with 261 additions and 153 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let package = Package(
name: "GitBuddy",
platforms: [
.macOS(.v10_15)
],
],
products: [
.executable(name: "GitBuddy", targets: ["GitBuddy"])
],
Expand Down
2 changes: 1 addition & 1 deletion Sources/GitBuddyCore/Changelog/ChangelogItemsFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct ChangelogItemsFactory {
return [ChangelogItem(input: pullRequest, closedBy: pullRequest)]
}
return resolvedIssues.map { issue -> ChangelogItem in
return ChangelogItem(input: issue, closedBy: pullRequest)
ChangelogItem(input: issue, closedBy: pullRequest)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/GitBuddyCore/Changelog/ChangelogProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import OctoKit

/// Capable of producing a changelog based on input parameters.
final class ChangelogProducer: URLSessionInjectable {

enum Since {
case date(date: Date)
case tag(tag: String)
case latestTag

/// Gets the date for the current Since property.
/// In the case of a tag, we add 60 seconds to make sure that the Changelog does not include the commit that is used for creating the tag.
/// In the case of a tag, we add 60 seconds to make sure that the Changelog does not include the commit that
/// is used for creating the tag.
/// This is needed as the tag creation date equals the commit creation date.
func get() throws -> Date {
switch self {
Expand All @@ -32,7 +32,7 @@ final class ChangelogProducer: URLSessionInjectable {
}
}

private lazy var octoKit: Octokit = Octokit()
private lazy var octoKit: Octokit = .init()
let baseBranch: Branch
let since: Since
let from: Date
Expand Down
3 changes: 1 addition & 2 deletions Sources/GitBuddyCore/Commands/ChangelogCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
// Created by Antoine van der Lee on 09/04/2020.
//

import Foundation
import ArgumentParser
import Foundation

struct ChangelogCommand: ParsableCommand {

public static let configuration = CommandConfiguration(commandName: "changelog", abstract: "Create a changelog for GitHub repositories")

@Option(name: .shortAndLong, help: "The tag to use as a base. Defaults to the latest tag.")
Expand Down
4 changes: 2 additions & 2 deletions Sources/GitBuddyCore/Commands/GitBuddy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// Copyright © 2020 WeTransfer. All rights reserved.
//

import Foundation
import ArgumentParser
import Foundation

/// Entry class of GitBuddy that registers commands and handles execution.
public struct GitBuddy: ParsableCommand {
Expand All @@ -20,5 +20,5 @@ public struct GitBuddy: ParsableCommand {
subcommands: [ChangelogCommand.self, ReleaseCommand.self, TagDeletionsCommand.self]
)

public init() { }
public init() {}
}
42 changes: 36 additions & 6 deletions Sources/GitBuddyCore/Commands/ReleaseCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
// Created by Antoine van der Lee on 09/04/2020.
//

import Foundation
import ArgumentParser
import Foundation

struct ReleaseCommand: ParsableCommand {

public static let configuration = CommandConfiguration(commandName: "release", abstract: "Create a new release including a changelog and publish comments on related issues.")
public static let configuration = CommandConfiguration(
commandName: "release",
abstract: "Create a new release including a changelog and publish comments on related issues."
)

@Option(name: .shortAndLong, help: "The path to the Changelog to update it with the latest changes.")
var changelogPath: String?
Expand All @@ -21,21 +23,49 @@ struct ReleaseCommand: ParsableCommand {
@Flag(name: [.customLong("use-pre-release"), .customShort("p")], help: "Create the release as a pre-release.")
var isPrerelease: Bool = false

@Option(name: .shortAndLong, help: "Specifies the commitish value that determines where the Git tag is created from. Can be any branch or commit SHA. Unused if the Git tag already exists. Default: the repository's default branch (usually main).")
// Main
@Option(
name: .shortAndLong,
help: """
Specifies the commitish value that determines where the Git tag is created from. Can be any branch or commit SHA.
Unused if the Git tag already exists.
Default: the repository's default branch (usually main).
"""
)
var targetCommitish: String?

@Option(name: [.long, .customShort("n")], help: "The name of the tag. If set, `changelogToTag` is required too. Default: takes the last created tag to publish as a GitHub release.")
// 1.3.1b1072
@Option(
name: [.long, .customShort("n")],
help: """
The name of the tag. If set, `changelogToTag` is required too.
Default: takes the last created tag to publish as a GitHub release.
"""
)
var tagName: String?

// // 1.3.1b1072 - App Store Release
@Option(name: .shortAndLong, help: "The title of the release. Default: uses the tag name.")
var releaseTitle: String?

// 1.3.0b1039
@Option(name: .shortAndLong, help: "The last release tag to use as a base for the changelog creation. Default: previous tag.")
var lastReleaseTag: String?

@Option(name: .customLong("changelogToTag"), help: "If set, the date of this tag will be used as the limit for the changelog creation. This variable should be passed when `tagName` is set. Default: latest tag.")
@Option(
name: .customLong("changelogToTag"),
help: """
If set, the date of this tag will be used as the limit for the changelog creation.
This variable should be passed when `tagName` is set.
Default: latest tag.
"""
)
var changelogToTag: String?

// Develop
@Option(name: .shortAndLong, help: "The base branch to compare with for generating the changelog. Defaults to master.")
var baseBranch: String?

Expand Down
8 changes: 5 additions & 3 deletions Sources/GitBuddyCore/Commands/TagDeletionsCommand.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Foundation
import ArgumentParser
import Foundation

struct TagDeletionsCommand: ParsableCommand {

public static let configuration = CommandConfiguration(commandName: "tagDeletion", abstract: "Delete a batch of tags based on given predicates.")
public static let configuration = CommandConfiguration(
commandName: "tagDeletion",
abstract: "Delete a batch of tags based on given predicates."
)

@Option(name: .shortAndLong, help: "The date of this tag will be used as a limit. Defaults to the latest tag.")
private var upUntilTag: String?
Expand Down
25 changes: 15 additions & 10 deletions Sources/GitBuddyCore/GitHub/Commenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ enum Comment {
case .releasedPR(let release):
return "Congratulations! :tada: This was released as part of [Release \(release.title)](\(release.url)) :rocket:"
case .releasedIssue(let release, let pullRequestID):
var body = "The pull request #\(pullRequestID) that closed this issue was merged and released as part of [Release \(release.title)](\(release.url)) :rocket:\n"
body += "Please let us know if the functionality works as expected as a reply here. If it does not, please open a new issue. Thanks!"
var body = "The pull request #\(pullRequestID) that closed this issue was merged and released "
body += "as part of [Release \(release.title)](\(release.url)) :rocket:\n"
body += "Please let us know if the functionality works as expected as a reply here. "
body += "If it does not, please open a new issue. Thanks!"
return body
}
}
Expand All @@ -43,14 +45,17 @@ enum Commenter {
static func post(_ comment: Comment, on issueID: Int, at project: GITProject, completion: @escaping () -> Void) {
var body = comment.body
body += watermark
Octokit().commentIssue(urlSession, owner: project.organisation, repository: project.repository, number: issueID, body: body) { response in
switch response {
case .success(let comment):
Log.debug("Successfully posted comment at: \(comment.htmlURL)")
case .failure(let error):
Log.debug("Posting comment for issue #\(issueID) failed: \(error)")
Octokit()
.commentIssue(urlSession, owner: project.organisation, repository: project.repository, number: issueID,
body: body)
{ response in
switch response {
case .success(let comment):
Log.debug("Successfully posted comment at: \(comment.htmlURL)")
case .failure(let error):
Log.debug("Posting comment for issue #\(issueID) failed: \(error)")
}
completion()
}
completion()
}
}
}
4 changes: 1 addition & 3 deletions Sources/GitBuddyCore/GitHub/IssuesFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Foundation
import OctoKit

struct IssuesFetcher {

let octoKit: Octokit
let project: GITProject

Expand All @@ -20,7 +19,7 @@ struct IssuesFetcher {

var result: Result<[Issue], Swift.Error>!

octoKit.issues(session, owner: project.organisation, repository: project.repository, state: .closed) { (response) in
octoKit.issues(session, owner: project.organisation, repository: project.repository, state: .closed) { response in
switch response {
case .success(let issues):
result = .success(issues)
Expand All @@ -42,5 +41,4 @@ struct IssuesFetcher {
return closedAt > fromDate && closedAt < toDate
}
}

}
5 changes: 2 additions & 3 deletions Sources/GitBuddyCore/GitHub/IssuesResolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ struct IssuesResolver {
}

extension String {

/// Extracts the resolved issues from a Pull Request body.
func resolvingIssues() -> [Int] {
var resolvedIssues = Set<Int>()
Expand All @@ -69,8 +68,8 @@ extension String {
guard index + 1 <= splits.count - 1 else { break }
let nextSplit = splits[index + 1]

let numberPrefixString = nextSplit.prefix { (character) -> Bool in
return character.isNumber
let numberPrefixString = nextSplit.prefix { character -> Bool in
character.isNumber
}

if !numberPrefixString.isEmpty, let numberPrefix = Int(numberPrefixString.description) {
Expand Down
1 change: 0 additions & 1 deletion Sources/GitBuddyCore/GitHub/OctoKit+Authentication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Foundation
import OctoKit

extension Octokit {

static var protocolClasses: [AnyClass]?
static var environment: [String: String] = ProcessInfo.processInfo.environment

Expand Down
12 changes: 9 additions & 3 deletions Sources/GitBuddyCore/GitHub/PullRequestFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import OctoKit
typealias Branch = String

struct PullRequestFetcher {

let octoKit: Octokit
let baseBranch: Branch
let project: GITProject
Expand All @@ -23,7 +22,15 @@ struct PullRequestFetcher {

var result: Result<[PullRequest], Swift.Error>!

octoKit.pullRequests(session, owner: project.organisation, repository: project.repository, base: baseBranch, state: .closed, sort: .updated, direction: .desc) { (response) in
octoKit.pullRequests(
session,
owner: project.organisation,
repository: project.repository,
base: baseBranch,
state: .closed,
sort: .updated,
direction: .desc
) { response in
switch response {
case .success(let pullRequests):
result = .success(pullRequests)
Expand All @@ -39,5 +46,4 @@ struct PullRequestFetcher {
return mergedAt > fromDate && mergedAt < toDate
}
}

}
2 changes: 1 addition & 1 deletion Sources/GitBuddyCore/Helpers/DateFormatters.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// DateFormatters.swift
//
//
//
// Created by Antoine van der Lee on 25/01/2022.
// Copyright © 2020 WeTransfer. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion Sources/GitBuddyCore/Helpers/DependencyInjectors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

/// Adds a `urlSession` property which defaults to `URLSession.shared`.
protocol URLSessionInjectable { }
protocol URLSessionInjectable {}

extension URLSessionInjectable {
var urlSession: URLSession { return URLSessionInjector.urlSession }
Expand Down
6 changes: 3 additions & 3 deletions Sources/GitBuddyCore/Helpers/Shell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ extension Process {
guard let outputData = String(data: data, encoding: String.Encoding.utf8) else { return "" }

return outputData
.reduce("") { (result, value) in
return result + String(value)
.reduce("") { result, value in
result + String(value)
}
.trimmingCharacters(in: .whitespacesAndNewlines)
}
Expand All @@ -65,7 +65,7 @@ private enum Shell: ShellExecuting {
}

/// Adds a `shell` property which defaults to `Shell.self`.
protocol ShellInjectable { }
protocol ShellInjectable {}

extension ShellInjectable {
static var shell: ShellExecuting.Type { ShellInjector.shell }
Expand Down
8 changes: 4 additions & 4 deletions Sources/GitBuddyCore/Models/Changelog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct SingleSectionChangelog: Changelog {

init(items: [ChangelogItem]) {
description = ChangelogBuilder(items: items).build()
self.itemIdentifiers = items.reduce(into: [:], { (result, item) in
itemIdentifiers = items.reduce(into: [:]) { result, item in
let pullRequestID: PullRequestID = item.closedBy.number

if var pullRequestIssues = result[pullRequestID] {
Expand All @@ -40,7 +40,7 @@ struct SingleSectionChangelog: Changelog {
} else {
result[pullRequestID] = []
}
})
}
}
}

Expand All @@ -50,7 +50,7 @@ struct SectionedChangelog: Changelog {
let description: String

let itemIdentifiers: [PullRequestID: [IssueID]]

init(issues: [Issue], pullRequests: [PullRequest]) {
description =
"""
Expand All @@ -63,7 +63,7 @@ struct SectionedChangelog: Changelog {
\(ChangelogBuilder(items: pullRequests.map { ChangelogItem(input: $0, closedBy: $0) }).build())
"""

itemIdentifiers = pullRequests.reduce(into: [:]) { (result, item) in
itemIdentifiers = pullRequests.reduce(into: [:]) { result, item in
result[item.number] = item.body?.resolvingIssues()
}
}
Expand Down
15 changes: 13 additions & 2 deletions Sources/GitBuddyCore/Models/ChangelogItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ protocol ChangelogInput {
var htmlURL: Foundation.URL? { get }
var username: String? { get }
}
protocol ChangelogIssue: ChangelogInput { }
protocol ChangelogPullRequest: ChangelogInput { }

protocol ChangelogIssue: ChangelogInput {}
protocol ChangelogPullRequest: ChangelogInput {}

extension PullRequest: ChangelogPullRequest {
var username: String? { user?.login ?? assignee?.login }
Expand All @@ -34,6 +35,8 @@ struct ChangelogItem {
var title: String? {
guard var title = input.title else { return nil }
title = title.prefix(1).uppercased() + title.dropFirst()
title = title.removingEmojis().trimmingCharacters(in: .whitespaces)

if let htmlURL = input.htmlURL {
title += " ([#\(input.number)](\(htmlURL)))"
}
Expand All @@ -43,3 +46,11 @@ struct ChangelogItem {
return title
}
}

private extension String {
func removingEmojis() -> String {
String(unicodeScalars.filter {
!$0.properties.isEmojiPresentation
})
}
}
Loading

0 comments on commit e694195

Please sign in to comment.