Skip to content

Commit

Permalink
artifact download findBy to query other workflow (#333)
Browse files Browse the repository at this point in the history
* artifact download findBy to query other workflow

* pass token
  • Loading branch information
jbolda authored Jun 25, 2024
1 parent 9a38f65 commit 8735759
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
31 changes: 23 additions & 8 deletions packages/action/src/comment/postGithubCommentFromArtifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import type { Logger } from "@covector/types";
export function* postGithubCommentFromArtifact({
logger,
octokit,
token,
payload,
}: {
logger: Logger;
octokit: InstanceType<typeof GitHub>;
token: string;
payload: WorkflowRunEvent;
}): Operation<void> {
const {
Expand All @@ -22,22 +24,28 @@ export function* postGithubCommentFromArtifact({
owner: { login: owner },
},
} = payload;
const allArtifacts = yield octokit.rest.actions.listWorkflowRunArtifacts({
owner: owner,
repo: repo,

const artifacts = yield octokit.rest.actions.listWorkflowRunArtifacts({
owner,
repo,
run_id: payload.workflow_run.id,
});

// download the artifact from `pull_request` workflow that generated it
const commentArtifact = allArtifacts.data.artifacts.filter((artifact) => {
const commentArtifact = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == "covector-comment";
})[0];

const artifact = new DefaultArtifactClient();
const artifactRoot = process.env.RUNNER_TEMP ?? "..";
// this is required to elevate iternal permissions to fetch artifacts from another workflow
const findBy = {
token,
workflowRunId: payload.workflow_run.id,
repositoryOwner: owner,
repositoryName: repo,
};
const { downloadPath } = yield artifact.downloadArtifact(commentArtifact.id, {
// optional: download destination path. otherwise defaults to $GITHUB_WORKSPACE
path: artifactRoot,
findBy,
});

const comment = yield fs.readFile(
Expand All @@ -52,5 +60,12 @@ export function* postGithubCommentFromArtifact({
);
const prNumber = parseInt(prNumberAsString, 10);

yield postGithubComment({ logger, comment, octokit, repo, owner, prNumber });
yield postGithubComment({
logger,
comment,
octokit,
repo,
owner,
prNumber,
});
}
7 changes: 6 additions & 1 deletion packages/action/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ export function* run(logger: Logger): Generator<any, any, any> {
if (github.context.eventName === "workflow_run") {
const octokit = github.getOctokit(token);
const payload = github.context.payload as WorkflowRunEvent;
yield postGithubCommentFromArtifact({ logger, octokit, payload });
yield postGithubCommentFromArtifact({
logger,
octokit,
token,
payload,
});
} else {
const covectored: CovectorStatus = yield covector({
logger,
Expand Down

0 comments on commit 8735759

Please sign in to comment.