2023-04-04 09:28:36 +00:00
|
|
|
name: "Apply issue labels to PR"
|
|
|
|
|
|
|
|
on:
|
2023-06-01 11:48:36 +00:00
|
|
|
pull_request_target:
|
|
|
|
types:
|
|
|
|
- opened
|
|
|
|
|
2023-04-04 09:28:36 +00:00
|
|
|
jobs:
|
|
|
|
label_on_pr:
|
|
|
|
runs-on: ubuntu-latest
|
2023-08-15 00:42:10 +00:00
|
|
|
|
2023-04-04 09:28:36 +00:00
|
|
|
permissions:
|
2023-06-01 11:48:36 +00:00
|
|
|
contents: none
|
|
|
|
issues: read
|
2023-04-04 09:28:36 +00:00
|
|
|
pull-requests: write
|
2023-08-15 00:42:10 +00:00
|
|
|
|
2023-04-04 09:28:36 +00:00
|
|
|
steps:
|
2023-06-01 11:48:36 +00:00
|
|
|
- name: Apply labels from linked issue to PR
|
|
|
|
uses: actions/github-script@v5
|
2023-04-04 09:28:36 +00:00
|
|
|
with:
|
2023-06-01 11:48:36 +00:00
|
|
|
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
script: |
|
|
|
|
async function getLinkedIssues(owner, repo, prNumber) {
|
|
|
|
const query = `query GetLinkedIssues($owner: String!, $repo: String!, $prNumber: Int!) {
|
|
|
|
repository(owner: $owner, name: $repo) {
|
|
|
|
pullRequest(number: $prNumber) {
|
|
|
|
closingIssuesReferences(first: 10) {
|
|
|
|
nodes {
|
|
|
|
number
|
|
|
|
labels(first: 10) {
|
|
|
|
nodes {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`;
|
2023-04-04 09:28:36 +00:00
|
|
|
|
2023-06-01 11:48:36 +00:00
|
|
|
const variables = {
|
|
|
|
owner: owner,
|
|
|
|
repo: repo,
|
|
|
|
prNumber: prNumber,
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = await github.graphql(query, variables);
|
|
|
|
return result.repository.pullRequest.closingIssuesReferences.nodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
const pr = context.payload.pull_request;
|
|
|
|
const linkedIssues = await getLinkedIssues(
|
|
|
|
context.repo.owner,
|
|
|
|
context.repo.repo,
|
|
|
|
pr.number
|
|
|
|
);
|
|
|
|
|
|
|
|
const labelsToAdd = new Set();
|
|
|
|
for (const issue of linkedIssues) {
|
|
|
|
if (issue.labels && issue.labels.nodes) {
|
|
|
|
for (const label of issue.labels.nodes) {
|
|
|
|
labelsToAdd.add(label.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (labelsToAdd.size) {
|
|
|
|
await github.rest.issues.addLabels({
|
|
|
|
owner: context.repo.owner,
|
|
|
|
repo: context.repo.repo,
|
|
|
|
issue_number: pr.number,
|
|
|
|
labels: Array.from(labelsToAdd),
|
|
|
|
});
|
|
|
|
}
|