Esempio n. 1
0
        /// <summary>
        /// Checks that the creator of <see cref="pr"/> has received the contributor badge.
        /// </summary>
        /// <param name="pr">The PR.</param>
        /// <param name="hangfire">A reference to the Hangfire context if running from Hangfire.</param>
        public void CheckContributorBadges(Issue pr, PerformContext hangfire = null)
        {
            if (IsLive == false)
            {
                return;
            }

            string prefix = $"[{pr.RepoSlug}/{pr.Number}]";

            // Look for a "merged" event in the list of events
            var merged = pr.Events.FirstOrDefault(x => x.Name == "merged");

            // We don't care about PR that hasn't been merged yet
            if (merged == null)
            {
                hangfire.WriteLine($"{prefix} PR has not been merged yet.");
                return;
            }

            // Return now if we've already sent the reply
            if (GitHubAutoReply.HasReply(pr, GitHubAutoReplyType.ContributorsBadgeAdded))
            {
                hangfire.WriteLine($"{prefix} Auto reply to {pr.User.Login} has already been posted.");
                return;
            }

            // Get the first member matching the GitHub user ID (or null if not found)
            var creator = _github.GetMemberByGitHubUserId(pr.User.Id);

            // Member wasn't found in Umbraco
            if (creator == null)
            {
                // Return now if we've already sent the reply
                if (GitHubAutoReply.HasReply(pr, GitHubAutoReplyType.ContributorsBadgeMemberNotFound))
                {
                    hangfire.WriteLine($"{prefix} No corresponding member found for creator {pr.User.Login} (ID: {pr.User.Id})");
                    return;
                }

                // Add the comment to the issue on GitHub
                if (IsLive)
                {
                    _github.AddCommentToIssue(pr, GitHubAutoReplyType.ContributorsBadgeMemberNotFound, GetContributorBadgeMemberNotFoundMessage(pr));
                }

                return;
            }

            // Return if the member is already part of the role (aka has the contrib badge)
            if (Roles.IsUserInRole(creator.Email, Constants.MemberGroups.Contributor))
            {
                hangfire.WriteLine($"{prefix} Creator {pr.User.Login} already has the contributors badge");
                return;
            }

            // Add the member to the contrib role
            Roles.AddUserToRole(creator.Email, Constants.MemberGroups.Contributor);

            // Add the comment to the issue on GitHub
            if (IsLive)
            {
                _github.AddCommentToIssue(pr, GitHubAutoReplyType.ContributorsBadgeAdded, GetContributorBadgeAddedMessage(pr, creator));
            }

            hangfire.WriteLine($"{prefix} Creator {pr.User.Login} has successfully been given the contributors badge");
        }