Skip to content

Commit

Permalink
Refresh token every 50 minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
EnviousSwan committed Sep 8, 2024
1 parent d9619bf commit e2d6f01
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/action/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ async function run(): Promise<void> {
const files: Files = {...fs, ...io}
const inputs = Input.from(core, files, logger).all()
const gitHubApiUrl = inputs.github.apiUrl.value
const gitHubToken = await gitHubAppToken(inputs.github.app, gitHubApiUrl, 'installation') ?? inputs.github.token.value
const octokit = getOctokit(gitHubToken, {baseUrl: gitHubApiUrl})
const gitHubToken = async () => await gitHubAppToken(inputs.github.app, gitHubApiUrl, 'installation') ?? inputs.github.token.value
const octokit = getOctokit(await gitHubToken(), {baseUrl: gitHubApiUrl})
const github = GitHub.from(logger, octokit)
const workspace = Workspace.from(logger, files, os, cache)

Expand Down Expand Up @@ -114,7 +114,7 @@ async function gitHubAppToken(app: GitHubAppInfo | undefined, gitHubApiUrl: stri

const response = type === 'app'
? await auth({type: 'app'})
: (app.installation ? await auth({type: 'installation', installationId: app.installation.value}) : undefined)
: (app.installation ? await auth({type: 'installation', installationId: app.installation.value, refresh: true}) : undefined)

return response?.token
}
Expand Down
19 changes: 15 additions & 4 deletions src/modules/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ test.after(() => {
test('`Workspace.prepare()` → prepares the workspace', async t => {
const {workspace, calls} = fixture()

await workspace.prepare('- owner/repo1\n- owner/repo2', '123', undefined)
await workspace.prepare('- owner/repo1\n- owner/repo2', async () => '123', undefined)

const expected: string[] = [
'mkdirP("/home/scala-steward")',
Expand All @@ -80,7 +80,7 @@ test('`Workspace.prepare()` → prepares the workspace when using a GitHub App',
key: mandatory('this-is-the-key'),
}

await workspace.prepare('this will not be used', '123', gitHubAppInfo)
await workspace.prepare('this will not be used', async () => '123', gitHubAppInfo)

const expected: string[] = [
'mkdirP("/home/scala-steward")',
Expand All @@ -103,7 +103,7 @@ test('`Workspace.prepare()` → uses the repos input when GitHub App is "auth on
key: mandatory('this-is-the-key'),
}

await workspace.prepare('- owner/repo', '123', gitHubAppInfo)
await workspace.prepare('- owner/repo', async () => '123', gitHubAppInfo)

const expected: string[] = [
'mkdirP("/home/scala-steward")',
Expand All @@ -115,6 +115,18 @@ test('`Workspace.prepare()` → uses the repos input when GitHub App is "auth on
t.deepEqual(calls, expected)
})

test('`Workspace.writeAskPass()` → writes a token to the askpass.sh', async t => {
const {workspace, calls} = fixture()

await workspace.writeAskPass(async () => '123')

const expected: string[] = [
'writeFileSync("/home/scala-steward/askpass.sh", "#!/bin/sh\n\necho \'123\'")',
]

t.deepEqual(calls, expected)
})

test('`Workspace.remove()` → removes the workspace', async t => {
const {workspace, calls} = fixture()

Expand Down Expand Up @@ -189,4 +201,3 @@ test('`Workspace.saveWorkspaceCache()` → saves cache', async t => {

t.deepEqual(calls, expected)
})

19 changes: 17 additions & 2 deletions src/modules/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class Workspace {
* @param token The GitHub Token used to authenticate into GitHub.
* @param gitHubAppInfo The GitHub App information as provided by the user.
*/
async prepare(reposList: string, token: string, gitHubAppInfo: GitHubAppInfo | undefined): Promise<void> {
async prepare(reposList: string, token: () => Promise<string>, gitHubAppInfo: GitHubAppInfo | undefined): Promise<void> {
try {
await this.files.mkdirP(this.directory)

Expand All @@ -122,7 +122,12 @@ export class Workspace {
this.files.writeFileSync(this.repos_md.value, reposList)
}

this.files.writeFileSync(this.askpass_sh.value, `#!/bin/sh\n\necho '${token}'`)
await this.writeAskPass(token)
setInterval(async () => {
await this.writeAskPass(token)
this.logger.info('✓ GitHub Token refreshed')
}, 1000 * 60 * 50)

this.files.chmodSync(this.askpass_sh.value, 0o755)

this.logger.info('✓ Scala Steward workspace created')
Expand All @@ -139,6 +144,16 @@ export class Workspace {
await this.files.rmRF(this.directory)
}

/**
* Writes a GitHub Token to the git-ask-pass file
*
* @param fetchToken - A function that returns a Promise resolving to a new token string.
*/
async writeAskPass(fetchToken: () => Promise<string>): Promise<void> {
const token = await fetchToken()
this.files.writeFileSync(this.askpass_sh.value, `#!/bin/sh\n\necho '${token}'`)
}

/**
* Gets the first eight characters of the SHA-256 hash value for the
* provided file's contents.
Expand Down

0 comments on commit e2d6f01

Please sign in to comment.