-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaction.yml
More file actions
86 lines (75 loc) · 3.53 KB
/
Copy pathaction.yml
File metadata and controls
86 lines (75 loc) · 3.53 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Copyright 2024 Craig Motlin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: 'Forbid Merge Commits'
description: 'A composite action to forbid merge commits in pull requests.'
branding:
icon: 'git-merge'
color: 'purple'
inputs:
fail-on-merge-commits:
description: 'Whether to fail the check when merge commits are found, or just warn'
required: false
default: 'true'
fail-notice:
description: 'Message to display when merge commits are found'
required: false
default: 'Refer to [handling failure messages](https://github.com/motlin/forbid-merge-commits-action#handling-failure-messages) for guidance.'
runs:
using: "composite"
steps:
- name: Check for Pull Request event
if: ${{ github.event_name != 'pull_request' }}
shell: bash
run: |
echo "::warning::This action is intended to be used in pull_request events only. The event type was '${{ github.event_name }}'. See: https://github.com/motlin/forbid-merge-commits-action#how-to-add-this-action-to-your-repository."
echo "## This action is intended to be used in pull_request events only. The event type was '${{ github.event_name }}'." >> $GITHUB_STEP_SUMMARY
echo "See: [How to add this action to your repository](https://github.com/motlin/forbid-merge-commits-action#how-to-add-this-action-to-your-repository)." >> $GITHUB_STEP_SUMMARY
exit 1
- name: Checkout code
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Check for merge commits
shell: bash
env:
FAIL_ON_MERGE_COMMITS: ${{ inputs.fail-on-merge-commits }}
FAIL_NOTICE: ${{ inputs.fail-notice }}
run: |
set -Eeuo pipefail
# Transform [text](url) -> url
unwrap_markdown_urls() {
echo "$1" | sed -E 's/\[([^\]]+)\]\(([^)]+)\)/\2/g'
}
MERGE_COMMITS=$(git log --merges --format=%H origin/${{github.base_ref}}..${{github.event.pull_request.head.sha}} --)
if [ -n "$MERGE_COMMITS" ]; then
ANNOTATION_LEVEL=$([ "$FAIL_ON_MERGE_COMMITS" = "true" ] && echo "error" || echo "warning")
CONSOLE_FAIL_NOTICE=$(unwrap_markdown_urls "$FAIL_NOTICE")
echo "::$ANNOTATION_LEVEL title=Found merge commits::Found merge commits. ${CONSOLE_FAIL_NOTICE}"
echo -e '## Found merge commits.\n\n${FAIL_NOTICE}\n' >> $GITHUB_STEP_SUMMARY
for COMMIT_HASH in $MERGE_COMMITS; do
echo "::warning::Merge commit: $COMMIT_HASH"
echo "::group::git show $COMMIT_HASH"
git show $COMMIT_HASH
echo '::endgroup::'
echo '' >> $GITHUB_STEP_SUMMARY
echo '```console' >> $GITHUB_STEP_SUMMARY
git show $COMMIT_HASH >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
done
if [ "$FAIL_ON_MERGE_COMMITS" = "true" ]; then
exit 1
fi
else
echo 'Success: No merge commits found'
fi