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,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

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

View File

@ -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<DownloadArtifactResponse>
}
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<ListArtifactsResponse> {
@ -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

View File

@ -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