Commit to Work Item Link Between AZURE DEVOPS Servers Fails

I am attempting to migrate a commit link from an old Azure DevOps server to a new internal Azure DevOps server. The goal is to link a specific commit from the repository on the old server to a Work Item on the new server using an ArtifactLink relation. Despite following the Azure DevOps API documentation and sending a properly structured PATCH request to the new server, the commit link is not being added successfully to the Work Item.

    import requests
import json
import base64

# Azure DevOps connection details
organization_name = "DefaultCollection"
project_name = "Nativ-Clearance-API"
project = "2379ab9f-dae0-4f6e-9f52-2e3f746d64d8"
work_item_id = 24594  # The ID of the work item you want to update
repository_id_get = "0a149557-bd34-480c-b9de-37f972aca43c"
repository_id_post ="f87dde0c-4a19-4693-81b6-44400390f5cf"
commit_id = "e888f8744e2cc40d43e2e3eaf91805a2d0ac4fa9"  # The commitId you want to add
personal_access_token = "**************"  # Replace with your personal access token
base64_encoded_token = base64.b64encode(f":{personal_access_token}".encode()).decode("utf-8")

import requests
import json
from base64 import b64encode

# Old server data
old_server = {
    "organization": "HMS-RD",
    "project": "Nativ-Clearance-API",
    "repo_id": "0a149557-bd34-480c-b9de-37f972aca43c",  # Repository ID from the old server
    "commit_id": "1489545200cdf2f0d0852ce129bb31f21ddcad07",  # Commit ID
    "personal_access_token": "**************"  # Replace with the old server's PAT
}

# New server data
new_server = {
    "url": "http://192.168.1.132//DefaultCollection",
    "project": "Nativ-Clearance-API",
    "repo_id": "f87dde0c-4a19-4693-81b6-44400390f5cf",  # Define new repository ID in the internal server
    "work_item_id": 24594,
    "personal_access_token": "**************"  # Replace with the new server's PAT
}

# Step 1: Authentication with the old server and fetching commit_id
old_commit_url = f"https://dev.azure.com/{old_server['organization']}/{old_server['project']}/_apis/git/repositories/{old_server['repo_id']}/commits/{old_server['commit_id']}?api-version=7.1-preview.1"
old_headers = {
    "Authorization": f"Basic {b64encode(f':{old_server['personal_access_token']}'.encode()).decode()}"
}

# Sending the request to the old server
response = requests.get(old_commit_url, headers=old_headers)
if response.status_code == 200:
    commit_data = response.json()
    print("Commit data fetched successfully!")
else:
    print(f"Failed to fetch commit data: {response.status_code}")
    print(response.text)
    exit()

# Step 2: Sending PATCH request to the new server to link commit to the work item
new_work_item_url = f"{new_server['url']}/{new_server['project']}/_apis/wit/workitems/{new_server['work_item_id']}?api-version=7.1-preview.2"
new_headers = {
    "Content-Type": "application/json-patch+json",
    "Authorization": f"Basic {b64encode(f':{new_server['personal_access_token']}'.encode()).decode()}"
}

# Payload to link commit to the work item
payload = [
    {
        "op": "add",
        "path": "/relations/-",
        "value": {
            "rel": "ArtifactLink",
            "url": f"vstfs:///Git/{new_server['repo_id']}/commit/{old_server['commit_id']}",
            "attributes": {
                "comment": "Migrated commit link from old server",
                "name": "Nativ-Clearance-API"
            }
        }
    }
]

# Sending the request to the new server
response = requests.patch(new_work_item_url, headers=new_headers, data=json.dumps(payload))
if response.status_code == 200:
    print("Commit successfully linked to work item!")
else:
    print(f"Failed to link commit: {response.status_code}")
    print(response.text)

1

Below code is to migrate a commit id between Azure DevOps servers and merge it to a work item.

Refer this link to Create Work Items with REST API in Azure DevOps

Refer this doc to Link git commit or branch to work item via command line.

import requests
import base64
import json
old_org_url = "https://old-server.dev.azure.com/organization_name"
new_org_url = "https://new-server.dev.azure.com/organization_name"
old_pat = "OLD_PAT" 
new_pat = "NEW_PAT"  
api_version = "7.1"
def get_headers(pat):
    token = base64.b64encode(f":{pat}".encode("ascii")).decode("ascii")
    return {
        "Authorization": f"Basic {token}",
        "Content-Type": "application/json"
    }

old_headers = get_headers(old_pat)
new_headers = get_headers(new_pat)

def get_commit_details(org_url, project, repository_id, commit_id, headers):
    url = f"{org_url}/{project}/_apis/git/repositories/{repository_id}/commits/{commit_id}?api-version={api_version}"
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        return response.json()
    else:
        print(f"Error fetching commit details: {response.status_code} - {response.text}")
        return None

link_commit_to_work_item(org_url, project, work_item_id, commit_url, headers):
    url = f"{org_url}/{project}/_apis/wit/workitems/{work_item_id}?api-version={api_version}"
    body = [
        {
            "op": "add",
            "path": "/relations/-",
            "value": {
                "rel": "ArtifactLink",
                "url": commit_url,
                "attributes": {"name": "Fixed in Commit"}
            }
        }
    ]
    response = requests.patch(url, headers=headers, json=body)
    if response.status_code in [200, 201]:
        return response.json()
    else:
        print(f"Error linking commit to work item: {response.status_code} - {response.text}")
        return None
def migrate_commit_link(old_org_url, new_org_url, old_headers, new_headers, old_project, new_project, work_item_id, repository_id, commit_id):
    commit_details = get_commit_details(old_org_url, old_project, repository_id, commit_id, old_headers)
    if not commit_details:
        print("Failed to retrieve commit details from the old server.")
        return
    commit_url = commit_details["_links"]["web"]["href"]
    print(f"Commit URL from old server: {commit_url}")
    result = link_commit_to_work_item(new_org_url, new_project, work_item_id, commit_url, new_headers)
    if result:
        print("Successfully linked commit to the work item on the new server.")
        print(json.dumps(result, indent=4))
    else:
        print("Failed to link the commit to the work item.")
old_project = "OldProjectName"
new_project = "NewProjectName"
repository_id = "OldRepositoryID"  
commit_id = "CommitID"  
work_item_id = 123  
migrate_commit_link(
    old_org_url,
    new_org_url,
    old_headers,
    new_headers,
    old_project,
    new_project,
    work_item_id,
    repository_id,
    commit_id
)

Output:

Recognized by Microsoft Azure Collective

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật