|
1 | | -# github-custom-action-examples |
2 | | -Examples of custom GitHub Actions |
| 1 | +# PR Size Checker |
| 2 | + |
| 3 | +A reusable GitHub Action that automatically checks Pull Request size and adds appropriate labels. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Automatically calculates total lines changed (additions + deletions) |
| 8 | +- Adds size-based labels: `small`, `medium`, `large`, `extra-large` |
| 9 | +- Automatically creates labels if they don't exist in the repository |
| 10 | +- Removes old labels when size changes |
| 11 | +- Comments on large PRs suggesting split (optional) |
| 12 | +- Configurable with custom thresholds |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +### Basic Setup |
| 17 | + |
| 18 | +Create a `.github/workflows/pr-size-check.yml` file in your repository: |
| 19 | + |
| 20 | +```yaml |
| 21 | +name: PR Size Check |
| 22 | + |
| 23 | +on: |
| 24 | + pull_request: |
| 25 | + types: [opened, synchronize, reopened] |
| 26 | + |
| 27 | +jobs: |
| 28 | + check-pr-size: |
| 29 | + runs-on: ubuntu-latest |
| 30 | + name: Check PR Size |
| 31 | + steps: |
| 32 | + - name: Check PR Size |
| 33 | + uses: automationDojo/github-custom-action-examples@main |
| 34 | + with: |
| 35 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 36 | +``` |
| 37 | +
|
| 38 | +### Advanced Configuration |
| 39 | +
|
| 40 | +```yaml |
| 41 | +name: PR Size Check |
| 42 | + |
| 43 | +on: |
| 44 | + pull_request: |
| 45 | + types: [opened, synchronize, reopened] |
| 46 | + |
| 47 | +jobs: |
| 48 | + check-pr-size: |
| 49 | + runs-on: ubuntu-latest |
| 50 | + name: Check PR Size |
| 51 | + steps: |
| 52 | + - name: Check PR Size |
| 53 | + uses: automationDojo/github-custom-action-examples@main |
| 54 | + with: |
| 55 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 56 | + small-threshold: 100 # PRs up to 100 lines = small |
| 57 | + medium-threshold: 300 # PRs up to 300 lines = medium |
| 58 | + large-threshold: 600 # PRs up to 600 lines = large |
| 59 | + comment-on-large: true # Comment on large PRs |
| 60 | +``` |
| 61 | +
|
| 62 | +## Inputs |
| 63 | +
|
| 64 | +| Input | Description | Required | Default | |
| 65 | +|-------|-----------|-------------|---------| |
| 66 | +| `github-token` | GitHub token for API calls | Yes | - | |
| 67 | +| `small-threshold` | Maximum lines changed for a small PR | No | `100` | |
| 68 | +| `medium-threshold` | Maximum lines changed for a medium PR | No | `300` | |
| 69 | +| `large-threshold` | Maximum lines changed for a large PR | No | `600` | |
| 70 | +| `comment-on-large` | Whether to comment on large PRs | No | `true` | |
| 71 | + |
| 72 | +## Outputs |
| 73 | + |
| 74 | +| Output | Description | |
| 75 | +|--------|-----------| |
| 76 | +| `size-label` | Label applied to the PR (small, medium, large, extra-large) | |
| 77 | +| `lines-changed` | Total number of lines changed | |
| 78 | + |
| 79 | +## Example Usage with Outputs |
| 80 | + |
| 81 | +```yaml |
| 82 | +- name: Check PR Size |
| 83 | + id: pr-size |
| 84 | + uses: automationDojo/github-custom-action-examples@main |
| 85 | + with: |
| 86 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 87 | +
|
| 88 | +- name: Display PR Size |
| 89 | + run: | |
| 90 | + echo "PR Size: ${{ steps.pr-size.outputs.size-label }}" |
| 91 | + echo "Lines Changed: ${{ steps.pr-size.outputs.lines-changed }}" |
| 92 | +``` |
| 93 | + |
| 94 | +## Labels Created |
| 95 | + |
| 96 | +The action automatically creates the following labels: |
| 97 | + |
| 98 | +| Label | Color | Description | |
| 99 | +|-------|-----|-----------| |
| 100 | +| `small` | Green | Small PRs, easy to review | |
| 101 | +| `medium` | Yellow | Medium-sized PRs | |
| 102 | +| `large` | Orange | Large PRs, consider splitting | |
| 103 | +| `extra-large` | Red | Very large PRs, splitting recommended | |
| 104 | + |
| 105 | +## How It Works |
| 106 | + |
| 107 | +1. The action is triggered on pull request events |
| 108 | +2. Calculates total lines changed (additions + deletions) |
| 109 | +3. Determines size based on configured thresholds |
| 110 | +4. Removes old size labels |
| 111 | +5. Adds the appropriate new label |
| 112 | +6. If the PR is large or extra-large, adds a comment suggesting split |
| 113 | + |
| 114 | +## Local Development |
| 115 | + |
| 116 | +### Prerequisites |
| 117 | + |
| 118 | +- Node.js 20 or higher |
| 119 | +- npm |
| 120 | + |
| 121 | +### Setup |
| 122 | + |
| 123 | +```bash |
| 124 | +npm install |
| 125 | +``` |
| 126 | + |
| 127 | +### File Structure |
| 128 | + |
| 129 | +``` |
| 130 | +github-custom-action-examples/ |
| 131 | +├── action.yml # Action metadata |
| 132 | +├── index.js # Main logic |
| 133 | +├── package.json # Dependencies |
| 134 | +├── README.md # Documentation |
| 135 | +└── .github/ |
| 136 | + └── workflows/ |
| 137 | + ├── pr-size-check.yml # Local usage example |
| 138 | + └── pr-size-check-external.yml # External usage example |
| 139 | +``` |
| 140 | +
|
| 141 | +## Contributing |
| 142 | +
|
| 143 | +Contributions are welcome! Please: |
| 144 | +
|
| 145 | +1. Fork the repository |
| 146 | +2. Create a branch for your feature |
| 147 | +3. Commit your changes |
| 148 | +4. Open a Pull Request |
| 149 | +
|
| 150 | +## License |
| 151 | +
|
| 152 | +MIT |
| 153 | +
|
| 154 | +## Author |
| 155 | +
|
| 156 | +AutomationDojo |
| 157 | +
|
| 158 | +## Useful Links |
| 159 | +
|
| 160 | +- [GitHub Actions Documentation](https://docs.github.com/en/actions) |
| 161 | +- [Creating a JavaScript Action](https://docs.github.com/en/actions/creating-actions/creating-a-javascript-action) |
0 commit comments