Skip to content

Commit

Permalink
feat: add get latest commit hash function (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasyl-ivanchuk authored Sep 12, 2024
1 parent eec623d commit 8f54c9b
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions src/utils/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,39 @@ export const cloneRepo = async (repoUrl: string, destination: string, options?:
await executeCommand(command, options);
};

export const getLatestReleaseVersion = async (repo: string): Promise<string> => {
const apiUrl = `https://api.github.com/repos/${repo}/releases/latest`;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const gitApiRequest = async (url: string): Promise<any> => {
try {
const response = await fetch(apiUrl);
const response = await fetch(url);
if (!response.ok) {
throw new Error(`GitHub API request failed with status: ${response.status}`);
}
const releaseInfo = await response.json();
if (typeof releaseInfo?.tag_name !== "string") {
throw new Error(`Failed to parse the latest release version: ${JSON.stringify(releaseInfo)}`);
}
return releaseInfo.tag_name;
return await response.json();
} catch (error) {
if (error instanceof Error) {
throw new Error(`Failed to fetch the latest release version: ${error.message}`);
throw new Error(`Failed to make the GitHub API request: ${error.message}`);
}
throw error;
}
};

export const getLatestReleaseVersion = async (repo: string): Promise<string> => {
const releaseInfo = await gitApiRequest(`https://api.github.com/repos/${repo}/releases/latest`);
if (typeof releaseInfo?.tag_name !== "string") {
throw new Error(`Failed to parse the latest release version: ${JSON.stringify(releaseInfo)}`);
}
return releaseInfo.tag_name;
};

export const getLatestCommitHash = async (repo: string): Promise<string> => {
const commitsInfo = await gitApiRequest(`https://api.github.com/repos/${repo}/commits?per_page=1`);
if (!commitsInfo?.length) {
throw new Error(
`Unable to get the latest commit hash. Latest commit not found. The response: ${JSON.stringify(commitsInfo)}`
);
}
if (typeof commitsInfo[0].sha !== "string") {
throw new Error(`Failed to parse the latest commit hash: ${JSON.stringify(commitsInfo)}`);
}
return commitsInfo[0].sha;
};

0 comments on commit 8f54c9b

Please sign in to comment.