-
Notifications
You must be signed in to change notification settings - Fork 1
206 lines (177 loc) · 8.34 KB
/
fetch-sfs-workflow.yml
File metadata and controls
206 lines (177 loc) · 8.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
name: Hämta nya SFS-författningar från Regeringskansliets publika söktjänst
on:
schedule:
# Kör varje natt kl 02:00 UTC (03:00/04:00 svensk tid beroende på säsong)
- cron: '0 2 * * *'
workflow_dispatch: # Tillåter manuell körning
inputs:
days:
description: 'Antal dagar bakåt att hämta dokument för (vid manuell körning)'
required: false
default: '30'
type: string
enable_git_commit:
description: 'Aktivera git commits av markdown-filer'
required: false
default: true
type: boolean
branch_name:
description: 'Branch namn att använda (krävs om enable_git_commit är true)'
required: false
default: 'workflow-artifact-data'
type: string
permissions:
contents: write
actions: write
jobs:
fetch-documents:
runs-on: ubuntu-latest
environment: Test # Använd Test environment för R2 secrets
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Fetch JSON from beta.rkrattsbaser.gov.se
run: |
python downloaders/fetch_new_sfs_docs.py --days ${{ inputs.days || '1' }} --output data/sfs_json
env:
PYTHONPATH: ${{ github.workspace }}
- name: Process JSON files to Markdown files with Selex tags
run: |
python sfs_processor.py --input data/sfs_json --output data/md-markers --formats md-markers
env:
PYTHONPATH: ${{ github.workspace }}
- name: Upload JSON source files to Cloudflare R2 for permanent storage
run: |
aws configure set aws_access_key_id ${{ secrets.CLOUDFLARE_R2_ACCESS_KEY_ID }}
aws configure set aws_secret_access_key ${{ secrets.CLOUDFLARE_R2_SECRET_ACCESS_KEY }}
aws configure set region us-east-1
aws configure set output json
# Upload all JSON files to R2 for backup
aws s3 sync data/sfs_json/ s3://${{ vars.CLOUDFLARE_R2_RAWDATA_BUCKET_NAME }}/ \
--endpoint-url https://${{ secrets.CLOUDFLARE_R2_ACCOUNT_ID }}.r2.cloudflarestorage.com \
--content-type "application/json" \
--exclude "*.md" \
--include "*.json"
env:
AWS_DEFAULT_REGION: us-east-1
- name: Configure Git
if: inputs.enable_git_commit != 'false'
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
- name: Commit and push changes
if: inputs.enable_git_commit != 'false'
id: commit_changes
run: |
# Get current branch
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
# Check if we have changes (without staging yet)
if git diff --quiet data/md-markers/ data/sfs_json/ && \
! git ls-files --others --exclude-standard data/md-markers/ data/sfs_json/ | grep -q .; then
echo "Inga nya filer att committa"
echo "has_changes=false" >> $GITHUB_OUTPUT
else
# Use fixed branch name from input (default: workflow-artifact-data)
COMMIT_BRANCH="${{ inputs.branch_name || 'workflow-artifact-data' }}"
# Check if branch exists remotely
if git ls-remote --heads origin "$COMMIT_BRANCH" | grep -q "$COMMIT_BRANCH"; then
echo "📥 Branch '$COMMIT_BRANCH' exists, checking out and merging with main..."
# Branch exists - stash new untracked files to avoid checkout conflict
git fetch origin "$COMMIT_BRANCH"
git stash push --include-untracked -m "temp: new sfs files before branch switch"
git checkout "$COMMIT_BRANCH"
# Merge latest main into fixed branch to keep it updated
git merge origin/main --no-edit --strategy-option theirs || echo "Merge completed (conflicts auto-resolved)"
# Restore the newly generated files
git stash pop
else
echo "🆕 Branch '$COMMIT_BRANCH' doesn't exist, creating new..."
# Branch doesn't exist - create new
git checkout -b "$COMMIT_BRANCH"
fi
# Now add files after we're on the correct branch
git add data/md-markers/ data/sfs_json/
# Commit with multi-line message
git commit -m "Automatisk uppdatering av SFS-författningar $(date +'%Y-%m-%d')" \
-m "" \
-m "Inkluderar:" \
-m "- Käll-JSON (data/sfs_json/)" \
-m "- Markdown med selex-taggar (data/md-markers/)" \
-m "- Backup till Cloudflare R2"
echo "has_changes=true" >> $GITHUB_OUTPUT
echo "commit_branch=${COMMIT_BRANCH}" >> $GITHUB_OUTPUT
# Push the new branch with error handling
echo "Försöker pusha branch '${COMMIT_BRANCH}' till origin..."
if git push origin "$COMMIT_BRANCH" 2>&1; then
echo "✅ Git push lyckades för branch '${COMMIT_BRANCH}'"
echo "push_success=true" >> $GITHUB_OUTPUT
echo "push_error=" >> $GITHUB_OUTPUT
else
PUSH_EXIT_CODE=$?
PUSH_ERROR="Git push misslyckades med exit code: ${PUSH_EXIT_CODE}"
echo "❌ ${PUSH_ERROR}"
echo "push_success=false" >> $GITHUB_OUTPUT
echo "push_error=${PUSH_ERROR}" >> $GITHUB_OUTPUT
# Logga mer detaljerad information för debugging
echo "Git status:"
git status
echo "Git remote info:"
git remote -v
echo "Git log (senaste commit):"
git log --oneline -1
# Switch back to original branch even if push failed
git checkout "$CURRENT_BRANCH"
# Fail the job if push failed
echo "::error::Git push misslyckades: ${PUSH_ERROR}"
exit 1
fi
# Manage PR lifecycle: close old, create new
echo "🔄 Hanterar PR för branch '$COMMIT_BRANCH'..."
# Close existing PR for this branch if it exists
EXISTING_PR=$(gh pr list --head "$COMMIT_BRANCH" --json number --jq '.[0].number' 2>/dev/null || echo "")
if [ -n "$EXISTING_PR" ]; then
echo "Stänger befintlig PR #$EXISTING_PR"
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')"
fi
# Create new PR
echo "📝 Skapar ny PR..."
gh pr create --base main --head "$COMMIT_BRANCH" \
--title "SFS Data Update - $(date +'%Y-%m-%d %H:%M')" \
--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 }}" \
|| echo "⚠️ PR may already exist"
# Switch back to original branch
git checkout "$CURRENT_BRANCH"
echo "✅ Commits gjorda i branch '${COMMIT_BRANCH}'"
fi
- name: Trigger HTML export workflow
if: (inputs.enable_git_commit != 'false' && steps.commit_changes.outputs.has_changes == 'true' && steps.commit_changes.outputs.push_success == 'true') || (inputs.enable_git_commit == 'false')
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
// Always use the fixed branch name
const sourceRef = '${{ inputs.branch_name || 'workflow-artifact-data' }}';
github.rest.actions.createWorkflowDispatch({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: 'html-export-workflow.yml',
ref: sourceRef || context.ref,
inputs: {
source_ref: sourceRef || 'workflow-artifact-data'
}
})
- name: Report push failure
if: steps.commit_changes.outputs.has_changes == 'true' && steps.commit_changes.outputs.push_success == 'false'
run: |
echo "::warning::Git push misslyckades: ${{ steps.commit_changes.outputs.push_error }}"
echo "HTML export workflow kommer inte att triggas på grund av push-fel."