Warning: "Please make sure to fetch this ref before pull request analysis" in Azure pipeline

  • SonarQube 8.6.0.39681
  • SonarScanner for MSBuild 5.0.4

I am using a build yaml pipeline

I am trying to get cogent results from Pull Request scans but I keep getting odd Code Smells and missing errors.

I’ve tried adding a step to the pipeline
- checkout: self
… or …

    resources:
      repositories:
      - repository: BaseBranch
        type: git
        name: MyRepoName
        ref: development

I still get the warning:

Could not find ref ‘‘refs/heads/development’’ in refs/heads, refs/remotes/upstream or refs/remotes/origin. You may see unexpected issues and changes. Please make sure to fetch this ref before pull request analysis.

I can tell I’m not getting valid results from the Pull Request builds because I am manually putting in errors and they are not showing in the analysis.

How do I fix this message?

Hi @padillah ,

Welcome to SonarSource Community! :sonarsource:

That warning defines the strict matching of branch names between what is defined in SonarQube and what is defined in your SCM (is this Azure Repos Git?).

If you do not ensure the branches are matched exactly, you may end with closed issues being reopened or other unexpected behaviors as you observed.

I suggest checking that your branch analysis is correct without doing a PR and make sure that is good first.

Make sure to check the 3 PR analysis parameters here: Pull Request Analysis | SonarQube Docs

If you still can’t get it, start with a simpler example which may expose issues with your pipeline.yml.

  1. create a clone of your original repository
  2. create a new project in SonarQube to scan this new clone of the repository
  3. make sure the analysis scans look correct on you main branch
  4. scan a branch and make sure branch analysis scans look good
  5. now create a new branch and make sure the scan looks good
  6. make adjustments in your new branch to create a PR

Joe

Yes, this is an Azure Repo Git.

I ran a plain Full Build and got the following ‘warnings’:

  • There are problems with file encoding in the source code. Please check the scanner logs for more details.
  • Missing blame information for 98 files. This may lead to some features not working correctly. Please check the analysis logs.

I then created a branch and made the SQ PR build a build policy. The pipeline adds the following inputs:

${{ if eq(parameters.isPullRequest, true) }}:
  extraProperties: |
   sonar.pullrequest.key='$(System.PullRequest.PullRequestId)'
   sonar.pullrequest.branch='$(System.PullRequest.SourceBranch)'
   sonar.pullrequest.base='$(System.PullRequest.TargetBranch)'

When this build finishes running the SonarQube site has the following three warnings for the pull request branch:

  • Could not find ref ‘‘refs/heads/pipeline/padillah/fixSonarQube’’ in refs/heads, refs/remotes, refs/remotes/upstream or refs/remotes/origin. You may see unexpected issues and changes. Please make sure to fetch this ref before pull request analysis.
  • There are problems with file encoding in the source code. Please check the scanner logs for more details.
  • Missing blame information for 98 files. This may lead to some features not working correctly. Please check the analysis logs.

Hi @padillah ,

Can you take a screenshot of what kind of build policy and/or status check you have set up? It should something that looks like this:

Anyways, let’s address the easier problems first:

  • There are problems with file encoding in the source code. Please check the scanner logs for more details.
    • This can be fixed by setting the correct file encoding on the file or forcefully setting sonar.sourceEncoding parameter in your extraProperties of SonarQubePrepare task. Run your Azure Pipeline with sonar.verbose=true to get debug level details and check the logs. Please share this with me if you do not understand it.
  • Missing blame information for 98 files. This may lead to some features not working correctly. Please check the analysis logs.
    • Again, set sonar.verbose=true to get debug level for more details, but the scanner logs should tell you which files did not have a git commit for it. Make sure to git commit those files so that your PR and SonarQube understand the file changes.

Regarding Could not find ref... issue, can you post a screenshot of what logs show when you click on the “Job” and then the “6 queue time variables used” drop down?

Image: ubuntu-20.04
Queued: Just now [manage parallel jobs]
Agent: Hosted Agent
Started: Just now
Duration: 12s

The agent request is already running or has already completed.
Job preparation parameters
6 queue time variables used
system.pullRequest.pullRequestId : 25
system.pullRequest.sourceBranch : refs/heads/feature/testing-pr-comments-azd
system.pullRequest.targetBranch : refs/heads/main
system.pullRequest.sourceCommitId : 692e7854f9bd01907478cc80be7c53aed48dda46
system.pullRequest.sourceRepositoryUri : https:/xxx@dev.azure.com/xxx/jojo-js/_git/jojo-js
system.pullRequest.pullRequestIteration : 3

Job live console data:
Starting: Job
Finishing: Job

Joe

Ok, I managed to address the two outstanding warnings so the Full build now runs with no warnings. Thank you so much for that.

The build policy I have defined as thus:

The six runtime variables that you asked about are:

And I am still getting the following warning on the Pull Request results:
image

This has been a huge help. My next step is to add the deliberate checkout of the pipeline/padillah/fixSonarQube branch to see if that addresses the warning.

1 Like

Hi @padillah ,

Thanks for the screenshots. I’m glad you got through some of the problems. How’s it going now? Did you get it fixed?

I noticed from your screenshot of the runtime variables, that your TargetBranch is not the typical develop or master branch style. Can you make sure that .../fixSonarQube branch is up-to-date and includes all commits from its parent branch? If possible, it might be best to create a new branch from the parent branch of .../fixSonarQube and try again from there.

In my pipeline in Azure DevOps, I use a git repo in Azure Devops (“Azure Repos Git”) and I don’t have to any of those suggestions you mentioned above. Perhaps start from a clean test repo and make sure the process works with a basic repo and then apply those same principles to your troublesome repo.

Joe

Hey, Joe.

The .../fix02SonarQube branch is branched from our develop branch (with the changes made to the pipeline yaml to fix the encoding errors). There should be no difference between the codebase in either branch.

The .../test02SqPr is branched from .../fix02SonarQube and rebased every time there is a change to the pipeline.

One thing that may be ggetting in the way… We are editing the AssembleInfo.xml and ServiceManifest.xml files before the build to set the version number. Could this be causing issues with the branching?

BTW: this issue is the same as the support request “SUPPORT-24094 cannot get the Pull Request Analysis to run”

Joe should be able to tell from the support request. But for the rest of you that come across this post:

It turned out the PAT used to register the Pull Request Decoration had expired. When the people in charge of that updated it - it worked like a charm with no need for any extra nonsense, just add the three PR variables and it works fine.

Thanks Joe and everyone else that helped with this. I’m sorry it was such a bizarre issue.

1 Like

Thanks @padillah for explaining your resolution. Glad it can be resolved!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.