Skip to content

Commit

Permalink
Skip collecting base branch if artifact available
Browse files Browse the repository at this point in the history
  • Loading branch information
matejchalk committed Mar 26, 2024
1 parent 7a3399e commit 4f73a6d
Show file tree
Hide file tree
Showing 8 changed files with 362 additions and 47 deletions.
23 changes: 15 additions & 8 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ArtifactClient, UploadArtifactResponse } from '@actions/artifact'
import * as core from '@actions/core'
import * as github from '@actions/github'
import type { Context } from '@actions/github/lib/context'
import type { components } from '@octokit/openapi-types'
import type { RestEndpointMethodTypes } from '@octokit/plugin-rest-endpoint-methods'
import {
copyFile,
Expand Down Expand Up @@ -86,16 +87,22 @@ describe('code-pushup action', () => {
paginate: (async () => []) as any,
rest: {
issues: {
createComment: async () =>
({
data: { id: 10 }
}) as RestEndpointMethodTypes['issues']['createComment']['response'],
createComment: async () => ({
data: { id: 10 }
}),
updateComment: async ({
comment_id
}: RestEndpointMethodTypes['issues']['updateComment']['parameters']) =>
({
data: { id: comment_id }
}) as RestEndpointMethodTypes['issues']['updateComment']['response']
}: RestEndpointMethodTypes['issues']['updateComment']['parameters']) => ({
data: { id: comment_id }
})
},
actions: {
getWorkflowRun: async () => ({ data: { workflow_id: 3 } }),
listWorkflowRuns: async () => ({
data: {
workflow_runs: [] as components['schemas']['workflow-run'][]
}
})
}
}
} as ReturnType<typeof github.getOctokit>
Expand Down
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
177 changes: 161 additions & 16 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

97 changes: 97 additions & 0 deletions src/artifact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import {
ArtifactNotFoundError,
type ArtifactClient,
type FindOptions
} from '@actions/artifact'
import * as core from '@actions/core'
import * as github from '@actions/github'
import fs from 'node:fs/promises'
import type { ActionInputs } from './inputs'
import { findPersistedFiles, type PersistedCliFiles } from './persist'

export const REPORT_ARTIFACT_NAME = 'code-pushup-report'
export const DIFF_ARTIFACT_NAME = 'code-pushup-report-diff'

const ARTIFACT_DIR = 'tmp'

export async function downloadReportArtifact(
artifact: ArtifactClient,
branch: { ref: string; sha: string },
{ token }: ActionInputs
): Promise<PersistedCliFiles | null> {
const octokit = github.getOctokit(token)

const {
data: { workflow_id }
} = await octokit.rest.actions.getWorkflowRun({
...github.context.repo,
run_id: github.context.runId
})
core.debug(
`Looked up workflow ID ${workflow_id} from workflow run ID ${github.context.runId}`
)

const {
data: { workflow_runs }
} = await octokit.rest.actions.listWorkflowRuns({
...github.context.repo,
workflow_id,
branch: branch.ref,
status: 'completed'
})
core.debug(
`Fetched ${workflow_runs.length} completed workflow runs in ${branch.ref} branch`
)

const workflowRun = workflow_runs.find(run => run.head_sha === branch.sha)
if (!workflowRun) {
core.info(
`Workflow run not found for ${branch.ref} branch's commit ${branch.sha}`
)
return null
}
core.debug(`Found workflow run ${workflowRun.id} for commit ${branch.sha}`)

try {
const findBy: FindOptions['findBy'] = {
workflowRunId: workflowRun.id,
repositoryOwner: github.context.repo.owner,
repositoryName: github.context.repo.repo,
token
}

const { artifact: reportArtifact } = await artifact.getArtifact(
REPORT_ARTIFACT_NAME,
{ findBy }
)
core.debug(
`Found ${reportArtifact.name} artifact with ID ${reportArtifact.id} in workflow run ${workflowRun.id}`
)

await fs.rm(ARTIFACT_DIR, { recursive: true, force: true })
const { downloadPath } = await artifact.downloadArtifact(
reportArtifact.id,
{ findBy, path: ARTIFACT_DIR }
)
if (!downloadPath) {
throw new Error('Unexpected empty downloadPath')
}
const files = await fs.readdir(downloadPath)
core.debug(
`Downloaded artifact to ${downloadPath}, contains files: ${files.join(', ')}`
)

return findPersistedFiles(downloadPath, files)
} catch (err) {
if (err instanceof ArtifactNotFoundError) {
core.info(
`Artifact not found for ${branch.ref} branch's commit ${branch.sha}`
)
return null
}
if (err instanceof Error || typeof err === 'string') {
core.error(err)
}
throw err
}
}
Loading

0 comments on commit 4f73a6d

Please sign in to comment.