0

I'm using the "Post to Slack" task as one of my build steps in TFS 2018 and I'm wondering how to access variables relating to that commit. I would like to include them as part of the Message field (something like "Commit: $(CommitMessage) link to changeset $(ChangesetLink)" but those variables don't exist). Here is where I need to reference the variables in TFS:

enter image description here

This document: link describes how to access build variables but it doesn't mention anything relating to the commit. I would like to access the commit message, associated commit changesets and the link to the changeset(s) associated with the commit. Does anyone know how to do this or know where I can find documentation for it? Thank you

Ryan
  • 524
  • 5
  • 16
  • TFS / VSTS REST APIs allows to do lot more than basics. have you done some research on REST APIs?. https://learn.microsoft.com/en-us/rest/api/vsts/git/commits/get%20commits?view=vsts-rest-5.0 – Sreejith Warrier May 09 '18 at 19:21
  • @cruiser that looks great, do you know how to access these as variables in the build definition? Like for instance comment, would it be $(GitCommit.comment)? – Ryan May 09 '18 at 19:31
  • I am do not know if there are predefined variables for your needs. But these REST API may get you there. If you are running on a Windows based agent, then you can write powershell scripts ( if not sh based) to invoke REST API, parse the results, then use ##vso[task.setvariable variable=name;]value to set the value to the list of variables. Reference Link from Stackoverflow https://stackoverflow.com/questions/37881017/tfs-ci-build-update-custom-define-variable-after-build-is-succeed – Sreejith Warrier May 09 '18 at 19:51
  • 1
    There is a Slack service hook built in. What about that doesn't meet your needs? https://learn.microsoft.com/en-us/vsts/service-hooks/services/slack?view=vsts – Daniel Mann May 09 '18 at 19:59
  • I'm not sure how to access/call the REST API from the build definition, if possible I'd like to do it from the UI for ease of use. I edited my post to include a picture that will clarify where I need to access these variable names. – Ryan May 09 '18 at 20:24
  • @DanielMann I'm not sure how that would help me with the "Post to Slack" task, could you please elaborate? – Ryan May 09 '18 at 20:26
  • @Ryan It wouldn't, it would completely remove the need for it. The Slack service hook allows messages to be posted to a Slack channel when certain events (such as, say, builds starting or completing) occur. – Daniel Mann May 09 '18 at 21:27

1 Answers1

1

Cruiser is right, no such Predefined variables in TFS, you can retrieve the needed information by REST API, then set corresponding variables using the Logging Commands.

  1. Create a PowerShell script to set the avariables (Reference below sample, you can also Use the OAuth token to access the REST API), then commit and push the script into TFS.
  2. Add a PowerShell task before the "Post to Slack" task in your definition to run the PS script
  3. Use the variables $(commitID), $(CommitMessage) and $(commitUrl) in "Post to Slack" task

Note: For Git it's commit, For TFVC it's changeset

You can use below script to set the variables:

Param(
   [string]$collectionurl = "http://server:8080/tfs/DefaultCollection", 
   [string]$repoid = "389e8215-1fb2-4fdc-bd04-ebc8a8a4410e",
   [string]$user = "username",
   [string]$token = "password"
)

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
$searchCriteria = "$" + "top=1"

$baseUrl = "$collectionurl/_apis/git/repositories/$repoid/commits?$searchCriteria"          
$response = (Invoke-RestMethod -Uri $baseUrl -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})

#Retrieve values 
$commitID = $response.value.commitID
$CommitMessage = $response.value.comment
$commitUrl = $response.value.remoteUrl

#Set variables
Write-Host "##vso[task.setvariable variable=commitID]$commitID"
Write-Host "##vso[task.setvariable variable=CommitMessage]$CommitMessage"
Write-Host "##vso[task.setvariable variable=commitUrl]$commitUrl"

UPDATE:

You can use this REST API to get the repository ID:

GET http://server:8080/tfs/DefaultCollection/{ProjectName}/_apis/git/repositories

enter image description here

Andy Li-MSFT
  • 28,712
  • 2
  • 33
  • 55