1
0
Fork 0

make FindOptions interface more user friendly

pull/1591/head
Rob Herley 2023-12-01 02:15:25 +00:00 committed by GitHub
parent 32549e8197
commit 4789a46578
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 34 deletions

View File

@ -7,7 +7,7 @@ import {
GetArtifactResponse, GetArtifactResponse,
ListArtifactsResponse, ListArtifactsResponse,
DownloadArtifactResponse, DownloadArtifactResponse,
LookupOptions FindOptions
} from './shared/interfaces' } from './shared/interfaces'
import {uploadArtifact} from './upload/upload-artifact' import {uploadArtifact} from './upload/upload-artifact'
import { import {
@ -38,18 +38,18 @@ 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.token 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
* @returns ListArtifactResponse object * @returns ListArtifactResponse object
*/ */
listArtifacts(options?: LookupOptions): Promise<ListArtifactsResponse> listArtifacts(options?: FindOptions): Promise<ListArtifactsResponse>
/** /**
* Finds an artifact by name. * Finds an artifact by name.
* *
* If options.token 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 > 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. * 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.
@ -60,13 +60,13 @@ export interface ArtifactClient {
*/ */
getArtifact( getArtifact(
artifactName: string, artifactName: string,
options?: LookupOptions options?: FindOptions
): Promise<GetArtifactResponse> ): Promise<GetArtifactResponse>
/** /**
* Downloads an artifact and unzips the content. * Downloads an artifact and unzips the content.
* *
* If options.token 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
@ -74,7 +74,7 @@ export interface ArtifactClient {
*/ */
downloadArtifact( downloadArtifact(
artifactId: number, artifactId: number,
options?: DownloadArtifactOptions & LookupOptions options?: DownloadArtifactOptions & FindOptions
): Promise<DownloadArtifactResponse> ): Promise<DownloadArtifactResponse>
} }
@ -93,7 +93,7 @@ export class Client implements ArtifactClient {
name: string, name: string,
files: string[], files: string[],
rootDirectory: string, rootDirectory: string,
options?: UploadOptions | undefined options?: UploadOptions
): Promise<UploadResponse> { ): Promise<UploadResponse> {
if (isGhes()) { if (isGhes()) {
warning( warning(
@ -125,7 +125,7 @@ If the error persists, please check whether Actions is operating normally at [ht
*/ */
async downloadArtifact( async downloadArtifact(
artifactId: number, artifactId: number,
options?: DownloadArtifactOptions & LookupOptions options?: Partial<DownloadArtifactOptions & FindOptions>
): Promise<DownloadArtifactResponse> { ): Promise<DownloadArtifactResponse> {
if (isGhes()) { if (isGhes()) {
warning( warning(
@ -137,9 +137,12 @@ If the error persists, please check whether Actions is operating normally at [ht
} }
try { try {
if (options?.token) { if (options?.findBy) {
const {repositoryOwner, repositoryName, token, ...downloadOptions} = const {
options findBy: {repositoryOwner, repositoryName, token},
...downloadOptions
} = options
return downloadArtifactPublic( return downloadArtifactPublic(
artifactId, artifactId,
repositoryOwner, repositoryOwner,
@ -168,7 +171,7 @@ If the error persists, please check whether Actions and API requests are operati
/** /**
* List Artifacts * List Artifacts
*/ */
async listArtifacts(options?: LookupOptions): Promise<ListArtifactsResponse> { async listArtifacts(options?: FindOptions): Promise<ListArtifactsResponse> {
if (isGhes()) { if (isGhes()) {
warning( warning(
`@actions/artifact v2.0.0+ and download-artifact@v4+ are not currently supported on GHES.` `@actions/artifact v2.0.0+ and download-artifact@v4+ are not currently supported on GHES.`
@ -179,12 +182,16 @@ If the error persists, please check whether Actions and API requests are operati
} }
try { try {
if (options?.token) { if (options?.findBy) {
const {
findBy: {workflowRunId, repositoryOwner, repositoryName, token}
} = options
return listArtifactsPublic( return listArtifactsPublic(
options.workflowRunId, workflowRunId,
options.repositoryOwner, repositoryOwner,
options.repositoryName, repositoryName,
options.token token
) )
} }
@ -209,7 +216,7 @@ If the error persists, please check whether Actions and API requests are operati
*/ */
async getArtifact( async getArtifact(
artifactName: string, artifactName: string,
options?: LookupOptions options?: FindOptions
): Promise<GetArtifactResponse> { ): Promise<GetArtifactResponse> {
if (isGhes()) { if (isGhes()) {
warning( warning(
@ -221,13 +228,17 @@ If the error persists, please check whether Actions and API requests are operati
} }
try { try {
if (options?.token) { if (options?.findBy) {
const {
findBy: {workflowRunId, repositoryOwner, repositoryName, token}
} = options
return getArtifactPublic( return getArtifactPublic(
artifactName, artifactName,
options.workflowRunId, workflowRunId,
options.repositoryOwner, repositoryOwner,
options.repositoryName, repositoryName,
options.token token
) )
} }

View File

@ -126,15 +126,17 @@ export interface Artifact {
size: number size: number
} }
// LookupOptions 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 PAT with actions:read scope for cross run/repo lookup otherwise these will be ignored.
export interface LookupOptions { export interface FindOptions {
// Token with actions:read permissions findBy?: {
token: string // Token with actions:read permissions
// WorkflowRun of the artifact(s) to lookup token: string
workflowRunId: number // WorkflowRun of the artifact(s) to lookup
// Repository owner workflowRunId: number
repositoryOwner: string // Repository owner
// Repository name repositoryOwner: string
repositoryName: string // Repository name
repositoryName: string
}
} }