microsoft / PR-Metrics

A GitHub Action & Azure Pipelines task for augmenting pull request titles to let reviewers quickly determine PR size and test coverage.
https://github.com/microsoft/Omex
MIT License
58 stars 12 forks source link

Can't get PR Metrics to work on Azure Devops Server (not cloud) #404

Closed actionthomas closed 1 year ago

actionthomas commented 1 year ago

Summary

PR Metrics task fails with message Could not access sufficient Git history. Set 'fetchDepth: 0' as a parameter to the 'checkout' task (YAML) or disable 'Shallow fetch' under the build process phase settings (classic). even though fetchDepth is set.

Reproduction Steps

Here is an extract of the yaml present in the pipelines :

steps:
    - checkout: self
      lfs: true
      fetchDepth: 0

    - task: PRMetrics@1
      displayName: 'PR Metrics'
      continueOnError: true
      env:
        PR_METRICS_ACCESS_TOKEN: $(System.AccessToken)

Troubleshooting Undertaken

I injected a script to execute the command provided in the troubleshooting document, and one mimicking what the extension does.

variables:
- name: realBranchName
  value: $[replace(variables['Build.SourceBranch'], 'refs/', '')]
- name: realTargetName
  value: $[replace(variables['System.PullRequest.TargetBranch'], 'refs/heads/', 'origin/')]
# other stuff...
    - task: CmdLine@2
      inputs:
        script: |
          git diff --numstat --ignore-all-space $(realTargetName)...$(realBranchName)

          git rev-parse --branch $(realTargetName)...$(realBranchName)
