forked from Azure/azure-rest-api-specs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c9653b
commit 0a67219
Showing
4 changed files
with
128 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}'`); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters