Get GitHub pull request number in VSTS build - github

Is it somehow possible to get the pull request number in a Visual Studio Team Service (vNext) build which is linked to a GitHub repository for builds run for pull requests?
I would like to do some code anylsis using sonar and write the finding back as comment to the pull request using Sonar GitHub Plugin.

I don't know any direct way to do this. The way I can think is add a PowerShell step to call "git log" command and read the information from the log. Since the commit information for pull request usually has a format like "Merge pull request #6 from XXX". We can use RegEx to get the pull request number.
git log -1 >log.txt
$file = Get-Content log.txt
$reg = "Merge.pull.request.+(?<pullnumber>\w+?).from+"
foreach($line in $file){
if($line -match $reg){
$Matches.pullnumber;
}
}

Related

Pull Request "reviewers" using github "history"

Is there any way (for on premise github) to :
For N number of files in the Pull Request.
Look at the history of those files.
And add any/all github users (on the history) .. to the code reviewers list of users?
I have searched around.
I found "in general" items like this:
https://www.freecodecamp.org/news/how-to-automate-code-reviews-on-github-41be46250712/
But cannot find anything in regards to the specific "workflow" I describe above.
We can get the list of changed files to the text file from PR. Then we can run the git command below to get the list of users included in last version's blame. For each file we get from file list, run the blame command. This might be also simple script.
Generate txt file from list of files of PR.
Traverse all filenames through txt file. (python, bash etc.)
Run blame command and store in a list.
Add reviewers to the PR from that list manually or some JS script for it.
For github spesific: list-pull-requests-files
The blame command is something like :
git blame filename --porcelain | grep "^author " | sort -u
As a note, if there are users who are not available in github anymore. Extra step can be added after we get usernames to check whether they exist or not. (It looks achievable through github API)

How do I get the last commit programmatically in Java code? Jenkins / sbt

