Skip to content

Commit

Permalink
fix(store-github): ignore create action if file already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Oct 6, 2024
1 parent bc2a677 commit e1138d5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 22 deletions.
14 changes: 14 additions & 0 deletions helpers/mock-agent/store-github.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ export function mockClient() {

const origin = "https://api.github.com";
const path = /\/repos\/user\/repo\/contents\/\D{3}\.md/;
const createNewResponse = {
content: {
path: "foobar.txt",
html_url: "https://github.com/user/repo/blob/main/foobar.txt",
},
commit: { message: "Message" },
};
const createResponse = {
content: {
path: "foo.txt",
Expand All @@ -34,6 +41,13 @@ export function mockClient() {
commit: { message: "Message" },
};

// Create new file
agent
.get(origin)
.intercept({ path: /.*foobar\.md/, method: "PUT" })
.reply(201, createNewResponse)
.persist();

// Create/update file
agent
.get(origin)
Expand Down
49 changes: 28 additions & 21 deletions packages/store-github/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,28 +124,35 @@ export default class GithubStore {

let createResponse;
try {
debug(`Try creating file ${filePath} in repo ${repo}, branch ${branch}`);
createResponse = await this.#client(filePath, "PUT", {
branch,
content: Buffer.from(content).toString("base64"),
message,
});
debug(`Created file ${filePath}`);
} catch (error) {
const message = crudErrorMessage({
error,
operation: "create",
filePath,
repo,
branch,
});
debug(message);
throw new Error(message);
debug(`Try reading file ${filePath} in repo ${repo}, branch ${branch}`);
await this.#client(`${filePath}?ref=${branch}`);
} catch {
try {
debug(
`Try creating file ${filePath} in repo ${repo}, branch ${branch}`,
);
createResponse = await this.#client(filePath, "PUT", {
branch,
content: Buffer.from(content).toString("base64"),
message,
});
debug(`Creating file ${filePath}`);

const file = await createResponse.json();

return file.content.html_url;
} catch (error) {
const message = crudErrorMessage({
error,
operation: "create",
filePath,
repo,
branch,
});
debug(message);
throw new Error(message);
}
}

const file = await createResponse.json();

return file.content.html_url;
}

/**
Expand Down
10 changes: 9 additions & 1 deletion packages/store-github/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,19 @@ describe("store-github", async () => {
});

it("Creates file", async () => {
const result = await github.createFile("foobar.md", "foobar", {
message: "Message",
});

assert.equal(result, "https://github.com/user/repo/blob/main/foobar.txt");
});

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

assert.equal(result, "https://github.com/user/repo/blob/main/foo.txt");
assert.equal(result, undefined);
});

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

0 comments on commit e1138d5

Please sign in to comment.