From befa19f3a87363fe3fa311f48ecb55a5400f3aa8 Mon Sep 17 00:00:00 2001 From: Rob Herley Date: Wed, 6 Dec 2023 04:00:07 +0000 Subject: [PATCH] initalize artifact client as default export --- .github/workflows/artifact-tests.yml | 10 ++--- packages/artifact/src/artifact.ts | 12 ++---- packages/artifact/src/internal/client.ts | 39 +++++++------------ .../src/internal/shared/interfaces.ts | 2 +- 4 files changed, 23 insertions(+), 40 deletions(-) diff --git a/.github/workflows/artifact-tests.yml b/.github/workflows/artifact-tests.yml index 7bbb4aa9..4c7c6ab6 100644 --- a/.github/workflows/artifact-tests.yml +++ b/.github/workflows/artifact-tests.yml @@ -58,21 +58,21 @@ jobs: uses: actions/github-script@v7 with: script: | - const artifact = require('./packages/artifact/lib/artifact') + const {default: artifact} = require('./packages/artifact/lib/artifact') const artifactName = 'my-artifact-${{ matrix.runs-on }}' console.log('artifactName: ' + artifactName) const fileContents = ['artifact-path/first.txt','artifact-path/second.txt'] - const uploadResult = await artifact.create().uploadArtifact(artifactName, fileContents, './') + const uploadResult = await artifact.uploadArtifact(artifactName, fileContents, './') console.log(uploadResult) const size = uploadResult.size const id = uploadResult.id console.log(`Successfully uploaded artifact ${id}`) - + verify: runs-on: ubuntu-latest needs: [build] @@ -100,14 +100,14 @@ jobs: uses: actions/github-script@v7 with: script: | - const artifact = require('./packages/artifact/lib/artifact') + const {default: artifact} = require('./packages/artifact/lib/artifact') const workflowRunId = process.env.GITHUB_RUN_ID const repository = process.env.GITHUB_REPOSITORY const repositoryOwner = repository.split('/')[0] const repositoryName = repository.split('/')[1] - const listResult = await artifact.create().listArtifacts(workflowRunId, repositoryOwner, repositoryName, '${{ secrets.GITHUB_TOKEN }}') + const listResult = await artifact.listArtifacts(workflowRunId, repositoryOwner, repositoryName, '${{ secrets.GITHUB_TOKEN }}') console.log(listResult) const artifacts = listResult.artifacts diff --git a/packages/artifact/src/artifact.ts b/packages/artifact/src/artifact.ts index 3ba38448..f5d91445 100644 --- a/packages/artifact/src/artifact.ts +++ b/packages/artifact/src/artifact.ts @@ -1,12 +1,8 @@ -import {ArtifactClient, Client} from './internal/client' +import {ArtifactClient, DefaultArtifactClient} from './internal/client' -/** - * Exported functionality that we want to expose for any users of @actions/artifact - */ export * from './internal/shared/interfaces' export * from './internal/shared/errors' -export {ArtifactClient} +export * from './internal/client' -export function create(): ArtifactClient { - return Client.create() -} +const client: ArtifactClient = new DefaultArtifactClient() +export default client diff --git a/packages/artifact/src/internal/client.ts b/packages/artifact/src/internal/client.ts index 4dd40b84..c4971d9f 100644 --- a/packages/artifact/src/internal/client.ts +++ b/packages/artifact/src/internal/client.ts @@ -19,9 +19,12 @@ import {getArtifactPublic, getArtifactInternal} from './find/get-artifact' import {listArtifactsPublic, listArtifactsInternal} from './find/list-artifacts' import {GHESNotSupportedError} from './shared/errors' +/** + * Generic interface for the artifact client. + */ export interface ArtifactClient { /** - * Uploads an artifact + * Uploads an artifact. * * @param name The name of the artifact, required * @param files A list of absolute or relative paths that denote what files should be uploaded @@ -40,7 +43,7 @@ export interface ArtifactClient { * Lists all artifacts that are part of the current workflow run. * This function will return at most 1000 artifacts per workflow run. * - * If options.findBy is specified, this will call the public List-Artifacts API which can list from other runs. + * If `options.findBy` is specified, this will call the public List-Artifacts API which can list from other runs. * https://docs.github.com/en/rest/actions/artifacts?apiVersion=2022-11-28#list-workflow-run-artifacts * * @param options Extra options that allow for the customization of the list behavior @@ -55,10 +58,10 @@ export interface ArtifactClient { * If there are multiple artifacts with the same name in the same workflow run, this will return the latest. * If the artifact is not found, it will throw. * - * If options.findBy is specified, this will use the public List Artifacts API with a name filter which can get artifacts from other runs. + * If `options.findBy` is specified, this will use the public List Artifacts API with a name filter which can get artifacts from other runs. * https://docs.github.com/en/rest/actions/artifacts?apiVersion=2022-11-28#list-workflow-run-artifacts - * @actions/artifact > 2.0.0 does not allow for creating multiple artifacts with the same name in the same workflow run. - * It is possible to have multiple artifacts with the same name in the same workflow run by using old versions of upload-artifact (v1,v2 and v3), @actions/artifact < v2.0.0 or it is a rerun. + * `@actions/artifact` v2+ does not allow for creating multiple artifacts with the same name in the same workflow run. + * It is possible to have multiple artifacts with the same name in the same workflow run by using old versions of upload-artifact (v1,v2 and v3), @actions/artifact < v2 or it is a rerun. * If there are multiple artifacts with the same name in the same workflow run this function will return the first artifact that matches the name. * * @param artifactName The name of the artifact to find @@ -72,7 +75,7 @@ export interface ArtifactClient { /** * Downloads an artifact and unzips the content. * - * If options.findBy is specified, this will use the public Download Artifact API https://docs.github.com/en/rest/actions/artifacts?apiVersion=2022-11-28#download-an-artifact + * If `options.findBy` is specified, this will use the public Download Artifact API https://docs.github.com/en/rest/actions/artifacts?apiVersion=2022-11-28#download-an-artifact * * @param artifactId The name of the artifact to download * @param options Extra options that allow for the customization of the download behavior @@ -84,17 +87,10 @@ export interface ArtifactClient { ): Promise } -export class Client implements ArtifactClient { - /** - * Constructs a Client - */ - static create(): Client { - return new Client() - } - - /** - * Upload Artifact - */ +/** + * The default artifact client that is used by the artifact action(s). + */ +export class DefaultArtifactClient implements ArtifactClient { async uploadArtifact( name: string, files: string[], @@ -120,9 +116,6 @@ If the error persists, please check whether Actions is operating normally at [ht } } - /** - * Download Artifact - */ async downloadArtifact( artifactId: number, options?: DownloadArtifactOptions & FindOptions @@ -161,9 +154,6 @@ If the error persists, please check whether Actions and API requests are operati } } - /** - * List Artifacts - */ async listArtifacts( options?: ListArtifactsOptions & FindOptions ): Promise { @@ -200,9 +190,6 @@ If the error persists, please check whether Actions and API requests are operati } } - /** - * Get Artifact - */ async getArtifact( artifactName: string, options?: FindOptions diff --git a/packages/artifact/src/internal/shared/interfaces.ts b/packages/artifact/src/internal/shared/interfaces.ts index dfd3e1da..e40fb661 100644 --- a/packages/artifact/src/internal/shared/interfaces.ts +++ b/packages/artifact/src/internal/shared/interfaces.ts @@ -127,7 +127,7 @@ export interface Artifact { } // FindOptions are for fetching Artifact(s) out of the scope of the current run. -// Must specify a PAT with actions:read scope for cross run/repo lookup otherwise these will be ignored. +// Must specify a token with actions:read scope for cross run/repo lookup otherwise these will be ignored. export interface FindOptions { findBy?: { // Token with actions:read permissions