I started writing a little tool that basically can do something (ex. compile or test code) and then send an email if it fails.
https://github.com/JohnReedLOL/EmailTestingBot
I want to add a feature where this tool can programmatically look at the last commit in the working directory, look at the author of the commit, extract their email, and then email that person whether their commit resulted in a pass or a failure.
For example, I want it to do something like: Git: See my last commit
Where the email basically says:
Subject: Test Results
Message: All your tests passed in dev for commit 0e39756383662573.
Does Jenkins provide this functionality already? I want to make my setup email the person who put in the most recent commit.
Also, is there a way I can obtain the email of the author of the most recent commit programmatically (ex. perhaps with http://www.eclipse.org/jgit/ or http://javagit.sourceforge.net )?
I don't really care how I get email notifications - I just want them and I can't use TravisCI.
I will try to give solutions part by part.
Part 1 :
Yes, you can run ShellScript(Shell Commands) from Jenkins Link.
Part 2
How to get the Email Id and other Stuff from GitCommit.
For that Jenkins sever should have git command installed in build server.
Create one conf file ex. /conf/reference which have
app {
git {
commit = "CURRENT_COMMIT"
repo = "CURRENT_REPO"
timestamp = "CURRENT_TIMESTAMP"
emailId = "EMAIL_ID"
}
}
When making your build run the command
sed -i'' "s/CURRENT_COMMIT/$(git rev-parse HEAD)/g" conf/reference.conf
sed -i'' "s^CURRENT_REPO^$(git config --get remote.origin.url)^g" conf/reference.conf
sed -i'' "s/CURRENT_TIMESTAMP/$(git show -s --format=%ci HEAD)/g" conf/reference.conf
sed -i'' "s/EMAIL_ID/git --no-pager show -s --format='%an <%ae>' CURRENT_COMMIT/g" conf/reference.conf
above code will put the values in reference.conf.
Now you can use to get the info and send the mail. As far as I know, Jenkins gives the capability to send the Email. Jenkins work on the environment variables rather than putting this into reference.conf you can put this in Environment variable and use the environment variables to send the mail.
FYI: I haven't tested this code but as far as I remember working in Jenkins, we used to send email through this way.
#HappyCoding

Load file from GitHub in PowerShell

I have written a PowerShell script which loads a json file and performs certain function on it. I am using:
$json = Get-Content 'C:\Users\Documents\test.json' | Out-String | ConvertFrom-Json
to load the file which works. But I want to store both these files in a git hub repository. How can I access the json file path once I store both the files in the same directory in a GitHub repository?
I am new to using GitHub so any help would be appreciated.
If you want to work with a remote path, you can use the webclient to download the file as a string and convert it using the ConvertFrom-Json cmdlet:
$jsonPath = 'https://raw.githubusercontent.com/jaypat/documents/master/test.json'
$json = (New-Object System.Net.WebClient).DownloadString($jsonPath) | ConvertFrom-Json
You might need to either use the Github API or commit and push using git. For the latter you should do something like the following from the local git repository:
git add test.json
git commit -m "Message for what changes you've made"
git pull
git push origin master
It'd be really helpful if you first read and understood what git is and how it works. Then you'd be ablt to use github seamlessly. Here is a great interactive tutorial for it: https://try.github.io/levels/1/challenges/1
I ended up using path binding to reference the file
$path = join-path $psscriptroot "env.json"
This sources to the directory where all the files are loaded.

What are my PowerShell options for determining a path that changes with every build (TeamCity)?

I'm on a project that uses TeamCity for builds.
I have a VM, and have written a PowerShell script that backs up a few files, opens a ZIP artifact that I manually download from TeamCity, and then copies it to my VM.
I'd like to enhance my script by having it retrieve the ZIP artifact (which always has the same name).
The problem is that the download path contains the build number which is always changing. Aside from requesting the download path for the ZIP artifact, I don't really care what it is.
An example artifact path might be:
http://{server}/repository/download/{project}/{build_number}:id/{project}.zip
There is a "Last Successful Build" page in TeamCity that I might be able to obtain the build number from.
What do you think the best way to approach this issue is?
I'm new to TeamCity, but it could also be that the answer is "TeamCity does this - you don't need a PowerShell script." So direction in that regard would be helpful.
At the moment, my PowerShell script does the trick and only takes about 30 seconds to run (which is much faster than my peers that do all of the file copying manually). I'd be happy with just automating the ZIP download so I can "fire and forget" my script and end up with an updated VM.
Seems like the smallest knowledge gap to fill and retrieving changing path info at run-time with PowerShell seems like a pretty decent skill to have.
I might just use C# within PS to collect this info, but I was hoping for a more PS way to do it.
Thanks in advance for your thoughts and advice!
Update: It turns out some other teams had been using Octopus Deploy (https://octopus.com/) for this sort of thing so I'm using that for now - though it actually seems more cumbersome than the PS solution overall since it involves logging into the Octopus server and going through a few steps to kick off a new build manually at this point.
I'm also waiting for the TC administrator to provide a Webhook or something to notify Octopus when a new build is available. Once I have that, the Octopus admin says we should be able to get the deployments to happen automagically.
On the bright side, I do have the build process integrated with Microsoft Teams via a webhook plugin that was available for Octopus. Also, the Developer of Octopus is looking at making a Microsoft Teams connector to simplify this. It's nice to get a notification that the new build is available right in my team chat.
You can try to get your artefact from this url:
http://<ServerUrl>/repository/downloadAll/<BuildId>/.lastSuccessful
Where BuildId is the unique identifier of the build configuration.
My implementation of this question is, in powershell:
#
# GetArtefact.ps1
#
Param(
[Parameter(Mandatory=$false)][string]$TeamcityServer="",
[Parameter(Mandatory=$false)][string]$BuildConfigurationId="",
[Parameter(Mandatory=$false)][string]$LocalPathToSave=""
)
Begin
{
$username = "guest";
$password = "guest";
function Execute-HTTPGetCommand() {
param(
[string] $target = $null
)
$request = [System.Net.WebRequest]::Create($target)
$request.PreAuthenticate = $true
$request.Method = "GET"
$request.Headers.Add("AUTHORIZATION", "Basic");
$request.Accept = "*"
$request.Credentials = New-Object System.Net.NetworkCredential($username, $password)
$response = $request.GetResponse()
$sr = [Io.StreamReader]($response.GetResponseStream())
$file = $sr.ReadToEnd()
return $file;
}
Execute-HTTPGetCommand http://$TeamcityServer/repository/downloadAll/$BuildConfigurationId/.lastSuccessful | Out-File $LocalPathToSave
}
And call this with the appropriate parameters.
EDIT: Note that the current credential I used here was the guest account. You should check if the guest account has the permissions to do this, or specify the appropriate account.
Try constructing the URL to download build artifact using TeamCity REST API.
You can get a permanent link using a wide range of criteria like last successful build or last tagged with a specific tag, etc.
e.g. to get last successful you can use something like:
http://{server}/app/rest/builds/buildType:(id:{build.conf.id}),status:SUCCESS/artifacts/content/{file.name}
TeamCity has the capability to publish its artifacts to a built in NuGet feed. You can then use NuGet to install the created package, not caring about where the artifacts are. Once you do that, you can install with nuget.exe by pointing your source to the NuGet feed URL. Read about how to configure the feed at https://confluence.jetbrains.com/display/TCD10/NuGet.
Read the file content of the path in TEAMCITY_BUILD_PROPERTIES_FILE environment variable.
Locate the teamcity.configuration.properties.file row in the file, iirc the value is backslash encoded.
Read THAT file, and locate the teamcity.serverUrl value, decode it.
Construct the url like this:
{serverurl}/httpAuth/repository/download/{buildtypeid}/.lastSuccessful/file.txt
Here's an example (C#):
https://github.com/WideOrbit/buildtools/blob/master/RunTests.csx#L272

Accessing Teamcity Artifact with Dynamic name

I want to download a TeamCity artifact via powershell. It needs to be the last successful build of a specific branch.
I've noticed two common url paths to access the artifacts. One seems to be
/repository/download/BUILD_TYPE_EXT_ID/.lastSuccessful/ARTIFACT_PATH
The problem is that the file at the end relies on the release version. Within TeamCity there is syntax to specify all files \*.msi. Is there any way to specify an artifact starting with FileName-{version.number}.msi when trying to access this url?
EDIT:
The other url I noticed is for the REST API.
http://teamcity/guestAuth/app/rest/builds/branch:[BRANCH],buildType:[BUILD TYPE],status:SUCCESS,state:finished/artifacts/[BUILD PATH]
The problem is that I can't download artifacts from here. If I want to download the artifacts I have to use the current build id. The above url gives the following url: /guestAuth/app/rest/builds/id:[Build ID]/artifacts/content/[Artifact Path] to download the artifact.
I can use the first REST url to eventually get the second through the returned xml, but would prefer a more straightforward approach.
Unfortunately as TeamCity artifacts are not browsable the usual workarounds like wget recursive download or wildcards are not applicable.
Using wildcards in wget or curl query
How do I use Wget to download all Images into a single Folder
One workaround you could try is formatting the link in the job, saving the link to a text file and storing that as an artifact as well, with a static name. Then you just need to download that text file to get the link.
I found you can format the artifact URL in TeamCity job by doing:
%teamcity.serverUrl%/repository/download/%system.teamcity.buildType.id%/%teamcity.build.id%:id/<path_to_artifact>
In a command line step. You can write this to a file by doing:
echo %teamcity.serverUrl%/repository/download/%system.teamcity.buildType.id%/%teamcity.build.id%:id/myMsi-1.2.3.4.msi > msiLink.txt"
Now you have an artifact with a constant name, that points to the installer (or other artifact) with the changing name.
If you use the artifact msiLink.txt you don't need to use the REST interface (it's still two calls, both through the same interface).
You can easy download the latest version from batch/cmd by using:
wget <url_server>/repository/download/BUILD_TYPE_EXT_ID/.lastSuccessful/msiLink.txt ---user #### --passsword ####
set /P msi_url=<msiLink.txt
wget %msi_url% --user #### --passsword ####
Hope it helps.
Update:
Sorry I just realized the question asked for PowerShell:
$WebClient = New-Object System.Net.WebClient
$WebClient.Credentials = New-Object System.Net.Networkcredential("yourUser", "yourPassword")
$WebClient.DownloadFile( "<url_server>/repository/download/BUILD_TYPE_EXT_ID/.lastSuccessful/msiLink.txt", "msiLink.txt" )
$msi_link = [IO.File]::ReadAllText(".\msiLink.txt")
$WebClient.DownloadFile( $msi_link, "yourPath.msi" )