Skip to content

Commit

Permalink
refact: improve lib
Browse files Browse the repository at this point in the history
Reduce four functions in lib.ts to two; merge publish request function
and wait processing request function into one.

Remove error code of no location in response header.
  • Loading branch information
wdzeng committed Oct 2, 2024
1 parent 0dc4b25 commit e5528aa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 41 deletions.
1 change: 0 additions & 1 deletion src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { logger, stringify } from '@/utils'

export const ERR_UPLOADING_PACKAGE = 1
export const ERR_PUBLISHING_PACKAGE = 4
export const RESPONSE_NO_LOCATION = 6
export const ERR_INVALID_INPUT = 9
export const ERR_UNKNOWN_HTTP = 254
export const ERR_UNKNOWN = 255
Expand Down
13 changes: 3 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import * as core from '@actions/core'

import { handleError } from '@/error'
import {
sendPackagePublishingRequest,
uploadPackage,
waitUntilPackagePublished,
waitUntilPackageValidated
} from '@/lib'
import { publishPackage, uploadPackage } from '@/lib'
import { tryResolvePath } from '@/utils'

async function run(
Expand All @@ -16,11 +11,9 @@ async function run(
clientId: string,
uploadOnly: boolean
): Promise<void> {
const uploadOperationId = await uploadPackage(productId, zipPath, apiKey, clientId)
await waitUntilPackageValidated(productId, apiKey, clientId, uploadOperationId)
await uploadPackage(productId, zipPath, apiKey, clientId)
if (!uploadOnly) {
const publishingOperationId = await sendPackagePublishingRequest(productId, apiKey, clientId)
await waitUntilPackagePublished(productId, apiKey, clientId, publishingOperationId)
await publishPackage(productId, apiKey, clientId)
}
}

Expand Down
69 changes: 39 additions & 30 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,13 @@ import axios from 'axios'

import type { PublishStatusResponse } from '@/api-types/publish'
import type { UploadStatusResponse } from '@/api-types/upload'
import {
ERR_PUBLISHING_PACKAGE,
ERR_UPLOADING_PACKAGE,
EdgeAddonActionError,
RESPONSE_NO_LOCATION
} from '@/error'
import { ERR_PUBLISHING_PACKAGE, ERR_UPLOADING_PACKAGE, EdgeAddonActionError } from '@/error'
import { logger, tryGetErrorMessage } from '@/utils'

import type { AxiosResponse } from 'axios'

const WAIT_DELAY = 10 * 1000 // 10 seconds
const MAX_WAIT_TIME = 10 * 60 * 1000 // 10 minutes

function getOperationIdFromResponse(response: AxiosResponse): string {
const responseHeaders = response.headers

if ('location' in responseHeaders) {
const operationId = responseHeaders.location as string
logger.debug(`Operation ID: ${operationId}`)
return operationId
}

logger.debug(JSON.stringify(responseHeaders))
throw new EdgeAddonActionError(
'The API server does not provide operation ID.',
RESPONSE_NO_LOCATION
)
}

export async function uploadPackage(
async function sendUploadPackageRequest(
productId: string,
zipPath: string,
apiKey: string,
Expand All @@ -51,12 +28,21 @@ export async function uploadPackage(
'X-ClientID': clientId
}
const response = await axios.post<never>(url, zipStream, { headers }) // 202 Accepted
const operationId = getOperationIdFromResponse(response)

const operationId = response.headers.location as string | undefined
if (!operationId) {
logger.debug(JSON.stringify(response.headers))
throw new EdgeAddonActionError(
'Failed to upload the add-on. The API server does not provide operation ID.',
ERR_UPLOADING_PACKAGE
)
}

logger.info('Package uploaded.')
return operationId
}

export async function waitUntilPackageValidated(
async function waitUntilPackageValidated(
productId: string,
apiKey: string,
clientId: string,
Expand Down Expand Up @@ -114,7 +100,17 @@ export async function waitUntilPackageValidated(
throw new EdgeAddonActionError('Failed to validate the add-on.', ERR_UPLOADING_PACKAGE)
}

export async function sendPackagePublishingRequest(
export async function uploadPackage(
productId: string,
zipPath: string,
apiKey: string,
clientId: string
) {
const operationId = await sendUploadPackageRequest(productId, zipPath, apiKey, clientId)
await waitUntilPackageValidated(productId, apiKey, clientId, operationId)
}

async function sendPackagePublishingRequest(
productId: string,
apiKey: string,
clientId: string
Expand All @@ -128,12 +124,20 @@ export async function sendPackagePublishingRequest(
{}, // Empty body
{ headers: { 'Authorization': `ApiKey ${apiKey}`, 'X-ClientID': clientId } }
)
const operationId = getOperationIdFromResponse(response)

const operationId = response.headers.location as string | undefined
if (!operationId) {
logger.debug(JSON.stringify(response.headers))
throw new EdgeAddonActionError(
'Failed to publish the add-on. The API server does not provide operation ID.',
ERR_PUBLISHING_PACKAGE
)
}
logger.info('Publishing request sent.')
return operationId
}

export async function waitUntilPackagePublished(
async function waitUntilPackagePublished(
productId: string,
apiKey: string,
clientId: string,
Expand Down Expand Up @@ -222,3 +226,8 @@ export async function waitUntilPackagePublished(

throw new EdgeAddonActionError('Failed to publish the add-on.', ERR_PUBLISHING_PACKAGE)
}

export async function publishPackage(productId: string, apiKey: string, clientId: string) {
const operationId = await sendPackagePublishingRequest(productId, apiKey, clientId)
await waitUntilPackagePublished(productId, apiKey, clientId, operationId)
}

0 comments on commit e5528aa

Please sign in to comment.