From e394e4a17aa9fa644af5b9ad7d10b5e7358c79e5 Mon Sep 17 00:00:00 2001 From: Paul Robert Lloyd Date: Sun, 6 Oct 2024 16:00:43 +0100 Subject: [PATCH] feat(store-bitbucket): check if file exists before creating --- packages/store-bitbucket/index.js | 27 +++++++++++++++++++ packages/store-bitbucket/test/index.js | 37 ++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/packages/store-bitbucket/index.js b/packages/store-bitbucket/index.js index 3afae3a91..7953f5eb8 100644 --- a/packages/store-bitbucket/index.js +++ b/packages/store-bitbucket/index.js @@ -75,6 +75,28 @@ export default class BitbucketStore { }); } + /** + * Check if file exists + * @param {string} filePath - Path to file + * @returns {Promise} File exists + * @see {@link https://bitbucketjs.netlify.app/#api-repositories-repositories_readSrc} + */ + async fileExists(filePath) { + try { + await this.#client.repositories.readSrc({ + format: "meta", + commit: this.options.branch, + path: filePath, + repo_slug: this.options.repo, + workspace: this.options.user, + }); + + return true; + } catch { + return false; + } + } + /** * Create file * @param {string} filePath - Path to file @@ -86,6 +108,11 @@ export default class BitbucketStore { */ async createFile(filePath, content, { message }) { try { + const fileExists = await this.fileExists(filePath); + if (fileExists) { + return; + } + await this.#client.repositories.createSrcFileCommit({ [filePath]: content, branch: this.options.branch, diff --git a/packages/store-bitbucket/test/index.js b/packages/store-bitbucket/test/index.js index 6e25c1e94..a9797447b 100644 --- a/packages/store-bitbucket/test/index.js +++ b/packages/store-bitbucket/test/index.js @@ -46,6 +46,20 @@ describe("store-bitbucket", () => { ); }); + it("Checks if file exists", async () => { + nock(bitbucketUrl) + .get("/2.0/repositories/username/repo/src/main/foo.txt") + .query({ format: "meta" }) + .reply(201, { path: "foo.txt", type: "meta" }); + nock(bitbucketUrl) + .post("/2.0/repositories/username/repo/src/main/bar.txt") + .query({ format: "meta" }) + .replyWithError("Not found"); + + assert.equal(await bitbucket.fileExists("foo.txt"), true); + assert.equal(await bitbucket.fileExists("bar.txt"), false); + }); + it("Creates file", async () => { nock(bitbucketUrl).post("/2.0/repositories/username/repo/src").reply(201, { "content-type": "application/json", @@ -58,6 +72,29 @@ describe("store-bitbucket", () => { assert.equal(result, "https://bitbucket.org/username/repo/foo.txt"); }); + it("Doesn’t create file if already exists", async () => { + nock(bitbucketUrl).post("/2.0/repositories/username/repo/src").reply(201, { + "content-type": "application/json", + }); + + // Create file + await bitbucket.createFile("foo.txt", "foo", { + message: "Message", + }); + + nock(bitbucketUrl) + .get("/2.0/repositories/username/repo/src/main/foo.txt") + .query({ format: "meta" }) + .reply(201, { path: "foo.txt", type: "meta" }); + + // Create file a second time + const result = await bitbucket.createFile("foo.txt", "foo", { + message: "Message", + }); + + assert.equal(result, undefined); + }); + it("Throws error creating file", async () => { nock(bitbucketUrl) .post("/2.0/repositories/username/repo/src")