Show the core of a JavaScript action using the toolkit.
AdvancedConceptCustom Actions & Reusable Workflows
Answer
const core = require('@actions/core');
const github = require('@actions/github');
async function run() {
try {
const token = core.getInput('token', { required: true });
const octokit = github.getOctokit(token);
const { owner, repo } = github.context.repo;
const pr = github.context.payload.pull_request;
if (!pr) { core.info('Not a PR, skipping'); return; }
const { data } = await octokit.rest.pulls.get({ owner, repo, pull_number: pr.number });
const lines = data.additions + data.deletions;
const size = lines < 50 ? 'xs' : lines < 200 ? 's' : lines < 800 ? 'm' : 'l';
await octokit.rest.issues.addLabels({ owner, repo, issue_number: pr.number, labels: [`size/${size}`] });
core.setOutput('size', size);
} catch (err) {
core.setFailed(err.message);
}
}
run();
core.setFailed sets exit code 1 and an error annotation.
More on Custom Actions & Reusable Workflows
What are the three kinds of custom action and how do you choose?BeginnerWrite a minimal composite action that installs Node and dependencies with caching.IntermediateWhat can a composite action not do compared to a job?IntermediateHow do you pass outputs out of a composite action?IntermediateWhat does `action.yml` contain for a JavaScript action?IntermediateWhy must a JavaScript action commit its `dist/` or `node_modules`?Intermediate