pull-request-label.yml 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. pull-requests: write
  12. issues: write
  13. jobs:
  14. conventional-commit-labeler:
  15. name: Label PR based on Conventional Commit Specification
  16. permissions:
  17. contents: read
  18. pull-requests: write
  19. runs-on: ubuntu-latest
  20. steps:
  21. - uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v7
  22. env:
  23. # If extended, don't forget to also include it in the verification step verify-labels.
  24. TYPE_TO_LABEL: |
  25. {
  26. "feat":"kind/feature",
  27. "fix":"kind/bug",
  28. "chore":"kind/chore",
  29. "ref":"kind/refactor",
  30. "clean":"kind/cleanup",
  31. "design":"kind/design",
  32. "docs":"kind/documentation",
  33. "test":"kind/testing",
  34. "perf":"kind/performance"
  35. }
  36. with:
  37. script: |
  38. console.log("Verify that the PR title follows the Conventional Commit format");
  39. // Parse mappings from environment variables
  40. const typeToLabel = JSON.parse(process.env.TYPE_TO_LABEL);
  41. console.log("Type-to-Label Mapping:", typeToLabel);
  42. // Dynamically generate allowed types
  43. const allowedTypes = Object.keys(typeToLabel).join('|');
  44. console.log(`Allowed Types: ${allowedTypes}`);
  45. const prTitle = context.payload.pull_request.title;
  46. console.log(`PR Title: ${prTitle}`);
  47. // We know this regex looks scary, but it's just to match the Conventional Commit format
  48. // It parses out a Title into several named regex groups, which we can use to extract various semantic patterns:
  49. // - type: The type of change (feat, fix, etc.)
  50. // - scope: The scope of the change (optional and set in brackets)
  51. // - breaking: A flag to indicate a breaking change (!)
  52. // - subject: The subject of the change
  53. // Example: feat(scope)!: add new feature
  54. // ^^^^ ^^^^^ ^ ^^^^^^^^^^^^^^^
  55. // type scope subject
  56. const regex = new RegExp(
  57. `^(((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)?)$)`
  58. );
  59. console.log(`Regex: ${regex}`);
  60. const match = prTitle.match(regex);
  61. console.log(`Match: ${match != null}`);
  62. if (match && match.groups) {
  63. const { type, scope, breaking } = match.groups;
  64. // Initialize labels array
  65. const labels = [];
  66. if (breaking) {
  67. console.log("Adding breaking change label");
  68. labels.push(process.env.BREAKING_CHANGE_LABEL);
  69. }
  70. // Add type-based label
  71. if (type && typeToLabel[type]) {
  72. labels.push(typeToLabel[type]);
  73. } else {
  74. console.log(`No label found for type: ${type}`);
  75. }
  76. // Add scope-based label. If no scope is provided, we don't add a label.
  77. // This action will just fail if the label doesn't exist.
  78. if (scope) {
  79. labels.push(`area/${scope}`);
  80. }
  81. if (labels.length > 0) {
  82. console.log(`Adding labels: ${labels}`);
  83. await github.rest.issues.addLabels({
  84. owner: context.repo.owner,
  85. repo: context.repo.repo,
  86. issue_number: context.payload.pull_request.number,
  87. labels: labels,
  88. });
  89. } else {
  90. console.log("No labels to add.");
  91. }
  92. } else {
  93. 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...");
  94. process.exit(1);
  95. }
  96. labeler:
  97. name: Label PR based on Config
  98. permissions:
  99. contents: read
  100. pull-requests: write
  101. runs-on: ubuntu-latest
  102. steps:
  103. - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
  104. with:
  105. sparse-checkout: |
  106. .github/config/labeler.yml
  107. - uses: actions/labeler@8558fd74291d67161a8a78ce36a881fa63b766a9 # 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@4ec67706cd878fbc1c8db0a5dcd28b6bb412e85a # 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. steps:
  139. - name: PRs should have at least one qualifying label
  140. uses: docker://agilepathway/pull-request-label-checker:latest@sha256:14f5f3dfda922496d07d53494e2d2b42885165f90677a1c03d600059b7706a61
  141. with:
  142. any_of: kind/chore,kind/bug,kind/feature,kind/dependency,kind/refactor,kind/design,kind/documentation,kind/testing,kind/performance,kind/cleanup
  143. repo_token: ${{ secrets.GITHUB_TOKEN }}