-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:WeTransfer/GitBuddy
- Loading branch information
Showing
14 changed files
with
579 additions
and
31 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import Foundation | ||
import ArgumentParser | ||
|
||
struct TagDeletionsCommand: ParsableCommand { | ||
|
||
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? | ||
|
||
@Option(name: .shortAndLong, help: "The limit of tags to delete in this batch. Defaults to 50") | ||
private var limit: Int = 50 | ||
|
||
@Flag(name: .long, help: "Delete pre releases only") | ||
private var prereleaseOnly: Bool = false | ||
|
||
@Flag(name: .long, help: "Does not actually delete but just logs which tags would be deleted") | ||
private var dryRun: Bool = false | ||
|
||
@Flag(name: .long, help: "Show extra logging for debugging purposes") | ||
var verbose: Bool = false | ||
|
||
func run() throws { | ||
Log.isVerbose = verbose | ||
|
||
let tagsDeleter = try TagsDeleter(upUntilTagName: upUntilTag, limit: limit, prereleaseOnly: prereleaseOnly, dryRun: dryRun) | ||
let deletedTags = try tagsDeleter.run() | ||
|
||
guard !deletedTags.isEmpty else { | ||
Log.message("There were no tags found to be deleted.") | ||
return | ||
} | ||
Log.message("Deleted tags: \(deletedTags)") | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// DateFormatters.swift | ||
// | ||
// | ||
// Created by Antoine van der Lee on 25/01/2022. | ||
// Copyright © 2020 WeTransfer. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
enum Formatter { | ||
static let gitDateFormatter: DateFormatter = { | ||
let dateFormatter = DateFormatter() | ||
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z" | ||
dateFormatter.locale = Locale(identifier: "en_US_POSIX") | ||
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0) | ||
return dateFormatter | ||
}() | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import Foundation | ||
import OctoKit | ||
|
||
final class TagsDeleter: URLSessionInjectable, ShellInjectable { | ||
|
||
private lazy var octoKit: Octokit = Octokit() | ||
let upUntilTagName: String? | ||
let limit: Int | ||
let prereleaseOnly: Bool | ||
let dryRun: Bool | ||
|
||
init(upUntilTagName: String? = nil, limit: Int, prereleaseOnly: Bool, dryRun: Bool) throws { | ||
try Octokit.authenticate() | ||
|
||
self.upUntilTagName = upUntilTagName | ||
self.limit = limit | ||
self.prereleaseOnly = prereleaseOnly | ||
self.dryRun = dryRun | ||
} | ||
|
||
@discardableResult public func run() throws -> [String] { | ||
let upUntilTag = try upUntilTagName.map { try Tag(name: $0) } ?? Tag.latest() | ||
Log.debug("Deleting up to \(limit) tags before \(upUntilTag.name) (Dry run: \(dryRun.description))") | ||
|
||
let currentProject = GITProject.current() | ||
let releases = try fetchReleases(project: currentProject, upUntil: upUntilTag.created) | ||
|
||
guard !releases.isEmpty else { | ||
return [] | ||
} | ||
try deleteReleases(releases, project: currentProject) | ||
try deleteTags(releases, project: currentProject) | ||
|
||
return releases.map { $0.tagName } | ||
} | ||
|
||
private func fetchReleases(project: GITProject, upUntil: Date) throws -> [OctoKit.Release] { | ||
let group = DispatchGroup() | ||
group.enter() | ||
|
||
var result: Result<[OctoKit.Release], Swift.Error>! | ||
octoKit.listReleases(urlSession, owner: project.organisation, repository: project.repository, perPage: 100) { response in | ||
result = response | ||
group.leave() | ||
} | ||
group.wait() | ||
|
||
let releases = try result.get() | ||
Log.debug("Fetched releases: \(releases.map { $0.tagName }.joined(separator: ", "))") | ||
|
||
return releases | ||
.filter({ release in | ||
guard !prereleaseOnly || release.prerelease else { | ||
return false | ||
} | ||
return release.createdAt < upUntil | ||
}) | ||
.suffix(limit) | ||
} | ||
|
||
private func deleteReleases(_ releases: [OctoKit.Release], project: GITProject) throws { | ||
let releasesToDelete = releases.map { $0.tagName } | ||
Log.debug("Deleting tags: \(releasesToDelete.joined(separator: ", "))") | ||
|
||
let group = DispatchGroup() | ||
var lastError: Error? | ||
for release in releases { | ||
group.enter() | ||
Log.debug("Deleting release \(release.tagName) with id \(release.id) url: \(release.htmlURL)") | ||
guard !dryRun else { | ||
group.leave() | ||
return | ||
} | ||
|
||
octoKit.deleteRelease(urlSession, owner: project.organisation, repository: project.repository, releaseId: release.id) { error in | ||
defer { group.leave() } | ||
guard let error = error else { | ||
Log.debug("Successfully deleted release \(release.tagName)") | ||
return | ||
} | ||
Log.debug("Deletion of release \(release.tagName) failed: \(error)") | ||
lastError = error | ||
} | ||
} | ||
group.wait() | ||
|
||
if let lastError = lastError { | ||
throw lastError | ||
} | ||
} | ||
|
||
private func deleteTags(_ releases: [OctoKit.Release], project: GITProject) throws { | ||
let tagsToDelete = releases.map { $0.tagName } | ||
Log.debug("Deleting tags: \(tagsToDelete.joined(separator: ", "))") | ||
|
||
let group = DispatchGroup() | ||
var lastError: Error? | ||
for release in releases { | ||
group.enter() | ||
Log.debug("Deleting tag \(release.tagName) with id \(release.id) url: \(release.htmlURL)") | ||
guard !dryRun else { | ||
group.leave() | ||
return | ||
} | ||
|
||
octoKit.deleteReference(urlSession, owner: project.organisation, repository: project.repository, ref: "tags/\(release.tagName)") { error in | ||
defer { group.leave() } | ||
guard let error = error else { | ||
Log.debug("Successfully deleted tag \(release.tagName)") | ||
return | ||
} | ||
Log.debug("Deletion of tag \(release.tagName) failed: \(error)") | ||
lastError = error | ||
} | ||
} | ||
group.wait() | ||
if let lastError = lastError { | ||
throw lastError | ||
} | ||
} | ||
} |
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
Oops, something went wrong.