pull-request-label.yml 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. name: Pull Request Maintenance
  2. on:
  3. pull_request_target:
  4. # make sure that when the PR changes, we also update
  5. types:
  6. - opened
  7. - edited
  8. - synchronize
  9. - reopened
  10. permissions:
  11. contents: read
  12. jobs:
  13. conventional-commit-labeler:
  14. name: Label PR based on Conventional Commit Specification
  15. permissions:
  16. contents: read
  17. pull-requests: write
  18. runs-on: ubuntu-latest
  19. steps:
  20. - uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v7
  21. env:
  22. # If extended, don't forget to also include it in the verification step verify-labels.
  23. TYPE_TO_LABEL: |
  24. {
  25. "feat":"kind/feature",
  26. "fix":"kind/bug",
  27. "chore":"kind/chore",
  28. "ref":"kind/refactor",
  29. "clean":"kind/cleanup",
  30. "design":"kind/design",
  31. "docs":"kind/documentation",
  32. "test":"kind/testing",
  33. "perf":"kind/performance"
  34. }
  35. with:
  36. script: |
  37. console.log("Verify that the PR title follows the Conventional Commit format");
  38. // Parse mappings from environment variables
  39. const typeToLabel = JSON.parse(process.env.TYPE_TO_LABEL);
  40. console.log("Type-to-Label Mapping:", typeToLabel);
  41. // Dynamically generate allowed types
  42. const allowedTypes = Object.keys(typeToLabel).join('|');
  43. console.log(`Allowed Types: ${allowedTypes}`);
  44. const prTitle = context.payload.pull_request.title;
  45. console.log(`PR Title: ${prTitle}`);
  46. // We know this regex looks scary, but it's just to match the Conventional Commit format
  47. // It parses out a Title into several named regex groups, which we can use to extract various semantic patterns:
  48. // - type: The type of change (feat, fix, etc.)
  49. // - scope: The scope of the change (optional and set in brackets)
  50. // - breaking: A flag to indicate a breaking change (!)
  51. // - subject: The subject of the change
  52. // Example: feat(scope)!: add new feature
  53. // ^^^^ ^^^^^ ^ ^^^^^^^^^^^^^^^
  54. // type scope subject
  55. const regex = new RegExp(
  56. `^(((Initial commit)|(Merge [^\\r\\n]+(\\s)[^\\r\\n]+((\\s)((\\s)[^\\r\\n]+)+)*(\\s)?)|^((?<type>${allowedTypes})(\\((?<scope>[\\w\\-]+)\\))?(?<breaking>!?): (?<subject>[^\\r\\n]+((\\s)((\\s)[^\\r\\n]+)+)*))(\\s)?)$)`
  57. );
  58. console.log(`Regex: ${regex}`);
  59. const match = prTitle.match(regex);
  60. console.log(`Match: ${match != null}`);
  61. if (match && match.groups) {
  62. const { type, scope, breaking } = match.groups;
  63. // Initialize labels array
  64. const labels = [];
  65. if (breaking) {
  66. console.log("Adding breaking change label");
  67. labels.push("breaking-change");
  68. }
  69. // Add type-based label
  70. if (type && typeToLabel[type]) {
  71. labels.push(typeToLabel[type]);
  72. } else {
  73. console.log(`No label found for type: ${type}`);
  74. }
  75. // Add scope-based label. If no scope is provided, we don't add a label.
  76. // This action will just fail if the label doesn't exist.
  77. if (scope) {
  78. labels.push(`area/${scope}`);
  79. }
  80. if (labels.length > 0) {
  81. console.log(`Adding labels: ${labels}`);
  82. await github.rest.issues.addLabels({
  83. owner: context.repo.owner,
  84. repo: context.repo.repo,
  85. issue_number: context.payload.pull_request.number,
  86. labels: labels,
  87. });
  88. } else {
  89. console.log("No labels to add.");
  90. }
  91. } else {
  92. console.log("Invalid PR title format. Make sure you named the PR after the specification at https://www.conventionalcommits.org/en/v1.0.0/#specification. Exiting...");
  93. process.exit(1);
  94. }
  95. labeler:
  96. name: Label PR based on Config
  97. permissions:
  98. contents: read
  99. pull-requests: write
  100. runs-on: ubuntu-latest
  101. steps:
  102. - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
  103. with:
  104. sparse-checkout: |
  105. .github/config/labeler.yml
  106. persist-credentials: false
  107. - uses: actions/labeler@634933edcd8ababfe52f92936142cc22ac488b1b # v5
  108. with:
  109. configuration-path: .github/config/labeler.yml
  110. size-labeler:
  111. runs-on: ubuntu-latest
  112. name: Label PR based on size
  113. permissions:
  114. issues: write
  115. pull-requests: write
  116. steps:
  117. - uses: codelytv/pr-size-labeler@095a41fca88b8764fd9e008ad269bcdb82bb38b9 # v1
  118. with:
  119. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  120. xs_label: 'size/xs'
  121. xs_max_size: '10'
  122. s_label: 'size/s'
  123. s_max_size: '100'
  124. m_label: 'size/m'
  125. m_max_size: '500'
  126. l_label: 'size/l'
  127. l_max_size: '10000'
  128. xl_label: 'size/xl'
  129. fail_if_xl: 'false'
  130. message_if_xl: >
  131. This PR exceeds the recommended size of 10000 lines.
  132. Please make sure you are NOT addressing multiple issues with one PR.
  133. Note this PR might be rejected due to its size.
  134. verify-labels:
  135. needs: [labeler, size-labeler, conventional-commit-labeler]
  136. name: verify labels
  137. runs-on: ubuntu-latest
  138. permissions:
  139. contents: read
  140. pull-requests: read
  141. steps:
  142. - name: PRs should have at least one qualifying label
  143. uses: docker://agilepathway/pull-request-label-checker:latest@sha256:14f5f3dfda922496d07d53494e2d2b42885165f90677a1c03d600059b7706a61
  144. with:
  145. any_of: kind/chore,kind/bug,kind/feature,kind/dependency,kind/refactor,kind/design,kind/documentation,kind/testing,kind/performance,kind/cleanup
  146. repo_token: ${{ secrets.GITHUB_TOKEN }}