77permissions :           #  minimum perms the job needs
88  contents : write       #  push the sync branch
99  pull-requests : write  #  open, approve & merge the PR
10-   actions : write        #  update workflow files
1110
1211concurrency :           #  never let two syncs race
1312  group : ${{ github.workflow }}-${{ github.ref }} 
@@ -22,34 +21,133 @@ jobs:
2221      - uses : actions/checkout@v4 
2322        with :
2423          fetch-depth : 0 
24+           token : ${{ secrets.GITHUB_TOKEN }} 
2525
2626      #  2. fetch upstream & copy it to a side branch
2727      - name : Update upstream-sync branch 
28+         env :
29+           GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }} 
2830        run : | 
31+           # Configure git identity 
32+           git config --global user.email "[email protected] " 33+           git config --global user.name "GitHub Action" 
34+            
2935          git remote add upstream https://github.com/openjdk/jdk17u-dev.git 
3036          git fetch upstream master 
31-           git checkout -B upstream-sync upstream/master 
37+            
38+           echo "=== Current branch status ===" 
39+           git log --oneline -5 
40+           echo "=== Upstream status ===" 
41+           git log --oneline -5 upstream/master 
42+            
43+           # Create sync branch from current master to preserve workflows 
44+           git checkout -B upstream-sync origin/master 
45+            
46+           echo "=== About to merge upstream changes ===" 
47+           git log --oneline -1 HEAD 
48+           git log --oneline -1 upstream/master 
49+            
50+           # Simple merge approach - let's see what happens 
51+           if git merge upstream/master --no-edit --allow-unrelated-histories; then 
52+             echo "=== Merge successful ===" 
53+             git log --oneline -5 
54+           else 
55+             echo "=== Merge failed, trying alternative approach ===" 
56+             git merge --abort || true  
57+             git reset --hard upstream/master 
58+             # Restore our workflow files after taking upstream 
59+             git checkout origin/master -- .github/workflows/ 
60+             git add .github/workflows/ 
61+             git commit -m "Preserve local workflow files during upstream sync" 
62+             echo "=== Alternative approach completed ===" 
63+             git log --oneline -5 
64+           fi 
65+            
66+           echo "=== Final branch status before push ===" 
67+           git log --oneline -10 
3268          git push -f origin upstream-sync 
3369
3470#  3. Open or update the PR `upstream-sync -> master`
35-       - uses : peter-evans/create-pull-request@v7 
36-         id : cpr 
37-         with :
38-           branch : upstream-sync 
39-           base :   master 
40-           title :  " Automated upstream merge" 
41-           body :   " Nightly sync of openjdk/jdk:master into this fork" 
71+       - name : Create or update pull request 
72+         env :
73+           GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }} 
74+         run : | 
75+           # Check if PR already exists using REST API 
76+           PR_EXISTS=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ 
77+             "https://api.github.com/repos/${{ github.repository }}/pulls?head=${{ github.repository_owner }}:upstream-sync&base=master" \ 
78+             | jq -r '.[0].number // empty') 
79+            
80+           if [ -n "$PR_EXISTS" ]; then 
81+             echo "PR #$PR_EXISTS already exists, updating it" 
82+             curl -s -X PATCH -H "Authorization: token $GITHUB_TOKEN" \ 
83+               -H "Accept: application/vnd.github.v3+json" \ 
84+               "https://api.github.com/repos/${{ github.repository }}/pulls/$PR_EXISTS" \ 
85+               -d '{ 
86+                 "title": "Automated upstream merge", 
87+                 "body": "Nightly sync of openjdk/jdk17u-dev:master into this fork" 
88+               }' 
89+           else 
90+             echo "Creating new PR" 
91+             echo "=== Checking branch differences first ===" 
92+             git log --oneline master..upstream-sync | head -10 
93+             echo "=== Making API call ===" 
94+             PR_RESPONSE=$(curl -s -X POST -H "Authorization: token $GITHUB_TOKEN" \ 
95+               -H "Accept: application/vnd.github.v3+json" \ 
96+               "https://api.github.com/repos/${{ github.repository }}/pulls" \ 
97+               -d '{ 
98+                 "title": "Automated upstream merge", 
99+                 "body": "Nightly sync of openjdk/jdk17u-dev:master into this fork", 
100+                 "head": "upstream-sync", 
101+                 "base": "master" 
102+               }') 
103+             echo "=== API Response ===" 
104+             echo "$PR_RESPONSE" 
105+             PR_NUMBER=$(echo "$PR_RESPONSE" | jq -r '.number') 
106+             echo "=== Extracted PR Number: $PR_NUMBER ===" 
107+              
108+             # Check if there was an error 
109+             ERROR_MESSAGE=$(echo "$PR_RESPONSE" | jq -r '.message // empty') 
110+             if [ -n "$ERROR_MESSAGE" ]; then 
111+               echo "=== API Error: $ERROR_MESSAGE ===" 
112+             fi 
113+           fi 
42114
43115#  4. Auto-approve that PR
44-       - if : steps.cpr.outputs.pull-request-operation != 'none' 
45-         uses : hmarr/auto-approve-action@v4 
46-         with :
47-           pull-request-number : ${{ steps.cpr.outputs.pull-request-number }} 
116+       - name : Auto-approve PR 
117+         env :
118+           GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }} 
119+         run : | 
120+           PR_NUMBER=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ 
121+             "https://api.github.com/repos/${{ github.repository }}/pulls?head=${{ github.repository_owner }}:upstream-sync&base=master" \ 
122+             | jq -r '.[0].number') 
123+            
124+           if [ "$PR_NUMBER" != "null" ] && [ -n "$PR_NUMBER" ]; then 
125+             echo "Auto-approving PR #$PR_NUMBER" 
126+             curl -s -X POST -H "Authorization: token $GITHUB_TOKEN" \ 
127+               -H "Accept: application/vnd.github.v3+json" \ 
128+               "https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER/reviews" \ 
129+               -d '{ 
130+                 "event": "APPROVE", 
131+                 "body": "Auto-approved upstream sync" 
132+               }' 
133+           fi 
48134
49135#  5. Enable auto-merge so GitHub merges as soon as
50136      #     branch protection requirements are satisfied
51-       - if : steps.cpr.outputs.pull-request-operation == 'created' 
52-         uses : peter-evans/enable-pull-request-automerge@v3 
53-         with :
54-           pull-request-number : ${{ steps.cpr.outputs.pull-request-number }} 
55-           merge-method : merge 
137+       - name : Enable auto-merge 
138+         env :
139+           GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }} 
140+         run : | 
141+           PR_NUMBER=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ 
142+             "https://api.github.com/repos/${{ github.repository }}/pulls?head=${{ github.repository_owner }}:upstream-sync&base=master" \ 
143+             | jq -r '.[0].number') 
144+            
145+           if [ "$PR_NUMBER" != "null" ] && [ -n "$PR_NUMBER" ]; then 
146+             echo "Enabling auto-merge for PR #$PR_NUMBER" 
147+             curl -s -X PUT -H "Authorization: token $GITHUB_TOKEN" \ 
148+               -H "Accept: application/vnd.github.v3+json" \ 
149+               "https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER/merge" \ 
150+               -d '{ 
151+                 "merge_method": "merge" 
152+               }' || echo "Auto-merge may not be available or already enabled" 
153+           fi 
0 commit comments