Skip to content

Commit

Permalink
chore(automation): remove previous labels when PR is updated (#3066)
Browse files Browse the repository at this point in the history
Co-authored-by: Heitor Lessa <[email protected]>
  • Loading branch information
sthulb and heitorlessa authored Sep 8, 2023
1 parent 22a6925 commit 704f765
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
16 changes: 16 additions & 0 deletions .github/scripts/label_pr_based_on_title.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
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) {
Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/reusable_export_pr_details.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"

0 comments on commit 704f765

Please sign in to comment.