Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions spec/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,11 @@ const MOCK_PR = {
],
};

const BOT_USER_NAME = 'trop[bot]';

describe('trop', () => {
let robot: Probot;
process.env = { ...process.env, BOT_USER_NAME: 'trop[bot]' };
process.env = { ...process.env, BOT_USER_NAME };

beforeEach(() => {
vi.clearAllMocks();
Expand Down Expand Up @@ -343,12 +345,37 @@ describe('trop', () => {
nock(GH_API)
.persist()
.get('/repos/codebytere/public-repo/pulls/1234')
.reply(200, MOCK_PR);
.reply(200, { ...MOCK_PR, user: { login: BOT_USER_NAME } });

await robot.receive(issueCommentUpdateBranchCreatedEvent);

expect(updatePRBranch).toHaveBeenCalled();
});

it('does not trigger a branch update on `/trop update-branch` comment when BOT_USER_NAME is not the author', async () => {
nock(GH_API)
.persist()
.get('/repos/codebytere/public-repo/pulls/1234')
.reply(200, { ...MOCK_PR, user: { login: 'someone-else' } });

let comment: string | undefined;
nock(GH_API)
.post(
'/repos/codebytere/public-repo/issues/1234/comments',
({ body }) => {
comment = body;
return true;
},
)
.reply(200);

await robot.receive(issueCommentUpdateBranchCreatedEvent);

expect(updatePRBranch).not.toHaveBeenCalled();
expect(comment).toEqual(
'This PR was not created by trop and cannot be updated via this command.',
);
});
});

describe('pull_request.opened event', () => {
Expand Down Expand Up @@ -968,7 +995,7 @@ Notes: <!-- One-line Change Summary Here-->`,
state: 'closed',
title: 'mirror',
user: {
login: 'trop[bot]',
login: BOT_USER_NAME,
},
};

Expand Down Expand Up @@ -1013,7 +1040,7 @@ Notes: <!-- One-line Change Summary Here-->`,
state: 'closed',
title: 'mirror',
user: {
login: 'trop[bot]',
login: BOT_USER_NAME,
},
};

Expand Down
13 changes: 13 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,19 @@ const probotHandler: ApplicationFunction = async (robot, { getRouter }) => {
context.repo({ pull_number: issue.number }),
);

if (pr.user?.login !== getEnvVar('BOT_USER_NAME')) {
robot.log(
`#${issue.number} is not a trop backport PR - skipping update-branch`,
);
await context.octokit.issues.createComment(
context.repo({
issue_number: issue.number,
body: 'This PR was not created by trop and cannot be updated via this command.',
}),
);
return false;
}

await updatePRBranch(context, pr as WebHookPR);
return true;
},
Expand Down