Example #1
0
        public static async Task <IList <GitPullRequest> > TagStalePullRequests(
            GitHttpClient gitClient,
            string project,
            Dictionary <GitPullRequest, bool> stalePRs,
            string warningPrefix,
            int warningCount,
            string warningMessage,
            string abandonMessage)
        {
            List <GitPullRequest> pullCollection = new List <GitPullRequest>();
            string commentMessage = string.Empty;

            foreach (KeyValuePair <GitPullRequest, bool> pr in stalePRs)
            {
                // Retrieve the tags so we can figure out what we need to remove, how many warnings have been given
                List <WebApiTagDefinition> allTags = await gitClient.GetPullRequestLabelsAsync(project, pr.Key.Repository.Id, pr.Key.PullRequestId);

                if (!pr.Value)
                {
                    // PR has been updated since the last bot action, remove tags
                    List <WebApiTagDefinition> tagsToRemove = allTags.Where(t => t.Name.StartsWith(warningPrefix)).ToList();

                    foreach (WebApiTagDefinition tag in tagsToRemove)
                    {
                        await gitClient.DeletePullRequestLabelsAsync(project, pr.Key.Repository.Id, pr.Key.PullRequestId, tag.Id.ToString());
                    }

                    commentMessage = warningMessage;
                }
                else
                {
                    // check how many tags are already on it, if it's more than the warning threshold, add it to the list of returned PRs
                    int botTagCount = allTags.Where(t => t.Name.StartsWith(warningPrefix)).Count();

                    if (botTagCount >= warningCount)
                    {
                        pullCollection.Add(pr.Key);
                        commentMessage = abandonMessage;
                    }
                    else
                    {
                        commentMessage = warningMessage;
                    }
                }

                // Add a comment to the PR describing that it's stale
                Comment comment = new Comment {
                    Content = string.Format(commentMessage, pr.Key.CreatedBy.Id)
                };
                List <Comment> commentList = new List <Comment> {
                    comment
                };
                GitPullRequestCommentThread commentThread = new GitPullRequestCommentThread
                {
                    Comments = commentList,
                    Status   = CommentThreadStatus.Active
                };
                await gitClient.CreateThreadAsync(commentThread, pr.Key.Repository.Id, pr.Key.PullRequestId);

                // add a tag for this run
                WebApiCreateTagRequestData newLabel = new WebApiCreateTagRequestData
                {
                    Name = string.Format("{0}: (UTC) {1}", warningPrefix, DateTime.UtcNow.ToString("g"))
                };
                await gitClient.CreatePullRequestLabelAsync(newLabel, pr.Key.Repository.Id, pr.Key.PullRequestId);
            }

            return(pullCollection);
        }