diff --git a/lib/modules/repository.js b/lib/modules/repository.js index 8e0d7f03..1c8c9a81 100644 --- a/lib/modules/repository.js +++ b/lib/modules/repository.js @@ -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; } @@ -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; diff --git a/lib/setup-repository.js b/lib/setup-repository.js index 63f924f5..71ea0be6 100644 --- a/lib/setup-repository.js +++ b/lib/setup-repository.js @@ -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; diff --git a/test/unit/setup-repository.test.js b/test/unit/setup-repository.test.js new file mode 100644 index 00000000..fd8d1a69 --- /dev/null +++ b/test/unit/setup-repository.test.js @@ -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); + }); +});