@@ -93,12 +93,22 @@ jobs:
9393 echo "Inga nya filer att committa"
9494 echo "has_changes=false" >> $GITHUB_OUTPUT
9595 else
96- # Create a unique branch name for the commits
97- TIMESTAMP=$(date +'%Y%m%d_%H%M%S')
98- COMMIT_BRANCH="sfs_updates_${TIMESTAMP}"
99-
100- # Create and switch to the new branch
101- git checkout -b "$COMMIT_BRANCH"
96+ # Use fixed branch name from input (default: workflow-artifact-data)
97+ COMMIT_BRANCH="${{ inputs.branch_name }}"
98+
99+ # Check if branch exists remotely
100+ if git ls-remote --heads origin "$COMMIT_BRANCH" | grep -q "$COMMIT_BRANCH"; then
101+ echo "📥 Branch '$COMMIT_BRANCH' exists, checking out and merging with main..."
102+ # Branch exists - fetch and checkout
103+ git fetch origin "$COMMIT_BRANCH"
104+ git checkout "$COMMIT_BRANCH"
105+ # Merge latest main into fixed branch to keep it updated
106+ git merge origin/main --no-edit --strategy-option theirs || echo "Merge completed (conflicts auto-resolved)"
107+ else
108+ echo "🆕 Branch '$COMMIT_BRANCH' doesn't exist, creating new..."
109+ # Branch doesn't exist - create new
110+ git checkout -b "$COMMIT_BRANCH"
111+ fi
102112
103113 # Commit with multi-line message
104114 git commit -m "Automatisk uppdatering av SFS-författningar $(date +'%Y-%m-%d')" \
@@ -139,11 +149,28 @@ jobs:
139149 echo "::error::Git push misslyckades: ${PUSH_ERROR}"
140150 exit 1
141151 fi
142-
152+
153+ # Manage PR lifecycle: close old, create new
154+ echo "🔄 Hanterar PR för branch '$COMMIT_BRANCH'..."
155+
156+ # Close existing PR for this branch if it exists
157+ EXISTING_PR=$(gh pr list --head "$COMMIT_BRANCH" --json number --jq '.[0].number' 2>/dev/null || echo "")
158+ if [ -n "$EXISTING_PR" ]; then
159+ echo "Stänger befintlig PR #$EXISTING_PR"
160+ gh pr close "$EXISTING_PR" --comment "🔄 Stänger för att öppna ny PR med uppdaterad data från $(date +'%Y-%m-%d %H:%M')"
161+ fi
162+
163+ # Create new PR
164+ echo "📝 Skapar ny PR..."
165+ gh pr create --base main --head "$COMMIT_BRANCH" \
166+ --title "SFS Data Update - $(date +'%Y-%m-%d %H:%M')" \
167+ --body "Automatisk uppdatering av SFS-data. Inkluderar Käll-JSON, Markdown med selex-taggar och backup till R2. Workflow: fetch-sfs-workflow, Branch: $COMMIT_BRANCH, Run: ${{ github.run_id }}" \
168+ || echo "⚠️ PR may already exist"
169+
143170 # Switch back to original branch
144171 git checkout "$CURRENT_BRANCH"
145-
146- echo "Commits gjorda i branch '${COMMIT_BRANCH}' istället för '${CURRENT_BRANCH }'"
172+
173+ echo "✅ Commits gjorda i branch '${COMMIT_BRANCH}'"
147174 fi
148175
149176 - name : Trigger HTML export workflow
@@ -152,10 +179,8 @@ jobs:
152179 with :
153180 github-token : ${{ secrets.GITHUB_TOKEN }}
154181 script : |
155- const enableGitCommit = '${{ inputs.enable_git_commit }}' !== 'false';
156- const sourceRef = enableGitCommit
157- ? '${{ steps.commit_changes.outputs.commit_branch }}'
158- : '${{ inputs.branch_name }}';
182+ // Always use the fixed branch name
183+ const sourceRef = '${{ inputs.branch_name }}';
159184
160185 github.rest.actions.createWorkflowDispatch({
161186 owner: context.repo.owner,
0 commit comments