diff --git a/.pipelines/.templates/vars.yml b/.pipelines/.templates/vars.yml index 08d4087..36cfc97 100644 --- a/.pipelines/.templates/vars.yml +++ b/.pipelines/.templates/vars.yml @@ -16,13 +16,16 @@ variables: # Files listed in the .order file will be deployed before other files and in the # order they are listed. # + # Set AZOPS_SKIP_PR_COMPLETION to true to opt-out from pull request completion. + # # - ARM_TENANT_ID # - ARM_SUBSCRIPTION_ID # - ARM_CLIENT_ID # - ARM_CLIENT_SECRET # - ARM_ENVIRONMENT - # - AZOPS_MODULE_VERSION - # - AZOPS_CUSTOM_SORT_ORDER + # - AZOPS_MODULE_VERSION + # - AZOPS_CUSTOM_SORT_ORDER + # - AZOPS_SKIP_PR_COMPLETION # - group: credentials @@ -36,4 +39,4 @@ variables: # - name: modulesFolder - value: '$(System.DefaultWorkingDirectory)/Modules' \ No newline at end of file + value: '$(System.DefaultWorkingDirectory)/Modules' diff --git a/.pipelines/pull.yml b/.pipelines/pull.yml index afad219..ae5303d 100644 --- a/.pipelines/pull.yml +++ b/.pipelines/pull.yml @@ -54,7 +54,7 @@ variables: # # Folder Name # By default we generate the hierachy within the - # 'azops' folder within the root of the repository. + # 'root' folder within the root of the repository. # If this property is modified, the config value within # the settings.json file - Core.State will also need # to be changed. @@ -129,7 +129,7 @@ jobs: # Set global options # - - task: Bash@3 + - task: PowerShell@2 displayName: "Configure" inputs: targetType: "inline" @@ -142,7 +142,7 @@ jobs: # Switch branches # - - task: Bash@3 + - task: PowerShell@2 displayName: "Checkout" inputs: targetType: "inline" @@ -169,26 +169,26 @@ jobs: # Check for data changes # - - task: Bash@3 + - task: PowerShell@2 displayName: "Status" inputs: targetType: "inline" script: | - STATUS=$(git status --short $(folder)) - echo $STATUS - if [ -z "$STATUS" ] - then - echo "##vso[task.setvariable variable=state]stop" - else - echo "##vso[task.setvariable variable=state]continue" - fi + $gitStatus = git status --short $(folder) + if ($null -ne $gitStatus) { + $gitStatus | Write-Host + Write-Host '##vso[task.setvariable variable=state]continue' + } + else { + Write-Host '##vso[task.setvariable variable=state]stop' + } # # Add # Add file content to index # - - task: Bash@3 + - task: PowerShell@2 displayName: "Add" condition: contains(variables['state'], 'continue') inputs: @@ -202,7 +202,7 @@ jobs: # Record changes to the repository # - - task: Bash@3 + - task: PowerShell@2 displayName: "Commit" condition: contains(variables['state'], 'continue') inputs: @@ -216,7 +216,7 @@ jobs: # Update remote refs along with associated objects # - - task: Bash@3 + - task: PowerShell@2 displayName: "Push" condition: contains(variables['state'], 'continue') inputs: @@ -230,31 +230,43 @@ jobs: # Update remote refs along with associated objects # - - task: Bash@3 + - task: PowerShell@2 displayName: "Merge" condition: contains(variables['state'], 'continue') inputs: targetType: "inline" script: | - # Open new PR - PROut=$( - az repos pr create \ - --title "$(pull_request)" \ - --source-branch "$(branch)" \ - --target-branch "main" \ - --squash true \ - --delete-source-branch true \ - --auto-complete true \ - ); - - # Get PR ID and check status - PRid=$(echo $PROut | jq -r '.pullRequestId'); - PRStatus=$(az repos pr show --id $PRid | jq .status); - - # If PR is not completed, then complete it bypassing policy - if [ $PRStatus == "\"active\"" ]; then - echo "Completing PR bypassing branch policy" - az repos pr update --status completed --id $PRid --bypass-policy true --bypass-policy-reason "Automated pull request" > /dev/null 2>&1 - fi; + # Check if active PR already exists + $pr = az repos pr list ` + --source-branch "$(branch)" ` + --target-branch "main" ` + --status "active" | ConvertFrom-Json -NoEnumerate + + if ($pr) { + Write-Host 'Active PR found with Id:' $pr.pullRequestId + } else { + # Open new PR + $pr = az repos pr create ` + --title "$(pull_request)" ` + --source-branch "$(branch)" ` + --target-branch "main" ` + --squash true ` + --delete-source-branch true ` + --auto-complete true | ConvertFrom-Json -NoEnumerate + + Write-Host 'PR created with Id:' $pr.pullRequestId + } + + # Complete any active PR, new or existing + $completePullRequest = $env:AZOPS_SKIP_PR_COMPLETION -ne 'true' + if ($completePullRequest) { + $prId = $pr.pullRequestId + $pr = az repos pr show --id $prId | ConvertFrom-Json -NoEnumerate + # If PR is not completed, then complete it bypassing policy + if ($pr.status -eq 'active') { + Write-Host 'Completing PR bypassing branch policy' + az repos pr update --status completed --id $prId --bypass-policy true --bypass-policy-reason 'Automated pull request' + } + } env: AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)