Skip to content

Commit 28276da

Browse files
committed
Add Sources/Release
1 parent 7e29e43 commit 28276da

File tree

5 files changed

+207
-2
lines changed

5 files changed

+207
-2
lines changed

Package.swift

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ let package = Package(
1010
],
1111
products: [
1212
.executable(name: "Comment", targets: ["Comment"]),
13+
.executable(name: "Release", targets: ["Release"]),
1314
],
1415
dependencies: [
1516
.package(url: "https://github.com/Wei18/github-rest-api-swift-openapi", from: "2.0.0"),
@@ -18,8 +19,7 @@ let package = Package(
1819
.package(url: "https://github.com/jpsim/Yams", from: "5.0.0"),
1920
],
2021
targets: [
21-
// Targets are the basic building blocks of a package, defining a module or a test suite.
22-
// Targets can depend on other targets in this package and products from dependencies.
22+
//
2323
.executableTarget(
2424
name: "Comment",
2525
dependencies: [
@@ -41,6 +41,29 @@ let package = Package(
4141
.target(name: "Middleware"),
4242
]
4343
),
44+
//
45+
.executableTarget(
46+
name: "Release",
47+
dependencies: [
48+
.product(name: "ArgumentParser", package: "swift-argument-parser"),
49+
.target(name: "ReleaseCLI"),
50+
]
51+
),
52+
.target(
53+
name: "ReleaseCLI",
54+
dependencies: [
55+
.target(name: "ReleaseCore"),
56+
.target(name: "Extensions"),
57+
]
58+
),
59+
.target(
60+
name: "ReleaseCore",
61+
dependencies: [
62+
.product(name: "GitHubRestAPIRepos", package: "github-rest-api-swift-openapi"),
63+
.target(name: "Middleware"),
64+
.target(name: "Extensions"),
65+
]
66+
),
4467
.target(
4568
name: "Middleware",
4669
dependencies: [

Sources/Release/main.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//
2+
// main.swift
3+
//
4+
//
5+
// Created by zwc on 2024/9/17.
6+
//
7+
8+
import ReleaseCLI
9+
10+
Release.main()
11+

Sources/ReleaseCLI/Release.swift

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//
2+
// Release.swift
3+
//
4+
//
5+
// Created by zwc on 2024/9/17.
6+
//
7+
8+
import Foundation
9+
import ArgumentParser
10+
import ReleaseCore
11+
import Extensions
12+
13+
/// A command-line interface for creating or updating a comment on a GitHub issue or pull request.
14+
package struct Release: ParsableCommand {
15+
16+
/// The GitHub repository owner.
17+
@Option(name: .shortAndLong, help: "The owner of the GitHub repository.")
18+
var owner: String = #"environment["OWNER"]"#
19+
20+
/// The GitHub repository name.
21+
@Option(name: .shortAndLong, help: "The name of the GitHub repository.")
22+
var repo: String = #"environment["REPO"]"#
23+
24+
@Option(name: .shortAndLong, help: "The value of major | minor | patch (default)")
25+
var bumpType: String = #"environment["BUMP_TYPE"]"#
26+
27+
@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.")
28+
var gitRef: String = #"environment["GIT_REF"]"#
29+
30+
/// The GitHub API token for authentication.
31+
@Option(name: .shortAndLong, help: "The GitHub API token to authenticate requests.")
32+
var token: String = #"environment["TOKEN"]"#
33+
34+
package init() {}
35+
/// Runs the command, creating or updating the comment on the GitHub issue or pull request.
36+
package func run() throws {
37+
Task.synchronous {
38+
do {
39+
let owner = ProcessInfo.processInfo.environment["OWNER"] ?? owner
40+
print("inputs.owner: \(owner)")
41+
42+
let repo = ProcessInfo.processInfo.environment["REPO"] ?? repo
43+
print("inputs.repo: \(repo)")
44+
45+
let bumpType = BumpVersionType(rawValue: ProcessInfo.processInfo.environment["BUMP_TYPE"] ?? bumpType) ?? .patch
46+
print("inputs.bumpType: \(bumpType)")
47+
48+
let gitRef = ProcessInfo.processInfo.environment["GIT_REF"] ?? gitRef
49+
print("inputs.gitRef: \(gitRef)")
50+
51+
let token = ProcessInfo.processInfo.environment["TOKEN"] ?? token
52+
print("inputs.token: \(token)")
53+
54+
let useCase = try ReposUseCase(
55+
token: token,
56+
owner: owner,
57+
repo: repo
58+
)
59+
try await useCase.createRelease(
60+
type: bumpType,
61+
gitRef: gitRef)
62+
print("Release successfully created!")
63+
} catch {
64+
Self.exit(withError: error)
65+
}
66+
}
67+
}
68+
}

Sources/ReleaseCore/RepoUseCase.swift

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//
2+
// ReposUseCase.swift
3+
//
4+
//
5+
// Created by zwc on 2024/9/17.
6+
//
7+
8+
import Foundation
9+
import GitHubRestAPIRepos
10+
import OpenAPIURLSession
11+
import Middleware
12+
import Extensions
13+
14+
package struct ReposUseCase {
15+
16+
/// The client used to interact with GitHub's REST API for issues.
17+
private let client: GitHubRestAPIRepos.Client
18+
19+
/// The owner of the repository.
20+
let owner: String
21+
22+
/// The repository name.
23+
let repo: String
24+
25+
/// Initializes a new `CommentUseCase` instance.
26+
///
27+
/// - Parameters:
28+
/// - token: The GitHub API token to authenticate requests.
29+
/// - owner: The owner of the repository.
30+
/// - repo: The name of the repository.
31+
package init(token: String, owner: String, repo: String) throws {
32+
self.client = Client(
33+
serverURL: try Servers.server1(),
34+
transport: URLSessionTransport(),
35+
middlewares: [AuthenticationMiddleware(token: token)]
36+
)
37+
self.owner = owner
38+
self.repo = repo
39+
}
40+
41+
package func createRelease(type: BumpVersionType, gitRef: String) async throws {
42+
let tag = try await client.repos_sol_list_hyphen_releases(
43+
path: .init(owner: owner, repo: repo)
44+
).ok.body.json.first?.tag_name ?? "0.0.0"
45+
46+
var version = try Version(tag)
47+
version.bump(type: type)
48+
49+
_ = try await client.repos_sol_create_hyphen_release(
50+
path: .init(owner: owner, repo: repo),
51+
body: .json(
52+
.init(
53+
tag_name: version.string,
54+
target_commitish: gitRef,
55+
generate_release_notes: true
56+
)
57+
)
58+
).created
59+
}
60+
}

Sources/ReleaseCore/Version.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// Version.swift
3+
//
4+
//
5+
// Created by zwc on 2024/9/17.
6+
//
7+
8+
import Foundation
9+
10+
package enum BumpVersionType: String {
11+
case major
12+
case minor
13+
case patch
14+
}
15+
16+
struct Version {
17+
private(set) var major: Int
18+
private(set) var minor: Int
19+
private(set) var patch: Int
20+
21+
var string: String { "\(major).\(minor).\(patch)" }
22+
23+
init(_ string: String) throws {
24+
let components = string.components(separatedBy: ".")
25+
guard components.count == 3 else {
26+
throw NSError(domain: "Required count == 3", code: #line)
27+
}
28+
major = try Int(string: components[0])
29+
minor = try Int(string: components[1])
30+
patch = try Int(string: components[2])
31+
}
32+
33+
mutating func bump(type: BumpVersionType) {
34+
switch type {
35+
case .patch:
36+
patch += 1
37+
case .minor:
38+
minor += 1
39+
case .major:
40+
major += 1
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)