Skip to content

Commit

Permalink
fix(store-gitlab): 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 b4c88ce commit 101637b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
29 changes: 24 additions & 5 deletions helpers/mock-agent/store-gitlab.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export function mockClient() {
// Create file
agent
.get(origin)
.intercept({ path: filePath, method: "POST" })
.intercept({ path: /.*new\.txt/, method: "POST" })
.reply(
201,
{ file_path: "foo.txt", branch: "main" },
{ file_path: "new.txt", branch: "main" },
{ headers: { "content-type": "application/json" } },
)
.persist();
Expand All @@ -29,18 +29,37 @@ export function mockClient() {
.intercept({ path: /.*401\.txt/, method: "POST" })
.reply(401);

// Read file
agent
.get(origin)
.intercept({ path: /.*(foo|bar).txt\?ref=main/ })
.reply(
200,
{ content: "Zm9v" }, // ‘foo’ Base64 encoded
{ headers: { "content-type": "application/json" } },
)
.persist();

// Read raw file
agent
.get(origin)
.intercept({ path: /.*\D{3}.txt\/raw\?ref=main/ })
.reply(200, "foobar");
.intercept({ path: /.*(foo|bar).txt\/raw\?ref=main/ })
.reply(200, "foo")
.persist();

// Read raw file (Unauthorized)
agent
.get(origin)
.intercept({ path: /.*401\.txt\/raw\?ref=main/ })
.reply(401);

// Read raw file (Not Found)
agent
.get(origin)
.intercept({ path: /.*404\.txt/ })
.reply(404)
.persist();

// Update file
agent
.get(origin)
Expand All @@ -65,7 +84,7 @@ export function mockClient() {
// Update commit
agent
.get(origin)
.intercept({ path: commitPath, method: "POST", body: /.*\D{3}.txt/ })
.intercept({ path: commitPath, method: "POST", body: /.*(foo|bar).txt/ })
.reply(
200,
{ file_path: "foo.txt", message: "message" },
Expand Down
25 changes: 25 additions & 0 deletions packages/store-gitlab/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ export default class GitlabStore {
return client;
}

/**
* Check if file exists
* @param {string} filePath - Path to file
* @returns {Promise<boolean>} File exists
* @see {@link https://docs.gitlab.com/ee/api/repository_files.html#get-file-from-repository}
*/
async fileExists(filePath) {
try {
await this.#client.RepositoryFiles.show(
this.projectId,
filePath,
this.options.branch,
);

return true;
} catch {
return false;
}
}

/**
* Create file
* @param {string} filePath - Path to file
Expand All @@ -96,6 +116,11 @@ export default class GitlabStore {
*/
async createFile(filePath, content, { message }) {
try {
const fileExists = await this.fileExists(filePath);
if (fileExists) {
return;
}

const createResponse = await this.#client.RepositoryFiles.create(
this.projectId,
filePath,
Expand Down
23 changes: 18 additions & 5 deletions packages/store-gitlab/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,33 @@ describe("store-gitlab", async () => {
);
});

it("Creates file", { timeout: 60 }, async () => {
it("Checks if file exists", async () => {
assert.equal(await gitlab.fileExists("foo.txt"), true);
assert.equal(await gitlab.fileExists("404.txt"), false);
});

it("Creates file", async () => {
const result = await gitlab.createFile("new.txt", "new", {
message: "Message",
});

assert.equal(result, "https://gitlab.com/username/repo/new.txt");
});

it("Doesn’t create file if already exists", async () => {
const result = await gitlab.createFile("foo.txt", "foo", {
message: "Message",
});

assert.equal(result, "https://gitlab.com/username/repo/foo.txt");
assert.equal(result, undefined);
});

it("Creates file with projectId at custom instance", async () => {
const result = await gitlabInstance.createFile("foo.txt", "foo", {
const result = await gitlabInstance.createFile("new.txt", "new", {
message: "Message",
});

assert.equal(result, "https://gitlab.instance/projects/1234/foo.txt");
assert.equal(result, "https://gitlab.instance/projects/1234/new.txt");
});

it("Throws error creating file", async () => {
Expand All @@ -77,7 +90,7 @@ describe("store-gitlab", async () => {
it("Reads file", async () => {
const result = await gitlab.readFile("foo.txt");

assert.equal(result, "foobar");
assert.equal(result, "foo");
});

it("Throws error reading file", async () => {
Expand Down

0 comments on commit 101637b

Please sign in to comment.