name: Generate and push @deriv/deriv-charts
on:
    workflow_dispatch:

jobs:
    generate_and_push_deriv_charts:
        if: github.repository == 'binary-com/SmartCharts'
        runs-on: 'ubuntu-latest'
        steps:
            - name: Setup node and npm
              id: step1
              uses: actions/setup-node@9ced9a43a244f3ac94f13bfd896db8c8f30da67a
              with:
                  node-version: '18'
                  check-latest: true
                  registry-url: 'https://registry.npmjs.org'
              env:
                  NODE_AUTH_TOKEN: ${{ secrets.NPM_ACCESS_TOKEN }}
            - name: Checkout repo
              id: step2
              uses: actions/checkout@a12a3943b4bdde767164f792f33f40b04645d846
              with:
                  fetch-depth: 1
                  ref: master
            - name: Bump version and create PR
              id: step3
              run: |
                  package_name="@deriv/deriv-charts"
                  # Identify with Git, these values come from the shared Deriv FE account. https://github.com/DerivFE
                  git config --global user.name "DerivFE"
                  git config --global user.email "80095553+DerivFE@users.noreply.github.com"
                  npm i && npm i -g json@latest
                  old_version=$(json -f ./package.json version)
                  # check if branch already exists in remote
                  if [ $( git ls-remote origin "$package_name" ) ]
                  then
                    # if the branch already exists delete it
                    git push origin -d "$package_name"
                  fi
                    
                  git checkout -b "$package_name"
                  git reset --hard master
                  # Patch the version with minor patch
                  npm version minor
                  npm run build
                  npm publish
                  # this is a cloned repo, need to create PR to update package.json version in master
                  # Wait for the new version to be published
                  sleep 30s
                  new_version=$(npm show "$package_name" version)
                  rm package-lock.json
                  npm install --package-lock-json
                  git commit -a -m "feat: bump from $old_version to $new_version"
                  git push --set-upstream origin "$package_name" -f
                  sudo apt install gh
                  gh auth login --with-token <<< ${{ secrets.PERSONAL_ACCESS_TOKEN }}
                  diff_url="https://diff.intrinsic.com/$package_name/$old_version/$new_version.diff"
                  pr_body=$(curl $diff_url)
                  # close the PR if it already exists and create a new one
                  gh pr close "$package_name" || true
                  # Create new PR with diff as body
                  gh pr create --title "Bump $package_name from $old_version to $new_version" --body "\`\`\`diff ${pr_body:0:5000} \`\`\` Diff url: $diff_url" --base "master" --head "binary-com:$package_name"
