Search code examples
azureazure-devopsazure-rest-apiazure-boardsazure-service-hooks

How to Create a Userstory from project 1 to project 2 in azure DevOps?


I have diff project in the same organization each team has his own project and i want to automate the creation of userstory from one project to another for exemple if i have Userstory X in Project 1 assigned to Team1 and i need the intervention of team 2 so we will create a new related userstory in project 1 . i want that new US to be created automatically in the project2 of the second team .

  • service hooks to trigger when the userstory is updated and become related to another UserStory
  • External Service Endpoints

Solution

  • You can try to use the webhook based triggers to meet your demands with following configurations in project1:

    1. Go to "Project Settings" > "Service hooks" to create a web hook with the "Work item updated" event.

      enter image description here

      The request URL is with the format like as below.

      https://dev.azure.com/<ADO Organization>/_apis/public/distributedtask/webhooks/<WebHook Name>?api-version=6.0-preview
      

      For example: https://dev.azure.com/MyOrg/_apis/public/distributedtask/webhooks/UsAssignedToTeam1?api-version=6.0-preview

    2. Go to "Project Settings" > "Service connections" to create a new "Incoming Webhook" service connection. Ensure the WebHook Name is consistent with that in the request URL.

      enter image description here

    3. Set up a YAML pipeline like as below.

    resources:
      webhooks:
      - webhook: UsAssignedToTeam1
        connection: UsAssignedToTeam1Connetion
    
    stages:
    - stage: A
      jobs:
      - job: A1
        steps:
        - checkout: none
        
        - task: Bash@3
          displayName: 'Create User Story'
          env:
            SYSTEM_ACCESSTOKEN: $(System.AccessToken)
          inputs:
            targetType: inline
            script: |
              echo "Get the details of the User Story assigned to Team1 in Project1:"
              workItemId=${{ parameters.UsAssignedToTeam1.resource.workItemId }}
              workItemUrl=${{ parameters.UsAssignedToTeam1.resource._links.parent.href }}
              echo "workItemId = $workItemId"
              echo "workItemUrl = $workItemUrl"
    
              echo "------------------------------------------"
    
              echo "Create a new User Story in Project2:"
              url="https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/\$User%20Story?api-version=7.0"
              curl -X POST -u :$SYSTEM_ACCESSTOKEN $url \
              -H 'Content-Type: application/json-patch+json' \
              -d '[
                    # Set the title of the new User Story.
                    {
                      "op": "add",
                      "path": "/fields/System.Title",
                      "value": "New User Story in Project2 related to User Story ($workItemId) in project1"
                    },
                    # Set the Area Path of the new User Story. e.g.: Project2\\AreaTeam2\\SubArea
                    {
                      "op": "add",
                      "path": "/fields/System.AreaPath",
                      "value": "{Area Path of Team2 in Project2}"
                    },
                    # Set the Iteration Path of the new User Story. e.g.: Project2\\Iteration2024\\SubIteration
                    {
                      "op": "add",
                      "path": "/fields/System.IterationPath",
                      "value": "{Iteration Path in Project2}"
                    },
                    # Link the User Story in project1 as 'Related' on the new User Story.
                    {
                      "op": "add",
                      "path": "/relations/-",
                      "value": {
                        "rel": "System.LinkTypes.Related",
                        "url": "$workItemUrl",
                        "attributes": {
                          "isLocked": false,
                          "name": "Related"
                        }
                      }
                    }
                  ]'
    

    By this, when a User Story was assigned to the area path of Team1 in project1, it will trigger the web hook, then the web hook will trigger the YAML pipeline. The pipeline will call the RESI API "Work Items - Create" to create a new User Story in project2 and link the User Story in project1 as the "Related" type.

    enter image description here