-
Notifications
You must be signed in to change notification settings - Fork 521
89 lines (74 loc) · 3.13 KB
/
Open_PR.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
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
87
88
89
name: Sync v.next with main
# PRs are completed with squash and merge option - this commit will be cherry-picked for v.next.
on:
push:
branches:
- main
jobs:
sync_branches:
runs-on: ubuntu-latest
steps:
# Clone just the main branch of the samples repo.
- name: Checkout code
uses: actions/checkout@v2
with:
ref: main
fetch-depth: 2
# Cherry-pick the most recent commit from main into v.next.
- name: Cherry-pick from main into v.next
env:
GITHUB_TOKEN: ${{ secrets.BOT_TOKEN }}
run: |
# Configure the bot's git identity.
git config --global user.name "ArcGIS Maps SDK [bot]"
git config --global user.email "[email protected]"
echo "Configured git identity for bot."
# Store the commit hash of the most recent commit from main.
TRIGGER_COMMIT_HASH=$(git rev-parse HEAD)
echo "Job triggered by commit: $TRIGGER_COMMIT_HASH."
# Store the commit message.
COMMIT_MESSAGE=$(gh api repos/Esri/arcgis-maps-sdk-dotnet-samples/commits/$TRIGGER_COMMIT_HASH | jq -r '.commit.message')
echo "Commit message:"
echo "$COMMIT_MESSAGE."
# If the commit message contains the string "no-branch-sync", don't open a PR.
if [[ $COMMIT_MESSAGE == *no-branch-sync* ]]; then
echo "PR author intended the commit to only target main."
exit 0
fi
# Name the target branch for new PR.
SYNC_BRANCH="sync/$TRIGGER_COMMIT_HASH"
# Pull v.next and branch off.
git fetch --depth=2 origin v.next
git switch v.next
git switch -c $SYNC_BRANCH
# Attempt to cherry-pick the commit.
git cherry-pick $TRIGGER_COMMIT_HASH || CANNOT_CHERRY_PICK=$?
# Terminate the job if cherry-picking failed. Output the merge conflict.
if [ -n "$CANNOT_CHERRY_PICK" ]; then
echo "Failed to cherry-pick commit."
exit 1
fi
# Check for a diff between the new branch and v.next.
if ! git diff --quiet v.next; then
echo "Diff detected."
# Push the new head branch to the remote.
git push -u origin $SYNC_BRANCH
# Find the author's GitHub username.
COMMIT_AUTHOR=$(gh api repos/Esri/arcgis-maps-sdk-dotnet-samples/commits/$TRIGGER_COMMIT_HASH | jq -r '.author.login')
# Try to open a PR.
gh pr create \
--title "[Automated] Sync v.next with main" \
--body "This PR was automaticaly opened to update v.next with the most recent commit from main." \
--head "$SYNC_BRANCH" \
--base "v.next" \
--reviewer "$COMMIT_AUTHOR" || CANNOT_OPEN_PR=$?
if [ -n "$CANNOT_OPEN_PR" ]; then
echo "Failed to open PR."
git branch -D $SYNC_BRANCH
else
echo "PR successfully opened."
fi
else
echo "No diff detected."
git branch -D $SYNC_BRANCH
fi