88set -eo pipefail
99
1010MY_GIT_USER=" $( gh api user --jq ' .login' ) "
11+ BRANCH_NAME=bump-github-actions
1112
1213if [[ -z " $MY_GIT_USER " ]]; then
1314 echo " Could not determine the current user in gh. Try running \` gh auth login\` ."
@@ -34,7 +35,7 @@ function repo_dir() {
3435}
3536
3637function fetch_repo() {
37- if ! gh repo view " $MY_GIT_USER /$1 " --json name > /dev/null 2> /dev/null; then
38+ if ! GH_PAGER= ' ' gh repo view " $MY_GIT_USER /$1 " --json name > /dev/null 2> /dev/null; then
3839 echo " 🍽️ Forking $1 ..."
3940 gh repo fork --default-branch-only --clone=false " apple/$1 "
4041 fi
@@ -47,6 +48,8 @@ function fetch_repo() {
4748 else
4849 cd " $REPO_DIR "
4950 git fetch upstream 2>&1 | grep -v " From github.com" || true
51+ git reset & > /dev/null
52+ git checkout . & > /dev/null
5053 git checkout main & > /dev/null
5154 git reset --hard upstream/main & > /dev/null
5255 fi
@@ -57,16 +60,16 @@ function update_repo() {
5760 cd " $( repo_dir " $1 " ) /.github"
5861
5962 # if bump-github-actions branch exists, and the PklProject there has the correct version, just exit early.
60- if git show-ref --verify --quiet refs/heads/bump-github-actions ; then
61- git checkout bump-github-actions & > /dev/null || true
62- if grep -q " pkl.impl.ghactions@$LATEST_PACKAGE_VERSION " PklProject ; then
63+ if git show-ref --verify --quiet refs/heads/" $BRANCH_NAME " ; then
64+ git checkout " $BRANCH_NAME " & > /dev/null || true
65+ if git show upstream/main:.github/PklProject | grep -q " pkl.impl.ghactions@$LATEST_PACKAGE_VERSION " ; then
6366 echo " ✅ $1 already has the correct version $LATEST_PACKAGE_VERSION "
6467 SKIPPED_REPOS+=(" $1 (already up to date)" )
6568 return 0
6669 fi
6770 # wrong version, reset to upstream/main and continue
6871 git checkout main & > /dev/null || true
69- git branch -D bump-github-actions & > /dev/null || true
72+ git branch -D " $BRANCH_NAME " & > /dev/null || true
7073 git reset --hard upstream/main & > /dev/null || true
7174 fi
7275
@@ -82,25 +85,51 @@ function update_repo() {
8285 SKIPPED_REPOS+=(" $1 (no changes needed)" )
8386 return 0
8487 fi
85- if [[ -f licenserc.toml ]]; then
86- hawkeye format
87- fi
88- if [[ -f gradlew ]]; then
89- ./gradlew spotlessApply
90- fi
9188 echo " Creating branch and commit..."
92- git checkout -b bump-github-actions & > /dev/null
89+ git checkout -b " $BRANCH_NAME " & > /dev/null
9390 git add . & > /dev/null
9491 git commit -m " Bump pkl.impl.ghactions to version $LATEST_PACKAGE_VERSION " & > /dev/null
92+
93+ FORMATTED=0
94+ if [[ -f ../licenserc.toml ]]; then
95+ echo " ✍️ Formatting: hawkeye"
96+ (cd .. && hawkeye format --fail-if-updated false)
97+ FORMATTED=1
98+ fi
99+ if [[ -f ../gradlew ]]; then
100+ echo " ✍️ Formatting: spotless"
101+ test " $1 " = " pkl" && (cd .. && ./gradlew assemble)
102+ (cd .. && ./gradlew spotlessApply) || true
103+ FORMATTED=1
104+ fi
105+ if [[ " $FORMATTED " = 1 ]] && [[ -n " $( git diff) " ]]; then
106+ echo " Amending commit with formatting changes..."
107+ git add . & > /dev/null
108+ git commit --amend --no-edit & > /dev/null
109+ fi
110+
95111 echo " Pushing to origin..."
96- git push --force -u origin bump-github-actions 2>&1 | grep -v " branch 'bump-github-actions' set up" || true
97- echo " Creating pull request..."
98- PR_URL=$( gh pr create --repo " apple/$1 " --base main --head " $MY_GIT_USER :bump-github-actions" \
99- --title " Bump pkl.impl.ghactions to version $LATEST_PACKAGE_VERSION " \
100- --body " Updates pkl.impl.ghactions package to version $LATEST_PACKAGE_VERSION " 2>&1 | grep " https://" )
101- echo " $PR_URL "
102- CREATED_PRS+=(" $1 |$PR_URL " )
103- echo " ✅ Successfully created PR for $1 "
112+ git push --force -u origin " $BRANCH_NAME " 2>&1 | grep -v " branch '$BRANCH_NAME ' set up" || true
113+ echo " Checking for existing pull request..."
114+ PR_DATA=$( GH_PAGER=' ' gh pr view --repo " apple/$1 " " $MY_GIT_USER :$BRANCH_NAME " --json url,state)
115+ PR_DATA_STATUS=$?
116+ # if this is zero and the state is OPEN, the PR already exists
117+ if [ $PR_DATA_STATUS = 0 ] && [ " $( jq -r .state <<< " $PR_DATA" ) " = " OPEN" ]; then
118+ echo " Editing pull request..."
119+ PR_URL=$( jq -r .url <<< " $PR_DATA" )
120+ gh pr edit " $PR_URL " --repo " apple/$1 " \
121+ --title " Bump pkl.impl.ghactions to version $LATEST_PACKAGE_VERSION " \
122+ --body " Updates pkl.impl.ghactions package to version $LATEST_PACKAGE_VERSION "
123+ echo " ✅ PR already exists for $1 "
124+ else
125+ echo " Creating pull request..."
126+ PR_URL=$( gh pr create --repo " apple/$1 " --base main --head " $MY_GIT_USER :$BRANCH_NAME " \
127+ --title " Bump pkl.impl.ghactions to version $LATEST_PACKAGE_VERSION " \
128+ --body " Updates pkl.impl.ghactions package to version $LATEST_PACKAGE_VERSION " 2>&1 | grep " https://" )
129+ echo " $PR_URL "
130+ CREATED_PRS+=(" $1 |$PR_URL " )
131+ echo " ✅ Successfully created PR for $1 "
132+ fi
104133}
105134
106135for repo in " ${REPOS[@]} " ; do
0 commit comments