Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #5831: [Bug]: "Request failed with status code 409" on opening empty repo #5833

Merged
merged 5 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions frontend/__tests__/api/github.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { describe, expect, it, vi } from "vitest";
import { retrieveLatestGitHubCommit } from "../../src/api/github";

describe("retrieveLatestGitHubCommit", () => {
const { githubGetMock } = vi.hoisted(() => ({
githubGetMock: vi.fn(),
}));

vi.mock("../../src/api/github-axios-instance", () => ({
github: {
get: githubGetMock,
},
}));

it("should return the latest commit when repository has commits", async () => {
const mockCommit = {
sha: "123abc",
commit: {
message: "Initial commit",
},
};

githubGetMock.mockResolvedValueOnce({
data: [mockCommit],
});

const result = await retrieveLatestGitHubCommit("user/repo");
expect(result).toEqual(mockCommit);
});

it("should return null when repository is empty", async () => {
const error = new Error("Repository is empty");
(error as any).response = { status: 409 };
githubGetMock.mockRejectedValueOnce(error);

const result = await retrieveLatestGitHubCommit("user/empty-repo");
expect(result).toBeNull();
});

it("should throw error for other error cases", async () => {
const error = new Error("Network error");
(error as any).response = { status: 500 };
githubGetMock.mockRejectedValueOnce(error);

await expect(retrieveLatestGitHubCommit("user/repo")).rejects.toThrow();
});
});
33 changes: 23 additions & 10 deletions frontend/src/api/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,28 @@ export const retrieveGitHubUser = async () => {

export const retrieveLatestGitHubCommit = async (
repository: string,
): Promise<GitHubCommit> => {
const response = await github.get<GitHubCommit[]>(
`/repos/${repository}/commits`,
{
params: {
per_page: 1,
): Promise<GitHubCommit | null> => {
try {
const response = await github.get<GitHubCommit[]>(
`/repos/${repository}/commits`,
{
params: {
per_page: 1,
},
},
},
);

return response.data[0];
);
return response.data[0] || null;
} catch (error) {
if (!error || typeof error !== "object") {
throw new Error("Unknown error occurred");
}
const axiosError = error as { response?: { status: number } };
if (axiosError.response?.status === 409) {
// Repository is empty, no commits yet
return null;
}
throw new Error(
error instanceof Error ? error.message : "Unknown error occurred",
);
}
};
1 change: 1 addition & 0 deletions frontend/src/services/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export const saveSettings = async (
const { data } = await openHands.post("/api/settings", apiSettings);
return data;
} catch (error) {
// Error handled by returning false
return false;
}
};
Expand Down
Loading