From 94ac395bab48ad88ee3409360a4069d0818375fa Mon Sep 17 00:00:00 2001 From: Benedikt Volkel Date: Mon, 22 Apr 2024 11:14:29 +0200 Subject: [PATCH] Add possibility to add/remove all labels * "accepted" labels have been removed * if "all" or "!all" mentioned, take precedence over specific labels * "all" takes precedence over "!all" * do not add and remove labels at the same time, adding labels takes precedence --- .github/workflows/async-auto-label.yml | 28 +++++++++++++++++++++----- .github/workflows/async-list-label.yml | 8 ++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/.github/workflows/async-auto-label.yml b/.github/workflows/async-auto-label.yml index cfba4be1..ae7ef703 100644 --- a/.github/workflows/async-auto-label.yml +++ b/.github/workflows/async-auto-label.yml @@ -49,22 +49,40 @@ jobs: review.dismiss('Labels updated; please review again.') possible_labels = {label.name: label for label in repo.get_labels() if label.name.startswith('async-')} - add_labels, invalid_labels = [], [] + add_labels, remove_labels, invalid_labels = [], [], [] + + # "all" or "!all" takes precedence over specific labels + if 'all' in labels: + labels = list(possible_labels.keys()) + elif '!all' in labels: + labels = [f'!{name}' for name in possible_labels.keys()] + for label_name in labels: delete = label_name.startswith('!') label_name = label_name.lstrip('!') + try: label = possible_labels[label_name] except KeyError: print(f'::warning::Ignoring unknown label {label_name!r}') invalid_labels.append(label_name) continue + if delete: - pr.remove_from_labels(label) - else: - add_labels.append(label) + remove_labels.append(label) + continue + + add_labels.append(label) + + # adding labels takes precedence, so remove from this list the ones that should in fact be added + remove_labels = [label for label in remove_labels if label not in add_labels] + + for label in remove_labels: + # to be done label-by-label + pr.remove_from_labels(label) - pr.add_to_labels(*add_labels) + if add_labels: + pr.add_to_labels(*add_labels) if invalid_labels: pr.create_issue_comment( diff --git a/.github/workflows/async-list-label.yml b/.github/workflows/async-list-label.yml index dbbd3f2e..30be6553 100644 --- a/.github/workflows/async-list-label.yml +++ b/.github/workflows/async-list-label.yml @@ -32,6 +32,14 @@ jobs: +async-label , , ! ...\n \ \`\`\`\n \ This will add \`\` and \`\` and removes \`\`.\n\n \ + To add all labels, comment with \n \ + \`\`\`\n \ + +async-label all\n \ + \`\`\`\n \ + To remove all labels, comment with \n \ + \`\`\`\n \ + +async-label !all\n \ + \`\`\`\n \ **The following labels are available**\n${labels_line_break}" # read the body text from stdin echo -e "${body_text}" | gh pr comment "${nr}" --body-file - --repo "${GITHUB_REPOSITORY}"