Skip to content
TypeScript

fix(gitlab): bot authors are flagged as bots (#30383)

Antonye945fcc50fb2
diff --git a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts
index b05e692576d..7c386760b7a 100644
--- a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts
+++ b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts
@@ -466,6 +466,7 @@ export class GitLabPullRequestCli extends Context.Service<
{
readonly reactions: ReadonlyArray<PullRequestReaction>;
readonly reactionsByNoteId: ReadonlyMap<string, ReadonlyArray<PullRequestReaction>>;
+ readonly botLogins: ReadonlySet<string>;
},
GitLabPullRequestCliError
>;
@@ -1091,11 +1092,13 @@ export const make = Effect.gen(function* () {
readonly collected: {
readonly reactions: ReadonlyArray<PullRequestReaction>;
readonly reactionsByNoteId: Map<string, ReadonlyArray<PullRequestReaction>>;
+ readonly botLogins: Set<string>;
} | null;
}): Effect.Effect<
{
readonly reactions: ReadonlyArray<PullRequestReaction>;
readonly reactionsByNoteId: ReadonlyMap<string, ReadonlyArray<PullRequestReaction>>;
+ readonly botLogins: ReadonlySet<string>;
},
GitLabPullRequestCliError
> =>
@@ -1128,9 +1131,11 @@ export const make = Effect.gen(function* () {
const collected = input.collected ?? {
reactions: decoded.success.reactions,
reactionsByNoteId: new Map<string, ReadonlyArray<PullRequestReaction>>(),
+ botLogins: new Set<string>(),
};
for (const [id, reactions] of decoded.success.reactionsByNoteId)
collected.reactionsByNoteId.set(id, reactions);
+ for (const login of decoded.success.botLogins) collected.botLogins.add(login);
return decoded.success.nextCursor === null || input.page >= CONVERSATION_PAGES
? Effect.succeed(collected)
: awardsPage({
diff --git a/packages/source-control-gitlab/src/server/GitLabPullRequestProvider.test.ts b/packages/source-control-gitlab/src/server/GitLabPullRequestProvider.test.ts
index dbf25b531a2..7e687619963 100644
--- a/packages/source-control-gitlab/src/server/GitLabPullRequestProvider.test.ts
+++ b/packages/source-control-gitlab/src/server/GitLabPullRequestProvider.test.ts
@@ -484,7 +484,8 @@ describe("optional activity reads", () => {
listNotes: () => Effect.succeed({ comments: [comment], truncated: false }),
listCommits: () => Effect.succeed([commit]),
listDiscussions: () => Effect.succeed({ threads: [], truncated: false }),
- listReactions: () => Effect.succeed({ reactions: [], reactionsByNoteId: new Map() }),
+ listReactions: () =>
+ Effect.succeed({ reactions: [], reactionsByNoteId: new Map(), botLogins: new Set<string>() }),
};
const context = {
operation: "execute" as const,
@@ -561,6 +562,55 @@ describe("optional activity reads", () => {
});
}),
);
+
+ it.effect("flags comments by bot authors as bots, in the conversation and in threads", () =>
+ Effect.gen(function* () {
+ const bot = { login: "project_1_bot_abc", name: "Review bot", avatarUrl: null };
+ const human = { login: "julius", name: null, avatarUrl: null };
+ const provider = yield* make.pipe(
+ Effect.provide(
+ Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
+ ...reads,
+ listNotes: () =>
+ Effect.succeed({
+ comments: [
+ { ...comment, author: bot },
+ { ...comment, id: "43", author: human },
+ ],
+ truncated: false,
+ }),
+ listDiscussions: () =>
+ Effect.succeed({
+ threads: [
+ {
+ id: "d1",
+ path: "a.ts",
+ line: 1,
+ side: "right" as const,
+ isResolved: false,
+ isOutdated: false,
+ comments: [
+ { id: "44", author: bot, body: "x", createdAt: comment.createdAt, url: null },
+ ],
+ },
+ ],
+ truncated: false,
+ }),
+ listReactions: () =>
+ Effect.succeed({
+ reactions: [],
+ reactionsByNoteId: new Map(),
+ botLogins: new Set([bot.login.toUpperCase()]),
+ }),
+ }),
+ ),
+ );
+
+ const activity = yield* provider.getChangeRequestActivity(reference);
+ expect(activity.comments.map((entry) => entry.author?.isBot)).toEqual([true, undefined]);
+ expect(activity.reviewThreads[0]?.comments[0]?.author?.isBot).toBe(true);
+ }),
+ );
});
describe("review decisions", () => {
diff --git a/packages/source-control-gitlab/src/server/GitLabPullRequestProvider.ts b/packages/source-control-gitlab/src/server/GitLabPullRequestProvider.ts
index 124ba3ce70f..82712a2c5ef 100644
--- a/packages/source-control-gitlab/src/server/GitLabPullRequestProvider.ts
+++ b/packages/source-control-gitlab/src/server/GitLabPullRequestProvider.ts
@@ -5,6 +5,7 @@ import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import type {
+ PullRequestActor,
PullRequestCapabilities,
PullRequestReaction,
PullRequestReviewDecision,
@@ -330,28 +331,38 @@ export const make = Effect.gen(function* () {
recoverRead(cli.listReactions(input), {
reactions: [] as ReadonlyArray<PullRequestReaction>,
reactionsByNoteId: new Map<string, ReadonlyArray<PullRequestReaction>>(),
+ botLogins: new Set<string>(),
}),
],
{ concurrency: 4 },
).pipe(
Effect.mapError(fail("getChangeRequestActivity")),
- Effect.map(([notes, commits, discussions, awards]): ProviderChangeRequestActivity => ({
- reactions: awards.reactions,
- comments: notes.comments.map((comment) => ({
- ...comment,
- reactions: awards.reactionsByNoteId.get(comment.id) ?? [],
- })),
- commentCount: notes.comments.length,
- commentsTruncated: notes.truncated || discussions.truncated,
- reviewThreads: discussions.threads.map((thread) => ({
- ...thread,
- comments: thread.comments.map((comment) => ({
+ Effect.map(([notes, commits, discussions, awards]): ProviderChangeRequestActivity => {
+ const botLogins = new Set([...awards.botLogins].map((login) => login.toLowerCase()));
+ const withBotFlag = (author: PullRequestActor | null) =>
+ author !== null && botLogins.has(author.login.toLowerCase())
+ ? { ...author, isBot: true }
+ : author;
+ return {
+ reactions: awards.reactions,
+ comments: notes.comments.map((comment) => ({
...comment,
+ author: withBotFlag(comment.author),
reactions: awards.reactionsByNoteId.get(comment.id) ?? [],
})),
- })),
- commits,
- })),
+ commentCount: notes.comments.length,
+ commentsTruncated: notes.truncated || discussions.truncated,
+ reviewThreads: discussions.threads.map((thread) => ({
+ ...thread,
+ comments: thread.comments.map((comment) => ({
+ ...comment,
+ author: withBotFlag(comment.author),
+ reactions: awards.reactionsByNoteId.get(comment.id) ?? [],
+ })),
+ })),
+ commits,
+ };
+ }),
),
// Straight from the host, never the shared read: this is what a write is refused or let
diff --git a/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.test.ts b/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.test.ts
index b1762b6936d..7bec611ce07 100644
--- a/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.test.ts
+++ b/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.test.ts
@@ -496,7 +496,7 @@ describe("merge request viewer fields", () => {
});
describe("decodeAwardEmojiJson", () => {
- it("reads MR-level awards, keys per-note awards by the REST id inside their gid, and ignores an award outside the eight", () => {
+ it("reads MR-level awards, keys per-note awards by the REST id inside their gid, ignores an award outside the eight, and collects bot note authors", () => {
const result = expectSuccess(
decodeAwardEmojiJson(
JSON.stringify({
@@ -515,10 +515,12 @@ describe("decodeAwardEmojiJson", () => {
nodes: [
{
id: "gid://gitlab/DiffNote/42",
+ author: { username: "julius", bot: false },
awardEmoji: { nodes: [{ name: "heart", user: { username: "julius" } }] },
},
{
id: "gid://gitlab/Note/7",
+ author: { username: "project_278964_bot_89f97e2c", bot: true },
awardEmoji: {
nodes: [{ name: "partyparrot", user: { username: "bilal" } }],
},
@@ -538,6 +540,7 @@ describe("decodeAwardEmojiJson", () => {
expect([...result.reactionsByNoteId]).toEqual([
["42", [{ content: "heart", count: 1, actors: ["julius"], viewerHasReacted: false }]],
]);
+ expect([...result.botLogins]).toEqual(["project_278964_bot_89f97e2c"]);
expect(result.nextCursor).toBeNull();
});
diff --git a/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.ts b/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.ts
index 8ce3616536e..3f492bf20ee 100644
--- a/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.ts
+++ b/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.ts
@@ -780,7 +780,7 @@ export const AWARD_EMOJI_GRAPHQL_QUERY = `query($fullPath: ID!, $iid: String!, $
awardEmoji { nodes { name user { username } } }
notes(first: 100, after: $cursor) {
pageInfo { hasNextPage endCursor }
- nodes { id awardEmoji { nodes { name user { username } } } }
+ nodes { id author { username bot } awardEmoji { nodes { name user { username } } } }
}
}
}
@@ -832,6 +832,15 @@ const RawAwardEmojiPageSchema = Schema.Struct({
Schema.NullOr(
Schema.Struct({
id: Schema.optional(Schema.NullOr(Schema.String)),
+ // REST note authors carry no bot flag; GraphQL's does.
+ author: Schema.optional(
+ Schema.NullOr(
+ Schema.Struct({
+ username: Schema.optional(Schema.NullOr(Schema.String)),
+ bot: Schema.optional(Schema.NullOr(Schema.Boolean)),
+ }),
+ ),
+ ),
awardEmoji: RawAwardEmojiNodesSchema,
}),
),
@@ -886,6 +895,7 @@ function noteIdOf(gid: string | null | undefined): string | null {
export interface GitLabAwardEmojiPage {
readonly reactions: ReadonlyArray<PullRequestReaction>;
readonly reactionsByNoteId: ReadonlyMap<string, ReadonlyArray<PullRequestReaction>>;
+ readonly botLogins: ReadonlySet<string>;
readonly nextCursor: string | null;
}
@@ -900,7 +910,10 @@ export function decodeAwardEmojiJson(
const viewer = trimmed(data.currentUser?.username);
const mergeRequest = data.project?.mergeRequest;
const reactionsByNoteId = new Map<string, ReadonlyArray<PullRequestReaction>>();
+ const botLogins = new Set<string>();
for (const node of mergeRequest?.notes?.nodes ?? []) {
+ const author = trimmed(node?.author?.username);
+ if (author !== null && node?.author?.bot === true) botLogins.add(author);
const id = noteIdOf(node?.id);
if (id === null) continue;
const reactions = toReactions(node?.awardEmoji, viewer);
@@ -910,6 +923,7 @@ export function decodeAwardEmojiJson(
return Result.succeed({
reactions: toReactions(mergeRequest?.awardEmoji, viewer),
reactionsByNoteId,
+ botLogins,
nextCursor: pageInfo?.hasNextPage === true ? (trimmed(pageInfo.endCursor) ?? null) : null,
});
}