Script output ```txt warning: refname 'pull/42422/merge' is ambiguous. 6 1 Sources/azure-pipeline.yml 1 1 Sources/.cs 51 0 Sources/.sql 5 1 Sources//PostDeploymentScript/Script.PostDeployment.sql 2 0 Sources/.sqlproj warning: refname 'pull/42422/merge' is ambiguous. --branch b4c2a92a44a2d260de67bc0e06cbb338850986f7 27d8c29d9e661967e15e2a8f842d29932a10fdd8 ^27d8c29d9e661967e15e2a8f842d29932a10fdd8 ```
PR Metrics task output ```txt 2023-07-07T08:46:56.4357848Z ##[debug]Evaluating condition for step: 'PR Metrics' 2023-07-07T08:46:56.4359744Z ##[debug]Evaluating: SucceededNode() 2023-07-07T08:46:56.4361506Z ##[debug]Evaluating SucceededNode: 2023-07-07T08:46:56.4362936Z ##[debug]=> True 2023-07-07T08:46:56.4363455Z ##[debug]Result: True 2023-07-07T08:46:56.4363919Z ##[section]Démarrage : PR Metrics 2023-07-07T08:46:56.4556637Z ============================================================================== 2023-07-07T08:46:56.4556882Z Task : PR Metrics v1.5.4 2023-07-07T08:46:56.4557010Z Description : Augments pull request titles to let reviewers quickly determine PR size and test coverage. 2023-07-07T08:46:56.4557574Z Version : 1.5.4 2023-07-07T08:46:56.4557702Z Author : Microsoft Corporation 2023-07-07T08:46:56.4557854Z Help : https://aka.ms/PRMetrics/README 2023-07-07T08:46:56.4557991Z ============================================================================== 2023-07-07T08:46:56.5161684Z ##[debug]Using node path: E:\agent\1\externals\node10\bin\node.exe 2023-07-07T08:46:56.7994100Z ##[debug]agent.TempDirectory=E:\_w\1\_temp 2023-07-07T08:46:56.8028164Z ##[debug]loading inputs and endpoints 2023-07-07T08:46:56.8029042Z ##[debug]loading ENDPOINT_AUTH_PARAMETER_SYSTEMVSSCONNECTION_ACCESSTOKEN 2023-07-07T08:46:56.8045793Z ##[debug]loading ENDPOINT_AUTH_SCHEME_SYSTEMVSSCONNECTION 2023-07-07T08:46:56.8056461Z ##[debug]loading ENDPOINT_AUTH_SYSTEMVSSCONNECTION 2023-07-07T08:46:56.8066220Z ##[debug]loading SECRET_AGENT_PROXYPASSWORD 2023-07-07T08:46:56.8069697Z ##[debug]loading SECRET_SYSTEM_ACCESSTOKEN 2023-07-07T08:46:56.8083988Z ##[debug]loaded 5 2023-07-07T08:46:56.8092562Z ##[debug]Agent.ProxyUrl= 2023-07-07T08:46:56.8093216Z ##[debug]Agent.ProxyUsername= 2023-07-07T08:46:56.8099977Z ##[debug]Agent.ProxyPassword=*** 2023-07-07T08:46:56.8100848Z ##[debug]Agent.ProxyBypassList=[] 2023-07-07T08:46:56.8103754Z ##[debug]Agent.TempDirectory=E:\_w\1\_temp 2023-07-07T08:46:56.8128618Z ##[debug]expose agent proxy configuration. 2023-07-07T08:46:56.8130317Z ##[debug]Agent.CAInfo=undefined 2023-07-07T08:46:56.8130849Z ##[debug]Agent.ClientCert=undefined 2023-07-07T08:46:56.8131384Z ##[debug]Agent.SkipCertValidation=undefined 2023-07-07T08:46:56.9596564Z ##[debug]check path : E:\_w\1\_tasks\PRMetrics_907d3b28-6b37-4ac7-ac75-9631ee53e512\1.5.4\task.json 2023-07-07T08:46:56.9601862Z ##[debug]adding resource file: E:\_w\1\_tasks\PRMetrics_907d3b28-6b37-4ac7-ac75-9631ee53e512\1.5.4\task.json 2023-07-07T08:46:56.9602570Z ##[debug]system.culture=fr-FR 2023-07-07T08:46:56.9630078Z ##[debug]* CodeMetricsCalculator.shouldSkip 2023-07-07T08:46:56.9630779Z ##[debug]* PullRequest.isPullRequest 2023-07-07T08:46:56.9631355Z ##[debug]* PullRequest.isSupportedProvider 2023-07-07T08:46:56.9639660Z ##[debug]* CodeMetricsCalculator.shouldStop() 2023-07-07T08:46:56.9641759Z ##[debug]* ReposInvoker.isAccessTokenAvailable 2023-07-07T08:46:56.9642885Z ##[debug]* ReposInvoker.getReposInvoker() 2023-07-07T08:46:56.9643623Z ##[debug]* AzureReposInvoker.isAccessTokenAvailable 2023-07-07T08:46:56.9645649Z ##[debug]* GitInvoker.isGitRepo() 2023-07-07T08:46:56.9646520Z ##[debug]* GitInvoker.invokeGit() 2023-07-07T08:46:56.9656277Z ##[debug]which 'git' 2023-07-07T08:46:56.9695592Z ##[debug]found: 'E:\agent\1\externals\git\cmd\git.exe' 2023-07-07T08:46:56.9697545Z ##[debug]E:\agent\1\externals\git\cmd\git.exe arg: ["rev-parse","--is-inside-work-tree"] 2023-07-07T08:46:56.9698350Z ##[debug]E:\agent\1\externals\git\cmd\git.exe arg: ["rev-parse","--is-inside-work-tree"] 2023-07-07T08:46:56.9700325Z ##[debug]exec tool: E:\agent\1\externals\git\cmd\git.exe 2023-07-07T08:46:56.9701873Z ##[debug]exec tool: E:\agent\1\externals\git\cmd\git.exe 2023-07-07T08:46:56.9702380Z ##[debug]arguments: 2023-07-07T08:46:56.9702843Z ##[debug]arguments: 2023-07-07T08:46:56.9703292Z ##[debug] rev-parse 2023-07-07T08:46:56.9704679Z ##[debug] rev-parse 2023-07-07T08:46:56.9706083Z ##[debug] --is-inside-work-tree 2023-07-07T08:46:56.9706623Z ##[debug] --is-inside-work-tree 2023-07-07T08:46:56.9707581Z ##[debug][command]E:\agent\1\externals\git\cmd\git.exe rev-parse --is-inside-work-tree 2023-07-07T08:46:56.9850361Z (node:14028) Warning: Use Cipheriv for counter mode of aes-256-ctr 2023-07-07T08:46:56.9851887Z (node:14028) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead. 2023-07-07T08:46:57.0252184Z ##[debug]true 2023-07-07T08:46:57.0331105Z ##[debug]Exit code 0 received from tool 'E:\agent\1\externals\git\cmd\git.exe' 2023-07-07T08:46:57.0332087Z ##[debug]Exit code 0 received from tool 'E:\agent\1\externals\git\cmd\git.exe' 2023-07-07T08:46:57.0332746Z ##[debug]STDIO streams have closed for tool 'E:\agent\1\externals\git\cmd\git.exe' 2023-07-07T08:46:57.0333408Z ##[debug]STDIO streams have closed for tool 'E:\agent\1\externals\git\cmd\git.exe' 2023-07-07T08:46:57.0334000Z ##[debug]* GitInvoker.isPullRequestIdAvailable() 2023-07-07T08:46:57.0334532Z ##[debug]* GitInvoker.pullRequestIdInternal 2023-07-07T08:46:57.0335083Z ##[debug]* GitInvoker.pullRequestIdForAzurePipelines 2023-07-07T08:46:57.0335620Z ##[debug]* GitInvoker.isGitHistoryAvailable() 2023-07-07T08:46:57.0336130Z ##[debug]* GitInvoker.initialize() 2023-07-07T08:46:57.0336622Z ##[debug]* GitInvoker.targetBranch 2023-07-07T08:46:57.0337139Z ##[debug]* GitInvoker.pullRequestIdInternal 2023-07-07T08:46:57.0337643Z ##[debug]* GitInvoker.invokeGit() 2023-07-07T08:46:57.0338108Z ##[debug]which 'git' 2023-07-07T08:46:57.0353850Z ##[debug]found: 'E:\agent\1\externals\git\cmd\git.exe' 2023-07-07T08:46:57.0354706Z ##[debug]E:\agent\1\externals\git\cmd\git.exe arg: ["rev-parse","--branch","origin/develop...pull/42422/merge"] 2023-07-07T08:46:57.0355556Z ##[debug]E:\agent\1\externals\git\cmd\git.exe arg: ["rev-parse","--branch","origin/develop...pull/42422/merge"] 2023-07-07T08:46:57.0356325Z ##[debug]exec tool: E:\agent\1\externals\git\cmd\git.exe 2023-07-07T08:46:57.0356960Z ##[debug]exec tool: E:\agent\1\externals\git\cmd\git.exe 2023-07-07T08:46:57.0357538Z ##[debug]arguments: 2023-07-07T08:46:57.0358058Z ##[debug]arguments: 2023-07-07T08:46:57.0358581Z ##[debug] rev-parse 2023-07-07T08:46:57.0359094Z ##[debug] rev-parse 2023-07-07T08:46:57.0359602Z ##[debug] --branch 2023-07-07T08:46:57.0360118Z ##[debug] --branch 2023-07-07T08:46:57.0360657Z ##[debug] origin/develop...pull/42422/merge 2023-07-07T08:46:57.0361181Z ##[debug] origin/develop...pull/42422/merge 2023-07-07T08:46:57.0361876Z ##[debug][command]E:\agent\1\externals\git\cmd\git.exe rev-parse --branch origin/develop...pull/42422/merge 2023-07-07T08:46:57.0929672Z ##[debug]warning: refname 'pull/42422/merge' is ambiguous. 2023-07-07T08:46:57.0974436Z ##[debug]--branch 3d834567b6d60ffde091dd3433f6bd12b3f02223 27d8c29d9e661967e15e2a8f842d29932a10fdd8 ^27d8c29d9e661967e15e2a8f842d29932a10fdd8 2023-07-07T08:46:57.1011666Z ##[debug]Exit code 0 received from tool 'E:\agent\1\externals\git\cmd\git.exe' 2023-07-07T08:46:57.1012608Z ##[debug]Exit code 0 received from tool 'E:\agent\1\externals\git\cmd\git.exe' 2023-07-07T08:46:57.1013266Z ##[debug]STDIO streams have closed for tool 'E:\agent\1\externals\git\cmd\git.exe' 2023-07-07T08:46:57.1013899Z ##[debug]STDIO streams have closed for tool 'E:\agent\1\externals\git\cmd\git.exe' 2023-07-07T08:46:57.1065787Z ##[debug]task result: Failed 2023-07-07T08:46:57.1092384Z ##[error]Could not access sufficient Git history. Set 'fetchDepth: 0' as a parameter to the 'checkout' task (YAML) or disable 'Shallow fetch' under the build process phase settings (classic). 2023-07-07T08:46:57.1100962Z ##[debug]Processed: ##vso[task.issue type=error;]Could not access sufficient Git history. Set 'fetchDepth: 0' as a parameter to the 'checkout' task (YAML) or disable 'Shallow fetch' under the build process phase settings (classic). 2023-07-07T08:46:57.1103974Z ##[debug]Processed: ##vso[task.complete result=Failed;]Could not access sufficient Git history. Set 'fetchDepth: 0' as a parameter to the 'checkout' task (YAML) or disable 'Shallow fetch' under the build process phase settings (classic). 2023-07-07T08:46:57.1163431Z ##[section]Fin : PR Metrics ```

