1
0
Fork 0

initalize artifact client as default export

pull/1594/head
Rob Herley 2023-12-06 04:00:07 +00:00 committed by GitHub
parent e27efe5620
commit befa19f3a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 40 deletions

View File

@ -58,14 +58,14 @@ jobs:
uses: actions/github-script@v7 uses: actions/github-script@v7
with: with:
script: | script: |
const artifact = require('./packages/artifact/lib/artifact') const {default: artifact} = require('./packages/artifact/lib/artifact')
const artifactName = 'my-artifact-${{ matrix.runs-on }}' const artifactName = 'my-artifact-${{ matrix.runs-on }}'
console.log('artifactName: ' + artifactName) console.log('artifactName: ' + artifactName)
const fileContents = ['artifact-path/first.txt','artifact-path/second.txt'] 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) console.log(uploadResult)
const size = uploadResult.size const size = uploadResult.size
@ -100,14 +100,14 @@ jobs:
uses: actions/github-script@v7 uses: actions/github-script@v7
with: with:
script: | script: |
const artifact = require('./packages/artifact/lib/artifact') const {default: artifact} = require('./packages/artifact/lib/artifact')
const workflowRunId = process.env.GITHUB_RUN_ID const workflowRunId = process.env.GITHUB_RUN_ID
const repository = process.env.GITHUB_REPOSITORY const repository = process.env.GITHUB_REPOSITORY
const repositoryOwner = repository.split('/')[0] const repositoryOwner = repository.split('/')[0]
const repositoryName = repository.split('/')[1] 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) console.log(listResult)
const artifacts = listResult.artifacts const artifacts = listResult.artifacts

View File

@ -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/interfaces'
export * from './internal/shared/errors' export * from './internal/shared/errors'
export {ArtifactClient} export * from './internal/client'
export function create(): ArtifactClient { const client: ArtifactClient = new DefaultArtifactClient()
return Client.create() export default client
}

View File

@ -19,9 +19,12 @@ import {getArtifactPublic, getArtifactInternal} from './find/get-artifact'
import {listArtifactsPublic, listArtifactsInternal} from './find/list-artifacts' import {listArtifactsPublic, listArtifactsInternal} from './find/list-artifacts'
import {GHESNotSupportedError} from './shared/errors' import {GHESNotSupportedError} from './shared/errors'
/**
* Generic interface for the artifact client.
*/
export interface ArtifactClient { export interface ArtifactClient {
/** /**
* Uploads an artifact * Uploads an artifact.
* *
* @param name The name of the artifact, required * @param name The name of the artifact, required
* @param files A list of absolute or relative paths that denote what files should be uploaded * @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. * Lists all artifacts that are part of the current workflow run.
* This function will return at most 1000 artifacts per 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 * 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 * @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 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 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 * 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. * `@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.0.0 or it is a rerun. * 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. * 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 * @param artifactName The name of the artifact to find
@ -72,7 +75,7 @@ export interface ArtifactClient {
/** /**
* Downloads an artifact and unzips the content. * 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 artifactId The name of the artifact to download
* @param options Extra options that allow for the customization of the download behavior * @param options Extra options that allow for the customization of the download behavior
@ -84,17 +87,10 @@ export interface ArtifactClient {
): Promise<DownloadArtifactResponse> ): Promise<DownloadArtifactResponse>
} }
export class Client implements ArtifactClient {
/** /**
* Constructs a Client * The default artifact client that is used by the artifact action(s).
*/
static create(): Client {
return new Client()
}
/**
* Upload Artifact
*/ */
export class DefaultArtifactClient implements ArtifactClient {
async uploadArtifact( async uploadArtifact(
name: string, name: string,
files: string[], files: string[],
@ -120,9 +116,6 @@ If the error persists, please check whether Actions is operating normally at [ht
} }
} }
/**
* Download Artifact
*/
async downloadArtifact( async downloadArtifact(
artifactId: number, artifactId: number,
options?: DownloadArtifactOptions & FindOptions options?: DownloadArtifactOptions & FindOptions
@ -161,9 +154,6 @@ If the error persists, please check whether Actions and API requests are operati
} }
} }
/**
* List Artifacts
*/
async listArtifacts( async listArtifacts(
options?: ListArtifactsOptions & FindOptions options?: ListArtifactsOptions & FindOptions
): Promise<ListArtifactsResponse> { ): Promise<ListArtifactsResponse> {
@ -200,9 +190,6 @@ If the error persists, please check whether Actions and API requests are operati
} }
} }
/**
* Get Artifact
*/
async getArtifact( async getArtifact(
artifactName: string, artifactName: string,
options?: FindOptions options?: FindOptions

View File

@ -127,7 +127,7 @@ export interface Artifact {
} }
// FindOptions are for fetching Artifact(s) out of the scope of the current run. // 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 { export interface FindOptions {
findBy?: { findBy?: {
// Token with actions:read permissions // Token with actions:read permissions