From 849a2193a0387df1784dc5d62ca0384fd54f48c0 Mon Sep 17 00:00:00 2001 From: 49659410+tx0c <> Date: Mon, 22 Apr 2024 19:49:38 +0000 Subject: [PATCH] feat(auto-pr): update setup-node for auto create assets-pr --- .github/workflows/checkin-assets.yaml | 32 ++++++++++++++++++++------- README.md | 2 ++ bin/sync-up-latest-round.mjs | 28 ++++++++++++++++------- 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/.github/workflows/checkin-assets.yaml b/.github/workflows/checkin-assets.yaml index bdd530b..da5b1b5 100644 --- a/.github/workflows/checkin-assets.yaml +++ b/.github/workflows/checkin-assets.yaml @@ -3,7 +3,7 @@ name: Billboard Rounds - Create PR to update latest round on: workflow_dispatch jobs: - create-pull-request: + ci-round-assets: runs-on: ubuntu-latest steps: - name: Checkout Repo @@ -12,21 +12,37 @@ jobs: - name: Setup Node.js uses: actions/setup-node@v4 with: - node-version: '20' - cache: 'npm' + node-version: '>=20' - name: Install Dependencies run: npm install @aws-sdk/client-s3 ## the only dependency for ./bin/sync-up-latest-round.mjs - - run: ./bin/sync-up-latest-round.mjs + - name: Sync up Latest Round + run: "./bin/sync-up-latest-round.mjs | tee message.log" env: AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} AWS_REGION: ap-southeast-1 - - uses: gr2m/create-or-update-pull-request-action@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Create Pull Request + id: cpr + uses: peter-evans/create-pull-request@v6 with: - path: "./public/static" + ## token: ${{ secrets.BILLBOARD_GITHUB_TOKEN }} + commit-message: auto checkin latest Round assets as Preview + base: main + signoff: false + branch: auto/round-assets + delete-branch: true + # path: public/static + add-paths: public/static + title: check in latest round Assets + body-path: message.log + body: | + Update preview Assets + - Updated with *today's* date + - Auto-generated by [create-pull-request][1] + [1]: https://github.com/peter-evans/create-pull-request + labels: assets, automated pr + draft: true diff --git a/README.md b/README.md index dcf3497..126ea02 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/) +[![Billboard Rounds - Create PR to update latest round](https://github.com/thematters/billboard-app/actions/workflows/checkin-assets.yaml/badge.svg)](https://github.com/thematters/billboard-app/actions/workflows/checkin-assets.yaml) + ## Getting started ### Start local dev diff --git a/bin/sync-up-latest-round.mjs b/bin/sync-up-latest-round.mjs index 86b3cb6..84efaec 100755 --- a/bin/sync-up-latest-round.mjs +++ b/bin/sync-up-latest-round.mjs @@ -7,34 +7,43 @@ const client = new S3Client() // use credentials from environment const AssetsDir = './public/static' -export const main = async () => { +export const main = async (branch = 'prod') => { const controller = new AbortController() const { signal } = controller + const developPrefix = branch !== 'prod' ? 'web-develop/' : '' + console.log('```console') try { // The Body object also has 'transformToByteArray' and 'transformToWebStream' methods. const roundsContent = await ( await client.send( new GetObjectCommand({ Bucket: 'matters-billboard', - Key: 'rounds/rounds.json', + Key: `${developPrefix}rounds/rounds.json`, }) ) ).Body.transformToString() const rounds = JSON.parse(roundsContent) + const lastRound = rounds[rounds.length - 1] + console.log( new Date(), `got ${rounds.length} rounds with latest:`, - rounds[rounds.length - 1] + lastRound ) - await writeFile(`${AssetsDir}/rounds.json`, roundsContent, { signal }) + if (lastRound.draft) { + delete lastRound.draft // finalize the last round if still draft + // roundsContent = JSON.stringify(rounds); + } + + await writeFile(`${AssetsDir}/rounds.json`, JSON.stringify(rounds), { + signal, + }) console.log( new Date(), `written rounds.json with ${(roundsContent.length / 1024).toFixed(1)} KBytes.` ) - const lastRound = rounds[rounds.length - 1] - console.log(new Date(), 'latest round:', lastRound.id, lastRound.dirpath) const createDir = await mkdir(`${AssetsDir}/${lastRound.dirpath}`, { @@ -47,7 +56,7 @@ export const main = async () => { await client.send( new GetObjectCommand({ Bucket: 'matters-billboard', - Key: `rounds/${lastRound.dirpath}/${file}`, + Key: `${developPrefix}rounds/${lastRound.dirpath}/${file}`, }) ) ).Body.transformToString() @@ -66,6 +75,9 @@ export const main = async () => { } catch (err) { console.error(new Date(), `ERROR:`, err) } + + console.log('```') } -await main() +// console.log('run with argv:', process.argv) +await main(process.argv?.[2] || 'prod')