fallback to REST API to download repo (#104)
parent
cab31617d8
commit
a572f640b0
@ -0,0 +1,88 @@
|
|||||||
|
const mockCore = jest.genMockFromModule('@actions/core') as any
|
||||||
|
mockCore.info = (message: string) => {
|
||||||
|
info.push(message)
|
||||||
|
}
|
||||||
|
let info: string[]
|
||||||
|
let retryHelper: any
|
||||||
|
|
||||||
|
describe('retry-helper tests', () => {
|
||||||
|
beforeAll(() => {
|
||||||
|
// Mocks
|
||||||
|
jest.setMock('@actions/core', mockCore)
|
||||||
|
|
||||||
|
// Now import
|
||||||
|
const retryHelperModule = require('../lib/retry-helper')
|
||||||
|
retryHelper = new retryHelperModule.RetryHelper(3, 0, 0)
|
||||||
|
})
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
// Reset info
|
||||||
|
info = []
|
||||||
|
})
|
||||||
|
|
||||||
|
afterAll(() => {
|
||||||
|
// Reset modules
|
||||||
|
jest.resetModules()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('first attempt succeeds', async () => {
|
||||||
|
const actual = await retryHelper.execute(async () => {
|
||||||
|
return 'some result'
|
||||||
|
})
|
||||||
|
expect(actual).toBe('some result')
|
||||||
|
expect(info).toHaveLength(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('second attempt succeeds', async () => {
|
||||||
|
let attempts = 0
|
||||||
|
const actual = await retryHelper.execute(() => {
|
||||||
|
if (++attempts == 1) {
|
||||||
|
throw new Error('some error')
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve('some result')
|
||||||
|
})
|
||||||
|
expect(attempts).toBe(2)
|
||||||
|
expect(actual).toBe('some result')
|
||||||
|
expect(info).toHaveLength(2)
|
||||||
|
expect(info[0]).toBe('some error')
|
||||||
|
expect(info[1]).toMatch(/Waiting .+ seconds before trying again/)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('third attempt succeeds', async () => {
|
||||||
|
let attempts = 0
|
||||||
|
const actual = await retryHelper.execute(() => {
|
||||||
|
if (++attempts < 3) {
|
||||||
|
throw new Error(`some error ${attempts}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve('some result')
|
||||||
|
})
|
||||||
|
expect(attempts).toBe(3)
|
||||||
|
expect(actual).toBe('some result')
|
||||||
|
expect(info).toHaveLength(4)
|
||||||
|
expect(info[0]).toBe('some error 1')
|
||||||
|
expect(info[1]).toMatch(/Waiting .+ seconds before trying again/)
|
||||||
|
expect(info[2]).toBe('some error 2')
|
||||||
|
expect(info[3]).toMatch(/Waiting .+ seconds before trying again/)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('all attempts fail succeeds', async () => {
|
||||||
|
let attempts = 0
|
||||||
|
let error: Error = (null as unknown) as Error
|
||||||
|
try {
|
||||||
|
await retryHelper.execute(() => {
|
||||||
|
throw new Error(`some error ${++attempts}`)
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
error = err
|
||||||
|
}
|
||||||
|
expect(error.message).toBe('some error 3')
|
||||||
|
expect(attempts).toBe(3)
|
||||||
|
expect(info).toHaveLength(4)
|
||||||
|
expect(info[0]).toBe('some error 1')
|
||||||
|
expect(info[1]).toMatch(/Waiting .+ seconds before trying again/)
|
||||||
|
expect(info[2]).toBe('some error 2')
|
||||||
|
expect(info[3]).toMatch(/Waiting .+ seconds before trying again/)
|
||||||
|
})
|
||||||
|
})
|
@ -1,10 +1,24 @@
|
|||||||
#!/bin/bash
|
#!/bin/sh
|
||||||
|
|
||||||
if [ ! -f "./basic/basic-file.txt" ]; then
|
if [ ! -f "./basic/basic-file.txt" ]; then
|
||||||
echo "Expected basic file does not exist"
|
echo "Expected basic file does not exist"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Verify auth token
|
if [ "$1" = "--archive" ]; then
|
||||||
cd basic
|
# Verify no .git folder
|
||||||
git fetch
|
if [ -d "./basic/.git" ]; then
|
||||||
|
echo "Did not expect ./basic/.git folder to exist"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
# Verify .git folder
|
||||||
|
if [ ! -d "./basic/.git" ]; then
|
||||||
|
echo "Expected ./basic/.git folder to exist"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Verify auth token
|
||||||
|
cd basic
|
||||||
|
git fetch --no-tags --depth=1 origin +refs/heads/master:refs/remotes/origin/master
|
||||||
|
fi
|
||||||
|
File diff suppressed because one or more lines are too long
@ -0,0 +1,92 @@
|
|||||||
|
import * as assert from 'assert'
|
||||||
|
import * as core from '@actions/core'
|
||||||
|
import * as fs from 'fs'
|
||||||
|
import * as github from '@actions/github'
|
||||||
|
import * as io from '@actions/io'
|
||||||
|
import * as path from 'path'
|
||||||
|
import * as retryHelper from './retry-helper'
|
||||||
|
import * as toolCache from '@actions/tool-cache'
|
||||||
|
import {default as uuid} from 'uuid/v4'
|
||||||
|
import {ReposGetArchiveLinkParams} from '@octokit/rest'
|
||||||
|
|
||||||
|
const IS_WINDOWS = process.platform === 'win32'
|
||||||
|
|
||||||
|
export async function downloadRepository(
|
||||||
|
accessToken: string,
|
||||||
|
owner: string,
|
||||||
|
repo: string,
|
||||||
|
ref: string,
|
||||||
|
commit: string,
|
||||||
|
repositoryPath: string
|
||||||
|
): Promise<void> {
|
||||||
|
// Download the archive
|
||||||
|
let archiveData = await retryHelper.execute(async () => {
|
||||||
|
core.info('Downloading the archive')
|
||||||
|
return await downloadArchive(accessToken, owner, repo, ref, commit)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Write archive to disk
|
||||||
|
core.info('Writing archive to disk')
|
||||||
|
const uniqueId = uuid()
|
||||||
|
const archivePath = path.join(repositoryPath, `${uniqueId}.tar.gz`)
|
||||||
|
await fs.promises.writeFile(archivePath, archiveData)
|
||||||
|
archiveData = Buffer.from('') // Free memory
|
||||||
|
|
||||||
|
// Extract archive
|
||||||
|
core.info('Extracting the archive')
|
||||||
|
const extractPath = path.join(repositoryPath, uniqueId)
|
||||||
|
await io.mkdirP(extractPath)
|
||||||
|
if (IS_WINDOWS) {
|
||||||
|
await toolCache.extractZip(archivePath, extractPath)
|
||||||
|
} else {
|
||||||
|
await toolCache.extractTar(archivePath, extractPath)
|
||||||
|
}
|
||||||
|
io.rmRF(archivePath)
|
||||||
|
|
||||||
|
// Determine the path of the repository content. The archive contains
|
||||||
|
// a top-level folder and the repository content is inside.
|
||||||
|
const archiveFileNames = await fs.promises.readdir(extractPath)
|
||||||
|
assert.ok(
|
||||||
|
archiveFileNames.length == 1,
|
||||||
|
'Expected exactly one directory inside archive'
|
||||||
|
)
|
||||||
|
const archiveVersion = archiveFileNames[0] // The top-level folder name includes the short SHA
|
||||||
|
core.info(`Resolved version ${archiveVersion}`)
|
||||||
|
const tempRepositoryPath = path.join(extractPath, archiveVersion)
|
||||||
|
|
||||||
|
// Move the files
|
||||||
|
for (const fileName of await fs.promises.readdir(tempRepositoryPath)) {
|
||||||
|
const sourcePath = path.join(tempRepositoryPath, fileName)
|
||||||
|
const targetPath = path.join(repositoryPath, fileName)
|
||||||
|
if (IS_WINDOWS) {
|
||||||
|
await io.cp(sourcePath, targetPath, {recursive: true}) // Copy on Windows (Windows Defender may have a lock)
|
||||||
|
} else {
|
||||||
|
await io.mv(sourcePath, targetPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
io.rmRF(extractPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function downloadArchive(
|
||||||
|
accessToken: string,
|
||||||
|
owner: string,
|
||||||
|
repo: string,
|
||||||
|
ref: string,
|
||||||
|
commit: string
|
||||||
|
): Promise<Buffer> {
|
||||||
|
const octokit = new github.GitHub(accessToken)
|
||||||
|
const params: ReposGetArchiveLinkParams = {
|
||||||
|
owner: owner,
|
||||||
|
repo: repo,
|
||||||
|
archive_format: IS_WINDOWS ? 'zipball' : 'tarball',
|
||||||
|
ref: commit || ref
|
||||||
|
}
|
||||||
|
const response = await octokit.repos.getArchiveLink(params)
|
||||||
|
if (response.status != 200) {
|
||||||
|
throw new Error(
|
||||||
|
`Unexpected response from GitHub API. Status: ${response.status}, Data: ${response.data}`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return Buffer.from(response.data) // response.data is ArrayBuffer
|
||||||
|
}
|
@ -0,0 +1,61 @@
|
|||||||
|
import * as core from '@actions/core'
|
||||||
|
|
||||||
|
const defaultMaxAttempts = 3
|
||||||
|
const defaultMinSeconds = 10
|
||||||
|
const defaultMaxSeconds = 20
|
||||||
|
|
||||||
|
export class RetryHelper {
|
||||||
|
private maxAttempts: number
|
||||||
|
private minSeconds: number
|
||||||
|
private maxSeconds: number
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
maxAttempts: number = defaultMaxAttempts,
|
||||||
|
minSeconds: number = defaultMinSeconds,
|
||||||
|
maxSeconds: number = defaultMaxSeconds
|
||||||
|
) {
|
||||||
|
this.maxAttempts = maxAttempts
|
||||||
|
this.minSeconds = Math.floor(minSeconds)
|
||||||
|
this.maxSeconds = Math.floor(maxSeconds)
|
||||||
|
if (this.minSeconds > this.maxSeconds) {
|
||||||
|
throw new Error('min seconds should be less than or equal to max seconds')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async execute<T>(action: () => Promise<T>): Promise<T> {
|
||||||
|
let attempt = 1
|
||||||
|
while (attempt < this.maxAttempts) {
|
||||||
|
// Try
|
||||||
|
try {
|
||||||
|
return await action()
|
||||||
|
} catch (err) {
|
||||||
|
core.info(err.message)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sleep
|
||||||
|
const seconds = this.getSleepAmount()
|
||||||
|
core.info(`Waiting ${seconds} seconds before trying again`)
|
||||||
|
await this.sleep(seconds)
|
||||||
|
attempt++
|
||||||
|
}
|
||||||
|
|
||||||
|
// Last attempt
|
||||||
|
return await action()
|
||||||
|
}
|
||||||
|
|
||||||
|
private getSleepAmount(): number {
|
||||||
|
return (
|
||||||
|
Math.floor(Math.random() * (this.maxSeconds - this.minSeconds + 1)) +
|
||||||
|
this.minSeconds
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private async sleep(seconds: number): Promise<void> {
|
||||||
|
return new Promise(resolve => setTimeout(resolve, seconds * 1000))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function execute<T>(action: () => Promise<T>): Promise<T> {
|
||||||
|
const retryHelper = new RetryHelper()
|
||||||
|
return await retryHelper.execute(action)
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
import * as core from '@actions/core'
|
||||||
|
import * as coreCommand from '@actions/core/lib/command'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates whether the POST action is running
|
||||||
|
*/
|
||||||
|
export const IsPost = !!process.env['STATE_isPost']
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The repository path for the POST action. The value is empty during the MAIN action.
|
||||||
|
*/
|
||||||
|
export const RepositoryPath =
|
||||||
|
(process.env['STATE_repositoryPath'] as string) || ''
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save the repository path so the POST action can retrieve the value.
|
||||||
|
*/
|
||||||
|
export function setRepositoryPath(repositoryPath: string) {
|
||||||
|
coreCommand.issueCommand(
|
||||||
|
'save-state',
|
||||||
|
{name: 'repositoryPath'},
|
||||||
|
repositoryPath
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Publish a variable so that when the POST action runs, it can determine it should run the cleanup logic.
|
||||||
|
// This is necessary since we don't have a separate entry point.
|
||||||
|
if (!IsPost) {
|
||||||
|
coreCommand.issueCommand('save-state', {name: 'isPost'}, 'true')
|
||||||
|
}
|
Loading…
Reference in New Issue