-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAzureDevOpsBugCreator.cs
More file actions
53 lines (46 loc) · 2.07 KB
/
Copy pathAzureDevOpsBugCreator.cs
File metadata and controls
53 lines (46 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using Newtonsoft.Json;
using RestSharp;
using System;
using System.IO;
namespace SeleniumTests
{
public class AzureDevOpsBugCreator
{
private string azureDevOpsUrl;
private string project;
private string personalAccessToken;
public AzureDevOpsBugCreator()
{
var config = File.ReadAllText("appsettings.json");
dynamic settings = JsonConvert.DeserializeObject(config);
azureDevOpsUrl = settings.AzureDevOpsUrl;
project = settings.Project;
personalAccessToken = settings.PersonalAccessToken;
}
public void CreateBug(string bugTitle)
{
var client = new RestClient($"{azureDevOpsUrl}/{project}/_apis/wit/workitems/$Bug?api-version=6.0");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json-patch+json");
string authToken = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($":{personalAccessToken}"));
request.AddHeader("Authorization", $"Basic {authToken}");
var bugData = new[]
{
new { op = "add", path = "/fields/System.Title", value = bugTitle },
new { op = "add", path = "/fields/System.Description", value = "Bug created automatically due to failed Selenium test." },
new { op = "add", path = "/fields/System.AssignedTo", value = "shahab@tecoholic.com" },
new { op = "add", path = "/fields/Microsoft.VSTS.TCM.ReproSteps", value = "See attached logs for detailed error." }
};
request.AddParameter("application/json-patch+json", JsonConvert.SerializeObject(bugData), ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
if (response.IsSuccessful)
{
Console.WriteLine("Bug created successfully in Azure DevOps.");
}
else
{
Console.WriteLine("Failed to create bug: " + response.ErrorMessage);
}
}
}
}