Skip to content

Commit e118753

Browse files
authored
[update-labels] Add artifacts head-sha and issue-number (#36531)
1 parent 511fb93 commit e118753

File tree

3 files changed

+58
-13
lines changed

3 files changed

+58
-13
lines changed

.github/workflows/src/update-labels.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,52 @@
11
// @ts-check
22

3+
import { isFullGitSha } from "../../shared/src/git.js";
34
import { PER_PAGE_MAX } from "../../shared/src/github.js";
45
import { extractInputs } from "../src/context.js";
56

67
/**
78
* @param {import('@actions/github-script').AsyncFunctionArguments} AsyncFunctionArguments
89
*/
910
export default async function updateLabels({ github, context, core }) {
10-
const { owner, repo, issue_number, run_id } = await extractInputs(github, context, core);
11-
await updateLabelsImpl({ owner, repo, issue_number, run_id, github, core });
11+
const { owner, repo, head_sha, issue_number, run_id } = await extractInputs(
12+
github,
13+
context,
14+
core,
15+
);
16+
await updateLabelsImpl({ owner, repo, head_sha, issue_number, run_id, github, core });
1217
}
1318

1419
/**
1520
* @param {Object} params
1621
* @param {string} params.owner
1722
* @param {string} params.repo
23+
* @param {string} params.head_sha
1824
* @param {number} params.issue_number
1925
* @param {number} params.run_id
2026
* @param {(import("@octokit/core").Octokit & import("@octokit/plugin-rest-endpoint-methods/dist-types/types.js").Api & { paginate: import("@octokit/plugin-paginate-rest").PaginateInterface; })} params.github
2127
* @param {typeof import("@actions/core")} params.core
2228
*/
23-
export async function updateLabelsImpl({ owner, repo, issue_number, run_id, github, core }) {
29+
export async function updateLabelsImpl({
30+
owner,
31+
repo,
32+
head_sha,
33+
issue_number,
34+
run_id,
35+
github,
36+
core,
37+
}) {
38+
if (isFullGitSha(head_sha)) {
39+
core.setOutput("head_sha", head_sha);
40+
} else {
41+
core.info(`head_sha is not a valid full git SHA: '${head_sha}'`);
42+
}
43+
44+
if (Number.isInteger(issue_number) && issue_number > 0) {
45+
core.setOutput("issue_number", issue_number);
46+
} else {
47+
core.info(`issue_number must be a positive integer: ${issue_number}`);
48+
}
49+
2450
/** @type {string[]} */
2551
let artifactNames = [];
2652

.github/workflows/test/update-labels.test.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { describe, expect, it } from "vitest";
22
import { PER_PAGE_MAX } from "../../shared/src/github.js";
3+
import { fullGitSha } from "../../shared/test/examples.js";
34
import updateLabels, { updateLabelsImpl } from "../src/update-labels.js";
45
import { createMockCore, createMockGithub, createMockRequestError } from "./mocks.js";
56

67
describe("updateLabels", () => {
78
it("loads inputs from context", async () => {
9+
const core = createMockCore();
10+
811
const github = createMockGithub();
912
github.rest.actions.listWorkflowRunArtifacts.mockResolvedValue({
1013
data: {
@@ -18,7 +21,7 @@ describe("updateLabels", () => {
1821
action: "completed",
1922
workflow_run: {
2023
event: "pull_request",
21-
head_sha: "abc123",
24+
head_sha: fullGitSha,
2225
id: 456,
2326
repository: {
2427
name: "TestRepoName",
@@ -31,13 +34,10 @@ describe("updateLabels", () => {
3134
},
3235
};
3336

34-
await expect(
35-
updateLabels({
36-
github: github,
37-
context: context,
38-
core: createMockCore(),
39-
}),
40-
).resolves.toBeUndefined();
37+
await expect(updateLabels({ github, context, core })).resolves.toBeUndefined();
38+
39+
expect(core.setOutput).toBeCalledWith("head_sha", fullGitSha);
40+
expect(core.setOutput).toBeCalledWith("issue_number", 123);
4141

4242
expect(github.rest.actions.listWorkflowRunArtifacts).toBeCalledWith({
4343
owner: "TestRepoOwnerLogin",
@@ -68,7 +68,9 @@ describe("updateLabelsImpl", () => {
6868
github: github,
6969
core: createMockCore(),
7070
}),
71-
).rejects.toThrow();
71+
).rejects.toThrowErrorMatchingInlineSnapshot(
72+
`[Error: Required input 'run_id' not found in env or context]`,
73+
);
7274

7375
expect(github.rest.issues.addLabels).toBeCalledTimes(0);
7476
expect(github.rest.issues.removeLabel).toBeCalledTimes(0);
@@ -192,7 +194,9 @@ describe("updateLabelsImpl", () => {
192194
github: github,
193195
core: createMockCore(),
194196
}),
195-
).rejects.toThrow();
197+
).rejects.toThrowErrorMatchingInlineSnapshot(
198+
`[Error: Invalid value for label 'baz': invalid. Expected "true" or "false".]`,
199+
);
196200

197201
expect(github.rest.actions.listWorkflowRunArtifacts).toBeCalledWith({
198202
owner: "owner",

.github/workflows/update-labels.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,24 @@ jobs:
3434
.github
3535
3636
- name: Update Labels
37+
id: update-labels
3738
uses: actions/github-script@v7
3839
with:
3940
script: |
4041
const { default: updateLabels } =
4142
await import('${{ github.workspace }}/.github/workflows/src/update-labels.js');
4243
await updateLabels({ github, context, core });
44+
45+
- if: ${{ always() && steps.update-labels.outputs.head_sha }}
46+
name: Upload artifact with head SHA
47+
uses: ./.github/actions/add-empty-artifact
48+
with:
49+
name: head-sha
50+
value: ${{ steps.update-labels.outputs.head_sha }}
51+
52+
- if: ${{ always() && steps.update-labels.outputs.issue_number }}
53+
name: Upload artifact with issue number
54+
uses: ./.github/actions/add-empty-artifact
55+
with:
56+
name: issue-number
57+
value: ${{ steps.update-labels.outputs.issue_number }}

0 commit comments

Comments
 (0)