Skip to content

Commit

Permalink
Update to Swift-OpenAPI 0.3 (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
MahdiBM authored Oct 23, 2023
1 parent d6c792d commit 13db2d7
Show file tree
Hide file tree
Showing 13 changed files with 5,945 additions and 6,318 deletions.
32 changes: 6 additions & 26 deletions Lambdas/GHHooks/EventHandler/Handlers/DocsIssuer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,44 +63,28 @@ struct DocsIssuer {
}

func getPRsRelatedToCommit() async throws -> [SimplePullRequest] {
let response = try await context.githubClient.repos_list_pull_requests_associated_with_commit(
try await context.githubClient.repos_list_pull_requests_associated_with_commit(
.init(path: .init(
owner: self.repo.owner.login,
repo: self.repo.name,
commit_sha: self.commitSHA
))
)

guard case let .ok(ok) = response,
case let .json(json) = ok.body
else {
throw Errors.httpRequestFailed(response: response)
}

return json
).ok.body.json
}

func getPRFiles(number: Int) async throws -> [DiffEntry] {
let response = try await context.githubClient.pulls_list_files(.init(
try await context.githubClient.pulls_list_files(.init(
path: .init(
owner: self.repo.owner.login,
repo: self.repo.name,
pull_number: number
)
))

guard case let .ok(ok) = response,
case let .json(json) = ok.body
else {
throw Errors.httpRequestFailed(response: response)
}

return json
)).ok.body.json
}

func fileIssue(number: Int) async throws {
let description = try await context.renderClient.translationNeededDescription(number: number)
let response = try await context.githubClient.issues_create(.init(
_ = try await context.githubClient.issues_create(.init(
path: .init(
owner: self.repo.owner.login,
repo: self.repo.name
Expand All @@ -109,10 +93,6 @@ struct DocsIssuer {
title: .case1("Translation needed for #\(number)"),
body: description
))
))

guard case .created = response else {
throw Errors.httpRequestFailed(response: response)
}
)).created
}
}
23 changes: 8 additions & 15 deletions Lambdas/GHHooks/EventHandler/Handlers/IssueHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ struct IssueHandler: Sendable {
createdAt: self.issue.created_at,
repoID: self.repo.id,
number: self.issue.number,
authorID: self.issue.user.id
authorID: self.issue.user.requireValue().id
)
}

