Skip to content

Commit 0e44434

Browse files
Add audit action for task.issue (#1033)
* Add audit action for issue * Added audit action for powershell * Added tests for node * Update old tests + move enum to .psm1 * Fix issue properties order * Add powershell tests * Bump versions * Update release notes
1 parent f886362 commit 0e44434

13 files changed

+185
-35
lines changed

node/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## 4.x
44

5+
### 4.11.0
6+
7+
- Added audit action for task.issue [#1033](https://github.com/microsoft/azure-pipelines-task-lib/pull/1033)
8+
59
### 4.10.0
610

711
- Added `correlation ID` property for logging commands [#1021](https://github.com/microsoft/azure-pipelines-task-lib/pull/1021)

node/internal.ts

+39-8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ export enum IssueSource {
3030
TaskInternal = 'TaskInternal'
3131
}
3232

33+
export enum IssueAuditAction {
34+
Unknown = 0,
35+
ShellTasksValidation = 1,
36+
}
37+
3338
//-----------------------------------------------------
3439
// Validation Checks
3540
//-----------------------------------------------------
@@ -52,11 +57,11 @@ export function _endsWith(str: string, end: string): boolean {
5257
}
5358

5459
export function _truncateBeforeSensitiveKeyword(str: string, sensitiveKeywordsPattern: RegExp): string {
55-
if(!str) {
60+
if (!str) {
5661
return str;
5762
}
5863

59-
const index = str.search(sensitiveKeywordsPattern);
64+
const index = str.search(sensitiveKeywordsPattern);
6065

6166
if (index <= 0) {
6267
return str;
@@ -250,7 +255,7 @@ export function _loc(key: string, ...param: any[]): string {
250255
* @param name name of the variable to get
251256
* @returns string
252257
*/
253-
export function _getVariable(name: string): string | undefined {
258+
export function _getVariable(name: string): string | undefined {
254259
let varval: string | undefined;
255260

256261
// get the metadata
@@ -305,12 +310,38 @@ export function _command(command: string, properties: any, message: string) {
305310
_writeLine(taskCmd.toString());
306311
}
307312

308-
export function _warning(message: string, source: IssueSource = IssueSource.TaskInternal): void {
309-
_command('task.issue', { 'type': 'warning', 'source': source, 'correlationId': _commandCorrelationId }, message);
313+
export function _warning(
314+
message: string,
315+
source: IssueSource = IssueSource.TaskInternal,
316+
auditAction?: IssueAuditAction
317+
): void {
318+
_command(
319+
'task.issue',
320+
{
321+
'type': 'warning',
322+
'source': source,
323+
'correlationId': _commandCorrelationId,
324+
'auditAction': auditAction
325+
},
326+
message
327+
);
310328
}
311329

312-
export function _error(message: string, source: IssueSource = IssueSource.TaskInternal): void {
313-
_command('task.issue', { 'type': 'error', 'source': source, 'correlationId': _commandCorrelationId }, message);
330+
export function _error(
331+
message: string,
332+
source: IssueSource = IssueSource.TaskInternal,
333+
auditAction?: IssueAuditAction
334+
): void {
335+
_command(
336+
'task.issue',
337+
{
338+
'type': 'error',
339+
'source': source,
340+
'correlationId': _commandCorrelationId,
341+
'auditAction': auditAction
342+
},
343+
message
344+
);
314345
}
315346

316347
export function _debug(message: string): void {
@@ -801,7 +832,7 @@ export function _loadData(): void {
801832
* @param path a path to a file.
802833
* @returns true if path starts with double backslash, otherwise returns false.
803834
*/
804-
export function _isUncPath(path: string) {
835+
export function _isUncPath(path: string) {
805836
return /^\\\\[^\\]/.test(path);
806837
}
807838

node/package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node/test/taskissuecommandtests.ts

+75-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import assert = require('assert');
55
import * as testutil from './testutil';
66
import * as tl from '../_build/task';
7-
import { IssueSource, _loadData } from '../_build/internal';
7+
import { IssueAuditAction, IssueSource, _loadData } from '../_build/internal';
88

99

1010
describe('Task Issue command test without correlation ID', function () {
@@ -177,4 +177,77 @@ describe('Task Issue command test with correlation ID', function () {
177177

178178
done();
179179
})
180-
});
180+
});
181+
182+
describe('Task Issue command, audit action tests', function () {
183+
before(function (done) {
184+
try {
185+
testutil.initialize();
186+
} catch (err) {
187+
assert.fail('Failed to load task lib: ' + err.message);
188+
}
189+
190+
done();
191+
});
192+
193+
after(function (done) {
194+
done();
195+
});
196+
197+
it('Audit action is present in issue', function (done) {
198+
this.timeout(1000);
199+
200+
const stdStream = testutil.createStringStream();
201+
tl.setStdStream(stdStream);
202+
203+
const expected = testutil.buildOutput(
204+
['##vso[task.issue type=error;auditAction=1;]Test error',
205+
'##vso[task.issue type=warning;auditAction=1;]Test warning']);
206+
207+
tl.error("Test error", null, IssueAuditAction.ShellTasksValidation);
208+
tl.warning("Test warning", null, IssueAuditAction.ShellTasksValidation);
209+
210+
const output = stdStream.getContents();
211+
212+
assert.strictEqual(output, expected);
213+
done();
214+
})
215+
216+
it('Audit action not present if unspecified', function (done) {
217+
this.timeout(1000);
218+
219+
const stdStream = testutil.createStringStream();
220+
tl.setStdStream(stdStream);
221+
222+
const expected = testutil.buildOutput(
223+
['##vso[task.issue type=error;]Test error',
224+
'##vso[task.issue type=warning;]Test warning']);
225+
226+
tl.error("Test error", null);
227+
tl.warning("Test warning", null);
228+
229+
const output = stdStream.getContents();
230+
231+
assert.strictEqual(output, expected);
232+
done();
233+
})
234+
235+
it('Audit action is present when value is not from enum', function (done) {
236+
this.timeout(1000);
237+
238+
const stdStream = testutil.createStringStream();
239+
tl.setStdStream(stdStream);
240+
241+
const expected = testutil.buildOutput(
242+
['##vso[task.issue type=error;auditAction=123;]Test error',
243+
'##vso[task.issue type=warning;auditAction=321;]Test warning']);
244+
245+
tl.error("Test error", null, 123 as IssueAuditAction);
246+
tl.warning("Test warning", null, 321 as IssueAuditAction);
247+
248+
const output = stdStream.getContents();
249+
250+
assert.strictEqual(output, expected);
251+
done();
252+
})
253+
});

powershell/Docs/ReleaseNotes.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Release Notes
22

3+
## 0.21.0
4+
5+
- Added audit action for task.issue [#1033](https://github.com/microsoft/azure-pipelines-task-lib/pull/1033)
6+
37
## 0.17.0
48

59
* Added `Invoke-VstsProcess` cmdlet (<https://github.com/microsoft/azure-pipelines-task-lib/pull/978>)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[CmdletBinding()]
2+
param()
3+
4+
# Arrange.
5+
. $PSScriptRoot\..\lib\Initialize-Test.ps1
6+
Invoke-VstsTaskScript -ScriptBlock {
7+
$vstsModule = Get-Module -Name VstsTaskSdk
8+
9+
# 1
10+
$actual = & $vstsModule Write-TaskError -Message "test error" -AsOutput
11+
$expected = "##vso[task.logissue type=error;source=TaskInternal]test error"
12+
Assert-TaskIssueMessagesAreEqual $expected $actual "No audit action in issue if not specified."
13+
14+
# 2
15+
$actual = & $vstsModule Write-TaskWarning -Message "test warning" -AuditAction '1' -AsOutput
16+
$expected = "##vso[task.logissue type=warning;source=TaskInternal;auditAction=1]test warning"
17+
Assert-TaskIssueMessagesAreEqual $expected $actual "String audit action."
18+
19+
#3
20+
$actual = & $vstsModule Write-TaskError -Message "test error" -AuditAction 1 -AsOutput
21+
$expected = "##vso[task.logissue type=error;source=TaskInternal;auditAction=1]test error"
22+
Assert-TaskIssueMessagesAreEqual $expected $actual "Int audit action."
23+
}

powershell/Tests/L0/Write-LoggingCommand.WritesTaskIssue.ps1

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ Invoke-VstsTaskScript -ScriptBlock {
88

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

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

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

2424
#4
2525
$actual = & $vstsModule Write-TaskWarning -Message "test warning" -IssueSource $IssueSources.CustomerScript -AsOutput
26-
$expected = "##vso[task.logissue source=CustomerScript;type=warning]test warning"
26+
$expected = "##vso[task.logissue type=warning;source=CustomerScript]test warning"
2727
Assert-TaskIssueMessagesAreEqual $expected $actual "Adds the specified issue source for warnings."
2828
}

powershell/Tests/L0/Write-LoggingCommand.WritesTaskIssueWithCorrelationId.ps1

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ Invoke-VstsTaskScript -ScriptBlock {
1717

1818
# 3
1919
$actual = & $vstsModule Write-TaskError -Message "test error" -AsOutput
20-
$expected = "##vso[task.logissue correlationId=test_id123;source=TaskInternal;type=error]test error"
20+
$expected = "##vso[task.logissue type=error;source=TaskInternal;correlationId=test_id123]test error"
2121
Assert-TaskIssueMessagesAreEqual $expected $actual "The default 'TastInternal' source and the correlation ID were added for errors."
2222

2323
# 4
2424
$actual = & $vstsModule Write-TaskWarning -Message "test warning" -AsOutput
25-
$expected = "##vso[task.logissue correlationId=test_id123;source=TaskInternal;type=warning]test warning"
25+
$expected = "##vso[task.logissue type=warning;source=TaskInternal;correlationId=test_id123]test warning"
2626
Assert-TaskIssueMessagesAreEqual $expected $actual "The default 'TastInternal' source and the correlation ID were added for warnings."
2727

2828
# 5
2929
$actual = & $vstsModule Write-TaskError -Message "test error" -IssueSource $IssueSources.CustomerScript -AsOutput
30-
$expected = "##vso[task.logissue correlationId=test_id123;source=CustomerScript;type=error]test error"
30+
$expected = "##vso[task.logissue type=error;source=CustomerScript;correlationId=test_id123]test error"
3131
Assert-TaskIssueMessagesAreEqual $expected $actual "Adds the specified issue source and the correlation ID for errors."
3232

3333
# 6
3434
$actual = & $vstsModule Write-TaskWarning -Message "test warning" -IssueSource $IssueSources.CustomerScript -AsOutput
35-
$expected = "##vso[task.logissue correlationId=test_id123;source=CustomerScript;type=warning]test warning"
35+
$expected = "##vso[task.logissue type=warning;source=CustomerScript;correlationId=test_id123]test warning"
3636
Assert-TaskIssueMessagesAreEqual $expected $actual "Adds the specified issue source and the correlation ID for warnings."
3737
}

powershell/Tests/lib/TestHelpersModule/PublicFunctions.ps1

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function Assert-TaskIssueMessagesAreEqual {
2222
[CmdletBinding()]
2323
param([string]$Expected, [string]$Actual, [string]$Message)
2424

25+
Write-Verbose "Asserting Task issue messages are equal. Expected: '$Expected' ; Actual: '$Actual'."
2526
if ($Expected -ne $Actual) {
2627
throw ("Assert are equal failed. Expected: '$Expected' ; Actual: '$Actual'. $Message".Trim())
2728
}

powershell/VstsTaskSdk/LoggingCommandFunctions.ps1

+22-14
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,9 @@ function Write-TaskError {
298298
[string]$LineNumber,
299299
[string]$ColumnNumber,
300300
[switch]$AsOutput,
301-
[string]$IssueSource)
301+
[string]$IssueSource,
302+
[string]$AuditAction
303+
)
302304

303305
Write-LogIssue -Type error @PSBoundParameters
304306
}
@@ -335,7 +337,9 @@ function Write-TaskWarning {
335337
[string]$LineNumber,
336338
[string]$ColumnNumber,
337339
[switch]$AsOutput,
338-
[string]$IssueSource)
340+
[string]$IssueSource,
341+
[string]$AuditAction
342+
)
339343

340344
Write-LogIssue -Type warning @PSBoundParameters
341345
}
@@ -517,7 +521,7 @@ function Format-LoggingCommand {
517521
[Parameter(Mandatory = $true)]
518522
[string]$Event,
519523
[string]$Data,
520-
[hashtable]$Properties)
524+
[System.Collections.IDictionary]$Properties)
521525

522526
# Append the preamble.
523527
[System.Text.StringBuilder]$sb = New-Object -TypeName System.Text.StringBuilder
@@ -560,17 +564,21 @@ function Write-LogIssue {
560564
[switch]$AsOutput,
561565
[AllowNull()]
562566
[ValidateSet('CustomerScript', 'TaskInternal')]
563-
[string]$IssueSource = $IssueSources.TaskInternal)
564-
565-
$command = Format-LoggingCommand -Area 'task' -Event 'logissue' -Data $Message -Properties @{
566-
'type' = $Type
567-
'code' = $ErrCode
568-
'sourcepath' = $SourcePath
569-
'linenumber' = $LineNumber
570-
'columnnumber' = $ColumnNumber
571-
'source' = $IssueSource
572-
'correlationId' = $commandCorrelationId
573-
}
567+
[string]$IssueSource = $IssueSources.TaskInternal,
568+
[string]$AuditAction
569+
)
570+
571+
$properties = [ordered]@{
572+
'type' = $Type
573+
'code' = $ErrCode
574+
'sourcepath' = $SourcePath
575+
'linenumber' = $LineNumber
576+
'columnnumber' = $ColumnNumber
577+
'source' = $IssueSource
578+
'correlationId' = $commandCorrelationId
579+
'auditAction' = $AuditAction
580+
}
581+
$command = Format-LoggingCommand -Area 'task' -Event 'logissue' -Data $Message -Properties $properties
574582
if ($AsOutput) {
575583
return $command
576584
}

powershell/VstsTaskSdk/VstsTaskSdk.psm1

+6
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,14 @@ Export-ModuleMember -Function @(
9595
'Get-ClientCertificate'
9696
)
9797

98+
$IssueAuditActions = @{
99+
Unknown = '0'
100+
ShellTasksValidation = '1'
101+
}
102+
98103
Export-ModuleMember -Variable @(
99104
'IssueSources'
105+
$IssueAuditActions
100106
)
101107

102108
# Override Out-Default globally.

powershell/package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

powershell/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.20.1",
2+
"version": "0.21.0",
33
"private": true,
44
"scripts": {
55
"build": "node make.js build",

0 commit comments

Comments
 (0)