Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wait_until_signed input option #59

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ inputs:
description: 'API secret for AMO'
src_path:
description: 'path to src zip file for verification. To be used if your extension uses minification, or other build steps that make it difficult to read the code of the xpi.'
wait_until_signed:
required: false
default: false
description: 'wait until AMO finished signing before exiting. this can take up to 24 hours or longer if your sumbission is selected for manual review.'
runs:
using: 'node16'
main: 'dist/index.js'
65 changes: 56 additions & 9 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

60 changes: 53 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as core from '@actions/core'
import {generateJWT} from './util'
import {createUpload, tryUpdateExtension} from './request'
import {createUpload, getVersionDetails, tryUpdateExtension} from './request'

async function run(): Promise<void> {
try {
Expand All @@ -9,22 +9,68 @@ async function run(): Promise<void> {
const key = core.getInput('api_key', {required: true})
const secret = core.getInput('api_secret', {required: true})
const srcPath = core.getInput('src_path')
const waitUntilSigned = core.getBooleanInput('wait_until_signed') || false

const token = generateJWT(key, secret)
let token = generateJWT(key, secret)
const uploadDetails = await createUpload(xpiPath, token)

const timeout = 10 * 60 * 1000
const sleepTime = 5 * 1000
const updateVersionRetryDelay = 5 * 1000
const startTime = Date.now()

const interval = setInterval(async () => {
let versionID: number
const updateVersionInterval = setInterval(async () => {
if (Date.now() - timeout > startTime) {
throw new Error('Extension validation timed out')
}
if (await tryUpdateExtension(guid, uploadDetails.uuid, token, srcPath)) {
clearInterval(interval)
token = generateJWT(key, secret)
const result = await tryUpdateExtension(
guid,
uploadDetails.uuid,
token,
srcPath
)
if (result.success) {
versionID = result.versionDetails.id
clearInterval(updateVersionInterval)
if (waitUntilSigned) {
core.info('Wating for version to be reviewed and approved')
checkVersion()
}
}
}, updateVersionRetryDelay)

const initRetryDelay = 5 * 1000
const maxRetryDelay = 60 * 60 * 1000
const backoffFactor = 1.5
let retryCount = 0

// check if file finished being reviewed
const checkVersion = async (): Promise<void> => {
token = generateJWT(key, secret)
const versionDetails = await getVersionDetails(guid, token, versionID)
if (versionDetails.file.status === 'unreviewed') {
const retryDelay = Math.min(
initRetryDelay * Math.pow(backoffFactor, retryCount),
maxRetryDelay
)
core.info(
`Check #${
retryCount + 1
}: unreviewed at ${new Date().toLocaleString()}, trying again after ${Math.floor(
retryDelay / 1000
)}s`
)
retryCount++
setTimeout(checkVersion, retryDelay)
return
}
if (versionDetails.file.status === 'public') {
core.info('\u001b[38;5;2mVersion has been approved\u001b[0m')
} else if (versionDetails.file.status === 'disabled') {
core.setFailed('Version has been rejected, disabled, or not reviewed')
}
}, sleepTime)
}
} catch (error) {
if (typeof error === 'object' && error && 'response' in error) {
core.debug('Failed Request')
Expand Down
31 changes: 27 additions & 4 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import {baseURL} from './util'
import {resolve} from 'path'
import {createReadStream} from 'fs'
import axios from 'axios'
import {InitialUploadDetails, UploadDetails} from './types.d'
import {
InitialUploadDetails,
TryUpdateResult,
UploadDetails,
VersionDetails
} from './types.d'

export async function createUpload(
xpiPath: string,
Expand Down Expand Up @@ -32,10 +37,10 @@ export async function tryUpdateExtension(
uuid: string,
token: string,
srcPath?: string
): Promise<boolean> {
): Promise<TryUpdateResult> {
const details = await getUploadDetails(uuid, token)
if (!details.valid) {
return false
return {success: false}
}

const url = `${baseURL}/addons/addon/${guid}/versions/`
Expand All @@ -54,7 +59,10 @@ export async function tryUpdateExtension(
}
})
core.debug(`Create version response: ${JSON.stringify(response.data)}`)
return true
return {
success: true,
versionDetails: response.data
}
}

export async function getUploadDetails(
Expand All @@ -72,3 +80,18 @@ export async function getUploadDetails(
)
return response.data
}

export async function getVersionDetails(
guid: string,
token: string,
versionID: number
): Promise<VersionDetails> {
const url = `${baseURL}/addons/addon/${guid}/versions/${versionID}`
const response = await axios.get(url, {
headers: {
Authorization: `JWT ${token}` // if it's unlisted we do need authorization
}
})
core.debug(`Get version details response: ${JSON.stringify(response.data)}`)
return response.data
}
18 changes: 18 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,21 @@ export interface UploadDetails extends BaseUploadDetails {
ending_tier: number
}
}

export interface VersionDetails {
id: number
file: {
id: number
status: string
url: string
}
}

export type TryUpdateResult =
| {
success: true
versionDetails: VersionDetails
}
| {
success: false
}