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
36 changes: 32 additions & 4 deletions lib/modules/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,23 @@ class Repository {
await Promise.all(createOrUpdateFilesMultiple);
}

async getPullRequestsBy({ branchName, state }) {
try {
return await this.octokit.pulls.list({
owner: this.owner,
repo: this.repo,
state,
head: `${this.owner}:${branchName}`,
});
} catch(error) {
return { data: [] }
}
}

async getPullRequestURL({ branchName }) {
const results = await this.octokit.pulls.list({
owner: this.owner,
repo: this.repo,
const results = await this.getPullRequestsBy({
branchName,
state: "open",
head: `${this.owner}:${branchName}`,
});
return results.data[0].html_url;
}
Expand Down Expand Up @@ -259,6 +270,23 @@ class Repository {
branchName,
});
}

async deleteBranch({branchName}) {
try {
await this.octokit.git.deleteRef({
owner: this.owner,
repo: this.repo,
ref: `heads/${branchName}`
});
} catch (error) {
// /* istanbul ignore if */
// if (error.status !== 404) {
// throw error;
// }

// throw new BranchNotFoundError(branchName);
}
}
}

module.exports = Repository;
17 changes: 17 additions & 0 deletions lib/setup-repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,28 @@ const { BranchNotFoundError } = require("./modules/errors");
async function setupRepository({ context, branchName }) {
const repository = new Repository(context);

const hasOpenPullRequest = async ({branchName}) => {
const result = await repository.getPullRequestsBy({
branchName,
state: "open"
});

// TODO: Possibly make sure the existing pull request is created by the bot?
return result.data.length > 0;
};

try {
await repository.getRef(branchName);
const openPrExists = await hasOpenPullRequest(branchName)

context.log.debug(
`Branch "${branchName}" exists, will work from this branch`
);

if (!openPrExists) {
repository.deleteBranch(branchName);
}

repository.setBaseBranch(branchName);
} catch (error) {
const isBranchNotFoundError = error instanceof BranchNotFoundError;
Expand Down
57 changes: 57 additions & 0 deletions test/unit/setup-repository.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const Repository = require("../../lib/modules/repository");
const { BranchNotFoundError } = require("../../lib/modules/errors");
const setupRepository = require("../../lib/setup-repository");

jest.mock("../../lib/modules/repository");

describe("Setup Repository", () => {
const mockBranchName = "all-contributors/add-hertzg-tenshiAMD";
const mockContext = {
log: {
debug() {},
},
};

afterEach(() => jest.clearAllMocks());

it("should base off latest on first time request", async () => {
Repository.prototype.getRef.mockReturnValue(
Promise.reject(new BranchNotFoundError(mockBranchName))
);

await setupRepository({ branchName: mockBranchName, context: mockContext });

expect(Repository.prototype.getRef).toBeCalledWith(mockBranchName);
expect(Repository.prototype.deleteBranch).not.toBeCalled();
expect(Repository.prototype.getPullRequestsBy).not.toBeCalled();
expect(Repository.prototype.setBaseBranch).not.toBeCalled();
});

it("should delete existing dangling (already merged) branches then recreate new pr", async () => {
Repository.prototype.getRef.mockReturnValue(Promise.resolve());
Repository.prototype.getPullRequestsBy.mockReturnValue(
Promise.resolve({ data: [] })
);

await setupRepository({ branchName: mockBranchName, context: mockContext });

expect(Repository.prototype.getRef).toBeCalledWith(mockBranchName);
expect(Repository.prototype.deleteBranch).toBeCalledTimes(1);
expect(Repository.prototype.setBaseBranch).toBeCalledTimes(1);
expect(Repository.prototype.getPullRequestsBy).toBeCalledTimes(1);
});

it("should use existing branch only if pr for it was not merged yet", async () => {
Repository.prototype.getRef.mockReturnValue(Promise.resolve());
Repository.prototype.getPullRequestsBy.mockReturnValue(
Promise.resolve({ data: [{}] })
);

await setupRepository({ branchName: mockBranchName, context: mockContext });

expect(Repository.prototype.getRef).toBeCalledWith(mockBranchName);
expect(Repository.prototype.deleteBranch).not.toBeCalled();
expect(Repository.prototype.getPullRequestsBy).toBeCalledTimes(1);
expect(Repository.prototype.setBaseBranch).toBeCalledTimes(1);
});
});