Additional Information

My ADO agents are in version 2.218.1.

Thank you for your help.

muiriswoulfe commented 1 year ago

Thanks @actionthomas for trying out PR Metrics and for your detailed response.

Unfortunately, I don't have access to Azure DevOps Server, so I'm not able to try this out locally. It looks like Azure DevOps Server is using the commit SHA as the branch rather than the standard same branch name format.

Earlier in the run, you should be able to see where Azure DevOps checks out a branch with the contents it's running locally. In my case, I see this at the top the checkout command with a statement like: Starting: Checkout <repo>@refs/pull/2138197/merge to s...

Can you find the line, so that we can see what is the correct branch format should be?

actionthomas commented 1 year ago

Hi @muiriswoulfe and thanks having a look at this issue so quickly :) I have the same line here at checkout : Démarrage : Extraire <repo>@refs/pull/37574/merge pour s.

muiriswoulfe commented 1 year ago

The error message from PR Metrics mentions PR number 42422 while the last statement you have mentions PR number 37574. Are they from different PRs? I'm trying to narrow down whether the difference is relevant.

actionthomas commented 1 year ago

Oh yes, the message is from a different PR, but they have always this form. For PR 42422 it's Démarrage : Extraire <repo>@refs/pull/42422/merge pour s.

muiriswoulfe commented 1 year ago

