Skip to content

Commit

Permalink
Add action "set-label"
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeharder committed Nov 13, 2024
1 parent 5c9653b commit 0a67219
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/actions/get-keyvalue-artifacts/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ module.exports = async ({ github, context, core }) => {
let run_id = parseInt(process.env.RUN_ID || "");

if (!owner || !repo || !run_id) {
// TODO: Add support for more event types
if (context.eventName == "workflow_run" && context.payload.action == "completed") {
// Add support for more event types as needed
if (context.eventName === "workflow_run" && context.payload.action === "completed") {
const payload =
/** @type {import("@octokit/webhooks-types").WorkflowRunCompletedEvent} */ (
context.payload
Expand Down
78 changes: 78 additions & 0 deletions .github/actions/set-label/action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// @ts-check

/**
* @param {import('github-script').AsyncFunctionArguments} AsyncFunctionArguments
*/
module.exports = async ({ github, context, core }) => {
console.log("context: " + JSON.stringify(context, null, 2));

let owner = process.env.OWNER;
let repo = process.env.REPO;
let issue_number = parseInt(process.env.ISSUE_NUMBER || "");

if (!owner || !repo || !issue_number) {
// Add support for more event types as needed
if (
context.eventName === "workflow_run" &&
context.payload.action === "completed"
) {
const payload =
/** @type {import("@octokit/webhooks-types").WorkflowRunCompletedEvent} */ (
context.payload
);

// Only update vars not already set
owner = owner || payload.workflow_run.repository.owner.login;
repo = repo || payload.workflow_run.repository.name;

if (!issue_number) {
let commit_sha =
process.env.COMMIT_SHA || payload.workflow_run.head_sha;

// Must call this API, since 'payload.workflow_run.pull_requests' is empty for fork PRs
const { data: pullRequests } =
await github.rest.repos.listPullRequestsAssociatedWithCommit({
owner: owner,
repo: repo,
commit_sha: commit_sha,
});

if (pullRequests.length === 1) {
issue_number = pullRequests[0].number;
} else {
throw new Error(
`Unexpected number of pull requests associated with commit '${commit_sha}'. Expected: '1'. Actual '${pullRequests.length}'.`,
);
}
}
} else {
throw new Error(
`Invalid context: '${context.eventName}:${context.payload.action}'. Expected 'workflow_run:completed'.`,
);
}
}

let name = process.env.NAME;
if (!name) {
throw new Error(`Invalid name: '${name}'`);
}

let value = process.env.VALUE;
if (value && value.toLowerCase() === "true") {
await github.rest.issues.addLabels({
owner: owner,
repo: repo,
issue_number: issue_number,
labels: [name],
});
} else if (value && value.toLowerCase() === "false") {
await github.rest.issues.removeLabel({
owner: owner,
repo: repo,
issue_number: issue_number,
name: name,
});
} else {
throw new Error(`Invalid value: '${value}'`);
}
};
42 changes: 42 additions & 0 deletions .github/actions/set-label/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Set Label
description: Adds or removes label to set state matching value

# If any inputs are not set, we will attempt to extract them from the event context
inputs:
name:
description: Name of the label
required: true
value:
description: Whether to add or remove the label
required: true
owner:
description: The account owner of the repository. The name is not case sensitive.
required: false
repo:
description: The name of the repository without the .git extension. The name is not case sensitive.
required: false
issue_number:
description: The number that identifies the issue.
required: false
commit_sha:
description: The SHA of the commit associated with the pull request.
required: false


runs:
using: composite

steps:
- name: Set Label
uses: actions/github-script@v7
env:
NAME: ${{ inputs.name }}
VALUE: ${{ inputs.value }}
OWNER: ${{ inputs.owner }}
REPO: ${{ inputs.repo }}
ISSUE_NUMBER: ${{ inputs.issue_number }}
COMMIT_SHA: ${{ inputs.commit_sha }}
with:
script: |
const action = require('./.github/actions/set-label/action.js')
await action({ github, context, core });
11 changes: 6 additions & 5 deletions .github/workflows/typespec-requirement-completed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ jobs:
sparse-checkout: |
.github
- id: get-keyvalue-artifacts
name: Get KeyValue artifacts
- name: Get KeyValue artifacts
uses: ./.github/actions/get-keyvalue-artifacts
with:
owner: ${{ inputs.owner }}
repo: ${{ inputs.repo }}
run_id: ${{ inputs.run_id }}

# TODO: Add label
- if: env.spec-lifecycle-resource-manager == 'brownfield'
run: echo "brownfield"
- name: Set Label "brownfield"
uses: ./.github/actions/set-label
with:
name: brownfield
value: ${{ env.spec-lifecycle-resource-manager == 'brownfield' }}

0 comments on commit 0a67219

Please sign in to comment.