Skip to content

Commit 1a044f3

Browse files
authored
Add Git Issues Events API endpoints (#212)
* Add Git Issues Events API endpoints Implements GET /repos/:owner/:repo/issues/:number/events, GET /repos/:owner/:repo/issues/events, and GET /repos/:owner/:repo/issues/events/:id with both callback and async/await variants. * Simplify IssueEvent API to async/await only Remove callback and paginated variants. New endpoints going forward will only expose async/await methods.
1 parent cc8251b commit 1a044f3

8 files changed

Lines changed: 11501 additions & 0 deletions

File tree

OctoKit/IssueEvent.swift

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import Foundation
2+
import RequestKit
3+
#if canImport(FoundationNetworking)
4+
import FoundationNetworking
5+
#endif
6+
7+
// MARK: - Models
8+
9+
open class IssueEvent: Codable {
10+
open var id: Int
11+
open var url: URL?
12+
open var actor: User?
13+
open var event: String?
14+
open var commitId: String?
15+
open var commitUrl: URL?
16+
open var createdAt: Date?
17+
open var issue: Issue?
18+
19+
enum CodingKeys: String, CodingKey {
20+
case id, url, actor, event, issue
21+
case commitId = "commit_id"
22+
case commitUrl = "commit_url"
23+
case createdAt = "created_at"
24+
}
25+
}
26+
27+
// MARK: - Requests
28+
29+
public extension Octokit {
30+
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
31+
func issueEvents(owner: String,
32+
repository: String,
33+
issueNumber: Int,
34+
page: String = "1",
35+
perPage: String = "100") async throws -> [IssueEvent] {
36+
let router = IssueEventRouter.readIssueEvents(configuration, owner, repository, issueNumber, page, perPage)
37+
return try await router.load(session, decoder: configuration.decoder, expectedResultType: [IssueEvent].self)
38+
}
39+
40+
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
41+
func repositoryIssueEvents(owner: String,
42+
repository: String,
43+
page: String = "1",
44+
perPage: String = "100") async throws -> [IssueEvent] {
45+
let router = IssueEventRouter.readRepositoryIssueEvents(configuration, owner, repository, page, perPage)
46+
return try await router.load(session, decoder: configuration.decoder, expectedResultType: [IssueEvent].self)
47+
}
48+
49+
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
50+
func issueEvent(owner: String,
51+
repository: String,
52+
id: Int) async throws -> IssueEvent {
53+
let router = IssueEventRouter.readIssueEvent(configuration, owner, repository, id)
54+
return try await router.load(session, decoder: configuration.decoder, expectedResultType: IssueEvent.self)
55+
}
56+
}
57+
58+
// MARK: - Router
59+
60+
enum IssueEventRouter: JSONPostRouter {
61+
case readIssueEvents(Configuration, String, String, Int, String, String)
62+
case readRepositoryIssueEvents(Configuration, String, String, String, String)
63+
case readIssueEvent(Configuration, String, String, Int)
64+
65+
var configuration: Configuration {
66+
switch self {
67+
case let .readIssueEvents(config, _, _, _, _, _): return config
68+
case let .readRepositoryIssueEvents(config, _, _, _, _): return config
69+
case let .readIssueEvent(config, _, _, _): return config
70+
}
71+
}
72+
73+
var method: HTTPMethod {
74+
return .GET
75+
}
76+
77+
var encoding: HTTPEncoding {
78+
return .url
79+
}
80+
81+
var params: [String: Any] {
82+
switch self {
83+
case let .readIssueEvents(_, _, _, _, page, perPage):
84+
return ["page": page, "per_page": perPage]
85+
case let .readRepositoryIssueEvents(_, _, _, page, perPage):
86+
return ["page": page, "per_page": perPage]
87+
case .readIssueEvent:
88+
return [:]
89+
}
90+
}
91+
92+
var path: String {
93+
switch self {
94+
case let .readIssueEvents(_, owner, repo, issueNumber, _, _):
95+
return "repos/\(owner)/\(repo)/issues/\(issueNumber)/events"
96+
case let .readRepositoryIssueEvents(_, owner, repo, _, _):
97+
return "repos/\(owner)/\(repo)/issues/events"
98+
case let .readIssueEvent(_, owner, repo, id):
99+
return "repos/\(owner)/\(repo)/issues/events/\(id)"
100+
}
101+
}
102+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import ArgumentParser
2+
import Foundation
3+
import OctoKit
4+
import Rainbow
5+
6+
@available(macOS 12.0, *)
7+
struct IssueEvent: AsyncParsableCommand {
8+
static let configuration = CommandConfiguration(abstract: "Operate on Issue Events",
9+
subcommands: [
10+
GetList.self,
11+
GetRepoList.self,
12+
Get.self
13+
])
14+
15+
init() {}
16+
}
17+
18+
@available(macOS 12.0, *)
19+
extension IssueEvent {
20+
struct GetList: AsyncParsableCommand {
21+
@Argument(help: "The owner of the repository")
22+
var owner: String
23+
24+
@Argument(help: "The name of the repository")
25+
var repository: String
26+
27+
@Argument(help: "The number of the issue")
28+
var issueNumber: Int
29+
30+
@Argument(help: "The path to put the file in")
31+
var filePath: String?
32+
33+
@Flag(help: "Verbose output flag")
34+
var verbose: Bool = false
35+
36+
init() {}
37+
38+
mutating func run() async throws {
39+
let session = JSONInterceptingURLSession()
40+
let octokit = makeOctokit(session: session)
41+
_ = try await octokit.issueEvents(owner: owner, repository: repository, issueNumber: issueNumber)
42+
session.verbosePrint(verbose: verbose)
43+
try session.printResponseToFileOrConsole(filePath: filePath)
44+
}
45+
}
46+
47+
struct GetRepoList: AsyncParsableCommand {
48+
@Argument(help: "The owner of the repository")
49+
var owner: String
50+
51+
@Argument(help: "The name of the repository")
52+
var repository: String
53+
54+
@Argument(help: "The path to put the file in")
55+
var filePath: String?
56+
57+
@Flag(help: "Verbose output flag")
58+
var verbose: Bool = false
59+
60+
init() {}
61+
62+
mutating func run() async throws {
63+
let session = JSONInterceptingURLSession()
64+
let octokit = makeOctokit(session: session)
65+
_ = try await octokit.repositoryIssueEvents(owner: owner, repository: repository)
66+
session.verbosePrint(verbose: verbose)
67+
try session.printResponseToFileOrConsole(filePath: filePath)
68+
}
69+
}
70+
71+
struct Get: AsyncParsableCommand {
72+
@Argument(help: "The owner of the repository")
73+
var owner: String
74+
75+
@Argument(help: "The name of the repository")
76+
var repository: String
77+
78+
@Argument(help: "The event ID")
79+
var id: Int
80+
81+
@Argument(help: "The path to put the file in")
82+
var filePath: String?
83+
84+
@Flag(help: "Verbose output flag")
85+
var verbose: Bool = false
86+
87+
init() {}
88+
89+
mutating func run() async throws {
90+
let session = JSONInterceptingURLSession()
91+
let octokit = makeOctokit(session: session)
92+
_ = try await octokit.issueEvent(owner: owner, repository: repository, id: id)
93+
session.verbosePrint(verbose: verbose)
94+
try session.printResponseToFileOrConsole(filePath: filePath)
95+
}
96+
}
97+
}

OctoKitCLI/Sources/OctoKitCLI/OctoKitCLI.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public struct OctokitCLI: AsyncParsableCommand {
2020
User.self,
2121
Gist.self,
2222
Git.self,
23+
IssueEvent.self,
2324
Notification.self,
2425
SortedJSONKeys.self
2526
])
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
{
2+
"actor" : {
3+
"avatar_url" : "https:\/\/avatars.githubusercontent.com\/u\/759730?v=4",
4+
"events_url" : "https:\/\/api.github.com\/users\/pietbrauer\/events{\/privacy}",
5+
"followers_url" : "https:\/\/api.github.com\/users\/pietbrauer\/followers",
6+
"following_url" : "https:\/\/api.github.com\/users\/pietbrauer\/following{\/other_user}",
7+
"gists_url" : "https:\/\/api.github.com\/users\/pietbrauer\/gists{\/gist_id}",
8+
"gravatar_id" : "",
9+
"html_url" : "https:\/\/github.com\/pietbrauer",
10+
"id" : 759730,
11+
"login" : "pietbrauer",
12+
"node_id" : "MDQ6VXNlcjc1OTczMA==",
13+
"organizations_url" : "https:\/\/api.github.com\/users\/pietbrauer\/orgs",
14+
"received_events_url" : "https:\/\/api.github.com\/users\/pietbrauer\/received_events",
15+
"repos_url" : "https:\/\/api.github.com\/users\/pietbrauer\/repos",
16+
"site_admin" : false,
17+
"starred_url" : "https:\/\/api.github.com\/users\/pietbrauer\/starred{\/owner}{\/repo}",
18+
"subscriptions_url" : "https:\/\/api.github.com\/users\/pietbrauer\/subscriptions",
19+
"type" : "User",
20+
"url" : "https:\/\/api.github.com\/users\/pietbrauer",
21+
"user_view_type" : "public"
22+
},
23+
"commit_id" : "f3f149bee52170170bf5b083db5b10ca1f98fc43",
24+
"commit_url" : "https:\/\/api.github.com\/repos\/nerdishbynature\/octokit.swift\/commits\/f3f149bee52170170bf5b083db5b10ca1f98fc43",
25+
"created_at" : "2015-01-14T07:18:16Z",
26+
"event" : "head_ref_force_pushed",
27+
"id" : 218585596,
28+
"issue" : {
29+
"active_lock_reason" : null,
30+
"assignee" : null,
31+
"assignees" : [
32+
33+
],
34+
"author_association" : "MEMBER",
35+
"body" : "Missing:\n- [x] Error handling\n- [x] Network tests (using Nocilla)\n- [x] Documentation\n",
36+
"closed_at" : "2015-01-14T15:51:41Z",
37+
"comments" : 0,
38+
"comments_url" : "https:\/\/api.github.com\/repos\/nerdishbynature\/octokit.swift\/issues\/1\/comments",
39+
"created_at" : "2015-01-13T21:09:12Z",
40+
"draft" : false,
41+
"events_url" : "https:\/\/api.github.com\/repos\/nerdishbynature\/octokit.swift\/issues\/1\/events",
42+
"html_url" : "https:\/\/github.com\/nerdishbynature\/octokit.swift\/pull\/1",
43+
"id" : 54248528,
44+
"labels" : [
45+
46+
],
47+
"labels_url" : "https:\/\/api.github.com\/repos\/nerdishbynature\/octokit.swift\/issues\/1\/labels{\/name}",
48+
"locked" : false,
49+
"milestone" : null,
50+
"node_id" : "MDExOlB1bGxSZXF1ZXN0MjczMDQ5MjY=",
51+
"number" : 1,
52+
"performed_via_github_app" : null,
53+
"pull_request" : {
54+
"diff_url" : "https:\/\/github.com\/nerdishbynature\/octokit.swift\/pull\/1.diff",
55+
"html_url" : "https:\/\/github.com\/nerdishbynature\/octokit.swift\/pull\/1",
56+
"merged_at" : "2015-01-14T15:51:41Z",
57+
"patch_url" : "https:\/\/github.com\/nerdishbynature\/octokit.swift\/pull\/1.patch",
58+
"url" : "https:\/\/api.github.com\/repos\/nerdishbynature\/octokit.swift\/pulls\/1"
59+
},
60+
"reactions" : {
61+
"-1" : 0,
62+
"+1" : 0,
63+
"confused" : 0,
64+
"eyes" : 0,
65+
"heart" : 0,
66+
"hooray" : 0,
67+
"laugh" : 0,
68+
"rocket" : 0,
69+
"total_count" : 0,
70+
"url" : "https:\/\/api.github.com\/repos\/nerdishbynature\/octokit.swift\/issues\/1\/reactions"
71+
},
72+
"repository_url" : "https:\/\/api.github.com\/repos\/nerdishbynature\/octokit.swift",
73+
"state" : "closed",
74+
"state_reason" : null,
75+
"timeline_url" : "https:\/\/api.github.com\/repos\/nerdishbynature\/octokit.swift\/issues\/1\/timeline",
76+
"title" : "Authenticatio",
77+
"type" : null,
78+
"updated_at" : "2015-01-14T15:51:48Z",
79+
"url" : "https:\/\/api.github.com\/repos\/nerdishbynature\/octokit.swift\/issues\/1",
80+
"user" : {
81+
"avatar_url" : "https:\/\/avatars.githubusercontent.com\/u\/759730?v=4",
82+
"events_url" : "https:\/\/api.github.com\/users\/pietbrauer\/events{\/privacy}",
83+
"followers_url" : "https:\/\/api.github.com\/users\/pietbrauer\/followers",
84+
"following_url" : "https:\/\/api.github.com\/users\/pietbrauer\/following{\/other_user}",
85+
"gists_url" : "https:\/\/api.github.com\/users\/pietbrauer\/gists{\/gist_id}",
86+
"gravatar_id" : "",
87+
"html_url" : "https:\/\/github.com\/pietbrauer",
88+
"id" : 759730,
89+
"login" : "pietbrauer",
90+
"node_id" : "MDQ6VXNlcjc1OTczMA==",
91+
"organizations_url" : "https:\/\/api.github.com\/users\/pietbrauer\/orgs",
92+
"received_events_url" : "https:\/\/api.github.com\/users\/pietbrauer\/received_events",
93+
"repos_url" : "https:\/\/api.github.com\/users\/pietbrauer\/repos",
94+
"site_admin" : false,
95+
"starred_url" : "https:\/\/api.github.com\/users\/pietbrauer\/starred{\/owner}{\/repo}",
96+
"subscriptions_url" : "https:\/\/api.github.com\/users\/pietbrauer\/subscriptions",
97+
"type" : "User",
98+
"url" : "https:\/\/api.github.com\/users\/pietbrauer",
99+
"user_view_type" : "public"
100+
}
101+
},
102+
"node_id" : "MDIzOkhlYWRSZWZGb3JjZVB1c2hlZEV2ZW50MjE4NTg1NTk2",
103+
"performed_via_github_app" : null,
104+
"url" : "https:\/\/api.github.com\/repos\/nerdishbynature\/octokit.swift\/issues\/events\/218585596"
105+
}

0 commit comments

Comments
 (0)