Skip to content

Commit 980039d

Browse files
authored
Prevent failure if branch or pr already exists (#42)
* handle a branch or pr already existing without failure * add debug logging option * fix script * add force option and skip build if false and pr is open * put release notes in PR and release * fix logic
1 parent 96cbcc7 commit 980039d

File tree

2 files changed

+126
-47
lines changed

2 files changed

+126
-47
lines changed

.github/workflows/package.yml

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,22 @@ name: Publish Release
22

33
on:
44
workflow_dispatch:
5+
inputs:
6+
logLevel:
7+
description: 'Log level'
8+
required: true
9+
default: 'info'
10+
type: choice
11+
options:
12+
- info
13+
- debug
14+
force:
15+
description: 'Force re-creating the package if a pull request is already open'
16+
required: false
17+
type: boolean
518
schedule:
6-
- cron: '*/60 */2 * * *' # Every 2 hours
19+
# Every day at 05:00 EST (10:00 UTC)
20+
- cron: '0 10 * * *'
721

822
jobs:
923
build:
@@ -13,7 +27,16 @@ jobs:
1327
uses: actions/checkout@v2
1428

1529
- name: Release
16-
run: cd .scripts && sh ./package.sh
30+
run: |
31+
cd .scripts
32+
33+
if [ "${LOG_LEVEL}" == "debug" ]; then
34+
sh -x ./package.sh
35+
else
36+
sh ./package.sh
37+
fi
1738
env:
1839
AUTHOR_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1940
REVIEWER_TOKEN: ${{ secrets.AFRESHY_PAT }}
41+
LOG_LEVEL: ${{ inputs.logLevel || 'info' }}
42+
FORCE: ${{ inputs.force || false }}

.scripts/package.sh

Lines changed: 101 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ latest_release_number () {
99
head -1 || echo '0.0.0'
1010
}
1111

12+
branch_exists () {
13+
# Check if the branch exists on the remote
14+
git ls-remote --heads origin "$1" 2>/dev/null | grep -q "$1" || echo 0
15+
}
16+
17+
pr_exists_and_open () {
18+
# Check if the PR exists and is open on the remote
19+
gh pr view $1 --json state | jq -r '.state' | grep -q "OPEN" || echo 0
20+
}
21+
22+
is_pr_approved () {
23+
# Check if the PR needs review
24+
gh pr view $1 --json reviewDecision | jq -r '.reviewDecision' | grep -q "APPROVED" || echo 0
25+
}
26+
1227
xcframework_name () {
1328
# Filter out path and extension to get the framework name
1429
# Ex. xcframework_name "FirebaseFirestore/leveldb-library.xcframework" = "leveldb-library"
@@ -269,16 +284,41 @@ login_reviewer() {
269284
}
270285

271286
commit_changes() {
272-
branch=$1
273-
git checkout -b $branch
287+
local latest=$1
288+
local branch=$2
289+
local scratch=$3
290+
local repo=$4
291+
274292
git add .
275-
git commit -m"Updated Package.swift and sources for latest firebase sdks"
293+
git commit -m "Updated Package.swift and sources for latest firebase sdks"
276294
git push -u origin $branch
277-
gh pr create --fill
278-
login_reviewer
279-
gh pr review --approve
295+
296+
local pr_exists_and_open=$(pr_exists_and_open $branch)
297+
local release_notes=$(gh release view --repo $repo --json body | jq -r '.body')
298+
299+
if [[ $pr_exists_and_open -eq 1 ]]; then
300+
echo "Pull request $branch already exists."
301+
else
302+
echo "Creating pull request for $branch..."
303+
gh pr create --title "Update to Firebase $latest" --body "$release_notes" --base main --head $branch
304+
fi
305+
306+
local is_pr_approved=$(is_pr_approved $branch)
307+
308+
if [[ $is_pr_approved -eq 1 ]]; then
309+
echo "Pull request $branch is already approved."
310+
else
311+
echo "Approving pull request $branch..."
312+
login_reviewer
313+
gh pr review --approve
314+
fi
315+
316+
echo "Merging pull request $branch..."
280317
login_default
281318
gh pr merge --squash
319+
320+
echo "Creating release..."
321+
gh release create --title "$latest" --notes "$release_notes" --target "$branch" $latest $scratch/dist/*.xcframework.zip
282322
}
283323

284324
# Exit when any command fails
@@ -304,52 +344,68 @@ if [[ $latest != $current || $debug ]]; then
304344
distribution="dist"
305345
sources="Sources"
306346
package="Package.swift"
347+
branch="release/$latest"
348+
349+
git fetch origin
307350

308-
# Generate files in a temporary directory
309-
# Use subshell to return to original directory when finished with scratchwork
310-
create_scratch
311-
(
312-
cd $scratch
313-
home=$OLDPWD
314-
echo "Downloading latest release..."
315-
gh release download --pattern 'Firebase.zip' --repo $firebase_repo
316-
echo "Unzipping.."
317-
unzip -q Firebase.zip
318-
echo "Preparing xcframeworks for distribution..."
319-
cd Firebase
320-
rename_frameworks "_"
321-
zip_frameworks
322-
echo "Creating distribution files..."
323-
prepare_files_for_distribution "../$distribution"
324-
echo "Creating source files..."
325-
generate_sources "../$sources"
326-
# Create test package using local binaries and make sure it builds
327-
generate_swift_package "../$package" "$home/package_template.swift" "../$distribution" $xcframeworks_repo $distribution
328-
echo "Validating..."
329-
(cd ..; swift package dump-package | read pac)
330-
(cd ..; swift build) # TODO: create tests and replace this line with `(cd ..; swift test)`
331-
# Create release package using remote binaries and make sure the Package.swift file is parseable
332-
generate_swift_package "../$package" "$home/package_template.swift" "../$distribution" $xcframeworks_repo ''
333-
echo "Validating..."
334-
(cd ..; swift package dump-package | read pac)
335-
)
336-
337-
echo "Moving files to repo..."; cd ..
338-
# Remove any existing files
339-
if [ -d $sources ]; then rm -rf "$sources"; fi
340-
if [ -f $package ]; then rm -f "$package"; fi
341-
# Move generated files into the repo directory
342-
mv "$scratch/$sources" "$sources"
343-
mv "$scratch/$package" "$package"
351+
release_branch_exists=$(branch_exists $branch)
352+
pr_exists_and_open=$(pr_exists_and_open $branch)
353+
354+
if [[ $release_branch_exists -eq 1 ]]; then
355+
echo "Branch $branch already exists."
356+
git checkout $branch
357+
else
358+
echo "Creating branch $branch..."
359+
git checkout -b $branch
360+
fi
361+
362+
if [[ $pr_exists_and_open -eq 0 || $FORCE ]]; then
363+
# Generate files in a temporary directory
364+
# Use subshell to return to original directory when finished with scratchwork
365+
create_scratch
366+
(
367+
cd $scratch
368+
home=$OLDPWD
369+
echo "Downloading latest release..."
370+
gh release download --pattern 'Firebase.zip' --repo $firebase_repo
371+
echo "Unzipping.."
372+
unzip -q Firebase.zip
373+
echo "Preparing xcframeworks for distribution..."
374+
cd Firebase
375+
rename_frameworks "_"
376+
zip_frameworks
377+
echo "Creating distribution files..."
378+
prepare_files_for_distribution "../$distribution"
379+
echo "Creating source files..."
380+
generate_sources "../$sources"
381+
# Create test package using local binaries and make sure it builds
382+
generate_swift_package "../$package" "$home/package_template.swift" "../$distribution" $xcframeworks_repo $distribution
383+
echo "Validating..."
384+
(cd ..; swift package dump-package | read pac)
385+
(cd ..; swift build) # TODO: create tests and replace this line with `(cd ..; swift test)`
386+
# Create release package using remote binaries and make sure the Package.swift file is parseable
387+
generate_swift_package "../$package" "$home/package_template.swift" "../$distribution" $xcframeworks_repo ''
388+
echo "Validating..."
389+
(cd ..; swift package dump-package | read pac)
390+
)
391+
392+
echo "Moving files to repo..."; cd ..
393+
# Remove any existing files
394+
if [ -d $sources ]; then rm -rf "$sources"; fi
395+
if [ -f $package ]; then rm -f "$package"; fi
396+
# Move generated files into the repo directory
397+
mv "$scratch/$sources" "$sources"
398+
mv "$scratch/$package" "$package"
399+
else
400+
echo "Pull request $branch already exists and is open."
401+
fi
344402

345403
# Skips deploy
346404
if [[ $skip_release ]]; then echo "Done."; exit 0; fi
347405

348406
# Deploy to repository
349407
echo "Merging changes to Github..."
350-
commit_changes "release/$latest"
351-
echo "Creating release draft"
352-
echo "Release $latest" | gh release create --title "$latest" --target "release/$latest" $latest $scratch/dist/*.xcframework.zip
408+
commit_changes "$latest" "$branch" "$scratch" "$firebase_repo"
353409
else
354410
echo "$current is up to date."
355411
fi

0 commit comments

Comments
 (0)