Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Sdk/AzurePipelines/descriptions.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@
},
"trim": {
"description": "* Returns the parameter without leading and trailing white spaces\n* Min parameters: 1. Max parameters: 1\n* Example: `trim(' variable ') ` returns 'variable'"
},
"substring": {
"description": "* Returns a substring from the input string, starting at the specified index and having the specified length.\n* Min parameters: 3. Max parameters: 3\n* The first parameter is the input string. The second parameter is the zero-based starting index of the substring. The third parameter is the length of the substring.\n* Example: `substring('Hello world', 0, 5)` returns 'Hello'"
}
}
}
2 changes: 2 additions & 0 deletions src/Sdk/DTExpressions2/Expressions2/ExpressionConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ static ExpressionConstants()
// https://learn.microsoft.com/en-us/azure/devops/release-notes/2025/pipelines/sprint-248-update#new-pipeline-expression-functions
AddAzureFunction<GitHub.DistributedTask.Expressions2.Sdk.Functions.v1.Iif>("iif", 3, 3);
AddAzureFunction<GitHub.DistributedTask.Expressions2.Sdk.Functions.v1.Trim>("trim", 1, 1);
// Undocumented:
AddAzureFunction<GitHub.DistributedTask.Expressions2.Sdk.Functions.v1.SubString>("substring", 3, 3);
}

private static void AddFunction<T>(String name, Int32 minParameters, Int32 maxParameters)
Expand Down
18 changes: 18 additions & 0 deletions src/Sdk/DTExpressions2/Expressions2/Sdk/Functions/v1/SubString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace GitHub.DistributedTask.Expressions2.Sdk.Functions.v1
{
internal sealed class SubString : Function
{
protected sealed override Boolean TraceFullyRealized => false;

protected sealed override Object EvaluateCore(EvaluationContext context, out ResultMemory memory)
{
memory = null;
String left = Parameters[0].EvaluateString(context) as String ?? String.Empty;
Int32 startIndex = (Int32)Parameters[1].Evaluate(context).ConvertToNumber();
Int32 length = (Int32)Parameters[2].Evaluate(context).ConvertToNumber();
return left.Substring(startIndex, length);
}
}
}
11 changes: 11 additions & 0 deletions testworkflows/azpipelines/substring-function-1/pipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
trigger: none
pr: none

pool:
vmImage: 'ubuntu-latest'

steps:
- ${{ if ne(substring('Hello small World', 6, 5), 'small') }}:
- fail: true
- ${{ else }}:
- bash: echo "substring function works as expected"
46 changes: 46 additions & 0 deletions testworkflows/azpipelines/substring-function/pipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
trigger: none
pr: none

variables:
Tag: $[ substring(variables['Build.SourceVersion'], 0, 7) ]
ShortHash: $[ substring(variables['Build.SourceVersion'], 0, 7) ]
StaticSubstring: $[ substring('abcdef1234567890', 0, 7) ]

pool:
vmImage: 'ubuntu-latest'

steps:
- checkout: none

- bash: |
set -euo pipefail

echo "Build.SourceVersion: ${SOURCE_VERSION}"
echo "Tag: ${TAG_VALUE}"
echo "ShortHash: ${SHORT_HASH_VALUE}"
echo "StaticSubstring: ${STATIC_SUBSTRING_VALUE}"

expected_short_hash="${SOURCE_VERSION:0:7}"

if [ "${TAG_VALUE}" != "${expected_short_hash}" ]; then
echo "##vso[task.logissue type=error]Tag did not match expected short hash '${expected_short_hash}'"
exit 1
fi

if [ "${SHORT_HASH_VALUE}" != "${expected_short_hash}" ]; then
echo "##vso[task.logissue type=error]ShortHash did not match expected short hash '${expected_short_hash}'"
exit 1
fi

if [ "${STATIC_SUBSTRING_VALUE}" != "abcdef1" ]; then
echo "##vso[task.logissue type=error]StaticSubstring did not match expected value 'abcdef1'"
exit 1
fi

echo "substring runtime expressions evaluated successfully."
displayName: 'Test: substring runtime expression'
env:
SOURCE_VERSION: $(Build.SourceVersion)
TAG_VALUE: $(Tag)
SHORT_HASH_VALUE: $(ShortHash)
STATIC_SUBSTRING_VALUE: $(StaticSubstring)
Loading