Can I automatically add comments to Azure DevOps PR based on code changes - powershell

Occasionally in our codebase we need to use an //eslint-disable to bypass a styleguide rule on a line. I would like to somehow automatically add a comment on each new instance of that in PRs, requiring the developer to explain why they bypassed the styleguide.
I've found this question referencing how to create a comment programmatically, but what I'm not sure how to do is identify the new code and parse it for a certain piece of text, then add comments on those particular lines where the text was found.

This is one of the approaches to ingest scripts & achieve what you want, wherein Expected outcome is:
On every pull request, a pre build validation pipeline kicks off & adds comments on the PR.
Create a script (powershell/python/bash) with following logic:
Find file names in the given branch which contains //eslint-disable
In the files above (1.), get the location/line number of //eslint-disable
Foreach file.LineNumber (wrote like that just for representation): add comment on file.LineNumber using Pull Request Threads API. See line parameter
Create a pipeline containing above script & add that pipeline as build validation or if you have an existing build validation process, add these scripts as tasks in that pipeline.
Hope this helps :)

Related

How to Tag a branch Automatically in Azure DevOps using extension (Tag/Branch Git On Release)

With Azure DevOps release pipeline I'm planning to tag my brach Automatically by using the below extension which was created by Micheal Barry Tag\Branch Git on Release. See the below image:
I'm a bit wondering how to customize Tag name as UAT_$(date:yyyyMMdd)$(Rev:.r). Since this has limited documentation, this is how I try to add(See below)
How can I achieve this? Also, I'm more interested in how to fill these advanced options for this extension.
The $(date:yyyyMMdd)$(Rev:.r) is only supported in BuildNumber (Options=>Build Number Format) and ReleaseNumber (Options=>Release Name format). So if you put $(date:yyyyMMdd)$(Rev:.r) directly in Static Tag Name, the task can't evaluate its value.
Here're several directions to do what you want:
Use $(date:yyyyMMdd)$(Rev:.r) as release name format.
Then use UAT_$(Release.ReleaseName) in Static Tag Name input.
The result:
PS: If you set build pipeline as release pipeline's artifact source, you can also use $(Build.BuildNumber)/$(Build.DefinitionName) in your Release name format.
2.If you prefer to use Release-$(date:yyyyMMdd)$(Rev:.r) as release name format. Now since what you want is UAT_xxx, you need to use the Regex option:
Assuming your release name's instance is Release-20200518.5, now the tag would be UAT_20200518.5 if you configure the task following my inputs above.
In addition:
When release name format is $(date:yyyyMMdd)$(Rev:.r), you releases would be:
You can choose to use the Static Tag Name, check #1 above.
And when the name format is Release-$(date:yyyyMMdd)$(Rev:.r), you releases would be:
You should use regex option in that third-party task, check #2 above. About what is Regex see here, also there's many documents/blogs online about Regex topic...
You are using the wrong task in your pipeline. Would suggest using the git tag task, it works just fine to me and you can use your naming in the tag field
Git Tag Task
I find this extension much easier to setup and its satisfying our needs. So basically my git tag=assembly version. I am doing this every time we have a release on production environment(change assemblyInfo information and store that in Variable in the build definition). There are set of tasks on the marketplace to allow read from asemblyInfo and write to it. For the git tag task i just use the previously set Tag variable which basically is incremented by one every new release. You can check more in the pictures bellow
So i am actually just adding simple tag to mirror my assemblyVersion but in the tag message i am also adding my build informations that looks like this $(build.buildNumber)-$(Tag)
If you want to have a deeper look into azure devops predefined variables you can do that here Use predefined variables

My build pipeline not automatically triggered when I use folder/**/* as paths under trigger

My question is about Azure DevOps build pipeline. I wanted my build to automatically triggered when commit changes. So I include the files using Folder/**/* which any files under the folder including under subfolder. But when I commit changes in one of the file, my build does not triggered. But it worked when I use Folder/* instead. Can anyone explain what is the differences?
I think this could be by design. For the path filter, it could only recognize /* and not /**/*.
For paths:
You may include * as the final character, but it doesn't do anything
differently from specifying the directory name by itself.
You may not include * in the middle of a path filter, and you may not
use ?.
This is stated in the Wildcards of this document.
This is now possible as it is written here Support for wild cards in path filters
Wild cards can be used when specifying inclusion and exclusion branches for CI or PR triggers in a pipeline YAML file. However, they cannot be used when specifying path filters. For instance, you cannot include all paths that match src/app//myapp*. This has been pointed out as an inconvenience by several customers. This update fills this gap. Now, you can use wild card characters (, *, or ?) when specifying path filters.
The documentation is still not updated but you can use Folder/**/* in path filter.

Azure Devops delayed Continuous Integration build

