private PayloadViewModel BuildPayloadViewModel(JObject body) { PayloadViewModel vm = new PayloadViewModel(); //using var jsonDoc = JsonDocument.Parse(body); //var root = jsonDoc.RootElement; //var WorkItem = JsonSerializer.Deserialize<WorkItem>(body); //string url = WorkItem.Url; string url = body["resource"]["url"] == null ? null : body["resource"]["url"].ToString(); string org = GetOrganization(url); vm.workItemId = body["resource"]["workItemId"] == null ? -1 : Convert.ToInt32(body["resource"]["workItemId"].ToString()); //(int)WorkItem.Id; vm.workItemType = body["resource"]["revision"]["fields"]["System.WorkItemType"] == null ? null : body["resource"]["revision"]["fields"]["System.WorkItemType"].ToString(); //(string)WorkItem.Fields["System.WorkItemType"]; vm.eventType = body["eventType"] == null ? null : body["eventType"].ToString(); vm.rev = vm.rev = body["resource"]["rev"] == null ? -1 : Convert.ToInt32(body["resource"]["rev"].ToString()); //(int)WorkItem.Rev; // vm.url = body["resource"]["url"] == null ? null : body["resource"]["url"].ToString(); //WorkItem.Url; // vm.organization = org; vm.teamProject = body["resource"]["fields"]["System.AreaPath"] == null ? null : body["resource"]["fields"]["System.AreaPath"].ToString(); //(string)WorkItem.Fields["System.AreaPath"]; // vm.state = body["resource"]["fields"]["System.State"]["newValue"] == null ? null : body["resource"]["fields"]["System.State"]["newValue"].ToString(); //(string)WorkItem.Fields["System.State"]; // return(vm); }
public WorkItem UpdateWorkItem(JsonPatchDocument patchDocument, PayloadViewModel vm) { string pat = vm.pat; Uri baseUri = new Uri("https://dev.azure.com/" + vm.organization); VssCredentials clientCredentials = new VssCredentials(new VssBasicCredential("username", pat)); VssConnection connection = new VssConnection(baseUri, clientCredentials); WorkItemTrackingHttpClient client = connection.GetClient <WorkItemTrackingHttpClient>(); WorkItem result = null; try { result = client.UpdateWorkItemAsync(patchDocument, vm.id).Result; } catch (Exception ex) { result = null; } finally { clientCredentials = null; connection = null; client = null; } return(result); }
public IActionResult Index() { var model = new PayloadViewModel(); var reps = _deployDbContext.Query <Repository>("SELECT DISTINCT Id, Name, Full_name, Url FROM t_repository "); return(View()); }
private PayloadViewModel BuildPayloadViewModel(JObject body) { PayloadViewModel vm = new PayloadViewModel(); string url = body["resource"]["url"] == null ? null : body["resource"]["url"].ToString(); string org = GetOrganization(url); vm.workItemId = body["resource"]["workItemId"] == null ? -1 : Convert.ToInt32(body["resource"]["workItemId"].ToString()); vm.workItemType = body["resource"]["revision"]["fields"]["System.WorkItemType"] == null ? null : body["resource"]["revision"]["fields"]["System.WorkItemType"].ToString(); vm.eventType = body["eventType"] == null ? null : body["eventType"].ToString(); vm.rev = body["resource"]["rev"] == null ? -1 : Convert.ToInt32(body["resource"]["rev"].ToString()); vm.url = body["resource"]["url"] == null ? null : body["resource"]["url"].ToString(); vm.organization = org; vm.teamProject = body["resource"]["fields"]["System.AreaPath"] == null ? null : body["resource"]["fields"]["System.AreaPath"].ToString(); vm.state = body["resource"]["fields"]["System.State"]["newValue"] == null ? null : body["resource"]["fields"]["System.State"]["newValue"].ToString(); //debug System.Console.WriteLine(DateTime.Now + " Requested Payload eventType:" + vm.eventType); System.Console.WriteLine(DateTime.Now + " Requested Payload state:" + vm.state); System.Console.WriteLine(DateTime.Now + " Requested Payload workItemType:" + vm.workItemType); System.Console.WriteLine(DateTime.Now + " Requested Payload workItemId:" + vm.workItemId); System.Console.WriteLine(DateTime.Now + " Requested Payload orgnization:" + vm.organization); String tempParentId = body["resource"]["revision"]["fields"]["System.Parent"] == null ? null : body["resource"]["revision"]["fields"]["System.Parent"].ToString(); String tempTeamProject = body["resource"]["revision"]["fields"]["System.TeamProject"] == null ? null : body["resource"]["revision"]["fields"]["System.TeamProject"].ToString(); System.Console.WriteLine(DateTime.Now + " Requested Payload teamProject:" + tempTeamProject); System.Console.WriteLine(DateTime.Now + " Requested Payload parentId:" + tempParentId); return(vm); }
private PayloadViewModel BuildPayloadViewModel(JObject body) { PayloadViewModel vm = new PayloadViewModel(); string url = body["resource"]["url"] == null ? null : body["resource"]["url"].ToString(); string org = GetOrganization(url); vm.workItemId = body["resource"]["workItemId"] == null ? -1 : Convert.ToInt32(body["resource"]["workItemId"].ToString()); vm.workItemType = body["resource"]["revision"]["fields"]["System.WorkItemType"] == null ? null : body["resource"]["revision"]["fields"]["System.WorkItemType"].ToString(); vm.eventType = body["eventType"] == null ? null : body["eventType"].ToString(); vm.rev = body["resource"]["rev"] == null ? -1 : Convert.ToInt32(body["resource"]["rev"].ToString()); vm.url = body["resource"]["url"] == null ? null : body["resource"]["url"].ToString(); vm.organization = org; vm.teamProject = body["resource"]["fields"]["System.AreaPath"] == null ? null : body["resource"]["fields"]["System.AreaPath"].ToString(); vm.state = body["resource"]["fields"]["System.State"]["newValue"] == null ? null : body["resource"]["fields"]["System.State"]["newValue"].ToString(); return(vm); }
private PayloadViewModel BuildPayloadViewModel(JObject body) { PayloadViewModel vm = new PayloadViewModel(); string url = body["resource"]["url"] == null ? null : body["resource"]["url"].ToString(); string org = GetOrganization(url); vm.id = body["resource"]["id"] == null ? -1 : Convert.ToInt32(body["resource"]["id"].ToString()); vm.eventType = body["eventType"] == null ? null : body["eventType"].ToString(); vm.rev = body["resource"]["rev"] == null ? -1 : Convert.ToInt32(body["resource"]["rev"].ToString()); vm.url = body["resource"]["url"] == null ? null : body["resource"]["url"].ToString(); vm.organization = org; vm.teamProject = body["resource"]["fields"]["System.AreaPath"] == null ? null : body["resource"]["fields"]["System.AreaPath"].ToString(); vm.createdBy = body["resource"]["fields"]["System.CreatedBy"]["displayName"] == null ? null : body["resource"]["fields"]["System.CreatedBy"]["displayName"].ToString(); vm.assignedTo = body["resource"]["fields"]["System.AssignedTo"] == null ? null : body["resource"]["fields"]["System.Assigned"].ToString(); return(vm); }
public async Task <IActionResult> Post([FromBody] JObject payload) { PayloadViewModel vm = BuildPayloadViewModel(payload); //make sure pat is not empty, if it is, pull from appsettings vm.pat = _appSettings.Value.PersonalAccessToken; //if the event type is something other the updated, then lets just return an ok if (vm.eventType != "workitem.updated") { return(new OkResult()); } // create our azure devops connection Uri baseUri = new Uri("https://dev.azure.com/" + vm.organization); VssCredentials clientCredentials = new VssCredentials(new VssBasicCredential("username", vm.pat)); VssConnection vssConnection = new VssConnection(baseUri, clientCredentials); // load the work item posted WorkItem workItem = await _workItemRepo.GetWorkItem(vssConnection, vm.workItemId); // this should never happen, but if we can't load the work item from the id, then exit with error if (workItem == null) { return(new StandardResponseObjectResult("Error loading workitem '" + vm.workItemId + "'", StatusCodes.Status500InternalServerError)); } // get the related parent WorkItemRelation parentRelation = workItem.Relations.Where <WorkItemRelation>(x => x.Rel.Equals("System.LinkTypes.Hierarchy-Reverse")).FirstOrDefault(); // if we don't have any parents to worry about, then just abort if (parentRelation == null) { return(new OkResult()); } Int32 parentId = _helper.GetWorkItemIdFromUrl(parentRelation.Url); WorkItem parentWorkItem = await _workItemRepo.GetWorkItem(vssConnection, parentId); if (parentWorkItem == null) { return(new StandardResponseObjectResult("Error loading parent work item '" + parentId.ToString() + "'", StatusCodes.Status500InternalServerError)); } string parentState = parentWorkItem.Fields["System.State"] == null ? string.Empty : parentWorkItem.Fields["System.State"].ToString(); // load rules for updated work item RulesModel rulesModel = _rulesRepo.ListRules(vm.workItemType); //loop through each rule foreach (var rule in rulesModel.Rules) { if (rule.IfChildState.Equals(vm.state)) { if (!rule.AllChildren) { if (!rule.NotParentStates.Contains(parentState)) { await _workItemRepo.UpdateWorkItemState(vssConnection, parentWorkItem, rule.SetParentStateTo); return(new OkResult()); } } else { // get a list of all the child items to see if they are all closed or not List <WorkItem> childWorkItems = await _workItemRepo.ListChildWorkItemsForParent(vssConnection, parentWorkItem); // check to see if any of the child items are not closed, if so, we will get a count > 0 int count = childWorkItems.Where(x => !x.Fields["System.State"].ToString().Equals(rule.IfChildState)).ToList().Count; if (count.Equals(0)) { await _workItemRepo.UpdateWorkItemState(vssConnection, parentWorkItem, rule.SetParentStateTo); } return(new OkResult()); } } } return(new StandardResponseObjectResult("success", StatusCodes.Status200OK)); }
public IActionResult Post([FromBody] JObject payload) { string tags = ""; string authHeader = ""; string pat = ""; #if DEBUG isDebug = true; #endif if (!isDebug) { tags = Request.Headers.ContainsKey("Work-Item-Tags") ? Request.Headers["Work-Item-Tags"] : new StringValues(""); authHeader = Request.Headers.ContainsKey("Authorization") ? Request.Headers["Authorization"] : new StringValues(""); if (!authHeader.StartsWith("Basic")) { return(new StandardResponseObjectResult("missing basic authorization header", StatusCodes.Status401Unauthorized)); } //get pat from basic authorization header. This was set in the web hook pat = this.GetPersonalAccessToken(authHeader); } PayloadViewModel vm = this.BuildPayloadViewModel(payload); //make sure pat is not empty, if it is, pull from appsettings vm.pat = (!string.IsNullOrEmpty(pat)) ? pat : _appSettings.Value.AzureDevOpsToken; if (string.IsNullOrEmpty(vm.pat)) { return(new StandardResponseObjectResult("missing pat from authorization header and appsettings", StatusCodes.Status404NotFound)); } if (vm.eventType != "workitem.created") { return(new OkResult()); } if (vm.id == -1) { return(new OkResult()); } JsonPatchDocument patchDocument = new JsonPatchDocument(); patchDocument.Add( new JsonPatchOperation() { Operation = Operation.Test, Path = "/rev", Value = (vm.rev + 1).ToString() } ); if (string.IsNullOrEmpty(vm.assignedTo)) { patchDocument.Add( new JsonPatchOperation() { Operation = Operation.Add, Path = "/fields/System.AssignedTo", Value = vm.createdBy } ); } if (!string.IsNullOrEmpty(tags)) { patchDocument.Add( new JsonPatchOperation() { Operation = Operation.Add, Path = "/fields/System.Tags", Value = tags } ); } patchDocument.Add( new JsonPatchOperation() { Operation = Operation.Add, Path = "/fields/System.IterationPath", Value = vm.teamProject } ); var result = _workItemRepo.UpdateWorkItem(patchDocument, vm); return((result != null) ? new OkResult() : new StatusCodeResult(500)); }