Skip to content

Commit

Permalink
Including JSON format (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcanizalez authored Mar 4, 2022
1 parent 6a26f10 commit f87e633
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 18 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,11 @@ The following parameters are supported in the task:
| Variable Group ID | `variableGroupID` | Variable Group ID. You can get this value from Azure Devops in the url when you are in a Group Variable or using azure cli |
| Output Format | `outputFormat` | The output format for the variables in the Variable Group ID. Accepted values are: `Command Line Arguments` and `JSON` |

### Formats



### Output

The result of the task is expected in an output variable: `formattedVariables`. See the examples section for more information.

#### Formats
### Formats

If you have a Variable Group with the following values

Expand Down
13 changes: 12 additions & 1 deletion buildandreleasetask/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function run() {
try {
const projectId = tl.getVariable("System.TeamProjectId");
const variableGroupID = tl.getInput('variableGroupID', true);
const outputFormat = tl.getInput('outputFormat', true);
const accessToken = tl.getEndpointAuthorizationParameter('SYSTEMVSSCONNECTION', 'ACCESSTOKEN', false);
const endpointUrl = tl.getEndpointUrl('SYSTEMVSSCONNECTION', false);
if (variableGroupID == 'bad') {
Expand All @@ -49,7 +50,17 @@ function run() {
vstsTask.getVariableGroup(projectId, +variableGroupID).then((variableGroup) => {
console.log(`Got variable group ${variableGroup.name}`);
for (let key in variableGroup.variables) {
formattedVariables += `-${key} "${variableGroup.variables[key].value}" `;
switch (outputFormat) {
case "Command Line Arguments":
formattedVariables += `-${key} "${variableGroup.variables[key].value}" `;
break;
case "JSON":
formattedVariables += `{"name": "${key}" , "value" : "${variableGroup.variables[key].value}"},`;
break;
}
}
if (outputFormat == "JSON") {
formattedVariables = `[${formattedVariables.slice(0, -1)}]`;
}
console.log("formattedVariables: " + formattedVariables);
tl.setVariable('formattedVariables', formattedVariables);
Expand Down
32 changes: 22 additions & 10 deletions buildandreleasetask/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,40 @@ async function run() {
try {
const projectId: string | undefined = tl.getVariable("System.TeamProjectId");
const variableGroupID: string | undefined = tl.getInput('variableGroupID', true);
const accessToken: string | undefined = tl.getEndpointAuthorizationParameter('SYSTEMVSSCONNECTION', 'ACCESSTOKEN', false);
const endpointUrl: string |undefined= tl.getEndpointUrl('SYSTEMVSSCONNECTION', false);
const outputFormat: string | undefined = tl.getInput('outputFormat', true);
const accessToken: string | undefined = tl.getEndpointAuthorizationParameter('SYSTEMVSSCONNECTION', 'ACCESSTOKEN', false);
const endpointUrl: string | undefined = tl.getEndpointUrl('SYSTEMVSSCONNECTION', false);
if (variableGroupID == 'bad') {
tl.setResult(tl.TaskResult.Failed, 'Bad input was given');
return;
}
let authHandler = azdev.getPersonalAccessTokenHandler(accessToken!);
let authHandler = azdev.getPersonalAccessTokenHandler(accessToken!);
let connection = new azdev.WebApi(endpointUrl!, authHandler);
let vstsTask: ta.ITaskAgentApi = await connection.getTaskAgentApi();
let formattedVariables:string = "";
let formattedVariables: string = "";
console.log(`Getting variable group id ${variableGroupID}`);
vstsTask.getVariableGroup(projectId!,+variableGroupID!).then((variableGroup: ti.VariableGroup) => {
vstsTask.getVariableGroup(projectId!, +variableGroupID!).then((variableGroup: ti.VariableGroup) => {
console.log(`Got variable group ${variableGroup.name}`);
for (let key in variableGroup.variables!) {
formattedVariables += `-${key} "${variableGroup.variables![key].value}" `
}
console.log("formattedVariables: "+formattedVariables);
switch (outputFormat) {
case "Command Line Arguments":
formattedVariables += `-${key} "${variableGroup.variables![key].value}" `
break;
case "JSON":
formattedVariables += `{"name": "${key}" , "value" : "${variableGroup.variables![key].value}"},`
break;
}
}

if (outputFormat == "JSON") {
formattedVariables = `[${formattedVariables.slice(0,-1)}]`
}
console.log("formattedVariables: " + formattedVariables);
tl.setVariable('formattedVariables', formattedVariables);
});



}
catch (err) {
tl.setResult(tl.TaskResult.Failed, err.message);
Expand Down
2 changes: 1 addition & 1 deletion buildandreleasetask/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"author": "Devops Mindset",
"version": {
"Major": 0,
"Minor": 7,
"Minor": 11,
"Patch": 0
},
"instanceNameFormat": "Echo $(variableGroupID)",
Expand Down
2 changes: 1 addition & 1 deletion vss-extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifestVersion": 1,
"id": "variable-group-formatter",
"name": "Variable Group Formatter",
"version": "0.8.0",
"version": "0.11.0",
"publisher": "devopsmindset",
"targets": [
{
Expand Down

0 comments on commit f87e633

Please sign in to comment.