使用Azure DevOps API批量导入工作项
1. 前言
Azure DevOps可以通过Excel文件导入工作项, 同时也可以通过Azure DevOps API动态的导入。
2. 如何实现
首先, 准备好一个文件, 里面录入工作项数据。
示例代码:
string azureDevOpsOrganizationUrl = "https://dev.azure.com/{Organization}/{Project}/";
private void ImportWorkItems()
{
string[] workItems = File.ReadAllLines("<Path to CSV file>");
var personalAccessToken = "<Azure DevOps PAT token>";
string credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", personalAccessToken)));
//Just skipping the CSV file header
foreach (var row in workItems.Skip(1))
{
var columns = row.Split(',');
var type = columns[0];
var title = columns[1];
var description = columns[2];
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(azureDevOpsOrganizationUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json-patch+json"));
client.DefaultRequestHeaders.Add("User-Agent", "ManagedClientConsoleAppSample");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);
//connect to the REST endpoint
string uri = String.Format("_apis/wit/workitems/${0}?bypassRules=true&api-version=6.0", type);
//building JSON request body
var jsonstr = "[{\"op\": \"add\", \"path\": \"/fields/System.Title\",\"value\": \"" + title + "\" }";
jsonstr += ",{\"op\": \"add\", \"path\": \"/fields/System.Description\",\"value\": \"" + description + "\" }";
jsonstr += "]";
HttpContent body = new StringContent(jsonstr, Encoding.UTF8, "application/json-patch+json");
HttpResponseMessage response = client.PostAsync(uri, body).Result;
// check to see if we have a successful respond
if (response.IsSuccessStatusCode)
{
...
}
else
{
...
}
}
}
}
代码执行结果为: