@@ -29,22 +29,39 @@ export const cloneRepo = async (repoUrl: string, destination: string, options?:
29
29
await executeCommand ( command , options ) ;
30
30
} ;
31
31
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 > => {
34
34
try {
35
- const response = await fetch ( apiUrl ) ;
35
+ const response = await fetch ( url ) ;
36
36
if ( ! response . ok ) {
37
37
throw new Error ( `GitHub API request failed with status: ${ response . status } ` ) ;
38
38
}
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 ( ) ;
44
40
} catch ( error ) {
45
41
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 } ` ) ;
47
43
}
48
44
throw error ;
49
45
}
50
46
} ;
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