2
0

labels.yml 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. name: Labels
  2. on:
  3. pull_request_target:
  4. types: [ edited, opened, reopened, ready_for_review ]
  5. concurrency:
  6. group: ${{ github.workflow }}-${{ github.ref }}
  7. cancel-in-progress: true
  8. permissions:
  9. pull-requests: write
  10. jobs:
  11. no_ticket:
  12. name: "Flag if no Trac ticket is found in the title"
  13. runs-on: ubuntu-latest
  14. steps:
  15. - uses: actions/checkout@v4
  16. - name: "Check title and manage labels"
  17. uses: actions/github-script@v7
  18. with:
  19. script: |
  20. const title = context.payload.pull_request.title;
  21. const regex = /#[0-9]+[ ,:]?/gm;
  22. const label = "no ticket";
  23. const hasMatch = regex.test(title);
  24. const labels = context.payload.pull_request.labels.map(l => l.name);
  25. const owner = context.repo.owner;
  26. const repo = context.repo.repo;
  27. const pr_number = context.payload.pull_request.number;
  28. console.log(`=> Pull Request Title: ${title}`);
  29. console.log(`=> Labels on PR: [${labels}]`);
  30. if (hasMatch && labels.includes(label)) {
  31. console.log(`==> Removing label "${label}" from PR #${pr_number}`);
  32. await github.rest.issues.removeLabel({
  33. owner,
  34. repo,
  35. issue_number: pr_number,
  36. name: label
  37. });
  38. } else if (!hasMatch && !labels.includes(label)) {
  39. console.log(`==> Adding label "${label}" to PR #${pr_number}`);
  40. await github.rest.issues.addLabels({
  41. owner,
  42. repo,
  43. issue_number: pr_number,
  44. labels: [label]
  45. });
  46. } else {
  47. console.log(`No action needed for PR #${pr_number}`);
  48. }