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

# This action will automatically create a pull request against dev if the pushed branch
# has a branch path spec likev 1.0/pipelinebuild/*. Configure this action by updating the
# environment variable values[0].

name: "create beta pull request"

# Controls when the action will run. Triggers the workflow on push
# events but only for branches with the following branch spec: "beta/pipelinebuild/*"
on:
  push:
    branches:
      - "beta/pipelinebuild/*"
    paths:
      - 'src/Beta/**/*.php'
      - '!src/Core/GraphConstants.php'

defaults:
  run:
    shell: bash

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  bump-minor-version:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    
    - name: Git config user
      run: |
        git config --global user.email "GraphTooling@service.microsoft.com"
        git config --global user.name "Microsoft Graph DevX Tooling"
    
    - name: Run increment script
      run: php scripts/IncrementMinorVersion.php

    - name: Commit and push changes if any
      run: if git commit -am "Bump up minor version"; then git push origin $GITHUB_REF; fi
        
  create-pull-request:
    needs: bump-minor-version
    # The type of runner that the job will run on
    runs-on: ubuntu-latest
    # https://github.com/actions/virtual-environments/blob/master/images/linux/Ubuntu1804-README.md

    # 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

    # Create a pull request [1]
    - name: Create PR using the GitHub REST API via hub
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        MESSAGE_TITLE: Generated beta models using Typewriter
        MESSAGE_BODY: "This pull request was automatically created by the GitHub Action, **${{github.workflow}}**. \n\n The commit hash is _${{github.sha}}_. \n\n **Important** Check for unexpected deletions or changes in this PR. See [beta_metadata.xml](https://github.com/microsoftgraph/msgraph-metadata/blob/master/beta_metadata.xml) for metadata changes. \n\n Make sure the version number is incremented in /src/Core/GraphConstants.php. Compare the version against the latest release on [packagist.org](https://packagist.org/packages/microsoft/microsoft-graph) and update the version in src/Core/GraphConstants."
        LABELS: generated
        BASE: dev
      run: |
        curl -fsSL https://github.com/github/hub/raw/master/script/get | bash -s 2.14.1
        bin/hub pull-request -b "$BASE" -h "$GITHUB_REF" -m "$MESSAGE_TITLE" -m "$MESSAGE_BODY" -r "$REVIEWERS" -l "$LABELS"
# References
# [0] https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables
# [1] https://hub.github.com/hub-pull-request.1.html
# https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token
