Update liquid templates for functional review #16
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: update-liquid-templates-review | |
| run-name: Update liquid templates for functional review | |
| # We run this action when a "functional review" label is added to the PR | |
| on: | |
| pull_request: | |
| types: | |
| - labeled | |
| - synchronize | |
| jobs: | |
| check-functional-review-label: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| has_functional_review_label: ${{ steps.haslabel.outputs.labeled-2-functional-review }} | |
| steps: | |
| - name: Checkout Repository - Fetch all history for all tags and branches | |
| uses: actions/checkout@v3 | |
| with: | |
| fetch-depth: 0 | |
| - name: Labeled to functional review | |
| id: haslabel | |
| uses: DanielTamkin/HasLabel@v1.0.4 | |
| with: | |
| contains: "2-functional-review" | |
| update-templates-for-functional-review: | |
| needs: check-functional-review-label | |
| if: needs.check-functional-review-label.outputs.has_functional_review_label | |
| runs-on: ubuntu-latest | |
| env: | |
| SF_API_CLIENT_ID: "${{ secrets.SF_API_CLIENT_ID }}" | |
| SF_API_SECRET: "${{ secrets.SF_API_SECRET }}" | |
| steps: | |
| - name: Checkout Repository - Fetch all history for all tags and branches | |
| uses: actions/checkout@v3 | |
| with: | |
| fetch-depth: 0 | |
| - name: Setup Node v18 | |
| uses: actions/setup-node@v3 | |
| with: | |
| node-version: 18 | |
| - name: Add silverfin-cli package latest version | |
| run: | | |
| npm install https://github.com/silverfin/silverfin-cli.git | |
| VERSION=$(./node_modules/silverfin-cli/bin/cli.js -V) | |
| echo "CLI version: ${VERSION}" | |
| - name: Create Silverfin config file | |
| run: | | |
| mkdir -p $HOME/.silverfin/ | |
| touch $HOME/.silverfin/config.json | |
| echo '${{ secrets.CONFIG_JSON }}' > $HOME/.silverfin/config.json | |
| node ./node_modules/silverfin-cli/bin/cli.js config --set-firm="${{ vars.FIRM_ID_REVIEW }}" | |
| node ./node_modules/silverfin-cli/bin/cli.js config --get-firm | |
| - name: Get changed liquid files | |
| id: changed-files | |
| uses: tj-actions/changed-files@v35 | |
| with: | |
| since_last_remote_commit: false | |
| dir_names: true | |
| files: | | |
| **/*.liquid | |
| - name: List all changed files | |
| run: | | |
| for file in ${{ steps.changed-files.outputs.all_changed_files }}; do | |
| echo "$file was changed" | |
| done | |
| - name: Update liquid templates | |
| run: | | |
| declare -a ERRORS | |
| FIRM_ID=${{ vars.FIRM_ID_REVIEW }} | |
| for CURRENT_DIR in ${{ steps.changed-files.outputs.all_changed_files }}; do | |
| echo "Checking directory: ${CURRENT_DIR}" | |
| while [[ "${CURRENT_DIR}" != "." ]]; do | |
| if [[ -e "${CURRENT_DIR}/config.json" ]]; then | |
| HANDLE=$(cat ${CURRENT_DIR}/config.json | jq -r ".handle // .name") | |
| echo "Found config.json in ${CURRENT_DIR}. Handle: ${HANDLE}" | |
| FIRM_IDS=$(cat ${CURRENT_DIR}/config.json | jq -r ".id" | jq "keys_unsorted" | jq -r ".[]") | |
| # Set template type | |
| TEMPLATE_TYPE="" | |
| FLAG="" | |
| if [[ "${CURRENT_DIR}" == *reconciliation_texts* ]]; then | |
| TEMPLATE_TYPE="reconciliation" | |
| FLAG="--handle" | |
| elif [[ "${CURRENT_DIR}" == *shared_parts* ]]; then | |
| TEMPLATE_TYPE="shared-part" | |
| FLAG="--shared-part" | |
| fi | |
| # TODO: handle missing type | |
| # Do we already have the id of this template ? | |
| TEMPLATE_ID_PRESENT=false | |
| if [[ "${FIRM_IDS[*]}" =~ "${FIRM_ID}" ]]; then | |
| TEMPLATE_ID_PRESENT=true | |
| fi | |
| echo "Is firm id present in the config.json of template ${HANDLE}?: ${TEMPLATE_ID_PRESENT}" | |
| # If we don't have the ID, try to get it from the firm | |
| if [[ "$TEMPLATE_ID_PRESENT" == 'false' ]]; then | |
| echo "Try to fetch the ID from firm id: ${FIRM_ID}" | |
| # get-reconciliation-id or get-shared-part-id | |
| OUTPUT_ID=$(node ./node_modules/silverfin-cli/bin/cli.js get-"${TEMPLATE_TYPE}"-id "${FLAG}"="${HANDLE}" --yes) | |
| if [[ "$OUTPUT_ID" =~ "ID updated" ]]; then | |
| echo "Template: ${HANDLE} ID fetched from firm ${FIRM_ID}" | |
| TEMPLATE_ID_PRESENT=true | |
| elif [[ "$OUTPUT_ID" =~ "not found" ]]; then | |
| echo "Template: ${HANDLE} ID not found in firm ${FIRM_ID}" | |
| else | |
| echo "Command to fetch the ID failed" | |
| fi | |
| fi | |
| # Update or created. Based on the presence of a template ID | |
| if [[ "$TEMPLATE_ID_PRESENT" == 'true' ]]; then | |
| echo "Updating ${HANDLE}" | |
| # update-reconciliation or update-shared-part | |
| OUTPUT=$(node ./node_modules/silverfin-cli/bin/cli.js update-"${TEMPLATE_TYPE}" "${FLAG}"="${HANDLE}" --yes) | |
| OPERATION_TYPE="update" | |
| else | |
| echo "Creating ${HANDLE}" | |
| # create-reconciliation or create-shared-part | |
| OUTPUT=$(node ./node_modules/silverfin-cli/bin/cli.js create-"${TEMPLATE_TYPE}" "${FLAG}"="${HANDLE}") | |
| OPERATION_TYPE="create" | |
| fi | |
| if [[ "$OUTPUT" =~ "201" ]]; then | |
| echo "Template ${HANDLE}: succesfully ${OPERATION_TYPE}d in firm ${FIRM_ID}" | |
| else | |
| echo "Template ${HANDLE}: failed to ${OPERATION_TYPE} in firm ${FIRM_ID}" | |
| ERRORS+=("${OUTPUT}") | |
| fi | |
| break | |
| else | |
| echo "Config file not found in directory: ${CURRENT_DIR}" | |
| CURRENT_DIR="$(dirname "${CURRENT_DIR}")" | |
| fi | |
| done | |
| done | |
| # CHECK ERRORS PRESENT | |
| if [ ${#ERRORS[@]} -eq 0 ]; then | |
| echo "All templates have been updated and/or created" | |
| else | |
| echo "Errors: ${ERRORS[@]}" | |
| exit 1 | |
| fi | |
| - name: Add Comment to PR | |
| uses: actions/github-script@v6 | |
| with: | |
| script: | | |
| github.rest.issues.createComment({ | |
| issue_number: context.issue.number, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: 'All templates were updated (or created) succesfully in the "Staging firm"' | |
| }) | |
| - name: Prepare CONFIG_JSON for secret | |
| if: always() | |
| run: | | |
| CONFIG_JSON=$(cat $HOME/.silverfin/config.json | tr -d '\n') | |
| echo "CONFIG_JSON=${CONFIG_JSON}" >> $GITHUB_ENV | |
| - name: Store CONFIG_JSON as secret | |
| uses: gliech/create-github-secret-action@v1 | |
| if: always() | |
| with: | |
| name: CONFIG_JSON | |
| value: "${{ env.CONFIG_JSON }}" | |
| pa_token: ${{ secrets.REPO_ACCESS_TOKEN }} |