Skip to content

Commit

Permalink
feat(create-pr): update pr with base branch first (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
danadajian authored Jun 15, 2022
1 parent 67c258c commit 4d2506b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 17 deletions.
18 changes: 8 additions & 10 deletions dist/150.index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/150.index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 14 additions & 5 deletions src/helpers/create-pr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,30 @@ export class CreatePR extends HelperInputs {

export const createPr = async ({ title, body, head = context.ref.replace('refs/heads/', ''), base }: CreatePR) => {
const pr_base = base || (await getDefaultBranch());
const result = await octokit.pulls.create({
await updateHeadWithBaseBranch(pr_base, head);
const {
data: { number }
} = await octokit.pulls.create({
title,
head,
base: pr_base,
body,
maintainer_can_modify: true,
...context.repo
});
const pullNumber = result?.data?.number;
return pullNumber;
return number;
};

async function getDefaultBranch() {
const updateHeadWithBaseBranch = (base: string, head: string) =>
octokit.repos.merge({
base: head,
head: base,
...context.repo
});

const getDefaultBranch = async () => {
const {
data: { default_branch }
} = await octokit.repos.get({ ...context.repo });
return default_branch;
}
};
14 changes: 13 additions & 1 deletion test/helpers/create-pr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jest.mock('@actions/github', () => ({
context: { repo: { repo: 'repo', owner: 'owner' }, ref: 'refs/heads/source' },
getOctokit: jest.fn(() => ({
rest: {
repos: { get: jest.fn() },
repos: { get: jest.fn(), merge: jest.fn() },
pulls: { create: jest.fn() }
}
}))
Expand Down Expand Up @@ -50,6 +50,18 @@ describe('createPr', () => {
expect(octokit.repos.get).toHaveBeenCalledWith({ ...context.repo });
});

it('should call repos merge with correct params', async () => {
await createPr({
title,
body
});
expect(octokit.repos.merge).toHaveBeenCalledWith({
base: 'source',
head: 'default branch',
...context.repo
});
});

it('should call create with correct params', async () => {
await createPr({
title,
Expand Down

0 comments on commit 4d2506b

Please sign in to comment.