|
| 1 | +name: Update CIA Production Statistics |
| 2 | + |
| 3 | +# Schedule: Daily at 03:00 CET (after CIA extraction at 02:57) |
| 4 | +# Manual trigger: Allow manual workflow dispatch for testing |
| 5 | +on: |
| 6 | + schedule: |
| 7 | + # Run at 03:00 CET (02:00 UTC winter, 01:00 UTC summer due to DST) |
| 8 | + # Using 02:00 UTC for consistency (safe buffer after 02:57 extraction) |
| 9 | + - cron: '0 2 * * *' |
| 10 | + workflow_dispatch: # Allow manual trigger |
| 11 | + inputs: |
| 12 | + force_update: |
| 13 | + description: 'Force update even if cache is fresh' |
| 14 | + required: false |
| 15 | + type: boolean |
| 16 | + default: false |
| 17 | + |
| 18 | +permissions: |
| 19 | + contents: write # Required to commit and push updated statistics |
| 20 | + |
| 21 | +jobs: |
| 22 | + update-stats: |
| 23 | + name: Fetch and Update CIA Production Statistics |
| 24 | + runs-on: ubuntu-latest |
| 25 | + |
| 26 | + steps: |
| 27 | + - name: Harden Runner |
| 28 | + uses: step-security/harden-runner@0080882f6c36860b6ba35c610c98ce87d4e2f26f # v2.10.2 |
| 29 | + with: |
| 30 | + egress-policy: audit # Monitor network activity |
| 31 | + allowed-endpoints: > |
| 32 | + api.github.com:443 |
| 33 | + github.com:443 |
| 34 | + raw.githubusercontent.com:443 |
| 35 | + registry.npmjs.org:443 |
| 36 | + objects.githubusercontent.com:443 |
| 37 | + |
| 38 | + - name: Checkout repository |
| 39 | + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 40 | + with: |
| 41 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 42 | + fetch-depth: 1 |
| 43 | + |
| 44 | + - name: Setup Node.js |
| 45 | + uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0 |
| 46 | + with: |
| 47 | + node-version: '24' |
| 48 | + cache: 'npm' |
| 49 | + |
| 50 | + - name: Install dependencies |
| 51 | + run: npm ci |
| 52 | + |
| 53 | + - name: Fetch CIA production statistics |
| 54 | + id: fetch_stats |
| 55 | + run: | |
| 56 | + echo "Fetching CIA production statistics..." |
| 57 | + node scripts/load-cia-stats.js |
| 58 | + |
| 59 | + # Check if stats file was created/updated |
| 60 | + if [ -f "cia-data/production-stats.json" ]; then |
| 61 | + echo "stats_fetched=true" >> $GITHUB_OUTPUT |
| 62 | + |
| 63 | + # Extract key statistics for summary |
| 64 | + TOTAL_PERSONS=$(jq -r '.counts.total_persons' cia-data/production-stats.json) |
| 65 | + TOTAL_VOTES=$(jq -r '.counts.total_votes' cia-data/production-stats.json) |
| 66 | + LAST_UPDATED=$(jq -r '.metadata.last_updated' cia-data/production-stats.json) |
| 67 | + |
| 68 | + echo "total_persons=$TOTAL_PERSONS" >> $GITHUB_OUTPUT |
| 69 | + echo "total_votes=$TOTAL_VOTES" >> $GITHUB_OUTPUT |
| 70 | + echo "last_updated=$LAST_UPDATED" >> $GITHUB_OUTPUT |
| 71 | + |
| 72 | + echo "✅ Statistics fetched successfully" |
| 73 | + echo " Total Persons: $TOTAL_PERSONS" |
| 74 | + echo " Total Votes: $TOTAL_VOTES" |
| 75 | + echo " Last Updated: $LAST_UPDATED" |
| 76 | + else |
| 77 | + echo "stats_fetched=false" >> $GITHUB_OUTPUT |
| 78 | + echo "❌ Failed to fetch statistics" |
| 79 | + exit 1 |
| 80 | + fi |
| 81 | + |
| 82 | + - name: Update website files |
| 83 | + id: update_files |
| 84 | + if: steps.fetch_stats.outputs.stats_fetched == 'true' |
| 85 | + run: | |
| 86 | + echo "Updating website files with new statistics..." |
| 87 | + node scripts/update-stats-from-cia.js |
| 88 | + |
| 89 | + # Check if any files were modified |
| 90 | + if git diff --quiet; then |
| 91 | + echo "files_changed=false" >> $GITHUB_OUTPUT |
| 92 | + echo "ℹ️ No changes needed - statistics are already up to date" |
| 93 | + else |
| 94 | + echo "files_changed=true" >> $GITHUB_OUTPUT |
| 95 | + |
| 96 | + # Count changed files |
| 97 | + CHANGED_FILES=$(git diff --name-only | wc -l) |
| 98 | + echo "changed_count=$CHANGED_FILES" >> $GITHUB_OUTPUT |
| 99 | + |
| 100 | + echo "✅ Updated $CHANGED_FILES files" |
| 101 | + git diff --stat |
| 102 | + fi |
| 103 | + |
| 104 | + - name: Commit and push changes |
| 105 | + if: steps.update_files.outputs.files_changed == 'true' |
| 106 | + run: | |
| 107 | + git config --global user.name "github-actions[bot]" |
| 108 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" |
| 109 | + |
| 110 | + git add cia-data/production-stats.json |
| 111 | + git add index*.html |
| 112 | + |
| 113 | + # Create commit message with statistics |
| 114 | + cat > /tmp/commit_msg.txt << EOF |
| 115 | + Update statistics from CIA production database |
| 116 | + |
| 117 | + Automated daily update from extraction_summary_report.csv |
| 118 | + |
| 119 | + Statistics: |
| 120 | + - Total Persons: ${{ steps.fetch_stats.outputs.total_persons }} |
| 121 | + - Total Votes: ${{ steps.fetch_stats.outputs.total_votes }} |
| 122 | + - Last Updated: ${{ steps.fetch_stats.outputs.last_updated }} |
| 123 | + - Files Changed: ${{ steps.update_files.outputs.changed_count }} |
| 124 | + |
| 125 | + Source: https://github.com/Hack23/cia/blob/master/service.data.impl/sample-data/extraction_summary_report.csv |
| 126 | + Workflow: .github/workflows/update-cia-stats.yml |
| 127 | + EOF |
| 128 | + |
| 129 | + git commit -F /tmp/commit_msg.txt |
| 130 | + git push |
| 131 | + |
| 132 | + echo "✅ Changes committed and pushed" |
| 133 | + |
| 134 | + - name: Create summary |
| 135 | + if: always() |
| 136 | + run: | |
| 137 | + echo "## CIA Production Statistics Update" >> $GITHUB_STEP_SUMMARY |
| 138 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 139 | + |
| 140 | + if [ "${{ steps.fetch_stats.outputs.stats_fetched }}" == "true" ]; then |
| 141 | + echo "### ✅ Statistics Fetched Successfully" >> $GITHUB_STEP_SUMMARY |
| 142 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 143 | + echo "| Metric | Value |" >> $GITHUB_STEP_SUMMARY |
| 144 | + echo "|--------|-------|" >> $GITHUB_STEP_SUMMARY |
| 145 | + echo "| Total Persons | ${{ steps.fetch_stats.outputs.total_persons }} |" >> $GITHUB_STEP_SUMMARY |
| 146 | + echo "| Total Votes | ${{ steps.fetch_stats.outputs.total_votes }} |" >> $GITHUB_STEP_SUMMARY |
| 147 | + echo "| Last Updated | ${{ steps.fetch_stats.outputs.last_updated }} |" >> $GITHUB_STEP_SUMMARY |
| 148 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 149 | + |
| 150 | + if [ "${{ steps.update_files.outputs.files_changed }}" == "true" ]; then |
| 151 | + echo "### 📝 Files Updated" >> $GITHUB_STEP_SUMMARY |
| 152 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 153 | + echo "Updated ${{ steps.update_files.outputs.changed_count }} files with new statistics." >> $GITHUB_STEP_SUMMARY |
| 154 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 155 | + echo "Changes committed and pushed to repository." >> $GITHUB_STEP_SUMMARY |
| 156 | + else |
| 157 | + echo "### ℹ️ No Changes Needed" >> $GITHUB_STEP_SUMMARY |
| 158 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 159 | + echo "Statistics are already up to date." >> $GITHUB_STEP_SUMMARY |
| 160 | + fi |
| 161 | + else |
| 162 | + echo "### ❌ Statistics Fetch Failed" >> $GITHUB_STEP_SUMMARY |
| 163 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 164 | + echo "Failed to fetch statistics from CIA production database." >> $GITHUB_STEP_SUMMARY |
| 165 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 166 | + echo "Check workflow logs for details." >> $GITHUB_STEP_SUMMARY |
| 167 | + fi |
| 168 | + |
| 169 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 170 | + echo "**Source:** [extraction_summary_report.csv](https://github.com/Hack23/cia/blob/master/service.data.impl/sample-data/extraction_summary_report.csv)" >> $GITHUB_STEP_SUMMARY |
| 171 | + |
| 172 | + - name: Notify on failure |
| 173 | + if: failure() |
| 174 | + run: | |
| 175 | + echo "❌ Workflow failed - check logs for details" |
| 176 | + echo "This may indicate:" |
| 177 | + echo " - Network connectivity issues" |
| 178 | + echo " - CIA extraction_summary_report.csv unavailable" |
| 179 | + echo " - Parse errors in CSV format" |
| 180 | + echo " - File write permission issues" |
| 181 | +
|
| 182 | +# ISMS Compliance |
| 183 | +# - ISO 27001:2022 A.5.33 - Protection of records (audit trails via Git, source attribution) |
| 184 | +# - ISO 27001:2022 A.8.3 - Information lifecycle management (automated daily updates) |
| 185 | +# - ISO 27001:2022 A.8.10 - Information deletion (proper retention policies, no excessive storage) |
| 186 | +# - ISO 27001:2022 A.8.19 - Security in use (HTTPS-only data transmission) |
| 187 | +# - NIST CSF 2.0 PR.DS-5 - Data integrity (automated validation checks) |
| 188 | +# - NIST CSF 2.0 DE.CM-1 - Network monitoring (harden-runner egress auditing) |
| 189 | +# - CIS Control 3.1 - Data inventory (documented public data sources) |
| 190 | +# - CIS Control 3.14 - Data integrity validation (automated verification) |
| 191 | +# - GDPR Article 6(1)(e) - Public interest processing (democratic transparency, political accountability) |
| 192 | +# - GDPR Article 9(2)(e) - Political opinions manifestly made public (voting records, party affiliation) |
| 193 | +# - Swedish Offentlighetsprincipen - Public access to government information (constitutional right) |
| 194 | +# |
| 195 | +# Note: A.8.11 (Data Masking) NOT applicable - processes only public government data from |
| 196 | +# Swedish Riksdag/Government. No sensitive data requiring masking. Journalist/OSINT platform |
| 197 | +# covering public officials in official capacity, protected by Press Freedom Act (Tryckfrihetsförordningen). |
0 commit comments