Skip to content

Commit

Permalink
feat(store-bitbucket): check if file exists before creating
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Oct 8, 2024
1 parent fc24e1c commit e394e4a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
27 changes: 27 additions & 0 deletions packages/store-bitbucket/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,28 @@ export default class BitbucketStore {
});
}

/**
* Check if file exists
* @param {string} filePath - Path to file
* @returns {Promise<boolean>} 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
Expand All @@ -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,
Expand Down
37 changes: 37 additions & 0 deletions packages/store-bitbucket/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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")
Expand Down

0 comments on commit e394e4a

Please sign in to comment.