Skip to content

Commit

Permalink
Add task SDK token support (#1021)
Browse files Browse the repository at this point in the history
Added task SDK token support; added a default value for the issue source property.
  • Loading branch information
DenisNikulin5 authored Feb 26, 2024
1 parent 6a8ea3c commit d1d66d3
Show file tree
Hide file tree
Showing 13 changed files with 280 additions and 11 deletions.
14 changes: 10 additions & 4 deletions node/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import crypto = require('crypto');
export var _knownVariableMap: { [key: string]: _KnownVariableInfo; } = {};

export var _vault: vm.Vault;
var _taskSdkToken: string;

//-----------------------------------------------------
// Enums
//-----------------------------------------------------
Expand Down Expand Up @@ -303,12 +305,12 @@ export function _command(command: string, properties: any, message: string) {
_writeLine(taskCmd.toString());
}

export function _warning(message: string, source?: IssueSource): void {
_command('task.issue', { 'type': 'warning', 'source': source }, message);
export function _warning(message: string, source: IssueSource = IssueSource.TaskInternal): void {
_command('task.issue', { 'type': 'warning', 'source': source, 'token': _taskSdkToken }, message);
}

export function _error(message: string, source?: IssueSource): void {
_command('task.issue', { 'type': 'error', 'source': source }, message);
export function _error(message: string, source: IssueSource = IssueSource.TaskInternal): void {
_command('task.issue', { 'type': 'error', 'source': source, 'token': _taskSdkToken }, message);
}

export function _debug(message: string): void {
Expand Down Expand Up @@ -754,6 +756,10 @@ export function _loadData(): void {
}
_debug('loaded ' + loaded);

let token = process.env["TASK_SDK_COMMAND_TOKEN"];
delete process.env["TASK_SDK_COMMAND_TOKEN"];
_taskSdkToken = token ? String(token) : "";

// store public variable metadata
let names: string[];
try {
Expand Down
2 changes: 1 addition & 1 deletion node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "azure-pipelines-task-lib",
"version": "4.9.1",
"version": "4.10.0",
"description": "Azure Pipelines Task SDK",
"main": "./task.js",
"typings": "./task.d.ts",
Expand Down
180 changes: 180 additions & 0 deletions node/test/taskissuecommandtests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

import assert = require('assert');
import * as testutil from './testutil';
import * as tl from '../_build/task';
import { IssueSource, _loadData } from '../_build/internal';


describe('Task Issue command test without token', function () {

before(function (done) {
try {
testutil.initialize();
} catch (err) {
assert.fail('Failed to load task lib: ' + err.message);
}

done();
});

after(function (done) {
done();
});

it('adds issue sources for task.issue messages', function (done) {
this.timeout(1000);

var stdStream = testutil.createStringStream();
tl.setStdStream(stdStream);
tl.error("Test error", IssueSource.CustomerScript)
tl.warning("Test warning", IssueSource.TaskInternal)

var expected = testutil.buildOutput(
['##vso[task.issue type=error;source=CustomerScript;]Test error',
'##vso[task.issue type=warning;source=TaskInternal;]Test warning']);

var output = stdStream.getContents();

assert.equal(output, expected);

done();
})

it('adds the default "TaskInternal" source for task.issue command', function (done) {
this.timeout(1000);

var stdStream = testutil.createStringStream();
tl.setStdStream(stdStream);
tl.error("Test error");
tl.warning("Test warning");

var expected = testutil.buildOutput(
['##vso[task.issue type=error;source=TaskInternal;]Test error',
'##vso[task.issue type=warning;source=TaskInternal;]Test warning']);

var output = stdStream.getContents();

assert.equal(output, expected);

done();
})

it('adds the default "TaskInternal" source for the setResult', function (done) {
this.timeout(1000);

var stdStream = testutil.createStringStream();
tl.setStdStream(stdStream);
tl.setResult(tl.TaskResult.Failed, 'failed msg');

var expected = testutil.buildOutput(
['##vso[task.debug]task result: Failed',
'##vso[task.issue type=error;source=TaskInternal;]failed msg',
'##vso[task.complete result=Failed;]failed msg']);

var output = stdStream.getContents();

assert.equal(output, expected);

done();
})
});

describe('Task Issue command test with token', function () {

before(function (done) {
try {
testutil.initialize();
} catch (err) {
assert.fail('Failed to load task lib: ' + err.message);
}

process.env['TASK_SDK_COMMAND_TOKEN'] = 'test_token123';
_loadData();
done();
});

after(function (done) {
delete process.env['TASK_SDK_COMMAND_TOKEN'];
_loadData();
done();
});

it('removes the token from env var', function (done) {
this.timeout(1000);

assert.equal(process.env['TASK_SDK_COMMAND_TOKEN'], undefined);

done();
})

it('doesn\'t provide the token using task variables', function (done) {
this.timeout(1000);

process.env['AGENT_VERSION'] = '2.115.0'
let variable = tl.getVariable('TASK_SDK_COMMAND_TOKEN');
let taskVariable = tl.getTaskVariable('TASK_SDK_COMMAND_TOKEN');
assert.equal(variable, undefined);
assert.equal(taskVariable, undefined);

done();
})

it('adds the token for task.issue messages', function (done) {
this.timeout(1000);

var stdStream = testutil.createStringStream();
tl.setStdStream(stdStream);
tl.error("Test error", IssueSource.CustomerScript)
tl.warning("Test warning", IssueSource.TaskInternal)

var expected = testutil.buildOutput(
['##vso[task.issue type=error;source=CustomerScript;token=test_token123;]Test error',
'##vso[task.issue type=warning;source=TaskInternal;token=test_token123;]Test warning']);

var output = stdStream.getContents();

assert.equal(output, expected);

done();
})

it('adds the default "TaskInternal" source for task.issue command', function (done) {
this.timeout(1000);

var stdStream = testutil.createStringStream();
tl.setStdStream(stdStream);
tl.error("Test error");
tl.warning("Test warning");

var expected = testutil.buildOutput(
['##vso[task.issue type=error;source=TaskInternal;token=test_token123;]Test error',
'##vso[task.issue type=warning;source=TaskInternal;token=test_token123;]Test warning']);

var output = stdStream.getContents();

assert.equal(output, expected);

done();
})

it('adds the default "TaskInternal" source for the setResult', function (done) {
this.timeout(1000);

var stdStream = testutil.createStringStream();
tl.setStdStream(stdStream);
tl.setResult(tl.TaskResult.Failed, 'failed msg');

var expected = testutil.buildOutput(
['##vso[task.debug]task result: Failed',
'##vso[task.issue type=error;source=TaskInternal;token=test_token123;]failed msg',
'##vso[task.complete result=Failed;]failed msg']);

var output = stdStream.getContents();

assert.equal(output, expected);

done();
})
});
3 changes: 2 additions & 1 deletion node/test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"gethttpproxytests.ts",
"mockertests.ts",
"fakeModules/fakemodule1.ts",
"fakeModules/fakemodule2.ts"
"fakeModules/fakemodule2.ts",
"taskissuecommandtests.ts"
]
}
28 changes: 28 additions & 0 deletions powershell/Tests/L0/Write-LoggingCommand.WritesTaskIssue.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[CmdletBinding()]
param()

# Arrange.
. $PSScriptRoot\..\lib\Initialize-Test.ps1
Invoke-VstsTaskScript -ScriptBlock {
$vstsModule = Get-Module -Name VstsTaskSdk

# 1
$actual = & $vstsModule Write-TaskError -Message "test error" -AsOutput
$expected = "##vso[task.logissue source=TaskInternal;type=error]test error"
Assert-TaskIssueMessagesAreEqual $expected $actual "The default 'TastInternal' source was added for errors."

# 2
$actual = & $vstsModule Write-TaskWarning -Message "test warning" -AsOutput
$expected = "##vso[task.logissue source=TaskInternal;type=warning]test warning"
Assert-TaskIssueMessagesAreEqual $expected $actual "The default 'TastInternal' source was added for warnings."

#3
$actual = & $vstsModule Write-TaskError -Message "test error" -IssueSource $IssueSources.CustomerScript -AsOutput
$expected = "##vso[task.logissue source=CustomerScript;type=error]test error"
Assert-TaskIssueMessagesAreEqual $expected $actual "Adds the specified issue source for errors."

#4
$actual = & $vstsModule Write-TaskWarning -Message "test warning" -IssueSource $IssueSources.CustomerScript -AsOutput
$expected = "##vso[task.logissue source=CustomerScript;type=warning]test warning"
Assert-TaskIssueMessagesAreEqual $expected $actual "Adds the specified issue source for warnings."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[CmdletBinding()]
param()

$env:TASK_SDK_COMMAND_TOKEN = "test_token123"

# Arrange.
. $PSScriptRoot\..\lib\Initialize-Test.ps1
Invoke-VstsTaskScript -ScriptBlock {
$vstsModule = Get-Module -Name VstsTaskSdk

# 1
Assert-AreEqual $null $env:TASK_SDK_COMMAND_TOKEN "SDK removes the token after loading."

# 2
$actual = & $vstsModule Get-TaskVariable -Name "TASK_SDK_COMMAND_TOKEN"
Assert-AreEqual $null $actual "The token is inaccessible using task variables."

# 3
$actual = & $vstsModule Write-TaskError -Message "test error" -AsOutput
$expected = "##vso[task.logissue source=TaskInternal;type=error;token=test_token123]test error"
Assert-TaskIssueMessagesAreEqual $expected $actual "The default 'TastInternal' source and the token were added for errors."

# 4
$actual = & $vstsModule Write-TaskWarning -Message "test warning" -AsOutput
$expected = "##vso[task.logissue source=TaskInternal;type=warning;token=test_token123]test warning"
Assert-TaskIssueMessagesAreEqual $expected $actual "The default 'TastInternal' source and the token were added for warnings."

# 5
$actual = & $vstsModule Write-TaskError -Message "test error" -IssueSource $IssueSources.CustomerScript -AsOutput
$expected = "##vso[task.logissue source=CustomerScript;type=error;token=test_token123]test error"
Assert-TaskIssueMessagesAreEqual $expected $actual "Adds the specified issue source and token for errors."

# 6
$actual = & $vstsModule Write-TaskWarning -Message "test warning" -IssueSource $IssueSources.CustomerScript -AsOutput
$expected = "##vso[task.logissue source=CustomerScript;type=warning;token=test_token123]test warning"
Assert-TaskIssueMessagesAreEqual $expected $actual "Adds the specified issue source and token for warnings."
}
9 changes: 9 additions & 0 deletions powershell/Tests/lib/TestHelpersModule/PublicFunctions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ function Assert-AreNotEqual {
}
}

function Assert-TaskIssueMessagesAreEqual {
[CmdletBinding()]
param([string]$Expected, [string]$Actual, [string]$Message)

if ($Expected -ne $Actual) {
throw ("Assert are equal failed. Expected: '$Expected' ; Actual: '$Actual'. $Message".Trim())
}
}

function Assert-IsGreaterThan {
[CmdletBinding()]
param([object]$Expected, [object]$Actual, [string]$Message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ param()
Export-ModuleMember -Verbose:$false -Function @(
'Assert-AreEqual'
'Assert-AreNotEqual'
'Assert-TaskIssueMessagesAreEqual'
'Assert-IsNotNullOrEmpty'
'Assert-IsNullOrEmpty'
'Assert-IsGreaterThan'
Expand Down
9 changes: 8 additions & 1 deletion powershell/VstsTaskSdk/LoggingCommandFunctions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ $script:loggingCommandEscapeMappings = @( # TODO: WHAT ABOUT "="? WHAT ABOUT "%"
# TODO: BUG: Escape % ???
# TODO: Add test to verify don't need to escape "=".

$taskSDKToken = $env:TASK_SDK_COMMAND_TOKEN
if ($null -ne $taskSDKToken)
{
[System.Environment]::SetEnvironmentVariable("TASK_SDK_COMMAND_TOKEN", $null)
}

$IssueSources = @{
CustomerScript = "CustomerScript"
TaskInternal = "TaskInternal"
Expand Down Expand Up @@ -554,7 +560,7 @@ function Write-LogIssue {
[switch]$AsOutput,
[AllowNull()]
[ValidateSet('CustomerScript', 'TaskInternal')]
[string]$IssueSource)
[string]$IssueSource = $IssueSources.TaskInternal)

$command = Format-LoggingCommand -Area 'task' -Event 'logissue' -Data $Message -Properties @{
'type' = $Type
Expand All @@ -563,6 +569,7 @@ function Write-LogIssue {
'linenumber' = $LineNumber
'columnnumber' = $ColumnNumber
'source' = $IssueSource
'token' = $taskSDKToken
}
if ($AsOutput) {
return $command
Expand Down
2 changes: 1 addition & 1 deletion powershell/VstsTaskSdk/VstsTaskSdk.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
PowerShellVersion = '3.0'
FunctionsToExport = '*'
CmdletsToExport = ''
VariablesToExport = 'IssueSources'
VariablesToExport = '*'
AliasesToExport = ''
PrivateData = @{
PSData = @{
Expand Down
2 changes: 1 addition & 1 deletion powershell/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion powershell/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.19.0",
"version": "0.20.0",
"private": true,
"scripts": {
"build": "node make.js build",
Expand Down

0 comments on commit d1d66d3

Please sign in to comment.