Skip to content

chore(automation): remove previous labels when PR is updated #3066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/scripts/label_pr_based_on_title.js
Original file line number Diff line number Diff line change
@@ -17,6 +17,10 @@ module.exports = async ({github, context, core}) => {
"deprecated": DEPRECATED_REGEX,
}

// get PR labels from env
const prLabels = process.env.PR_LABELS.replaceAll("\"", "").split(",");
const labelKeys = Object.keys(labels);

// Maintenance: We should keep track of modified PRs in case their titles change
let miss = 0;
try {
@@ -26,6 +30,18 @@ module.exports = async ({github, context, core}) => {
if (matches != null) {
core.info(`Auto-labeling PR ${PR_NUMBER} with ${label}`)

for (const prLabel of prLabels) {
if (labelKeys.includes(prLabel) && prLabel !== label) {
core.info(`PR previously tagged with: ${prLabel}, removing.`);
await github.rest.issues.removeLabel({
issue_number: PR_NUMBER,
owner: context.repo.owner,
repo: context.repo.repo,
name: prLabel
})
}
}

await github.rest.issues.addLabels({
issue_number: PR_NUMBER,
owner: context.repo.owner,
14 changes: 12 additions & 2 deletions .github/scripts/save_pr_details.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
module.exports = async ({context, core}) => {
module.exports = async ({github, context, core}) => {
const fs = require('fs');
const filename = "pr.txt";

const labelsData = await github.rest.issues.listLabelsOnIssue({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: (context.payload.issue || context.payload.pull_request || context.payload).number,
});

const labels = labelsData.data.map((label) => {
return label['name'];
});

try {
fs.writeFileSync(`./${filename}`, JSON.stringify(context.payload));
fs.writeFileSync(`./${filename}`, JSON.stringify({...context.payload, ...{labels:labels.join(",")}}));

return `PR successfully saved ${filename}`
} catch (err) {
7 changes: 7 additions & 0 deletions .github/workflows/reusable_export_pr_details.yml
Original file line number Diff line number Diff line change
@@ -49,6 +49,9 @@ on:
prIsMerged:
description: "Whether PR is merged"
value: ${{ jobs.export_pr_details.outputs.prIsMerged }}
prLabels:
description: "PR Labels"
value: ${{ jobs.export_pr_details.outputs.prLabels }}

permissions:
contents: read
@@ -70,6 +73,7 @@ jobs:
prAuthor: ${{ steps.prAuthor.outputs.prAuthor }}
prAction: ${{ steps.prAction.outputs.prAction }}
prIsMerged: ${{ steps.prIsMerged.outputs.prIsMerged }}
prLabels: ${{ steps.prLabels.outputs.prLabels }}
steps:
- name: Checkout repository # in case caller workflow doesn't checkout thus failing with file not found
uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0
@@ -106,3 +110,6 @@ jobs:
- name: "Export Pull Request Merged status"
id: prIsMerged
run: echo prIsMerged="$(jq -c '.pull_request.merged' "${FILENAME}")" >> "$GITHUB_OUTPUT"
- name: "Export Pull Request labels"
id: prLabels
run: echo prLabels="$(jq -c '.labels' "${FILENAME}")" >> "$GITHUB_OUTPUT"