From d14078bdb45a557aea8af5999a754259f9135440 Mon Sep 17 00:00:00 2001 From: Joe Corall Date: Wed, 4 Dec 2024 14:39:15 -0500 Subject: [PATCH] Add node IDs to insert spreadsheet (#21) --- .github/workflows/run.yml | 12 ++++++------ scripts/insert-nids.sh | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) create mode 100755 scripts/insert-nids.sh diff --git a/.github/workflows/run.yml b/.github/workflows/run.yml index 31a3711..a371b4c 100644 --- a/.github/workflows/run.yml +++ b/.github/workflows/run.yml @@ -43,7 +43,7 @@ jobs: create_credentials_file: true service_account: ${{ secrets.WORKBENCH_GSA }} token_format: 'access_token' - access_token_scopes: "https://www.googleapis.com/auth/spreadsheets.readonly" + access_token_scopes: "https://www.googleapis.com/auth/spreadsheets" - name: Get Job ID from GH API id: get-job-id @@ -95,11 +95,11 @@ jobs: env: ISLANDORA_WORKBENCH_PASSWORD: ${{ secrets.ISLANDORA_WORKBENCH_PASSWORD }} - - name: Attach rollback - uses: actions/upload-artifact@v4 - with: - name: rollback.csv - path: islandora_workbench/input_data/rollback.csv + - name: add node IDs to sheet + run: ./scripts/insert-nids.sh + env: + URL: ${{ github.event.inputs.url }} + ACCESS_TOKEN: ${{ steps.auth.outputs.access_token }} - name: Notify Slack on Success if: ${{ success() }} diff --git a/scripts/insert-nids.sh b/scripts/insert-nids.sh new file mode 100755 index 0000000..796a5cc --- /dev/null +++ b/scripts/insert-nids.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +set -eou pipefail + +if [ ! -f islandora_workbench/input_data/rollback.csv ]; then + echo "rollback.csv does not exist" + exit 0 +fi + +regex='^(https:\/\/docs\.google\.com\/spreadsheets\/d\/[a-zA-Z0-9_-]+)' +if [[ "$URL" =~ $regex ]]; then + URL="${BASH_REMATCH[1]}" +else + echo "Invalid URL" + exit 1 +fi + +# extract sheet ID from https://docs.google.com/spreadsheets/d/foo/edit?gid=0#gid=0 +SHEET_ID=$(echo "$URL" | sed -n 's|.*/d/\(.*\)|\1|p') +API_URL="https://sheets.googleapis.com/v4/spreadsheets/$SHEET_ID/values/Sheet1!D2:append" +PAYLOAD=$(tail +4 islandora_workbench/input_data/rollback.csv | jq -R 'tonumber? | [if . == null then empty else . end]' | jq -s '{"values": [.[] | . ]}') + +STATUS=$(curl -s -X POST \ + -o /tmp/gsup.log \ + -w '%{http_code}' \ + --header "Authorization: Bearer $ACCESS_TOKEN" \ + --header "Content-Type: application/json" \ + --data @<(echo "$PAYLOAD") \ + "$API_URL?valueInputOption=RAW") + +if [ "${STATUS}" != 200 ]; then + echo "Failed to update spreadsheet with node IDs" + cat /tmp/gsup.log + exit 1 +fi