# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

parameters:
  filePath: ""
steps:
  - script: |
                  #!/bin/bash
                  set -eu
                  echo "Collecting sapcal_provisioning.json ${{ parameters.filePath }}"
                  cd ${FILE_PATH}
                  if [ -f "sapcal_provisioning.json" ]; then
                    echo "Found sapcal_provisioning.json"
                    git config --global user.email "${USER_EMAIL}"
                    git config --global user.name "${USER_NAME}"
                    echo "Checking out ${SOURCE_BRANCH} branch..."
                    git checkout -q ${SOURCE_BRANCH}
                    echo "Pulling last changes..."
                    git pull
                    echo "Adding sapcal_provisioning.json..."
                    git add sapcal_provisioning.json
                    if [ $(git diff --name-only --cached | wc -l) -gt 0 ]; then
                        echo "Committing changes..."
                        git commit -m "Adding sapcal_provisioning.json"
                        echo "Pushing changes..."
                        git push
                    else
                        echo "No changes to commit for sapcal_provisioning.json"
                    fi
                  else
                    echo "sapcal_provisioning.json not found"
                  fi
    displayName: Store SAP-CAL API response in repository
    enabled: true
    env:
      USER_EMAIL: $(Build.RequestedForEmail)
      USER_NAME: $(Build.RequestedFor)
      SOURCE_BRANCH: $(Build.SourceBranchName)
      SYSTEM_ACCESSTOKEN: $(System.AccessToken)
      FILE_PATH: ${{ parameters.filePath }}
