Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check for conflicting backport labels on PR #284

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
test: update tests for new helper function
alicelovescake committed Apr 21, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 5b3b97ea6f37abec62d592a646c52e6cf346e244
3 changes: 2 additions & 1 deletion spec/operations.spec.ts
Original file line number Diff line number Diff line change
@@ -22,8 +22,9 @@ const backportPRMergedEvent = require('./fixtures/backport_pull_request.merged.j
const backportPROpenedEvent = require('./fixtures/backport_pull_request.opened.json');

jest.mock('../src/utils', () => ({
tagBackportReviewers: jest.fn().mockReturnValue(Promise.resolve()),
tagBackportReviewers: jest.fn(),
isSemverMinorPR: jest.fn().mockReturnValue(false),
handleSemverMinorBackportLabel: jest.fn(),
}));

jest.mock('../src/utils/label-utils', () => ({
66 changes: 64 additions & 2 deletions spec/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import * as logUtils from '../src/utils/log-util';
import { BACKPORT_REQUESTED_LABEL } from '../src/constants';
import { LogLevel } from '../src/enums';
import { tagBackportReviewers } from '../src/utils';
import {
handleSemverMinorBackportLabel,
tagBackportReviewers,
} from '../src/utils';
import * as utils from '../src/utils';
import * as labelUtils from '../src/utils/label-utils';
import * as logUtils from '../src/utils/log-util';

const backportPROpenedEvent = require('./fixtures/backport_pull_request.opened.json');

@@ -74,4 +80,60 @@ describe('utils', () => {
);
});
});

describe('handleSemverMinorBackportLabel()', () => {
const context = {
octokit: {},
repo: {},
...backportPROpenedEvent,
};
const pr = context.payload.pull_request;
let labelsToAdd: string[];

beforeEach(() => {
labelsToAdd = [];
});

it('should add BACKPORT_REQUESTED_LABEL if PR is semver minor and not already approved', async () => {
jest.spyOn(labelUtils, 'labelExistsOnPR').mockResolvedValue(false);
jest.spyOn(utils, 'isSemverMinorPR').mockResolvedValue(true);

await handleSemverMinorBackportLabel(
context,
pr,
labelsToAdd,
'testFunction',
);
expect(labelsToAdd).toContain(BACKPORT_REQUESTED_LABEL);
});

it('should not add BACKPORT_REQUESTED_LABEL if PR is not semver minor', async () => {
jest.spyOn(utils, 'isSemverMinorPR').mockResolvedValue(false);

await handleSemverMinorBackportLabel(
context,
pr,
labelsToAdd,
'testFunction',
);

expect(labelsToAdd).not.toContain(BACKPORT_REQUESTED_LABEL);
});

it('should not add BACKPORT_REQUESTED_LABEL if PR is already approved', async () => {
jest.spyOn(utils, 'isSemverMinorPR').mockResolvedValue(true);

// Mocking labelExistsOnPR to return true (indicating backport is already approved)
jest.spyOn(labelUtils, 'labelExistsOnPR').mockResolvedValue(true);

await handleSemverMinorBackportLabel(
context,
pr,
labelsToAdd,
'testFunction',
);

expect(labelsToAdd).not.toContain(BACKPORT_REQUESTED_LABEL);
});
});
});
22 changes: 8 additions & 14 deletions src/operations/update-manual-backport.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import {
BACKPORT_LABEL,
BACKPORT_REQUESTED_LABEL,
SKIP_CHECK_LABEL,
} from '../constants';
import { BACKPORT_LABEL, SKIP_CHECK_LABEL } from '../constants';
import { PRChange, PRStatus, LogLevel } from '../enums';
import { WebHookPRContext } from '../types';
import { isSemverMinorPR, tagBackportReviewers } from '../utils';
import { handleSemverMinorBackportLabel, tagBackportReviewers } from '../utils';
import * as labelUtils from '../utils/label-utils';
import { log } from '../utils/log-util';

@@ -98,14 +94,12 @@ export const updateManualBackport = async (
}
}

if (await isSemverMinorPR(context, pr)) {
log(
'updateManualBackport',
LogLevel.INFO,
`Determined that ${pr.number} is semver-minor`,
);
newPRLabelsToAdd.push(BACKPORT_REQUESTED_LABEL);
}
await handleSemverMinorBackportLabel(
context,
pr,
newPRLabelsToAdd,
'updateManualBackport',
);

// We should only comment if there is not a previous existing comment
const commentBody = `@${pr.user.login} has manually backported this PR to "${pr.base.ref}", \