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

feat: add get latest commit hash function #171

Merged
merged 1 commit into from
Sep 12, 2024
Merged
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
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;
};
Loading