From 0ac8d89ca956a4123b434721ec1a3c5dd26b6dcb Mon Sep 17 00:00:00 2001 From: MahdiBM Date: Mon, 20 Jan 2025 22:19:44 +0330 Subject: [PATCH] Revert "Penny ignore reporting "Do Not Merge" PRs in Discord" This reverts commit 538c98ef56ee81f1a23e2df107e8e170e4c32796. --- .sourcekit-lsp/config.json | 2 +- Lambdas/GHHooks/Constants.swift | 7 -- .../EventHandler/Handlers/PRCoinGiver.swift | 9 +- .../EventHandler/Handlers/PRHandler.swift | 2 - .../EventHandler/Handlers/ReleaseMaker.swift | 20 ++-- Lambdas/GHHooks/Extensions/+PR.Label.swift | 39 ++++++++ Lambdas/GHHooks/Extensions/+PR.swift | 92 ------------------- Lambdas/GitHubAPI/Aliases.swift | 1 - 8 files changed, 52 insertions(+), 120 deletions(-) create mode 100644 Lambdas/GHHooks/Extensions/+PR.Label.swift delete mode 100644 Lambdas/GHHooks/Extensions/+PR.swift diff --git a/.sourcekit-lsp/config.json b/.sourcekit-lsp/config.json index a5dd3b1e..457abfc3 100644 --- a/.sourcekit-lsp/config.json +++ b/.sourcekit-lsp/config.json @@ -1,6 +1,6 @@ { "backgroundIndexing": true, - "backgroundPreparationMode": "enabled", + "backgroundPreparationMode": "build", "maxCoresPercentageToUseForBackgroundIndexing": 0.7, "experimentalFeatures": ["on-type-formatting"] } diff --git a/Lambdas/GHHooks/Constants.swift b/Lambdas/GHHooks/Constants.swift index 87be6016..18b546ec 100644 --- a/Lambdas/GHHooks/Constants.swift +++ b/Lambdas/GHHooks/Constants.swift @@ -5,13 +5,6 @@ enum Constants { static let guildID: GuildSnowflake = "431917998102675485" static let botDevUserID: UserSnowflake = "290483761559240704" - static let trustedGitHubUserIds: [Int64] = [ - 69_189_821, // Paul - 9_938_337, // Tim - 1_130_717, // Gwynne - 54_685_446, // Mahdi - ] - enum GitHub { /// The user id of Penny. static let userID = 139_480_971 diff --git a/Lambdas/GHHooks/EventHandler/Handlers/PRCoinGiver.swift b/Lambdas/GHHooks/EventHandler/Handlers/PRCoinGiver.swift index 1b55c698..ce195a7b 100644 --- a/Lambdas/GHHooks/EventHandler/Handlers/PRCoinGiver.swift +++ b/Lambdas/GHHooks/EventHandler/Handlers/PRCoinGiver.swift @@ -30,11 +30,8 @@ struct PRCoinGiver { repoFullName: repo.fullName, branch: branch ) - for pr in prs { - if pr.mergedAt == nil { - logger.debug("PR is not merged yet", metadata: ["pr": "\(pr)"]) - continue - } + for pr in try await getPRsRelatedToCommit() { + guard pr.mergedAt != nil else { continue } let user = try pr.user.requireValue() var usersToReceiveCoins = codeOwners.contains(user: user) ? [] : [user.id] @@ -91,8 +88,6 @@ struct PRCoinGiver { } func giveCoin(userId: Int64, pr: SimplePullRequest) async throws { - logger.trace("Giving a coin", metadata: ["userId": .stringConvertible(userId)]) - guard let member = try await context.requester.getDiscordMember( githubID: "\(userId)" diff --git a/Lambdas/GHHooks/EventHandler/Handlers/PRHandler.swift b/Lambdas/GHHooks/EventHandler/Handlers/PRHandler.swift index f4d54df1..c33c8730 100644 --- a/Lambdas/GHHooks/EventHandler/Handlers/PRHandler.swift +++ b/Lambdas/GHHooks/EventHandler/Handlers/PRHandler.swift @@ -49,12 +49,10 @@ struct PRHandler { } func onEdited() async throws { - if self.pr.isIgnorableDoNotMergePR { return } try await self.editPRReport() } func onOpened() async throws { - if self.pr.isIgnorableDoNotMergePR { return } try await self.makeReporter().reportCreation() } diff --git a/Lambdas/GHHooks/EventHandler/Handlers/ReleaseMaker.swift b/Lambdas/GHHooks/EventHandler/Handlers/ReleaseMaker.swift index a94ab2a8..1938d7f8 100644 --- a/Lambdas/GHHooks/EventHandler/Handlers/ReleaseMaker.swift +++ b/Lambdas/GHHooks/EventHandler/Handlers/ReleaseMaker.swift @@ -290,9 +290,7 @@ struct ReleaseMaker { } func isNewContributor(codeOwners: CodeOwners, existingContributors: Set) -> Bool { - pr.authorAssociation != .owner - && !pr.user.isBot - && !codeOwners.contains(user: pr.user) + pr.authorAssociation != .owner && !pr.user.isBot && !codeOwners.contains(user: pr.user) && !existingContributors.contains(pr.user.id) } @@ -307,9 +305,9 @@ struct ReleaseMaker { ) ) - do { - return try response.ok.body.json - } catch { + guard case let .ok(ok) = response, + case let .json(json) = ok.body + else { logger.warning( "Could not find reviews", metadata: [ @@ -318,6 +316,8 @@ struct ReleaseMaker { ) return [] } + + return json } func getExistingContributorIDs() async throws -> Set { @@ -357,9 +357,9 @@ struct ReleaseMaker { ) ) - do { - let ok = try response.ok - let json = try ok.body.json + if case let .ok(ok) = response, + case let .json(json) = ok.body + { /// Example of a `link` header: `; rel="prev", ; rel="next", ; rel="last", ; rel="first"` /// If the header contains `rel="next"` then we'll have a next page to fetch. let hasNext = @@ -378,7 +378,7 @@ struct ReleaseMaker { ] ) return (ids, hasNext) - } catch { + } else { logger.error( "Error when fetching contributors but will continue", metadata: [ diff --git a/Lambdas/GHHooks/Extensions/+PR.Label.swift b/Lambdas/GHHooks/Extensions/+PR.Label.swift new file mode 100644 index 00000000..d9ca916e --- /dev/null +++ b/Lambdas/GHHooks/Extensions/+PR.Label.swift @@ -0,0 +1,39 @@ +import GitHubAPI + +extension PullRequest { + + enum KnownLabel: String { + case semVerMajor = "semver-major" + case semVerMinor = "semver-minor" + case semVerPatch = "semver-patch" + case semVerNoOp = "semver-noop" + case release = "release" + case noReleaseNeeded = "no-release-needed" + case translationUpdate = "translation-update" + case noTranslationNeeded = "no-translation-needed" + + func toBump() -> SemVerBump? { + switch self { + case .semVerMajor: return .major + case .semVerMinor: return .minor + case .semVerPatch: return .patch + case .release: return .releaseStage + case .semVerNoOp, .noReleaseNeeded, .translationUpdate, .noTranslationNeeded: return nil + } + } + } + + var knownLabels: [KnownLabel] { + self.labels.compactMap { + KnownLabel(rawValue: $0.name) + } + } +} + +extension SimplePullRequest { + var knownLabels: [PullRequest.KnownLabel] { + self.labels.compactMap { + PullRequest.KnownLabel(rawValue: $0.name) + } + } +} diff --git a/Lambdas/GHHooks/Extensions/+PR.swift b/Lambdas/GHHooks/Extensions/+PR.swift deleted file mode 100644 index 49ed53d6..00000000 --- a/Lambdas/GHHooks/Extensions/+PR.swift +++ /dev/null @@ -1,92 +0,0 @@ -import GitHubAPI - -#if canImport(FoundationEssentials) -import FoundationEssentials -#else -import Foundation -#endif - -extension PullRequest { - - enum KnownLabel: String { - case semVerMajor = "semver-major" - case semVerMinor = "semver-minor" - case semVerPatch = "semver-patch" - case semVerNoOp = "semver-noop" - case release = "release" - case noReleaseNeeded = "no-release-needed" - case translationUpdate = "translation-update" - case noTranslationNeeded = "no-translation-needed" - - func toBump() -> SemVerBump? { - switch self { - case .semVerMajor: return .major - case .semVerMinor: return .minor - case .semVerPatch: return .patch - case .release: return .releaseStage - case .semVerNoOp, .noReleaseNeeded, .translationUpdate, .noTranslationNeeded: return nil - } - } - } - - var knownLabels: [KnownLabel] { - self.labels.compactMap { - KnownLabel(rawValue: $0.name) - } - } - - var isIgnorableDoNotMergePR: Bool { - isIgnorableDoNotMergePullRequest( - title: self.title, - userId: self.user.id, - authorAssociation: self.authorAssociation - ) - } -} - -extension SimplePullRequest { - var knownLabels: [PullRequest.KnownLabel] { - self.labels.compactMap { - PullRequest.KnownLabel(rawValue: $0.name) - } - } - - var isIgnorableDoNotMergePR: Bool { - isIgnorableDoNotMergePullRequest( - title: self.title, - userId: self.user?.id, - authorAssociation: self.authorAssociation - ) - } -} - -private func isIgnorableDoNotMergePullRequest( - title: String, - userId: Int64?, - authorAssociation: AuthorAssociation -) -> Bool { - let isTrustedUser = userId.map { Constants.trustedGitHubUserIds.contains($0) } ?? false - guard isTrustedUser || authorAssociation.isContributorOrHigher else { - return false - } - return title.hasDoNotMergePrefix -} - -extension AuthorAssociation { - fileprivate var isContributorOrHigher: Bool { - switch self { - case .collaborator, .contributor, .owner: return true - case .member, .firstTimer, .firstTimeContributor, .mannequin, .none: return false - } - } -} - -extension String { - fileprivate var hasDoNotMergePrefix: Bool { - let folded = self.lowercased() - .filter { !$0.isPunctuation } - .folding(options: .caseInsensitive, locale: nil) - .folding(options: .diacriticInsensitive, locale: nil) - return folded.hasPrefix("dnm") || folded.hasPrefix("do not merge") - } -} diff --git a/Lambdas/GitHubAPI/Aliases.swift b/Lambdas/GitHubAPI/Aliases.swift index a1e35b8d..6975b19e 100644 --- a/Lambdas/GitHubAPI/Aliases.swift +++ b/Lambdas/GitHubAPI/Aliases.swift @@ -2,7 +2,6 @@ package typealias Repository = Components.Schemas.Repository package typealias User = Components.Schemas.SimpleUser package typealias PullRequest = Components.Schemas.PullRequest package typealias SimplePullRequest = Components.Schemas.PullRequestSimple -package typealias AuthorAssociation = Components.Schemas.AuthorAssociation package typealias Issue = Components.Schemas.Issue package typealias Label = Components.Schemas.Label package typealias Release = Components.Schemas.Release