# This is a basic workflow to help you get started with Actions

name: Test sync to bitbucket

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the main branch
  pull_request:
    branches: [ main ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest
    
    env:
      UPSTREAM: ssh://git@bitbucket.ase.in.tum.de:7999/EIST22T02/eist22t02-whattocool48.git
      BITBUCKET_PUB_KEY: "[bitbucket.ase.in.tum.de]:7999,[131.159.89.140]:7999 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCO2i69e5LJibEDwVrwG9edX+xqeRB/Do+C8dNclgB5lFJjA5MlTFAZ4WewbLBeGXGAc5O64ckBlI82+FI1GwIkiKRoi/9qe+NGLmZFsjBRSz2fHLfKo/iEa8ytPP9E4iql1u7Rl+pnmY2claJ+ABQOt4XQgLWjYcIBHWpwhQ9tRjg73zUI/n/PKyEnODR14bEzJ/fBmTMgnm1ZdtSPViqLmByXgjgorCWPsRA3DrPsH3A9ncietUPj/qRbsWsPhiKK1yXXaKDHDgRD7ennVrwCAYG2hhTZEbZcjE8bbe9UY9WrRtoWLFKB/xDTn7pRv+ZxqXlcoP224we7J9s2ke1H"

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v3
        
      - name: Set up remote
        run: git remote add upstream ${{env.UPSTREAM}}
        
      - name: Setup SSH Agent
        uses: webfactory/ssh-agent@v0.5.4
        with:
          ssh-private-key: |
            ${{ secrets.PRIVATE_KEY }}
        
      - name: Copy ssh public_key
        run: echo "${{SECRETS.PUBLIC_KEY}}" > ~/.ssh/id_rsa.pub
        
      - name: Add bitbucket to known SSH-HOST
        run: echo ${{env.BITBUCKET_PUB_KEY}} >> ~/.ssh/known_hosts
        
      - name: Pull origin
        run: git fetch --unshallow
        
      - name: Specify Identity in case of rebasing
        run: git config --global user.email "organisation-name@github.com" && git config --global user.name "GitHub Action Runner"
      
      - name: Fix merge conflicts if any
        run: git pull --rebase -s recursive -X ours upstream main

