Skip to content

Commit ad92372

Browse files
committed
Fix fine-grained PAT authentication and add fallback to fork approach
1 parent 803b755 commit ad92372

File tree

2 files changed

+76
-21
lines changed

2 files changed

+76
-21
lines changed

.github/workflows/README.md

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,36 @@ This workflow automatically syncs changes from `main` branch to version-specific
1717

1818
### 1. Personal Access Token (PAT) - REQUIRED
1919

20-
**IMPORTANT**: The PAT must be created by a user with write access to the repository!
20+
**IMPORTANT**: Use a **Classic PAT** for simplicity, or configure Fine-grained PAT correctly.
2121

22-
Create a Personal Access Token with the following permissions:
23-
1. **Log in as a user with write access** to `Arcenox-co/TickerQ`
24-
2. Go to GitHub → Settings → Developer settings → Personal access tokens → Tokens (classic)
25-
3. Generate new token with these scopes:
22+
#### Option A: Classic Personal Access Token (Recommended)
23+
1. **Log in as a user with write access** to the repository
24+
2. Go to: https://github.com/settings/tokens
25+
3. Click "Generate new token (classic)"
26+
4. Select scopes:
2627
-`repo` (Full control of private repositories)
2728
-`workflow` (Update GitHub Action workflows)
28-
4. Add to repository secrets:
29-
- Go to Repository → Settings → Secrets and variables → Actions
30-
- Add new secret: `PAT_TOKEN` with your token value
29+
30+
#### Option B: Fine-grained Personal Access Token
31+
1. Go to: https://github.com/settings/personal-access-tokens/fine-grained
32+
2. Create/edit token with:
33+
- **Repository access**: Select `Arcenox-co/TickerQ`
34+
- **Repository permissions**:
35+
- Contents: Read and Write
36+
- Pull requests: Read and Write
37+
- Issues: Read and Write (for labels)
38+
- Actions: Read
39+
- Metadata: Read
40+
- Workflows: Write (if needed)
41+
42+
#### Add Token to Repository
43+
1. Go to Repository → Settings → Secrets and variables → Actions
44+
2. Add new secret: `PAT_TOKEN` with your token value
3145

3246
**Troubleshooting Access Issues:**
33-
- If you see "Permission denied", the PAT is from wrong user
34-
- Token must be from user with write access to the repository
35-
- For organization repos, user must be org member with appropriate permissions
47+
- Fine-grained tokens need explicit repository access configured
48+
- Classic tokens are simpler and work with `repo` scope
49+
- Error 403 usually means token lacks repository access
3650

3751
### 2. Repository Variables
3852

.github/workflows/sync-version-branches.yml

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,15 @@ jobs:
5353
with:
5454
fetch-depth: 0
5555
token: ${{ secrets.PAT_TOKEN }}
56+
persist-credentials: true
5657

5758
- name: Setup Git
5859
run: |
5960
git config --global user.name "github-actions[bot]"
6061
git config --global user.email "github-actions[bot]@users.noreply.github.com"
62+
63+
# Configure git to use the PAT for authentication
64+
git config --global url."https://${{ secrets.PAT_TOKEN }}@github.com/".insteadOf "https://github.com/"
6165
6266
- name: Extract target branches from input
6367
if: github.event_name == 'workflow_dispatch'
@@ -146,11 +150,35 @@ jobs:
146150
# Commit the changes
147151
git commit -m "Sync changes from main to ${{ matrix.target_branch }} - Applied recent commits, updated versions & framework, preserved .csproj files"
148152
149-
echo "📤 Pushing sync branch: $sync_branch"
150-
git push origin "$sync_branch"
153+
echo "📤 Attempting to push sync branch: $sync_branch"
154+
155+
# Try to push to origin, if it fails try to push to a fork
156+
if git push origin "$sync_branch" 2>/dev/null; then
157+
echo "✅ Pushed to origin successfully"
158+
PUSH_REPO="${{ github.repository }}"
159+
else
160+
echo "⚠️ Cannot push to origin, trying fork approach..."
161+
162+
# Check if we have a fork configured
163+
FORK_OWNER="${{ vars.FORK_OWNER || 'arcenox' }}"
164+
FORK_URL="https://${{ secrets.PAT_TOKEN }}@github.com/${FORK_OWNER}/TickerQ.git"
165+
166+
# Add fork as remote if not exists
167+
git remote add fork "$FORK_URL" 2>/dev/null || git remote set-url fork "$FORK_URL"
168+
169+
# Push to fork
170+
if git push fork "$sync_branch"; then
171+
echo "✅ Pushed to fork: ${FORK_OWNER}/TickerQ"
172+
PUSH_REPO="${FORK_OWNER}/TickerQ"
173+
else
174+
echo "❌ Failed to push to both origin and fork"
175+
exit 1
176+
fi
177+
fi
151178
152-
# Store branch name for PR creation
179+
# Store branch name and repo for PR creation
153180
echo "sync_branch=$sync_branch" >> $GITHUB_ENV
181+
echo "push_repo=$PUSH_REPO" >> $GITHUB_ENV
154182
else
155183
echo "ℹ️ No commits to apply"
156184
git checkout main
@@ -192,13 +220,26 @@ jobs:
192220
EOF
193221
)
194222
195-
# Create PR without labels first (they might not exist)
196-
gh pr create \
197-
--base "${{ matrix.target_branch }}" \
198-
--head "${{ env.sync_branch }}" \
199-
--title "🔄 Sync main branch changes to ${{ matrix.target_branch }}" \
200-
--body "$PR_BODY" \
201-
2>&1 | tee pr_output.txt
223+
# Create PR (from fork if necessary)
224+
if [ "${{ env.push_repo }}" = "${{ github.repository }}" ]; then
225+
# Creating PR from same repo
226+
gh pr create \
227+
--base "${{ matrix.target_branch }}" \
228+
--head "${{ env.sync_branch }}" \
229+
--title "🔄 Sync main branch changes to ${{ matrix.target_branch }}" \
230+
--body "$PR_BODY" \
231+
2>&1 | tee pr_output.txt
232+
else
233+
# Creating PR from fork
234+
FORK_OWNER=$(echo "${{ env.push_repo }}" | cut -d'/' -f1)
235+
gh pr create \
236+
--base "${{ matrix.target_branch }}" \
237+
--head "${FORK_OWNER}:${{ env.sync_branch }}" \
238+
--repo "${{ github.repository }}" \
239+
--title "🔄 Sync main branch changes to ${{ matrix.target_branch }}" \
240+
--body "$PR_BODY" \
241+
2>&1 | tee pr_output.txt
242+
fi
202243
203244
# Extract PR number if created
204245
PR_NUMBER=$(grep -oP '(?<=pull/)\d+' pr_output.txt || echo "")

0 commit comments

Comments
 (0)