It's a very strange error in that case, as I don't see why git would be confused about the branch that exists. Can you try this on another repo on the same server (and ideally on another server too if you have one), to see if this is localised? Others had been using PR Metrics on Azure DevOps Server before so I'm confused as to why this would be failing now.

actionthomas commented 1 year ago

I tried with a different repo, this one isusing classic builds. The result is the same. Unfortunately, I don't have another server to test with.

muiriswoulfe commented 1 year ago

Thanks for performing the check.

I've read through your detailed output again, and the first thing I would like to check is whether the issue still occurs when comparing against your main branch, e.g.:

variables:
- name: realBranchName
  value: $[replace(variables['Build.SourceBranch'], 'refs/', '')]
- name: realTargetName
  value: origin/main  # Or whatever the name of the main branch is called for your repo.
# other stuff...
    - task: CmdLine@2
      inputs:
        script: |
          git diff --numstat --ignore-all-space $(realTargetName)...$(realBranchName)         
          git rev-parse --branch $(realTargetName)...$(realBranchName)

The reason I'm asking, by the way, is because most people run PR Metrics against the main branch so I'm wondering if that is why it is failing in your case. That said, it should work against the develop branch as well.

To fix this, I'm not seeing a great alternative other than to:

To validate that this would be an appropriate action to take, could you confirm that the output you provided above (and listed below) is roughly correct, so that we wouldn't be just calculating and displaying incorrect information?

6   1   Sources/azure-pipeline.yml
1   1   Sources/<redacted file 1>.cs
51  0   Sources/<redacted file 2>.sql
5   1   Sources/<redacted path>/PostDeploymentScript/Script.PostDeployment.sql
2   0   Sources/<redacted file 3>.sqlproj

By the way, I'm a little concerned that this plan may result in the suppression of genuine errors, so I'm open to other suggestions. Perhaps you have noticed something I missed given your interaction with the Azure DevOps Server.

actionthomas commented 1 year ago

I performed the steps you asked, running the script against our master branch. I had the same warnings, unfortunately. I can also confirm that the changes reported by the git command are correct.

muiriswoulfe commented 1 year ago

Thanks for checking again. I'll put this on the backlog as an item to fix. I unfortunately can't guarantee when I'll get around to it as there are other items of higher priority. Feel free to contribute a solution if you need to get something in more quickly.

muiriswoulfe commented 1 year ago

@actionthomas It unfortunately took me some time to try to reproduce this locally, but I finally managed to get around to it. However, with a local instance of Azure DevOps Server, I'm not seeing the issue.

My guess is that this isn't specific to Azure DevOps Server, and that it is due to something that is different in your environment. That makes it much more difficult to resolve as my previous proposal of checking whether the run is executing on Azure DevOps Server will no longer resolve the issue, except potentially in your case.

I'd like to implement the fix properly if we need to make some change here, but I'm at a loss as to what that would be. Is there anything else you can think of that may be causing the issues on your side?

DxWang36 commented 4 months ago

Is it necessary to set fetchDepth = 0? my code repo size is very big, like 80g, so can I just pull a little commit ? like fetchDepth = 1 or fetchDepth = 200?