-
Notifications
You must be signed in to change notification settings - Fork 7
54 lines (47 loc) · 2.26 KB
/
_check-commit-count.yml
File metadata and controls
54 lines (47 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
name: CI
on:
workflow_call:
permissions:
pull-requests: read
issues: write
jobs:
check-commits:
runs-on: ubuntu-latest
steps:
- name: Check number of commits in PR
id: commits
uses: actions/github-script@v8
with:
script: |
const pr = context.payload.pull_request;
if (!pr) {
core.setFailed("This reusable workflow must be called from a pull_request workflow.");
return;
}
const commits = await github.rest.pulls.listCommits({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
});
core.setOutput("count", commits.data.length);
core.setOutput("from_fork", pr.head.repo.full_name !== pr.base.repo.full_name);
- name: Comment for internal PRs
if: steps.commits.outputs.count < 2 && steps.commits.outputs.from_fork == 'false'
uses: actions/github-script@v8
with:
script: |
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
body: '❗ This pull request has fewer than 2 commits.\n\nYou can add an empty commit with the following command:\n\n```bash\ngit commit -m "Empty commit" --allow-empty\ngit push\n```\n\nPlease update the PR accordingly.'
});
- name: Echo warning for external PRs
if: steps.commits.outputs.count < 2 && steps.commits.outputs.from_fork == 'true'
run: |
echo "⚠️ External PR detected with fewer than 2 commits. To add a commit, run: git commit -m \"Empty commit\" --allow-empty && git push."
- name: Fail the job if fewer than 2 commits
if: steps.commits.outputs.count < 2
run: |
echo "❌ Failing the job because there are fewer than 2 commits."
exit 1