Skip to content

Commit e66f0d5

Browse files
authored
[EngSys] Convert GitHub actions to ESM, add placeholder tests (#32376)
- Convert all actions to ESM - Add placeholder tests for actions - Add vitest to package.json - Rename and move update-labels JS
1 parent c24c5a5 commit e66f0d5

File tree

14 files changed

+134
-52
lines changed

14 files changed

+134
-52
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Setup Node and install dependencies
2+
description: Uses specified Node version and installs dependencies (typically using "npm")
3+
4+
inputs:
5+
node-version:
6+
description: 'Node version to use'
7+
default: 22.x
8+
install-command:
9+
description: 'Command to install dependencies'
10+
default: 'npm ci'
11+
working-directory:
12+
description: 'Working directory'
13+
default: '.'
14+
15+
runs:
16+
using: "composite"
17+
18+
steps:
19+
- uses: actions/setup-node@v4
20+
with:
21+
node-version: ${{ inputs.node-version }}
22+
23+
- run: |
24+
echo "::group::$INSTALL_COMMAND"
25+
$INSTALL_COMMAND
26+
echo "::endgroup::"
27+
shell: bash
28+
env:
29+
INSTALL_COMMAND: ${{ inputs.install-command }}
30+
working-directory: ${{ inputs.working-directory }}
31+
32+
- run: |
33+
echo "::group::npm ls -a"
34+
npm ls -a || true
35+
echo "::endgroup::"
36+
shell: bash
37+
working-directory: ${{ inputs.working-directory }}

.github/actions/setup-node-npm-ci/action.yaml

Lines changed: 0 additions & 27 deletions
This file was deleted.

.github/actions/update-labels/action.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ runs:
2020
using: composite
2121

2222
steps:
23-
- name: Set Label
23+
- name: Update Labels
2424
uses: actions/github-script@v7
2525
env:
2626
OWNER: ${{ inputs.owner }}
@@ -29,5 +29,6 @@ runs:
2929
RUN_ID: ${{ inputs.run_id }}
3030
with:
3131
script: |
32-
const action = require('./.github/actions/update-labels/action.js')
33-
await action({ github, context, core });
32+
const { default: updateLabels } =
33+
await import('${{ github.workspace }}/.github/actions/update-labels/src/update-labels.js');
34+
await updateLabels({ github, context, core });

.github/actions/update-labels/action.js renamed to .github/actions/update-labels/src/update-labels.js

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

3-
const { extractInputs } = require("../context");
3+
import { extractInputs } from "../../../src/context.js";
44

55
/**
66
* @param {import('github-script').AsyncFunctionArguments} AsyncFunctionArguments
77
*/
8-
module.exports = async ({ github, context, core }) => {
8+
export default async function updateLabels({ github, context, core }) {
99
let owner = process.env.OWNER;
1010
let repo = process.env.REPO;
1111
let issue_number = parseInt(process.env.ISSUE_NUMBER || "");
@@ -82,7 +82,7 @@ module.exports = async ({ github, context, core }) => {
8282
}
8383

8484
if (labelsToRemove.length > 0) {
85-
// Must loop over labelsToRemove ourselves, since GitHub doesn't expose a REST API to remove in bulk.
85+
// Must loop over labelsToRemove ourselves, since GitHub doesn't expose a REST API to remove in bulk.
8686
for (const name of labelsToRemove) {
8787
try {
8888
await github.rest.issues.removeLabel({
@@ -100,4 +100,4 @@ module.exports = async ({ github, context, core }) => {
100100
}
101101
}
102102
}
103-
};
103+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { describe, expect, it } from "vitest";
2+
import updateLabels from "../src/update-labels.js";
3+
4+
describe("update-labels", () => {
5+
// TODO: Replace with better tests
6+
it("throws if inputs null", async () => {
7+
await expect(
8+
updateLabels({ github: null, context: null, core: null }),
9+
).rejects.toThrow();
10+
});
11+
});

.github/package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
{
2+
"type": "module",
23
"devDependencies": {
34
"@types/node": "^20.0.0",
45
"@types/github-script": "github:actions/github-script",
56
"@octokit/webhooks-types": "^7.5.1",
6-
"prettier": "^3.3.3"
7+
"@vitest/coverage-v8": "^3.0.2",
8+
"prettier": "^3.3.3",
9+
"vitest": "^3.0.4"
10+
},
11+
"scripts": {
12+
"test": "vitest",
13+
"test:ci": "vitest run --coverage --reporter=verbose"
714
}
815
}

.github/actions/context.js renamed to .github/src/context.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
/**
44
* Extracts inputs from context based on event name and properties.
55
* run_id is only defined for "workflow_run:completed" events.
6-
*
6+
*
77
* @param {import('github-script').AsyncFunctionArguments['github']} github
88
* @param {import('github-script').AsyncFunctionArguments['context']} context
99
* @param {import('github-script').AsyncFunctionArguments['core']} core
1010
* @returns {Promise<{owner: string, repo: string, head_sha: string, issue_number: number, run_id: number }>}
1111
*/
12-
async function extractInputs(github, context, core) {
12+
export async function extractInputs(github, context, core) {
1313
core.info(`extractInputs(${context.eventName}, ${context.payload.action})`);
1414

1515
// Add support for more event types as needed
@@ -24,11 +24,11 @@ async function extractInputs(github, context, core) {
2424
repo: payload.repository.name,
2525
head_sha: payload.pull_request.head.sha,
2626
issue_number: payload.number,
27-
run_id: NaN
27+
run_id: NaN,
2828
};
29-
29+
3030
core.info(`inputs: ${JSON.stringify(inputs)}`);
31-
31+
3232
return inputs;
3333
} else if (
3434
context.eventName === "workflow_run" &&
@@ -91,5 +91,3 @@ async function extractInputs(github, context, core) {
9191
);
9292
}
9393
}
94-
95-
module.exports = { extractInputs };

.github/test/context.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { describe, expect, it } from "vitest";
2+
import { extractInputs } from "../src/context.js";
3+
4+
describe("context", () => {
5+
// TODO: Replace with better tests
6+
it("throws if inputs null", async () => {
7+
await expect(extractInputs(null, null, null)).rejects.toThrow();
8+
});
9+
});

.github/workflows/SDK-Suppressions-Label.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ jobs:
2020
# Required since "HEAD^" is passed to Get-ChangedFiles
2121
fetch-depth: 2
2222

23-
- name: Setup Node and run `npm ci`
24-
uses: ./.github/actions/setup-node-npm-ci
23+
- name: Setup Node and install deps
24+
uses: ./.github/actions/setup-node-install-deps
2525

2626
- name: Get GitHub PullRequest Changed Files
2727
shell: pwsh

.github/workflows/_reusable-eng-tools-test.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ jobs:
4646
eng
4747
${{ inputs.sparse-checkout-paths }}
4848
49-
- name: Setup Node ${{ matrix.node-version }} and run `npm ci`
50-
uses: ./.github/actions/setup-node-npm-ci
49+
- name: Setup Node ${{ matrix.node-version }} and install deps
50+
uses: ./.github/actions/setup-node-install-deps
5151
with:
5252
node-version: ${{ matrix.node-version }}.x
5353

0 commit comments

Comments
 (0)