From dfdb469a70b6f80ff91ded8c3c19f0f59a33855a Mon Sep 17 00:00:00 2001 From: Shawn Callegari <36091529+shawncal@users.noreply.github.com> Date: Fri, 7 Jul 2023 17:42:49 -0700 Subject: [PATCH] Update and rename label.yml to label-pr.yml --- .github/workflows/label-pr.yml | 84 ++++++++++++++++++++++++++++++++++ .github/workflows/label.yml | 21 --------- 2 files changed, 84 insertions(+), 21 deletions(-) create mode 100644 .github/workflows/label-pr.yml delete mode 100644 .github/workflows/label.yml diff --git a/.github/workflows/label-pr.yml b/.github/workflows/label-pr.yml new file mode 100644 index 0000000000..7359a25332 --- /dev/null +++ b/.github/workflows/label-pr.yml @@ -0,0 +1,84 @@ +# This workflow will triage pull requests and apply a label based on the +# paths that are modified in the pull request. +# +# To use this workflow, you will need to set up a .github/labeler.yml +# file with configuration. For more information, see: +# https://github.com/actions/labeler + +name: Label pull request +on: [pull_request_target] + +jobs: + label: + runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: write + + steps: + - uses: actions/labeler@v4 + with: + repo-token: "${{ secrets.GITHUB_TOKEN }}" + + add_title_prefix: + name: "PR: add title prefix" + continue-on-error: true + # Define a matrix of label and prefix pairs + strategy: + matrix: + include: + - {label: 'python', prefix: 'Python'} + - {label: 'java', prefix: 'Java'} + - {label: '.NET', prefix: '.Net'} + - {label: 'copilot chat', prefix: 'Copilot Chat'} + + runs-on: ubuntu-latest + permissions: + issues: write + pull-requests: write + + steps: + - uses: actions/github-script@v6 + name: "Issue/PR: update title" + if: contains( context.payload.pull_request.labels.*.name, ${{ matrix.label }}) + with: + script: | + // Get the label and prefix from the matrix + const label = '${{ matrix.label }}' + const prefix = '${{ matrix.prefix }}' + + // Get the title and labels + let title = context.payload.pull_request.title + + let originalTitle = title + + // Update the title based on the label and prefix + // Check if the issue or PR has the label + if (labels.Contains(label) { + // Check if the title starts with the prefix (case-sensitive) + if (!title.startsWith(prefix + ": ")) { + // If not, check if the first word is the label (case-insensitive) + if (title.match(new RegExp(`^${prefix}`, 'i'))) { + // If yes, replace it with the prefix (case-sensitive) + title = title.replace(new RegExp(`^${prefix}`, 'i'), prefix) + } else { + // If not, prepend the prefix to the title + title = prefix + ": " + title + } + } + } + + // Update the PR title, if changed + if (title != originalTitle ) { + try { + github.rest.pulls.update({ + pull_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + title: title + }); + } + catch (err) { + core.info("Update PR title failed: " + err.message) + } + } diff --git a/.github/workflows/label.yml b/.github/workflows/label.yml deleted file mode 100644 index 92b13d05de..0000000000 --- a/.github/workflows/label.yml +++ /dev/null @@ -1,21 +0,0 @@ -# This workflow will triage pull requests and apply a label based on the -# paths that are modified in the pull request. -# -# To use this workflow, you will need to set up a .github/labeler.yml -# file with configuration. For more information, see: -# https://github.com/actions/labeler - -name: Labeler -on: [pull_request_target] - -jobs: - label: - runs-on: ubuntu-latest - permissions: - contents: read - pull-requests: write - - steps: - - uses: actions/labeler@v4 - with: - repo-token: "${{ secrets.GITHUB_TOKEN }}"