MicrosoftPremier / VstsExtensions

Documentation and issue tracking for Microsoft Premier Services Visual Studio Team Services Extensions
MIT License
59 stars 14 forks source link

create custom field #175

Closed kostukp96 closed 2 years ago

kostukp96 commented 2 years ago

Hello! How i can create "Additional Fields" from custom file. For example i generate custom txt file every time in my pipeline, after this i create work item, and i need attach information from this file to description in my new work item. How i can do this ?

ReneSchumacher commented 2 years ago

Hi @kostukp96,

there is no "simple" way to to this since our task does not directly support getting work item values from files. My recommendation would be to use a script task to read the file content and create a variable from it. This might look like this:

steps:
  - pwsh: |
      $fileContent = Get-Content description.txt
      $singleLineContent = [string]::Join("<br/>", $fileContent)
      Write-Host "##vso[task.setvariable variable=description;isOutput=true;]$singleLineContent"
    name: readContent
    displayName: 'Read work item description from file'

  - task: CreateWorkItem@1
    displayName: 'Create new work item'
    inputs:
      workItemType: Task
      title: 'New work item with description from text file'
      fieldMappings: |
        Description = $(readContent.description)

As you can see, the PowerShell Core script gets then content from file, which results in an array with one entry per line in the file. Those lines are then concatenated to a single string, separating them with a <br/> (the HTML tag for a line break), and that value is then used to create a new variable named description. The variable can then be accessed with the syntax $(<task-name>.<variable-name>) and is used as the value for Description in the field mapping of the CreateWorkItem task.

kostukp96 commented 2 years ago

Maybe I didn't fully understand this meaning ##vso[task.setvariable variable=description;isOutput=true;]$singleLineContent But i am use classic editor and when i use your step in my work item in description i see this text $(readContent.description) that is, I do not get the text I need in the section description

ReneSchumacher commented 2 years ago

Ah, didn't know you were on classic. The line ##vso[task.setvariable variable=description;isOutput=true;]$singleLineContent basically just creates a new variable with the name description and the value set to the content of the variable $singleLineContent.

The reference to the variable is different in classic, though. When you're using a PowerShell task, the variable name defaults to $(PowerShell.<variable-name>) unless you set a specific reference name in the Output Variables section: image

Then you can reference the variable using $(<reference-name>.<variable-name>): image

kostukp96 commented 2 years ago

i need use task dump veriables ?

ReneSchumacher commented 2 years ago

No, this is just an internal helper task I wrote. Just set the reference name and use it in the variable reference.

kostukp96 commented 2 years ago

Thank you! its work

kostukp96 commented 2 years ago

Hello! Thank you for your help last time! Could you tell him how to change the name of the item? I mean, I'll enter that it would take an automatically generated variable from the json file as a name? For example i want generate this name "name": "CVE-2019-17359" from my json file to item name, but every time when i started mu pipeline this name can be change

ReneSchumacher commented 2 years ago

Hi again,

you can basically use variables for all fields of the Create Work Item task, which includes the title field. You just need to find a way to read the value from your json file and create another variable using the logging command ##vso[task.setvariable variable=<name>;isoutput=true;]<value>.

kostukp96 commented 2 years ago

thanks again! it works! sorry to bother you

kostukp96 commented 2 years ago

Hello! Sorry to write to you once again, but how can I change this value while the pipeline is running ? Capture ? My item type every time generate Risk, and i need to take value from my json file to Custom field How i can do that ?

ReneSchumacher commented 2 years ago

Hey,

don't worry about asking, I'm glad to help. Do you need to update that field in an existing item or are you asking how to set that field in a new item? For every field, you can hover over the label in the UI to see the field name. Then you can use that name in the fieldMappings setting in the task YAML or the Additional Fields box in the task UI. Just follow the syntax =. And similar to the previous questions, if the value is dynamic and should be taken from an external file, you need to create a variable for it.

kostukp96 commented 2 years ago

It will look like this: "Custom:Column.Risk = $(readContent)" ?

ReneSchumacher commented 2 years ago

No, the column only exists in the UI. The mapping is simply Risk = $(variable).

kostukp96 commented 2 years ago

ok, i will try

kostukp96 commented 2 years ago

now i have this error: "There was an error while creating the work item. The field 'Risk' contains the value 'MEDIUM' that is not in the list of supported values There was an error while creating the work item.

[error]There was an error while creating the work item."

I understand that the values ​​are written to the risk "High" "Medium" "Low". Maybe you kwon how i can transform my value "MEDIUM" to "Medium" ?

ReneSchumacher commented 2 years ago

I don't think the field rules are case sensitive. Is this the built-in Risk field? If so, the values should be something like 2 - Medium. Can you please check that? You might have to add some logic to your PowerShell script to prepend the number before the text value.

kostukp96 commented 2 years ago

ok, i will try

kostukp96 commented 2 years ago

How can I configure this task so that it does not duplicate items with the same name?

kostukp96 commented 2 years ago

@ReneSchumacher can you help me ? How can I configure this task so that it does not duplicate items with the same name?

ReneSchumacher commented 2 years ago

Hi,

this is all documented at https://github.com/MicrosoftPremier/VstsExtensions/blob/master/CreateWorkItem/en-US/overview.md#duplicates. You have to find a work item field or a combination of fields that can be checked by the task to figure out if the new work item would be a duplicate. If you need to, you can then enable duplicate update and change some fields in the duplicate item.

kostukp96 commented 2 years ago

Thanx