|
| 1 | +name: Generate and Release Calendar |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + paths: |
| 6 | + - 'calendar/cpp-aachen.jsoncal' |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + generate-calendar: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + |
| 16 | + steps: |
| 17 | + - name: Checkout repository |
| 18 | + uses: actions/checkout@v4 |
| 19 | + |
| 20 | + - name: Set up Python |
| 21 | + uses: actions/setup-python@v4 |
| 22 | + with: |
| 23 | + python-version: '3.11' |
| 24 | + cache: 'pip' |
| 25 | + |
| 26 | + - name: Install Python dependencies |
| 27 | + run: | |
| 28 | + python -m pip install --upgrade pip |
| 29 | + pip install icalendar |
| 30 | + |
| 31 | + - name: Download json2ical.py script |
| 32 | + run: | |
| 33 | + curl -o json2ical.py https://raw.githubusercontent.com/d-frey/json2ical/refs/heads/main/json2ical.py |
| 34 | + chmod +x json2ical.py |
| 35 | + |
| 36 | + - name: Generate iCal file |
| 37 | + run: | |
| 38 | + python json2ical.py calendar/cpp-aachen.jsoncal cpp-aachen.ics |
| 39 | + |
| 40 | + - name: Get current timestamp |
| 41 | + id: timestamp |
| 42 | + run: echo "timestamp=$(date +'%Y%m%d-%H%M%S')" >> $GITHUB_OUTPUT |
| 43 | + |
| 44 | + - name: Create or update latest release |
| 45 | + uses: actions/github-script@v7 |
| 46 | + with: |
| 47 | + script: | |
| 48 | + const fs = require('fs'); |
| 49 | + const { owner, repo } = context.repo; |
| 50 | + |
| 51 | + // Try to get existing "latest" release |
| 52 | + let release; |
| 53 | + try { |
| 54 | + const existingRelease = await github.rest.repos.getReleaseByTag({ |
| 55 | + owner, |
| 56 | + repo, |
| 57 | + tag: 'latest' |
| 58 | + }); |
| 59 | + release = existingRelease.data; |
| 60 | + |
| 61 | + // Delete existing assets |
| 62 | + const assets = await github.rest.repos.listReleaseAssets({ |
| 63 | + owner, |
| 64 | + repo, |
| 65 | + release_id: release.id |
| 66 | + }); |
| 67 | + |
| 68 | + for (const asset of assets.data) { |
| 69 | + await github.rest.repos.deleteReleaseAsset({ |
| 70 | + owner, |
| 71 | + repo, |
| 72 | + asset_id: asset.id |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + // Update release |
| 77 | + await github.rest.repos.updateRelease({ |
| 78 | + owner, |
| 79 | + repo, |
| 80 | + release_id: release.id, |
| 81 | + name: 'Latest Calendar Release', |
| 82 | + body: `Automatically generated iCal calendar file from cpp-aachen.jsoncal\\n\\nGenerated on: ${new Date().toISOString()}\\n\\nThis release is automatically updated when the calendar file changes.` |
| 83 | + }); |
| 84 | + |
| 85 | + } catch (error) { |
| 86 | + if (error.status === 404) { |
| 87 | + // Create new release if "latest" doesn't exist |
| 88 | + const newRelease = await github.rest.repos.createRelease({ |
| 89 | + owner, |
| 90 | + repo, |
| 91 | + tag_name: 'latest', |
| 92 | + name: 'Latest Calendar Release', |
| 93 | + body: `Automatically generated iCal calendar file from cpp-aachen.jsoncal\\n\\nGenerated on: ${new Date().toISOString()}\\n\\nThis release is automatically updated when the calendar file changes.`, |
| 94 | + draft: false, |
| 95 | + prerelease: false |
| 96 | + }); |
| 97 | + release = newRelease.data; |
| 98 | + } else { |
| 99 | + throw error; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // Upload the iCal file |
| 104 | + const fileContent = fs.readFileSync('cpp-aachen.ics'); |
| 105 | + |
| 106 | + await github.rest.repos.uploadReleaseAsset({ |
| 107 | + owner, |
| 108 | + repo, |
| 109 | + release_id: release.id, |
| 110 | + name: 'cpp-aachen.ics', |
| 111 | + data: fileContent, |
| 112 | + headers: { |
| 113 | + 'content-type': 'text/calendar' |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | + core.info(`Successfully uploaded cpp-aachen.ics to release ${release.tag_name}`); |
0 commit comments