|
| 1 | +import Foundation |
| 2 | +import OctoKit |
| 3 | + |
| 4 | +final class TagsDeleter: URLSessionInjectable, ShellInjectable { |
| 5 | + |
| 6 | + private lazy var octoKit: Octokit = Octokit() |
| 7 | + let upUntilTagName: String? |
| 8 | + let limit: Int |
| 9 | + let prereleaseOnly: Bool |
| 10 | + let dryRun: Bool |
| 11 | + |
| 12 | + init(upUntilTagName: String? = nil, limit: Int, prereleaseOnly: Bool, dryRun: Bool) throws { |
| 13 | + try Octokit.authenticate() |
| 14 | + |
| 15 | + self.upUntilTagName = upUntilTagName |
| 16 | + self.limit = limit |
| 17 | + self.prereleaseOnly = prereleaseOnly |
| 18 | + self.dryRun = dryRun |
| 19 | + } |
| 20 | + |
| 21 | + @discardableResult public func run() throws -> [String] { |
| 22 | + let upUntilTag = try upUntilTagName.map { try Tag(name: $0) } ?? Tag.latest() |
| 23 | + Log.debug("Deleting up to \(limit) tags before \(upUntilTag.name) (Dry run: \(dryRun.description))") |
| 24 | + |
| 25 | + let currentProject = GITProject.current() |
| 26 | + let releases = try fetchReleases(project: currentProject, upUntil: upUntilTag.created) |
| 27 | + |
| 28 | + guard !releases.isEmpty else { |
| 29 | + return [] |
| 30 | + } |
| 31 | + try deleteReleases(releases, project: currentProject) |
| 32 | + try deleteTags(releases, project: currentProject) |
| 33 | + |
| 34 | + return releases.map { $0.tagName } |
| 35 | + } |
| 36 | + |
| 37 | + private func fetchReleases(project: GITProject, upUntil: Date) throws -> [OctoKit.Release] { |
| 38 | + let group = DispatchGroup() |
| 39 | + group.enter() |
| 40 | + |
| 41 | + var result: Result<[OctoKit.Release], Swift.Error>! |
| 42 | + octoKit.listReleases(urlSession, owner: project.organisation, repository: project.repository, perPage: 100) { response in |
| 43 | + result = response |
| 44 | + group.leave() |
| 45 | + } |
| 46 | + group.wait() |
| 47 | + |
| 48 | + let releases = try result.get() |
| 49 | + Log.debug("Fetched releases: \(releases.map { $0.tagName }.joined(separator: ", "))") |
| 50 | + |
| 51 | + return releases |
| 52 | + .filter({ release in |
| 53 | + guard !prereleaseOnly || release.prerelease else { |
| 54 | + return false |
| 55 | + } |
| 56 | + return release.createdAt < upUntil |
| 57 | + }) |
| 58 | + .suffix(limit) |
| 59 | + } |
| 60 | + |
| 61 | + private func deleteReleases(_ releases: [OctoKit.Release], project: GITProject) throws { |
| 62 | + let releasesToDelete = releases.map { $0.tagName } |
| 63 | + Log.debug("Deleting tags: \(releasesToDelete.joined(separator: ", "))") |
| 64 | + |
| 65 | + let group = DispatchGroup() |
| 66 | + var lastError: Error? |
| 67 | + for release in releases { |
| 68 | + group.enter() |
| 69 | + Log.debug("Deleting release \(release.tagName) with id \(release.id) url: \(release.htmlURL)") |
| 70 | + guard !dryRun else { |
| 71 | + group.leave() |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + octoKit.deleteRelease(urlSession, owner: project.organisation, repository: project.repository, releaseId: release.id) { error in |
| 76 | + defer { group.leave() } |
| 77 | + guard let error = error else { |
| 78 | + Log.debug("Successfully deleted release \(release.tagName)") |
| 79 | + return |
| 80 | + } |
| 81 | + Log.debug("Deletion of release \(release.tagName) failed: \(error)") |
| 82 | + lastError = error |
| 83 | + } |
| 84 | + } |
| 85 | + group.wait() |
| 86 | + |
| 87 | + if let lastError = lastError { |
| 88 | + throw lastError |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private func deleteTags(_ releases: [OctoKit.Release], project: GITProject) throws { |
| 93 | + let tagsToDelete = releases.map { $0.tagName } |
| 94 | + Log.debug("Deleting tags: \(tagsToDelete.joined(separator: ", "))") |
| 95 | + |
| 96 | + let group = DispatchGroup() |
| 97 | + var lastError: Error? |
| 98 | + for release in releases { |
| 99 | + group.enter() |
| 100 | + Log.debug("Deleting tag \(release.tagName) with id \(release.id) url: \(release.htmlURL)") |
| 101 | + guard !dryRun else { |
| 102 | + group.leave() |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + octoKit.deleteReference(urlSession, owner: project.organisation, repository: project.repository, ref: "tags/\(release.tagName)") { error in |
| 107 | + defer { group.leave() } |
| 108 | + guard let error = error else { |
| 109 | + Log.debug("Successfully deleted tag \(release.tagName)") |
| 110 | + return |
| 111 | + } |
| 112 | + Log.debug("Deletion of tag \(release.tagName) failed: \(error)") |
| 113 | + lastError = error |
| 114 | + } |
| 115 | + } |
| 116 | + group.wait() |
| 117 | + if let lastError = lastError { |
| 118 | + throw lastError |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments