pr_labels.yaml 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. name: PR-labels
  2. on:
  3. pull_request_target:
  4. branches:
  5. - 'main'
  6. types:
  7. - 'opened'
  8. - 'edited'
  9. concurrency:
  10. group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
  11. cancel-in-progress: true
  12. jobs:
  13. update-labels:
  14. runs-on: ubuntu-latest
  15. steps:
  16. - name: Update PR labels
  17. id: update-pr-labels
  18. uses: actions/github-script@v7
  19. with:
  20. github-token: ${{ secrets.GITHUB_TOKEN }}
  21. script: |
  22. const { owner, repo } = context.repo;
  23. const prNumber = context.payload.pull_request.number;
  24. const description = context.payload.pull_request.body;
  25. const mapping = [
  26. ['* New feature', 'new-feature'],
  27. ['* Experimental feature', 'experimental-feature'],
  28. ['* Improvement', 'improvement'],
  29. ['* Performance improvement', 'performance'],
  30. ['* Bugfix', 'bugfix'],
  31. ['* Backward incompatible change', 'backward-incompatible'],
  32. ['* Documentation', 'documentation'],
  33. ['* Not for changelog', 'not-for-changelog']
  34. ];
  35. // remove all labels owned by current script
  36. for (let pair of mapping) {
  37. try {
  38. const result = await github.rest.issues.removeLabel({
  39. owner,
  40. repo,
  41. issue_number: prNumber,
  42. name: pair[1]
  43. });
  44. console.log('Removed label', pair[1]);
  45. } catch(e) {
  46. console.log(e);
  47. }
  48. }
  49. // add first encountered label
  50. if (!description) return;
  51. for (let pair of mapping) {
  52. if (!description.includes(pair[0])) continue;
  53. // add label
  54. try {
  55. const result = await github.rest.issues.addLabels({
  56. owner,
  57. repo,
  58. issue_number: prNumber,
  59. labels: [pair[1]]
  60. });
  61. console.log('Added label', pair[1]);
  62. } catch(e) {
  63. console.log(e);
  64. }
  65. break;
  66. }