diff --git a/.github/workflows/update-googlesheet-metadata.yaml b/.github/workflows/update-googlesheet-metadata.yaml new file mode 100644 index 0000000..9fd04d7 --- /dev/null +++ b/.github/workflows/update-googlesheet-metadata.yaml @@ -0,0 +1,73 @@ +name: Update GoogleSheet MetaData +on: + # Allows workflow to be manually run in Github UI + workflow_dispatch: + # Allows workflow to be called via HTTP calls to GitHub (with correct auth, etc) + repository_dispatch: + types: [update-googlesheet-data] + +permissions: + contents: write + pull-requests: write + +jobs: + create-or-update-pr: + runs-on: ubuntu-latest + steps: + - name: Checkout latest code from main branch + uses: actions/checkout@v4 + # Currently using a fork of https://github.com/jroehl/gsheet.action/ which adds + # the ability to output to file rather than using the standard GitHub Action + # output to an ENV. Outputting to ENV doesn't work when downloading large volumes + # of data. If/when this PR is merged we can go back to using the original repo: + # https://github.com/jroehl/gsheet.action/pull/615 + - uses: asibs/gsheet.action.asibs@release + name: Download latest data from Google Sheets + id: download_gsheets_data + env: + GSHEET_CLIENT_EMAIL: ${{ secrets.GSHEET_CLIENT_EMAIL }} + GSHEET_PRIVATE_KEY: ${{ secrets.GSHEET_PRIVATE_KEY }} + with: + spreadsheetId: ${{ vars.GSHEET_SPREADSHEET_ID }} + commands: | + [ + { + "command": "getData", + "args": { + "range": "'Metadata - EXPORT'!A:C", + "hasHeaderRow": true + } + } + ] + outputFile: /tmp/gsheet_action_results.json + - name: Output full results + run: | + cat /tmp/gsheet_action_results.json + - name: Write internal json metadata json + run: | + jq '.results[0].result.formatted | group_by( .source_type)[] | {(.[0].source_type): [.[] | { "description" : .description, "url" : .url } ]}' /tmp/gsheet_action_results.json > /tmp/jq_interim.json + jq -s 'reduce .[] as $item (null; . + $item)' /tmp/jq_interim.json > data/metadata.json + + # Add the updates to git + git add data/metadata.json + - name: Commit Changes and Create Pull Request + uses: peter-evans/create-pull-request@v6.0.1 + with: + # Note: Need to use a Personal Access Token here, NOT the standard + # secrets.GITHUB_TOKEN - if we use the standard Github Actions GITHUB_TOKEN + # then the commit/push/etc won't trigger subsequent Github Actions (such as + # the lint-pr action, which is required to merge PRs). The Personal Access + # Token must be setup as an Action Secret on the GitHub repo. Use the same + # Personal Access Token as the GHA_TOKEN env variable in your deployed app. + token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }} + base: main + branch: auto/constituency-data-changes + commit-message: Latest metadata data from Google Sheets + committer: GitHub Action Bot - Constituency Data + author: GitHub Action Bot - Constituency Data + title: "[MetaData] Latest Google Sheets Data" + body: | + Automatically created by Update-Googlesheet-Data Github action + labels: | + automated-pr + metadata-pr