-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmerge-to-release.sh
More file actions
140 lines (111 loc) · 4.41 KB
/
merge-to-release.sh
File metadata and controls
140 lines (111 loc) · 4.41 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
135
136
137
138
139
140
#!/bin/bash
# ============================================================================
# merge-to-release.sh
#
# Creates a PR to merge a development branch into its corresponding release
# branch. Used after a version bump PR has been merged.
#
# Usage:
# ./scripts/merge-to-release.sh <version> [--preview]
#
# Examples:
# ./scripts/merge-to-release.sh 2.5.0 # main → release/stable/v2
# ./scripts/merge-to-release.sh 2.5.1-preview --preview # preview → release/v2
#
# Prerequisites:
# - git and gh (GitHub CLI) must be installed and authenticated
# ============================================================================
set -euo pipefail
# ── Helpers ──────────────────────────────────────────────────────────────────
usage() {
cat <<EOF
Usage: $(basename "$0") <version> [--preview]
Arguments:
version The version that was just bumped (used to determine major version)
--preview Merge preview → release/v{major} instead of
main → release/stable/v{major}
Examples:
$(basename "$0") 2.5.0 # main → release/stable/v2
$(basename "$0") 2.5.1-preview --preview # preview → release/v2
EOF
exit 1
}
error() {
echo "ERROR: $1" >&2
exit 1
}
info() {
echo "── $1"
}
# ── Parse arguments ──────────────────────────────────────────────────────────
VERSION=""
IS_PREVIEW=false
while [[ $# -gt 0 ]]; do
case "$1" in
--preview)
IS_PREVIEW=true
shift
;;
-h|--help)
usage
;;
*)
if [[ -z "$VERSION" ]]; then
VERSION="$1"
else
error "Unexpected argument: $1"
fi
shift
;;
esac
done
[[ -z "$VERSION" ]] && usage
# Validate version format
if ! echo "$VERSION" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+(-preview)?$'; then
error "Invalid version format '$VERSION'. Expected: X.Y.Z or X.Y.Z-preview"
fi
# ── Determine branches ──────────────────────────────────────────────────────
# Extract major version (e.g. "2" from "2.5.0" or "2.5.1-preview")
MAJOR_VERSION=$(echo "$VERSION" | cut -d. -f1)
if [[ "$IS_PREVIEW" == true ]]; then
SOURCE_BRANCH="preview"
TARGET_BRANCH="release/v${MAJOR_VERSION}"
PR_TITLE="Merge preview to release/v${MAJOR_VERSION}"
else
SOURCE_BRANCH="main"
TARGET_BRANCH="release/stable/v${MAJOR_VERSION}"
PR_TITLE="Merge main to release/stable/v${MAJOR_VERSION}"
fi
info "Source branch : $SOURCE_BRANCH"
info "Target branch : $TARGET_BRANCH"
info "PR title : $PR_TITLE"
echo ""
# ── Confirm with user ────────────────────────────────────────────────────────
read -rp "Proceed? [y/N] " confirm
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
echo "Aborted."
exit 0
fi
echo ""
# ── Resolve project directory ────────────────────────────────────────────────
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
cd "$PROJECT_DIR"
# ── Fetch latest branches ───────────────────────────────────────────────────
info "Fetching latest branches..."
git fetch origin "$SOURCE_BRANCH"
git fetch origin "$TARGET_BRANCH"
# ── Create PR ────────────────────────────────────────────────────────────────
info "Creating pull request..."
PR_URL=$(gh pr create \
--base "$TARGET_BRANCH" \
--head "$SOURCE_BRANCH" \
--title "$PR_TITLE" \
--body "Merge \`$SOURCE_BRANCH\` into \`$TARGET_BRANCH\` after version bump \`$VERSION\`.
> **Important**: Use **Merge commit** (not squash) when merging this PR to preserve commit history.
---
*This PR was created automatically by \`scripts/merge-to-release.sh\`.*")
echo ""
info "Done! PR created: $PR_URL"
echo ""
echo "⚠️ Remember: Use \"Merge commit\" (not squash) when merging this PR."