-
Notifications
You must be signed in to change notification settings - Fork 6.6k
53 lines (45 loc) · 1.66 KB
/
clean-commit-messages.yml
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
name: Clean PR Commit Messages
on:
pull_request:
types: [opened, synchronize]
jobs:
clean-commit-message:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.ref }}
- name: Extract PR commits
id: extract_pr_commits
uses: actions/github-script@v7
with:
script: |
const commits = await github.rest.pulls.listCommits({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number
});
return {
commits: commits.data.map(commit => commit.sha).join('\n')
};
- name: Clean commit messages
run: |
commit_shas=$(echo "${{ steps.extract_pr_commits.outputs.commits }}" | tr '\n' ' ')
cleaned_messages=""
for sha in $commit_shas; do
message=$(git log --format=%B -n 1 $sha)
cleaned_message=$(echo "$message" | sed 's/<!--.*-->//g' | sed '/^$/d')
cleaned_messages+="$cleaned_message"$'\n'
done
git reset --soft HEAD~$(echo $commit_shas | wc -w)
IFS=$'\n'
for cleaned_message in ${cleaned_messages}; do
git commit -m "${cleaned_message}"
done
- name: Push cleaned commit messages
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_BRANCH: ${{ github.event.pull_request.head.ref }}
run: |
git push origin HEAD:$PR_BRANCH --force-with-lease