pull-request-label.yml 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7
  22. env:
  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(process.env.BREAKING_CHANGE_LABEL);
  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@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
  103. with:
  104. sparse-checkout: |
  105. .github/config/labeler.yml
  106. - uses: actions/labeler@8558fd74291d67161a8a78ce36a881fa63b766a9 # v5
  107. with:
  108. configuration-path: .github/config/labeler.yml
  109. size-labeler:
  110. runs-on: ubuntu-latest
  111. name: Label PR based on size
  112. permissions:
  113. issues: write
  114. pull-requests: write
  115. steps:
  116. - uses: codelytv/pr-size-labeler@4ec67706cd878fbc1c8db0a5dcd28b6bb412e85a # v1
  117. with:
  118. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  119. xs_label: 'size/xs'
  120. xs_max_size: '10'
  121. s_label: 'size/s'
  122. s_max_size: '100'
  123. m_label: 'size/m'
  124. m_max_size: '500'
  125. l_label: 'size/l'
  126. l_max_size: '10000'
  127. xl_label: 'size/xl'
  128. fail_if_xl: 'false'
  129. message_if_xl: >
  130. This PR exceeds the recommended size of 10000 lines.
  131. Please make sure you are NOT addressing multiple issues with one PR.
  132. Note this PR might be rejected due to its size.
  133. verify-labels:
  134. needs: [labeler, size-labeler, conventional-commit-labeler]
  135. name: verify labels
  136. runs-on: ubuntu-latest
  137. steps:
  138. - name: PRs should have at least one qualifying label
  139. uses: docker://agilepathway/pull-request-label-checker:latest@sha256:14f5f3dfda922496d07d53494e2d2b42885165f90677a1c03d600059b7706a61
  140. with:
  141. any_of: kind/chore,kind/bug,kind/feature,kind/dependency,kind/refactor,kind/design
  142. repo_token: ${{ secrets.GITHUB_TOKEN }}