Skip to content

Commit e394e4a

Browse files
feat(store-bitbucket): check if file exists before creating
1 parent fc24e1c commit e394e4a

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

packages/store-bitbucket/index.js

+27
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,28 @@ export default class BitbucketStore {
7575
});
7676
}
7777

78+
/**
79+
* Check if file exists
80+
* @param {string} filePath - Path to file
81+
* @returns {Promise<boolean>} File exists
82+
* @see {@link https://bitbucketjs.netlify.app/#api-repositories-repositories_readSrc}
83+
*/
84+
async fileExists(filePath) {
85+
try {
86+
await this.#client.repositories.readSrc({
87+
format: "meta",
88+
commit: this.options.branch,
89+
path: filePath,
90+
repo_slug: this.options.repo,
91+
workspace: this.options.user,
92+
});
93+
94+
return true;
95+
} catch {
96+
return false;
97+
}
98+
}
99+
78100
/**
79101
* Create file
80102
* @param {string} filePath - Path to file
@@ -86,6 +108,11 @@ export default class BitbucketStore {
86108
*/
87109
async createFile(filePath, content, { message }) {
88110
try {
111+
const fileExists = await this.fileExists(filePath);
112+
if (fileExists) {
113+
return;
114+
}
115+
89116
await this.#client.repositories.createSrcFileCommit({
90117
[filePath]: content,
91118
branch: this.options.branch,

packages/store-bitbucket/test/index.js

+37
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ describe("store-bitbucket", () => {
4646
);
4747
});
4848

49+
it("Checks if file exists", async () => {
50+
nock(bitbucketUrl)
51+
.get("/2.0/repositories/username/repo/src/main/foo.txt")
52+
.query({ format: "meta" })
53+
.reply(201, { path: "foo.txt", type: "meta" });
54+
nock(bitbucketUrl)
55+
.post("/2.0/repositories/username/repo/src/main/bar.txt")
56+
.query({ format: "meta" })
57+
.replyWithError("Not found");
58+
59+
assert.equal(await bitbucket.fileExists("foo.txt"), true);
60+
assert.equal(await bitbucket.fileExists("bar.txt"), false);
61+
});
62+
4963
it("Creates file", async () => {
5064
nock(bitbucketUrl).post("/2.0/repositories/username/repo/src").reply(201, {
5165
"content-type": "application/json",
@@ -58,6 +72,29 @@ describe("store-bitbucket", () => {
5872
assert.equal(result, "https://bitbucket.org/username/repo/foo.txt");
5973
});
6074

75+
it("Doesn’t create file if already exists", async () => {
76+
nock(bitbucketUrl).post("/2.0/repositories/username/repo/src").reply(201, {
77+
"content-type": "application/json",
78+
});
79+
80+
// Create file
81+
await bitbucket.createFile("foo.txt", "foo", {
82+
message: "Message",
83+
});
84+
85+
nock(bitbucketUrl)
86+
.get("/2.0/repositories/username/repo/src/main/foo.txt")
87+
.query({ format: "meta" })
88+
.reply(201, { path: "foo.txt", type: "meta" });
89+
90+
// Create file a second time
91+
const result = await bitbucket.createFile("foo.txt", "foo", {
92+
message: "Message",
93+
});
94+
95+
assert.equal(result, undefined);
96+
});
97+
6198
it("Throws error creating file", async () => {
6299
nock(bitbucketUrl)
63100
.post("/2.0/repositories/username/repo/src")

0 commit comments

Comments
 (0)