Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The pull pipeline should fail if old PR is still active during automated pull #741

Open
nishantmittal6239 opened this issue Feb 6, 2023 · 6 comments · May be fixed by Azure/AzOps-Accelerator#135

Comments

@nishantmittal6239
Copy link

Description:
We have a pull pipeline which run after every 6 hours to fetch the latest changes from the portal. This pipeline creates a PR that requires a manual approval so If the PR is not approved and completed before the next pull run then it should fail the pipeline instead of just giving the message. See screenshot attached.

az-ops_error_1

Screenshots

@jsandquist
Copy link
Contributor

Hi @nishantmittal6239,
this seems to match my use-case so I think a better option might be for you to comment out the complete operation if PR is still active - this line

# az repos pr update --status completed --id $PRid --bypass-policy true --bypass-policy-reason "Automated pull request" > /dev/null 2>&1

Then you will see additional updates to the active PR instead - and each update based on the latest commit to 'main' if there has been any other changes in between.

Works quite well for me!

@daltondhcp
Copy link
Contributor

@nishantmittal6239 , @jsandquist,
Do you think this is something we should change in terms of the default behavior, or provide as an additional option?

@daltondhcp daltondhcp mentioned this issue Feb 17, 2023
5 tasks
@daltondhcp daltondhcp linked a pull request Feb 17, 2023 that will close this issue
5 tasks
@daltondhcp daltondhcp removed a link to a pull request Feb 17, 2023
5 tasks
@jsandquist
Copy link
Contributor

An additional option could be fine - to communicate that there is an alternative way the client might prefer or at least consider.

@daltondhcp
Copy link
Contributor

@jsandquist - do you want to make a contribution to the pipeline with this as an option?

@jsandquist
Copy link
Contributor

Sure - I can give it a go. Further discussions in the PR as soon as it shows up around here somewhere...

The setting should probably go into a variable group for Azure Pipelines. And similar for GitHub Actions. My first cut below - for Azure Pipelines and rather untested - where PowerShell might be preferred following AzOps-Accelerator#120:

  - task: Bash@3
    displayName: "Merge"
    condition: contains(variables['state'], 'continue')
    inputs:
      targetType: "inline"
      script: |
        # Check if active PR already exists
        PRActive=$(
          az repos pr list \
          --source-branch "$(branch)" \
          --target-branch "main" \
          --status "active" \
          --repository "$(repository_name)" \
        );

        if [ -z "$PRActive" ]
        then
          echo "No active PR - creating new PR"
        else
          PRid=$(echo $PRActive | jq -r '.[0].pullRequestId');
          echo "Active Pull Request already exists - nothing more to do: $PRid"
          exit
        fi

        # 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 \
          --repository "$(repository_name)" \
        );

        # Get PR ID and check status
        PRid=$(echo $PROut | jq -r '.pullRequestId');
        PRStatus=$(az repos pr show --id $PRid | jq .status);
        echo "New Pull Request created with Id: $PRid"

        # If PR is not completed, then complete it bypassing policy (not allowed so commented out for now)
        if [ $PRStatus == "\"active\"" ]; then
          echo "Completing PR bypassing branch policy - would have been done here - if permitted but now a no-op"
          # az repos pr update --status completed --id $PRid --bypass-policy true --bypass-policy-reason "Automated pull request" > /dev/null 2>&1
        fi;
    env:
          AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)

@daltondhcp
Copy link
Contributor

This looks good to me, feel free to continue with PR in the accelerator-repo as well as wiki.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants