name: Autobump

on:
    pull_request:
        types: [closed]

jobs:
    label-version-bump:
        runs-on: ubuntu-latest
        if: |
            github.event.pull_request.merged
            && (
                contains(github.event.pull_request.labels.*.name, 'bump patch')
                || contains(github.event.pull_request.labels.*.name, 'bump minor')
                || contains(github.event.pull_request.labels.*.name, 'bump major')
            )
        steps:
            - name: Check out the repository
              uses: actions/checkout@v2
              with:
                  ref: ${{ github.event.pull_request.base.ref }}

            - name: Detect version bump type
              id: bump-type
              run: |
                  BUMP_TYPE=null
                  if [[ $BUMP_PATCH_PRESENT == 'true' ]]; then
                      BUMP_TYPE='patch'
                  fi
                  if [[ $BUMP_MINOR_PRESENT == 'true' ]]; then
                      BUMP_TYPE='minor'
                  fi
                  if [[ $BUMP_MAJOR_PRESENT == 'true' ]]; then
                      BUMP_TYPE='major'
                  fi
                  echo "::set-output name=bump-type::$BUMP_TYPE"
              env:
                  BUMP_PATCH_PRESENT: ${{ contains(github.event.pull_request.labels.*.name, 'bump patch') }}
                  BUMP_MINOR_PRESENT: ${{ contains(github.event.pull_request.labels.*.name, 'bump minor') }}
                  BUMP_MAJOR_PRESENT: ${{ contains(github.event.pull_request.labels.*.name, 'bump major') }}

            - name: Determine new version
              id: new-version
              if: steps.bump-type.outputs.bump-type != 'null'
              run: |
                  CURRENT_VERSION=`python setup.py --version`
                  if [[ "${{ steps.bump-type.outputs.bump-type }}" == 'major' ]]; then
                    NEW_VERSION=`IFS='.' read -ra SPLITVER <<< "$CURRENT_VERSION"; echo $(("${SPLITVER[0]}"+1))."${SPLITVER[1]}"."${SPLITVER[2]}";`
                  elif [[ "${{ steps.bump-type.outputs.bump-type }}" == 'minor' ]]; then
                    NEW_VERSION=`IFS='.' read -ra SPLITVER <<< "$CURRENT_VERSION"; echo "${SPLITVER[0]}".$(("${SPLITVER[1]}"+1))."${SPLITVER[2]}";`
                  elif [[ "${{ steps.bump-type.outputs.bump-type }}" == 'patch' ]]; then
                    NEW_VERSION=`IFS='.' read -ra SPLITVER <<< "$CURRENT_VERSION"; echo "${SPLITVER[0]}"."${SPLITVER[1]}".$(("${SPLITVER[2]}"+1));`
                  fi  
                  echo "::set-output name=new-version::$NEW_VERSION"
                  echo "::set-output name=current-version::$CURRENT_VERSION"
                  
            - name: Update version in setup.py
              if: steps.bump-type.outputs.bump-type != 'null'
              run: sed -i "s/${{ steps.new-version.outputs.current-version }}/${{ steps.new-version.outputs.new-version }}/g" setup.py
            
            - name: Update version in meta.yaml
              if: steps.bump-type.outputs.bump-type != 'null'
              run: sed -i "s/${{ steps.new-version.outputs.current-version }}/${{ steps.new-version.outputs.new-version }}/g" conda/meta.yaml
            
            - name: Commit bump
              if: steps.bump-type.outputs.bump-type != 'null'
              uses: EndBug/add-and-commit@v7.3.0
              with:
                  message: 'Bump version to ${{ steps.new-version.outputs.new-version }}'
                  committer_name: GitHub Actions
                  committer_email: 41898282+github-actions[bot]@users.noreply.github.com


