pull-request-label.yml 5.9 KB

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