# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

name: PowerShell Daily
on:
    workflow_dispatch:
    schedule:
    # At 13:00 UTC every day.
    - cron:  '0 13 * * *'

defaults:
  run:
    shell: pwsh

env:
  DOTNET_CLI_TELEMETRY_OPTOUT: 1
  POWERSHELL_TELEMETRY_OPTOUT: 1

jobs:
  update-dotnet-preview:
    name: Update .NET preview
    timeout-minutes: 15
    runs-on: windows-latest
    if: github.repository == 'PowerShell/PowerShell'
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Sync tags
        run: |
          git fetch --prune --unshallow --tags
      - name: Execute Update .NET script
        run: |
          $currentVersion = (Get-Content .\global.json | ConvertFrom-Json).sdk.version
          Write-Verbose "OLD_VERSION=$currentVersion" -Verbose
          "OLD_VERSION=$currentVersion" | Out-File $env:GITHUB_ENV -Append

          ./tools/UpdateDotnetRuntime.ps1 -UpdateMSIPackaging
          $newVersion = (Get-Content .\global.json | ConvertFrom-Json).sdk.version
          Write-Verbose "NEW_VERSION=$newVersion" -Verbose
          "NEW_VERSION=$newVersion" | Out-File $env:GITHUB_ENV -Append

          if ($currentVersion -ne $newVersion) {
            Write-Verbose "CREATE_PR=true" -Verbose
            "CREATE_PR=true" | Out-File $env:GITHUB_ENV -Append
          }
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v2
        id: cpr
        if: env.CREATE_PR == 'true'
        with:
          commit-message: "Update .NET SDK version from `${{ env.OLD_VERSION }}` to `${{ env.NEW_VERSION }}`"
          title: "Update .NET SDK version from `${{ env.OLD_VERSION }}` to `${{ env.NEW_VERSION }}`"
          base: master
          branch: dotnet_update


