@@ -3,6 +3,29 @@ import * as semver from 'semver';
33import * as core from '@actions/core' ;
44import * as httpm from '@actions/http-client' ;
55
6+ const maxRetries = 10 ;
7+ const timeoutMs = 1000 ;
8+ const withRetry = async < T > ( operation : ( ) => Promise < T > ) : Promise < T > => {
9+ let lastError : Error ;
10+
11+ for ( let attempt = 0 ; attempt <= maxRetries ; attempt ++ ) {
12+ try {
13+ return await operation ( ) ;
14+ } catch ( error ) {
15+ lastError = error as Error ;
16+
17+ if ( attempt === maxRetries ) {
18+ break ;
19+ }
20+
21+ core . debug ( `Attempt ${ attempt + 1 } failed, retrying in ${ timeoutMs } : ${ lastError . message } ` ) ;
22+ await new Promise ( resolve => setTimeout ( resolve , timeoutMs ) ) ;
23+ }
24+ }
25+
26+ throw lastError ;
27+ } ;
28+
629export interface GitHubRelease {
730 tag_name : string ;
831}
@@ -37,14 +60,20 @@ export const getReleaseTag = async (distribution: string, version: string): Prom
3760 const tag : string = ( await resolveVersion ( distribution , version ) ) || version ;
3861 const suffix : string = goreleaser . distribSuffix ( distribution ) ;
3962 const url = `https://goreleaser.com/static/releases${ suffix } .json` ;
40- const http : httpm . HttpClient = new httpm . HttpClient ( 'goreleaser-action' ) ;
41- const resp : httpm . HttpClientResponse = await http . get ( url ) ;
42- const body = await resp . readBody ( ) ;
43- const statusCode = resp . message . statusCode || 500 ;
44- if ( statusCode >= 400 ) {
45- throw new Error ( `Failed to get GoReleaser release ${ version } from ${ url } with status code ${ statusCode } : ${ body } ` ) ;
46- }
47- const releases = < Array < GitHubRelease > > JSON . parse ( body ) ;
63+
64+ const releases = await withRetry ( async ( ) => {
65+ const http : httpm . HttpClient = new httpm . HttpClient ( 'goreleaser-action' ) ;
66+ const resp : httpm . HttpClientResponse = await http . get ( url ) ;
67+ const body = await resp . readBody ( ) ;
68+ const statusCode = resp . message . statusCode || 500 ;
69+ if ( statusCode >= 400 ) {
70+ throw new Error (
71+ `Failed to get GoReleaser release ${ version } from ${ url } with status code ${ statusCode } : ${ body } `
72+ ) ;
73+ }
74+ return < Array < GitHubRelease > > JSON . parse ( body ) ;
75+ } ) ;
76+
4877 const res = releases . filter ( r => r . tag_name === tag ) . shift ( ) ;
4978 if ( res ) {
5079 return res ;
@@ -78,12 +107,13 @@ interface GitHubTag {
78107}
79108
80109const getAllTags = async ( distribution : string ) : Promise < Array < string > > => {
81- const http : httpm . HttpClient = new httpm . HttpClient ( 'goreleaser-action' ) ;
82110 const suffix : string = goreleaser . distribSuffix ( distribution ) ;
83111 const url = `https://goreleaser.com/static/releases${ suffix } .json` ;
84112 core . debug ( `Downloading ${ url } ` ) ;
85- const getTags = http . getJson < Array < GitHubTag > > ( url ) ;
86- return getTags . then ( response => {
113+
114+ return withRetry ( async ( ) => {
115+ const http : httpm . HttpClient = new httpm . HttpClient ( 'goreleaser-action' ) ;
116+ const response = await http . getJson < Array < GitHubTag > > ( url ) ;
87117 if ( response . result == null ) {
88118 return [ ] ;
89119 }
0 commit comments