Esempio n. 1
0
 public async Task <bool> IsReadyToMerge(GithubPullRequestDTO PR)
 {
     if (PR.state == "open")
     {
         if (await IsApproved(PR))
         {
             if (await IsMergeable(PR))
             {
                 if (SatisfiesLabels(PR))
                 {
                     return(true);
                 }
                 else
                 {
                     var message = $"Flag removal PR opened by {PR.user.login} is open, approved, mergeable but does not satisfy Date restrictions: [Link]({PR.url})";
                     _logger.alertFlowdock(message);
                 }
             }
             else
             {
                 var message = $"Flag removal PR opened by {PR.user.login} is open, and approved but not mergeable: [Link]({PR.url})";
                 _logger.alertFlowdock(message);
             }
         }
         else
         {
             var message = $"Flag removal PR opened by {PR.user.login} is open but not approved: [Link]({PR.url})";
             _logger.alertFlowdock(message);
         }
     }
     return(false);
 }
Esempio n. 2
0
        public bool SatisfiesLabels(GithubPullRequestDTO PR)
        {
            var labels = PR.labels;

            foreach (var label in PR.labels)
            {
                DateTime threshold;
                var      success = DateTime.TryParse(label.name, out threshold);
                if (success)
                {
                    return(DateTime.Now > threshold);
                }
            }
            return(true);
        }
Esempio n. 3
0
        private async Task ProcessPRForFlag(GithubPullRequestDTO PR, string flag)
        {
            var ready = await _api.IsReadyToMerge(PR);

            if (ready)
            {
                var response = await _api.MergePR(PR);

                if (response.merged)
                {
                    _flowdock.alertFlowdock($"[PR]({PR.url}) for the removal of {flag} has been automatically merged");
                }
                else
                {
                    _flowdock.alertFlowdock($"[PR]({PR.url}) for the removal of {flag} failed to merge automatically - {response.message}");
                }
            }
        }
Esempio n. 4
0
        public async Task <GithubMergeResponseDTO> MergePR(GithubPullRequestDTO PR)
        {
            return(new GithubMergeResponseDTO
            {
                merged = false,
                message = "Still in dry run mode, no merged will be performed"
            });

            var url    = $"https://{API_BASE}/{RepoOwner}/{RepoName}/pulls/{PR.number}/merge";
            var reqDTO = new GithubMergeRequestDTO
            {
                commit_title = $"Automatic merging of approved flag removal branch {PR.head.@ref}",
                merge_method = "squash"
            };

            var json = JsonConvert.SerializeObject(reqDTO);

            var response = await _client.Put(url, json);

            var content = await response.ReadAsStringAsync();

            return(JsonConvert.DeserializeObject <GithubMergeResponseDTO>(content));
        }
Esempio n. 5
0
        public async Task <bool> IsApproved(GithubPullRequestDTO PR)
        {
            var reviews = await GetPullRequestReviews(PR.number);

            Dictionary <string, ReviewStateDTO> userToReviewStateMap = new Dictionary <string, ReviewStateDTO>();

            foreach (var review in reviews)
            {
                string[] relevantStates = { "APPROVED", "CHANGES_REQUESTED" };
                if (!relevantStates.Contains(review.state))
                {
                    continue;
                }

                var reviewer = review.user.login;
                if (!userToReviewStateMap.ContainsKey(reviewer))
                {
                    userToReviewStateMap.Add(reviewer, new ReviewStateDTO
                    {
                        submitted = DateTime.Parse(review.submitted_at),
                        state     = review.state
                    });
                }
                else
                {
                    var newSubmissionDate = DateTime.Parse(review.submitted_at);
                    if (newSubmissionDate > userToReviewStateMap[reviewer].submitted)
                    {
                        userToReviewStateMap[reviewer].submitted = newSubmissionDate;
                        userToReviewStateMap[reviewer].state     = review.state;
                    }
                }
            }

            return(userToReviewStateMap.Count > 0 && userToReviewStateMap.Values.All(o => o.state == "APPROVED"));
        }
Esempio n. 6
0
        public async Task <bool> IsMergeable(GithubPullRequestDTO PR)
        {
            var fullPR = await GetPullRequestByNum(PR.number);

            return(fullPR.mergeable == true);
        }