I currently have an Azure Devops install that I am configuring for automated build and testing. I would like to enable a Continuous Integration trigger for the build process, however our check-in standards require different parts of our code to be checked in separate from each other.
For example: we are using nettiers auto generated code, so whenever a ticket requires a database change, the nettiers code base gets updated. Because that is auto generated code it gets checked in separately from manual modifications with a comment indicating that it is an auto generated check-in.
A build will fail if it does not have both the nettiers and the manual modifications checked in. However with Continuous Integration turned on, the first check-in will trigger a build to begin that will be missing the second half of the changes which are checked in a couple minutes later.
The ideal way I would like to fix this would be to implement a 5 minute delay between when the CI build first gets triggered, and when it actually begins its work. Even better would be if each successive check-in would cancel the first build and start a new timer with its own build to account for any subsequent check-ins.
An alternative was to solve the issue might be to have a gate on a work item query. However, I have been unsuccessful in figuring out how to implement either of these ideas, or in coming up with other options. Gates based on queries only seem to be available in Release pipelines, not Builds.
Has anyone out there solved a similar problem, or have thoughts on how to solve or work around this issue?
Azure Devops delayed Continuous Integration build
I am afraid there is no such out of box setting/method to set this specify continuous integration build for your case.
As workaround, we could generated code and gets checked in to some specify folder by using nettiers, like \NettiersGenerated.
Then we could exclude that folder by the Path filters under the Enable continuous integration:
In this case, the generated code will not trigger the build pipeline.
Update:
It would require that the nettiers code always gets checked in first
(which would be hard to enforce)
Yes, agree with you. If the build will fail if it does not have both the nettiers and the manual modifications checked in, my first is indeed not reasonable enough.
As another workaround, we could use the Azure DevOps counter and get the rest of the counter through a powershell script, build the pipeline only if the number is even, otherwise cancel the build, like:
Counter expression like
variables:
internalBuildNumber: 1
semanticBuildNumber: $[counter(variables['internalBuildNumber'], 0)]
Powershell scripts:
$value=$(semanticBuildNumber)
switch($value)
{
{($_ % 2) -ne 0} {"Go on build pipeline"}
{($_ % 2) -eq 0}
{
Write-Host "##vso[task.setvariable variable=agent.jobstatus;]canceled"
Write-Host "##vso[task.complete result=Canceled;]DONE"
}
}
In this case, the pipeline will be build when it triggered at second time.
Hope this helps.

How not to have 'version=GBmaster' in Azure DevOps links

Azure DevOps urls to files and to wiki entries contain &version=GBmaster and &wikiVersion=GBwikiMaster respectively.
Is it possible to configure DevOps not to append it?
Today I get:
https://myorg.visualstudio.com/MyProject/_git/MyRepo?path=%2Fsrc%2FMyFile.cs&version=GBmaster
https://dev.azure.com/rbtech/Redback/_wiki/wikis/MyOrg.wiki?pagePath=%2MyPage&pageId=204&wikiVersion=GBwikiMaster
I wish for:
https://myorg.visualstudio.com/MyProject/_git/MyRepo?path=%2Fsrc%2FMyFile.cs
https://dev.azure.com/rbtech/Redback/_wiki/wikis/MyOrg.wiki?pagePath=%2MyPage&pageId=204
This is the default behavior of AzureDevop basically &version/ &wikiversion is denoted which branch the code/wiki is showing in the UI. Even if you don't provide the Version AzureDevOps will automatically append it to indicate which branch is chosen.
Let's say in your repo you have multiple branches means like master/ develop then based on the branch you choose the &version will change automatically like &version=GBmaster or &version=GBdevelop if you don't provide the default branch will be appended in the query.
Is it possible to configure DevOps not to append it?
No, there isn't a way to configure it. It's the expected behavior as Jayendran described, it's a tag which marks the file version from which branch.
If you want to see the contents of the files without the &version=GBmaster, then you can try call the REST API : Items - Get
For example:
https://{organization}.visualstudio.com/{Project}/_apis/git/repositories/{Repository ID}/items?path=WAP/WAP.Tests/Properties/AssemblyInfo.cs

Build pipeline - Get title of check-in that triggered pipeline

I'm creating a new build pipeline, it will be triggered on a new check-in on a specific branch on a TFVC repository. I want to get the title of the check-in every time the pipeline triggers and store it in the variables.
Additionally, after getting the title I want to perform some checks and depending on the result ( e.g. title matches a specific template) either stop the build pipeline or move on to the next steps
I've looked in Variables and Triggers tabs, but I didn't find anything useful. I've also looked in the predefined variables, but I didn't find anything related to my issue either.
You can try this variable Build.SourceVersion to get the number and Build.SourceVersionMessage to get the comments.
Build.SourceVersionMessage The comment of the commit or changeset. Note: This variable is available from TFS 2015.4.
See also: https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml