name: initialize database
description: initialize database by using database of test environment
author: BinKrassDuFass

inputs:
  MONGODB_SERVICE_NAME:
    description: 'Name of the MongoDB service'
    required: true
    default: 'mongo'

  TIBI_USERNAME:
    description: 'Username of the Tibi account'
    required: true
    default: 'admin'

  TIBI_PASSWORD:
    description: 'Password of the Tibi account'
    required: true
    default: 'admin'

  TIBI_API_URL:
    description: 'URL of the Tibi API'
    required: true
    default: 'http://tibi-server:8080/api/v1'
  
  TIBI_API_CONFIG_PATH:
    description: 'Path of the Tibi API config'
    required: true
    default: "${{github.workspace}}/api/config.yml}}"
  
  TIBI_API_NAMESPACE:
    description: 'Namespace of the Tibi API'
    required: true
  
  PROJECT_NAME:
    description: 'Name of the project'
    required: true

runs:
    using: composite
    steps:
      - name: Setup mongo tools
        shell: bash
        run: |
              echo "::group::setup Mongo keys"
              wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
              sudo apt-get install -y gnupg
              wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
              echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
              echo "::endgroup::"

              echo "::group::mongodb tools"
              sudo apt-get update
              echo "aptitude install mongodb-database-tools"
              sudo apt-get install -y mongodb-database-tools
              echo "::endgroup::"

              echo "mongodump --version"
              mongodump --version

      - name: Restore MongoDB Data
        run: |
          echo "::group::initialize mongo data"
          mongorestore --uri "mongodb://${{inputs.MONGODB_SERVICE_NAME}}:27017" ./.github/actions/init-db/mongo-dump
          echo "::endgroup::"
        shell: bash

      - name: set config in tibi
        shell: bash
        run: ./.github/actions/init-db/setConfigInTibiProject.sh  ${{inputs.TIBI_USERNAME}} ${{inputs.TIBI_PASSWORD}} ${{inputs.TIBI_API_URL}} ${{inputs.TIBI_API_CONFIG_PATH}} ${{inputs.TIBI_API_NAMESPACE}} ${{inputs.PROJECT_NAME}}
              




      