Expand Down Expand Up @@ -129,15 +129,16 @@ struct IssueHandler: Sendable {
let status = Status(issue: issue)
let statusString = status.titleDescription.map { " - \($0)" } ?? ""
let title = "[\(repo.uiName)] Issue #\(number)\(statusString)".unicodesPrefix(256)
let user = try issue.user.requireValue()

let member = try await context.requester.getDiscordMember(githubID: "\(issue.user.id)")
let authorName = (member?.uiName).map { "@\($0)" } ?? issue.user.uiName
let member = try await context.requester.getDiscordMember(githubID: "\(user.id)")
let authorName = (member?.uiName).map { "@\($0)" } ?? user.uiName

var iconURL = member?.uiAvatarURL ?? issue.user.avatar_url
var iconURL = member?.uiAvatarURL ?? user.avatar_url
var footer = "By \(authorName)"
if let verb = status.closedByVerb,
let closedBy = try await self.maybeGetClosedByUser() {
if closedBy.id == issue.user.id {
if closedBy.id == issue.user?.id {
/// The same person opened and closed the issue.
footer = "Filed & \(verb) by \(authorName)"
} else {
Expand Down Expand Up @@ -174,21 +175,13 @@ struct IssueHandler: Sendable {
if action == .closed {
return (event.sender.id, event.sender.uiName)
} else {
let response = try await context.githubClient.issues_get(.init(
return try await context.githubClient.issues_get(.init(
path: .init(
owner: repo.owner.login,
repo: repo.name,
issue_number: issue.number
)
))

guard case let .ok(ok) = response,
case let .json(json) = ok.body
else {
throw Errors.httpRequestFailed(response: response)
}

return json.closed_by.map {
)).ok.body.json.closed_by.map {
($0.id, $0.uiName)
}
}
Expand Down
17 changes: 5 additions & 12 deletions Lambdas/GHHooks/EventHandler/Handlers/PRCoinGiver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ struct PRCoinGiver {
primaryBranch: repo.primaryBranch
)
for pr in try await getPRsRelatedToCommit() {
let user = try pr.user.requireValue()
if pr.merged_at == nil ||
codeOwners.contains(user: pr.user) {
codeOwners.contains(user: user) {
continue
}
guard let member = try await context.requester.getDiscordMember(
githubID: "\(pr.user.id)"
githubID: "\(user.id)"
), let discordID = member.user?.id else {
logger.debug("Found no Discord member for the GitHub user", metadata: [
"pr": "\(pr)",
Expand Down Expand Up @@ -79,20 +80,12 @@ struct PRCoinGiver {
}

func getPRsRelatedToCommit() async throws -> [SimplePullRequest] {
let response = try await context.githubClient.repos_list_pull_requests_associated_with_commit(
try await context.githubClient.repos_list_pull_requests_associated_with_commit(
.init(path: .init(
owner: repo.owner.login,
repo: repo.name,
commit_sha: commitSHA
))
)

guard case let .ok(ok) = response,
case let .json(json) = ok.body
else {
throw Errors.httpRequestFailed(response: response)
}

return json
).ok.body.json
}
}
36 changes: 8 additions & 28 deletions Lambdas/GHHooks/EventHandler/Handlers/ProjectBoardHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,49 +100,29 @@ struct ProjectBoardHandler {
}

func createCard(columnID: Int) async throws {
let response = try await self.context.githubClient.projects_create_card(.init(
_ = try await self.context.githubClient.projects_create_card(.init(
path: .init(column_id: columnID),
body: .json(.case1(.init(note: self.note)))
))

guard case .created = response else {
throw Errors.httpRequestFailed(response: response)
}
)).created
}

func move(toColumnID columnID: Int, cardID: Int) async throws {
let response = try await self.context.githubClient.projects_move_card(.init(
_ = try await self.context.githubClient.projects_move_card(.init(
path: .init(card_id: cardID),
body: .json(.init(position: "top", column_id: columnID))
))

guard case .created = response else {
throw Errors.httpRequestFailed(response: response)
}
)).created
}

func delete(cardID: Int) async throws {
let response = try await self.context.githubClient.projects_delete_card(.init(
_ = try await self.context.githubClient.projects_delete_card(.init(
path: .init(card_id: cardID)
))

guard case .noContent = response else {
throw Errors.httpRequestFailed(response: response)
}
)).noContent
}

func getCards(in columnID: Int) async throws -> [ProjectCard] {
let response = try await self.context.githubClient.projects_list_cards(.init(
try await self.context.githubClient.projects_list_cards(.init(
path: .init(column_id: columnID)
))

guard case let .ok(ok) = response,
case let .json(json) = ok.body
else {
throw Errors.httpRequestFailed(response: response)
}

return json
)).ok.body.json
}

private func moveOrCreate(targetColumn: Project.Column, in project: Project) async throws {
Expand Down
63 changes: 17 additions & 46 deletions Lambdas/GHHooks/EventHandler/Handlers/ReleaseMaker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,12 @@ struct ReleaseMaker {
func getLastRelease() async throws -> Release {
let latest = try await self.getLatestRelease()

let response = try await context.githubClient.repos_list_releases(.init(
let releases = try await context.githubClient.repos_list_releases(.init(
path: .init(
owner: repo.owner.login,
repo: repo.name
)
))

guard case let .ok(ok) = response,
case let .json(releases) = ok.body
else {
throw Errors.httpRequestFailed(response: response)
}
)).ok.body.json

let filteredReleases: [Release] = releases.compactMap {
release -> (Release, SemanticVersion)? in
Expand Down Expand Up @@ -135,13 +129,9 @@ struct ReleaseMaker {
)
))

switch response {
case let .ok(ok):
switch ok.body {
case let .json(json):
return json
}
default: break
if case let .ok(ok) = response,
case let .json(json) = ok.body {
return json
}

logger.warning("Could not find a 'latest' release", metadata: [
Expand All @@ -164,7 +154,7 @@ struct ReleaseMaker {
previousVersion: previousVersion,
newVersion: newVersion
)
let response = try await context.githubClient.repos_create_release(.init(
return try await context.githubClient.repos_create_release(.init(
path: .init(
owner: repo.owner.login,
repo: repo.name
Expand All @@ -178,23 +168,12 @@ struct ReleaseMaker {
prerelease: isPrerelease,
make_latest: isPrerelease ? ._false : ._true
))
))

switch response {
case let .created(created):
switch created.body {
case let .json(release):
return release
}
default: break
}

throw Errors.httpRequestFailed(response: response)
)).created.body.json
}

func sendComment(release: Release) async throws {
/// `"Issues" create comment`, but works for PRs too. Didn't find an endpoint for PRs.
let response = try await context.githubClient.issues_create_comment(.init(
_ = try await context.githubClient.issues_create_comment(.init(
path: .init(
owner: repo.owner.login,
repo: repo.name,
Expand All @@ -205,13 +184,7 @@ struct ReleaseMaker {
These changes are now available in [\(release.tag_name)](\(release.html_url))
"""
))
))

switch response {
case .created: return
default:
throw Errors.httpRequestFailed(response: response)
}
)).created
}

/**
Expand Down Expand Up @@ -293,16 +266,15 @@ struct ReleaseMaker {
))
)

guard case let .ok(ok) = response,
case let .json(json) = ok.body
else {
if case let .ok(ok) = response,
case let .json(json) = ok.body {
return json
} else {
logger.warning("Could not find review comments", metadata: [
"response": "\(response)"
])
return []
}

return json
}

func getExistingContributorIDs() async throws -> Set<Int> {
Expand All @@ -313,15 +285,14 @@ struct ReleaseMaker {
))
)

guard case let .ok(ok) = response,
case let .json(json) = ok.body
else {
if case let .ok(ok) = response,
case let .json(json) = ok.body {
return Set(json.compactMap(\.id))
} else {
logger.warning("Could not find current contributors", metadata: [
"response": "\(response)"
])
return []
}

return Set(json.compactMap(\.id))
}
}
Loading

0 comments on commit 13db2d7

Please sign in to comment.