Skip to content

Commit b06c8a8

Browse files
committed
CICD: Generate Patch for each PR (#22)
1 parent 33a041c commit b06c8a8

File tree

2 files changed

+114
-32
lines changed

2 files changed

+114
-32
lines changed

.github/workflows/format-patch.sh

Lines changed: 61 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ if [[ $(jq --version 1>/dev/null 2>&1 && echo yes) != "yes" ]]; then
99
exit 1
1010
fi
1111

12-
if [ -z "$1" ]; then
12+
PR_NUMBER="$1"
13+
PATCH_FILE="$2"
14+
if [ -z "$PR_NUMBER" ]; then
1315
echo "Please provide a PR link or number. For example: https://github.com/ossrs/ffmpeg-webrtc/pull/20"
1416
exit 1
1517
fi
@@ -29,92 +31,119 @@ echo "Fetching PR #$PR_NUMBER from $PR_URL"
2931
PR_DATA=$(curl -s "https://api.github.com/repos/ossrs/ffmpeg-webrtc/pulls/$PR_NUMBER")
3032
REPO_NAME=$(printf '%s' "$PR_DATA" | jq -r '.head.repo.full_name')
3133
BRANCH_NAME=$(printf '%s' "$PR_DATA" | jq -r '.head.ref')
34+
echo "Repository: $REPO_NAME, Branch: $BRANCH_NAME"
3235
if [[ -z "$REPO_NAME" || -z "$BRANCH_NAME" ]]; then
3336
echo "Error: REPO_NAME or BRANCH_NAME is empty!"
3437
exit 1
3538
fi
36-
echo "Repository: $REPO_NAME, Branch: $BRANCH_NAME"
3739

3840
PR_TITLE=$(printf '%s' "$PR_DATA" | jq -r '.title')
3941
PR_DESCRIPTION=$(printf '%s' "$PR_DATA" | jq -r '.body // ""')
40-
if [[ -z "$PR_TITLE" ]]; then
41-
echo "Error: PR title is empty!"
42-
exit 1
43-
fi
44-
4542
echo "PR information:"
4643
echo "==================================================================="
4744
echo "$PR_TITLE"
4845
echo "$PR_DESCRIPTION"
4946
echo "==================================================================="
5047
echo ""
48+
if [[ -z "$PR_TITLE" ]]; then
49+
echo "Error: PR title is empty!"
50+
exit 1
51+
fi
5152

52-
set -euxo pipefail
53-
54-
git checkout workflows
55-
echo "Switched to workflows branch."
56-
53+
git checkout workflows &&
54+
echo "Switched to workflows branch." &&
5755
git pull
5856
echo "Pulled latest changes from workflows branch."
57+
if [[ $? -ne 0 ]]; then
58+
echo "Failed to switch to workflows branch or pull latest changes."
59+
exit 1
60+
fi
5961

60-
REMOTE_NAME=patch-tmp
62+
REMOTE_NAME=patch-tmp &&
6163
if git remote | grep -q "^$REMOTE_NAME$"; then
6264
git remote rm "$REMOTE_NAME"
63-
fi
64-
git remote add $REMOTE_NAME https://github.com/${REPO_NAME}.git
65-
git fetch $REMOTE_NAME $BRANCH_NAME
65+
fi &&
66+
git remote add $REMOTE_NAME https://github.com/${REPO_NAME}.git &&
67+
git fetch $REMOTE_NAME $BRANCH_NAME &&
6668
echo "Fetch remote $REMOTE_NAME at $(git remote get-url $REMOTE_NAME)"
69+
if [[ $? -ne 0 ]]; then
70+
echo "Failed to fetch remote branch $BRANCH_NAME from $REMOTE_NAME."
71+
exit 1
72+
fi
6773

68-
TMP_BRANCH=tmp-branch-for-patch-$PR_NUMBER
74+
TMP_BRANCH=tmp-branch-for-patch-$PR_NUMBER &&
6975
if git branch --list "$TMP_BRANCH" | grep -q "^..$TMP_BRANCH$"; then
7076
git branch -D "$TMP_BRANCH"
71-
fi
72-
git checkout -b $TMP_BRANCH $REMOTE_NAME/$BRANCH_NAME
77+
fi &&
78+
git checkout -b $TMP_BRANCH $REMOTE_NAME/$BRANCH_NAME &&
7379
echo "Checkout branch $TMP_BRANCH from $REMOTE_NAME/$BRANCH_NAME"
80+
if [[ $? -ne 0 ]]; then
81+
echo "Failed to checkout branch $TMP_BRANCH from $REMOTE_NAME/$BRANCH_NAME."
82+
exit 1
83+
fi
7484

7585
FIRST_AUTHOR_NAME=$(git log workflows..HEAD --reverse --format='%an' | head -n1)
7686
FIRST_AUTHOR_EMAIL=$(git log workflows..HEAD --reverse --format='%ae' | head -n1)
7787
echo "Author: $FIRST_AUTHOR_NAME <$FIRST_AUTHOR_EMAIL>"
88+
if [[ -z "$FIRST_AUTHOR_NAME" || -z "$FIRST_AUTHOR_EMAIL" ]]; then
89+
echo "Error: Unable to determine the first author of the PR."
90+
exit 1
91+
fi
7892

7993
COAUTHORS=$(git log workflows..HEAD --format='Co-authored-by: %an <%ae>' |grep -v "$FIRST_AUTHOR_NAME" | sort -u)
8094
COAUTHOR_COUNT=$(echo "$COAUTHORS" | wc -l)
81-
if [ "$COAUTHOR_COUNT" -gt 0 ]; then
95+
if [[ "$COAUTHOR_COUNT" -gt 0 ]]; then
8296
echo "$COAUTHORS"
8397
fi
8498

8599
COMMIT_MSG="$PR_TITLE"
86-
if [ -n "$PR_DESCRIPTION" ]; then
100+
if [[ -n "$PR_DESCRIPTION" ]]; then
87101
COMMIT_MSG="$COMMIT_MSG\n\n$PR_DESCRIPTION"
88102
fi
89103

90-
if [ "$COAUTHOR_COUNT" -gt 0 ]; then
104+
if [[ "$COAUTHOR_COUNT" -gt 0 ]]; then
91105
COMMIT_MSG="$COMMIT_MSG\n"
92106
COMMIT_MSG="$COMMIT_MSG\n$COAUTHORS"
93107
fi
94108

95109
echo "Commit information:"
96110
echo "Author: $FIRST_AUTHOR_NAME <$FIRST_AUTHOR_EMAIL>"
97111
echo "==================================================================="
98-
echo -e "$COMMIT_MSG"
112+
echo -n -e "$COMMIT_MSG"
99113
echo "==================================================================="
100114
echo ""
101115

102-
git rebase workflows
103-
git reset --soft workflows
104-
git commit --author "$FIRST_AUTHOR_NAME <$FIRST_AUTHOR_EMAIL>" -m "$(echo -e "$COMMIT_MSG")"
105-
echo "Squashed commits into a single commit."
116+
git rebase workflows &&
117+
git reset --soft workflows &&
118+
git commit --author "$FIRST_AUTHOR_NAME <$FIRST_AUTHOR_EMAIL>" -m "$(echo -n -e "$COMMIT_MSG")" &&
119+
echo "Squashed commits into a single commit." &&
120+
if [[ $? -ne 0 ]]; then
121+
echo "Failed to rebase or commit changes."
122+
exit 1
123+
fi
124+
125+
git branch -vv &&
106126
git log -1 --pretty=format:"%an <%ae> %h %s"
127+
if [[ $? -ne 0 ]]; then
128+
echo "Failed to display branch information or last commit."
129+
exit 1
130+
fi
107131

108-
PATCH_FILE="patch-$PR_NUMBER-$(date +%s).patch"
109-
rm -f $PATCH_FILE
110-
git format-patch -1 --stdout > $PATCH_FILE
132+
if [[ -z "$PATCH_FILE" ]]; then
133+
PATCH_FILE="whip-patch-$PR_NUMBER-$(date +%s).patch"
134+
fi &&
135+
rm -f $PATCH_FILE &&
136+
git format-patch -1 --stdout > $PATCH_FILE &&
137+
echo "Created patch file: $PATCH_FILE"
138+
if [[ $? -ne 0 ]]; then
139+
echo "Failed to create patch file."
140+
exit 1
141+
fi
111142

112143
git checkout workflows
113144
#git br -D $TMP_BRANCH
114145
#echo "Removed temporary branch $TMP_BRANCH."
115146

116-
set +e +u +x +o pipefail
117-
118147
echo ""
119148
echo "Patch file created: $PATCH_FILE"
120149
echo ""

.github/workflows/test.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,58 @@ jobs:
633633
run: exit 1
634634
runs-on: ubuntu-22.04
635635

636+
generate-patch:
637+
name: "Generate Patch"
638+
if: ${{ github.event_name == 'pull_request' }}
639+
steps:
640+
# Checkout to workflows branch, make sure the base branch is available.
641+
- name: Checkout repository with workflows branch
642+
uses: actions/checkout@v4
643+
with:
644+
ref: workflows
645+
fetch-depth: 0
646+
- name: Try to checkout to workflows branch
647+
run: |
648+
set -euxo pipefail
649+
git checkout workflows
650+
git branch -vv
651+
# Checkout to PR commit, use the lastest script.
652+
- name: Checkout repository to PR commit
653+
uses: actions/checkout@v4
654+
- name: Show Git Info
655+
run: |
656+
set -euxo pipefail
657+
git branch -vv
658+
echo "Repository: ${{ github.repository }}"
659+
echo "Ref: ${{ github.ref }}"
660+
echo "Event Name: ${{ github.event_name }}"
661+
echo "Pull Request Number: ${{ github.event.pull_request.number }}"
662+
- name: Install Dependencies
663+
run: |
664+
set -euxo pipefail
665+
sudo apt-get update
666+
sudo apt-get install -y jq
667+
- name: Run Script
668+
id: format_patch
669+
run: |
670+
set -euxo pipefail
671+
672+
PR_NUMBER=${{ github.event.pull_request.number }}
673+
PATCH_FILENAME="whip-patch-$PR_NUMBER-$(date +%s)"
674+
echo "PR ID is ${{ github.event.pull_request.number }}"
675+
echo "Patch file is $PATCH_FILENAME.patch"
676+
677+
bash .github/workflows/format-patch.sh $PR_NUMBER "$PATCH_FILENAME.patch"
678+
echo "patch_file=$PATCH_FILENAME" >> $GITHUB_OUTPUT
679+
- name: Upload all patch files
680+
uses: actions/upload-artifact@v4
681+
with:
682+
name: ${{ steps.format_patch.outputs.patch_file }}
683+
path: |
684+
whip-*.patch
685+
retention-days: 90
686+
runs-on: ubuntu-22.04
687+
636688
test-done:
637689
needs:
638690
- fate
@@ -643,6 +695,7 @@ jobs:
643695
- openssl-1-1-0h
644696
- openssl-3-0
645697
- openssl-latest
698+
- generate-patch
646699
steps:
647700
- run: echo 'All done'
648701
runs-on: ubuntu-22.04

0 commit comments

Comments
 (0)