TF401398: The pull request cannot be activated because the source and/or the target branch no longer exists, or the requested refs are not branches
Asked Answered
F

3

6

I was getting the following errors while creating a pull-request using azuredevops golang API

error: TF401398: The pull request cannot be activated because the source and/or the target branch no longer exists, or the requested refs are not branches

Code :

    connection := azuredevops.NewPatConnection("organizationUrl", "PAToken")
    ctx := context.Background()
    client, _ := azuregit.NewClient(ctx, connection)
    pr := azuregit.CreatePullRequestArgs{}
    repoId := git.repository.Name
    proname := "MVC Test -Demo"
    pr.Project = &proname
    pr.RepositoryId = &repoId
    sourceBranch:= "feature-test"
    targetBranch:= "main"
    pr.GitPullRequestToCreate = &azuregit.GitPullRequest{
        TargetRefName: &targetBranch,
        SourceRefName: &sourceBranch,
        Description:   &git.configuration.PrRequestMessage,
        Title:         &git.configuration.PrRequestTitle,
    }
    _, err := client.CreatePullRequest(ctx, pr)
Fado answered 8/9, 2021 at 5:25 Comment(0)
F
6

This issue got fixed by prefixing a refs/heads/ to the branch

        sourceBranch:= "refs/heads/feature-test"
        targetBranch:= "refs/heads/main"
        pr.GitPullRequestToCreate = &azuregit.GitPullRequest{
            TargetRefName: &targetBranch,
            SourceRefName: &sourceBranch,
            Description:   &git.configuration.PrRequestMessage,
            Title:         &git.configuration.PrRequestTitle,
        }
        _, err := client.CreatePullRequest(ctx, pr)
Fado answered 8/9, 2021 at 5:25 Comment(0)
A
2

Having the same issue and let me tell you a secret!

Don't keep a branch with the name dependabot. So for each dependency, dependabot tries to create a branch under the folder "dependabot" but Azure fails to create a folder if a branch with the same name already exists.

Acaricide answered 8/8, 2023 at 10:7 Comment(0)
D
0

I had the same issue (though in Azure DevOps) and each time another fix worked:

Error: Pull Request creation failed with status 400. Message: TF401398: The pull request cannot be activated because the source and/or the target branch no longer exists, or the requested refs are not branches (StandardError)

  1. recreate the missing branch (see last 400 error in your pipeline log)
  2. add refs/heads/ to main in your YAML (see below example)
  3. go to project settings > select your repo > go to 'security'> select Project Collection Build Service Accounts user > enable 'Allow' for contribute to pull requests and 'Allow' for Create branch enter image description here
  4. add unique PAT for your agent (see example below)
        ---
    schedules:
      - cron: '0 2 * * *'
        displayName: 'yourdisplayname'
        always: true
        branches:
          include:
            - main
    
    trigger: none
    
    stages:
      - stage: CheckDependencies
        displayName: 'dependabot run'
        jobs:
          - job: Dependabot
            displayName: 'Run Dependabot'
            pool:
              vmImage: 'ubuntu-latest'
            steps:
              - task: AzureKeyVault@2
                inputs:
                  connectedServiceName: 'name'
                  keyVaultName: 'kvname'
                  secretsFilter: 'azureDevOpsAccessToken'
                  runAsPreJob: true
    
              - task: dependabot@1
                inputs:
                  azureDevOpsAccessToken: '$(azureDevOpsAccessToken)'  
    
              - task: dependabot@1
                inputs:
                  packageManager: 'nuget'
                  targetBranch: 'refs/heads/main'

Dyanna answered 5/4, 2023 at 14:13 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.