示例#1
0
 public CommentLinkUser(string nameIdentifier, string emailAddress, string name, OAuthTokenResponse token)
 {
     Id              = nameIdentifier;
     EmailAddress    = emailAddress;
     Name            = name;
     Token           = new AzDoToken(token);
     TokenExpiration = DateTime.UtcNow.AddSeconds(Convert.ToDouble(token.ExpiresIn));
     DisplayName     = name + " <" + emailAddress + ">";
 }
示例#2
0
 public void UpdateToken(AzDoToken toke)
 {
     Token           = toke;
     TokenExpiration = DateTime.UtcNow.AddSeconds(Convert.ToDouble(toke.ExpiresIn));
 }
示例#3
0
        public StatusCodeResult Post([FromBody] JsonElement jsonComment)
        {
            JObject comment     = JObject.Parse(jsonComment.ToString());
            string  htmlcomment = (string)comment["detailedMessage"]["html"];

            //trim off the first part of the message which tells us who commented to get us to the comment
            htmlcomment = htmlcomment.Substring(htmlcomment.IndexOf("<br/>") + 5);
            string ogHtmlComment = htmlcomment;

            //for each entry in the config injections, look for the regex and then replace using the evaluator
            // https://stackoverflow.com/questions/306527/how-would-i-pass-additional-parameters-to-matchevaluator
            foreach (Injection injection in _config.Injections)
            {
                htmlcomment = Regex.Replace(htmlcomment, injection.RegexToFind, new MatchEvaluator(match => CreateLink(match, injection)));
            }

            // see if comment needs to be written back to DevOps.  If not stop here
            if (string.Equals(ogHtmlComment, htmlcomment))
            {
                return(StatusCode(200));
            }

            // get useful data out of the stuff that AzDO sends us
            string commentId            = (string)comment["resource"]["commentVersionRef"]["commentId"];
            string commentApiUrl        = (string)comment["resource"]["commentVersionRef"]["url"];
            string organizationUrl      = (string)comment["resourceContainers"]["project"]["baseUrl"];
            string project              = (string)comment["resourceContainers"]["project"]["id"];
            string workItemId           = (string)comment["resource"]["id"];
            string changedByDisplayName = (string)comment["resource"]["fields"]["System.ChangedBy"];

            // get the user from cosmos db
            string cosmosQuery = "SELECT * FROM CommentLinkUsers c WHERE c.displayName = \"" + changedByDisplayName + "\"";
            IEnumerable <CommentLinkUser> cmls = _cosmosDb.GetItemsAsync(cosmosQuery).Result;

            if (cmls.Count() > 1)
            {
                throw new Exception("There shouldn't be more than one user with the same display name");
            }
            else if (cmls.Count() == 0)
            {
                return(StatusCode(200));
            }
            CommentLinkUser cml = cmls.First();

            // determine if we need to refresh the token
            if (DateTime.UtcNow > cml.TokenExpiration.AddSeconds(-300))
            {
                AzDoToken toke = _talker.RefreshToken(cml.Token.RefreshToken).Result;
                cml.UpdateToken(toke);
                _cosmosDb.UpsertItem(cml);
            }

            //now patch this back to AzureDevOps
            Dictionary <string, string> commentDictionary = new Dictionary <string, string>();

            commentDictionary.Add("text", htmlcomment);
            string      json           = JsonConvert.SerializeObject(commentDictionary, Formatting.Indented);
            HttpContent commentContent = new StringContent(json, Encoding.UTF8, "application/json");

            // start updating the comment
            string commentUri = organizationUrl + project + "/_apis/wit/workitems/" + workItemId + "/comments/" + commentId + "?api-version=6.0-preview.3";

            _ = _talker.Patch(commentUri, commentContent, cml.Token.AccessToken);
            return(StatusCode(200));
        }