diff --git a/.github/fetch-events.js b/.github/fetch-events.js index 0a60a329..d9985c32 100644 --- a/.github/fetch-events.js +++ b/.github/fetch-events.js @@ -9,12 +9,38 @@ const params = new URLSearchParams({ }); const url = `${peoplyUrl}?${params}`; +const eventIdFile = "_data/eventId.txt"; + +let prevEventId = ""; + +if (fs.existsSync(eventIdFile)) { + prevEventId = fs.readFileSync(eventIdFile, "utf-8").trim(); +}; fetch(url) .then((res) => res.json()) .then((data) => { console.log("Fetched events:", data); + if (!data || data.length === 0) { + console.log("No data returned from Peoply.") + process.exit(0); + }; + + // Accesss latest event ID + const latestEvent = data[0]; + const latestEventId = latestEvent.id; + + // Stop if no new event + if (latestEventId === prevEventId) { + console.log("No new events in Peoply."); + process.exit(0); + }; + + // New event found + console.log("New event added in Peoply."); + fs.writeFileSync(eventIdFile, latestEventId); + // Adjust event time to follow timezone in Norway: const updatedData = data.map(event => { const date = new Date(event.startDate); @@ -23,10 +49,11 @@ fetch(url) return {...event, startDate: updatedStartDate }; }) - fs.writeFileSync("_data/events.json", JSON.stringify(updatedData, 2)); + fs.writeFileSync("_data/events.json", JSON.stringify(updatedData, null, 2)); console.log("Events written to _data/events.json"); + process.exit(1); }) .catch((err) => { console.error("Failed to fetch events:", err); - process.exit(1); + process.exit(0); }); diff --git a/.github/workflows/check-update.yml b/.github/workflows/check-update.yml new file mode 100644 index 00000000..334794b0 --- /dev/null +++ b/.github/workflows/check-update.yml @@ -0,0 +1,35 @@ +name: Check for new events on Peoply + +permissions: + contents: read + actions: write + +on: + workflow_dispatch: + schedule: + - cron: "0 2 * * *" + +jobs: + check-events: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup Node + uses: actions/setup-node@v4 + with: + node-version: "18" + + - name: Run Peoply event check + id: check + run: node .github/fetch-events.js + continue-on-error: true + + - name: Trigger main build workflow + if: steps.check.conclusion == 'failure' + uses: benc-uk/workflow-dispatch@e2e5e9a103e331dad343f381a29e654aea3cf8fc + with: + workflow: Build and Deploy Jekyll site + ref: master diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ab2812c4..c78cebb9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -60,7 +60,8 @@ jobs: needs: build if: >- github.event_name == 'push' && - (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master') + (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master') || + github.event_name == 'workflow_dispatch' steps: - name: Deploy to GitHub Pages id: deployment diff --git a/_data/eventId.txt b/_data/eventId.txt new file mode 100644 index 00000000..bfe6805b --- /dev/null +++ b/_data/eventId.txt @@ -0,0 +1 @@ +29b91519-0d07-4d5f-abc5-5b9f1d6ddf59 \ No newline at end of file