-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathcreate-merge-release-into-dev-pr.yml
More file actions
134 lines (113 loc) · 5 KB
/
Copy pathcreate-merge-release-into-dev-pr.yml
File metadata and controls
134 lines (113 loc) · 5 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
name: create-merge-release-into-dev-pr
on:
workflow_dispatch:
schedule:
- cron: "30 3 * * *" # Daily at 03:30
env:
BASE_BRANCH: dev
permissions: {}
jobs:
setup:
if: github.repository == 'opf/openproject'
runs-on: ubuntu-latest
outputs:
latest_release_branch: ${{ steps.find_latest_release.outputs.branch }}
steps:
- id: find_latest_release
env:
GITHUB_TOKEN: ${{ secrets.OPENPROJECTCI_GH_CORE_PAT }}
GITHUB_REPOSITORY: ${{ github.repository }}
run: |
BRANCH=$(gh api --paginate \
"repos/$GITHUB_REPOSITORY/branches?protected=true&per_page=100" --jq '.[].name' | \
grep '^release/' | sort --version-sort | tail -1
)
if [ "$BRANCH" = "" ]; then
echo "Invalid release branch found: $BRANCH"
exit 1
fi
echo "branch=${BRANCH}" >> $GITHUB_OUTPUT
merge-or-create-pr:
env:
RELEASE_BRANCH: ${{ needs.setup.outputs.latest_release_branch }}
permissions:
contents: write # for git push
pull-requests: write # for creating pull request
runs-on: ubuntu-latest
needs: setup
timeout-minutes: 5
steps:
- name: Checkout repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
with:
ref: ${{ env.BASE_BRANCH }}
- name: Fetch dev branch upto release branch
run: git fetch --shallow-exclude "$RELEASE_BRANCH" origin "$BASE_BRANCH" || git fetch --depth 1 origin "$BASE_BRANCH"
- name: Fetch release branch upto dev branch
run: git fetch --shallow-exclude "$BASE_BRANCH" origin "$RELEASE_BRANCH" || git fetch --depth 1 origin "$RELEASE_BRANCH"
- name: Resolve bug in git https://stackoverflow.com/a/63879454/96823
run: git repack -d
- name: Get more commits to connect the history of dev and release branches
run: |
for i in $(seq 1 10); do
git fetch --deepen 10 origin "$BASE_BRANCH" "$RELEASE_BRANCH"
git merge-base --all HEAD origin/"$RELEASE_BRANCH" && exit 0
done
echo "Failed to fetch merge base" >&2
exit 1
- name: Create branch without checkout just to have shorter automatic commit message
run: git branch "$RELEASE_BRANCH" origin/"$RELEASE_BRANCH"
- name: Required git config
run: |
git config user.name "OpenProject Actions CI"
git config user.email "operations+ci@openproject.com"
- name: Show diff
run: git diff ..."$RELEASE_BRANCH"
- name: Try merging or create a PR
run: |
if git diff --exit-code --quiet ..."$RELEASE_BRANCH"; then
echo "Nothing to merge from $RELEASE_BRANCH into $BASE_BRANCH"
else
git merge --no-commit --no-ff "$RELEASE_BRANCH" || true
# ignore crowdin translations
git restore --source=HEAD --staged --worktree -- "config/locales/crowdin/*.yml" "modules/*/config/locales/crowdin/*.yml"
if git diff --cached --quiet; then
echo "Nothing to merge from $RELEASE_BRANCH into $BASE_BRANCH, besides ignored crowdin translations"
elif git commit --no-edit && git push origin "$BASE_BRANCH"; then
echo "Successfully merged $RELEASE_BRANCH into $BASE_BRANCH and pushed"
else
# Close all previous PRs with label
pr_numbers=$(gh pr list --label create-merge-release-into-dev-pr --json number --jq='.[].number')
for pr_number in $pr_numbers; do
gh pr close "$pr_number" --delete-branch
done
pr_body=$(
echo 'Created by GitHub action'
for pr_number in $pr_numbers; do
echo "Replaces #$pr_number"
done
)
TEMP_BRANCH="merge-$RELEASE_BRANCH-$(date "+%Y%m%d%H%M%S")"
# If merge failed, abort it and add a commit that removes crowdin translation changes
if git merge --abort; then
git switch -c "$TEMP_BRANCH" "$RELEASE_BRANCH"
git restore --source="$BASE_BRANCH" --staged --worktree -- "config/locales/crowdin/*.yml" "modules/*/config/locales/crowdin/*.yml"
if ! git diff --cached --quiet; then
git commit -m "use crowdin translations from $BASE_BRANCH"
fi
else
# Otherwise use successful merge commit for PR
git branch "$TEMP_BRANCH"
fi
git push origin "$TEMP_BRANCH"
gh pr create \
--base "$BASE_BRANCH" \
--head "$TEMP_BRANCH" \
--title "Merge $RELEASE_BRANCH into $BASE_BRANCH" \
--body "$pr_body" \
--label create-merge-release-into-dev-pr
echo "Created a PR to merge $RELEASE_BRANCH ($TEMP_BRANCH) into $BASE_BRANCH"
fi
fi
env:
GITHUB_TOKEN: ${{ secrets.OPENPROJECTCI_GH_CORE_PAT }}