Skip to content

Commit 8f54c9b

Browse files
feat: add get latest commit hash function (#171)
1 parent eec623d commit 8f54c9b

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

src/utils/git.ts

+26-9
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,39 @@ export const cloneRepo = async (repoUrl: string, destination: string, options?:
2929
await executeCommand(command, options);
3030
};
3131

32-
export const getLatestReleaseVersion = async (repo: string): Promise<string> => {
33-
const apiUrl = `https://api.github.com/repos/${repo}/releases/latest`;
32+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
33+
const gitApiRequest = async (url: string): Promise<any> => {
3434
try {
35-
const response = await fetch(apiUrl);
35+
const response = await fetch(url);
3636
if (!response.ok) {
3737
throw new Error(`GitHub API request failed with status: ${response.status}`);
3838
}
39-
const releaseInfo = await response.json();
40-
if (typeof releaseInfo?.tag_name !== "string") {
41-
throw new Error(`Failed to parse the latest release version: ${JSON.stringify(releaseInfo)}`);
42-
}
43-
return releaseInfo.tag_name;
39+
return await response.json();
4440
} catch (error) {
4541
if (error instanceof Error) {
46-
throw new Error(`Failed to fetch the latest release version: ${error.message}`);
42+
throw new Error(`Failed to make the GitHub API request: ${error.message}`);
4743
}
4844
throw error;
4945
}
5046
};
47+
48+
export const getLatestReleaseVersion = async (repo: string): Promise<string> => {
49+
const releaseInfo = await gitApiRequest(`https://api.github.com/repos/${repo}/releases/latest`);
50+
if (typeof releaseInfo?.tag_name !== "string") {
51+
throw new Error(`Failed to parse the latest release version: ${JSON.stringify(releaseInfo)}`);
52+
}
53+
return releaseInfo.tag_name;
54+
};
55+
56+
export const getLatestCommitHash = async (repo: string): Promise<string> => {
57+
const commitsInfo = await gitApiRequest(`https://api.github.com/repos/${repo}/commits?per_page=1`);
58+
if (!commitsInfo?.length) {
59+
throw new Error(
60+
`Unable to get the latest commit hash. Latest commit not found. The response: ${JSON.stringify(commitsInfo)}`
61+
);
62+
}
63+
if (typeof commitsInfo[0].sha !== "string") {
64+
throw new Error(`Failed to parse the latest commit hash: ${JSON.stringify(commitsInfo)}`);
65+
}
66+
return commitsInfo[0].sha;
67+
};

0 commit comments

Comments
 (0)