@@ -20,6 +20,7 @@ import { Identity } from '../models/interfaces';
2020import crypto from 'crypto' ;
2121import { IdentityModel } from '../models/IdentityModel' ;
2222import { StateLockRequest } from '../models/interfaces/StateLockRequest' ;
23+ import moment from 'moment' ;
2324// import seedrandom from 'seedrandom';
2425
2526export type IdentityWithToken = Identity & {
@@ -117,6 +118,7 @@ export class GithubService {
117118 workspace ?: string ,
118119 stateLockRequest ?: StateLockRequest ,
119120 ) : Promise < Identity > => {
121+ const now = moment ( ) ;
120122 const tokenSha = crypto . createHash ( 'sha256' ) . update ( auth ) . digest ( ) . toString ( 'base64' ) ;
121123
122124 console . log (
@@ -140,6 +142,16 @@ export class GithubService {
140142 return { ...storedIdentity . attrs , workspace : workspace || 'default' } ;
141143 }
142144
145+ if (
146+ storedIdentity &&
147+ ! stateLockRequest &&
148+ storedIdentity . attrs . meta . createdAt &&
149+ moment ( storedIdentity . attrs . meta . createdAt ) . add ( 1 , 'hour' ) . isAfter ( now )
150+ ) {
151+ console . log ( `Found previously known identity used within 1 hour (sha: ${ tokenSha } )` ) ;
152+ return { ...storedIdentity . attrs , workspace : workspace || 'default' } ;
153+ }
154+
143155 const octokit = new Octokit ( { auth } ) ;
144156
145157 // Server-to-server tokens from GH Actions are permitted on a single repository
@@ -176,6 +188,7 @@ export class GithubService {
176188 tokenSha,
177189 meta : {
178190 name,
191+ createdAt : now . toISOString ( ) ,
179192 } ,
180193 } ;
181194 }
@@ -197,6 +210,7 @@ export class GithubService {
197210 tokenSha,
198211 meta : {
199212 name,
213+ createdAt : now . toISOString ( ) ,
200214 } ,
201215 } ;
202216 }
@@ -218,6 +232,7 @@ export class GithubService {
218232 tokenSha,
219233 meta : {
220234 name,
235+ createdAt : now . toISOString ( ) ,
221236 } ,
222237 } ;
223238 }
0 commit comments