pr_labels.yaml 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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@v6
  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. for (let pair of mapping) {
  51. if (!description.includes(pair[0])) continue;
  52. // add label
  53. try {
  54. const result = await github.rest.issues.addLabels({
  55. owner,
  56. repo,
  57. issue_number: prNumber,
  58. labels: [pair[1]]
  59. });
  60. console.log('Added label', pair[1]);
  61. } catch(e) {
  62. console.log(e);
  63. }
  64. break;
  65. }