pr_labels.yaml 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. ['* User Interface', 'ui'],
  31. ['* Bugfix', 'bugfix'],
  32. ['* Backward incompatible change', 'backward-incompatible'],
  33. ['* Documentation', 'documentation'],
  34. ['* Not for changelog', 'not-for-changelog']
  35. ];
  36. // remove all labels owned by current script
  37. for (let pair of mapping) {
  38. try {
  39. const result = await github.rest.issues.removeLabel({
  40. owner,
  41. repo,
  42. issue_number: prNumber,
  43. name: pair[1]
  44. });
  45. console.log('Removed label', pair[1]);
  46. } catch(e) {
  47. console.log(e);
  48. }
  49. }
  50. // add first encountered label
  51. if (!description) return;
  52. for (let pair of mapping) {
  53. if (!description.includes(pair[0])) continue;
  54. // add label
  55. try {
  56. const result = await github.rest.issues.addLabels({
  57. owner,
  58. repo,
  59. issue_number: prNumber,
  60. labels: [pair[1]]
  61. });
  62. console.log('Added label', pair[1]);
  63. } catch(e) {
  64. console.log(e);
  65. }
  66. break;
  67. }