public IssueLabelModel(IssueReference issue, string label, bool labeled, DateTime lastChanged)
 {
     Issue       = issue;
     Label       = label;
     Labeled     = labeled;
     LastChanged = lastChanged;
 }
Ejemplo n.º 2
0
        public static void ValidateIssue(IssueReference issue)
        {
            if (issue is null)
            {
                throw new ArgumentNullException(nameof(issue));
            }

            if (issue.Id <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(issue.Id), issue.Id, "Issue Id has to be positive non zero number.");
            }

            if (issue.Number <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(issue.Number), issue.Number, "Issue number has to be positive non zero number.");
            }

            if (string.IsNullOrWhiteSpace(issue.RepositoryOwner))
            {
                throw new ArgumentException("Repository owner has to be non empty.", nameof(issue.RepositoryOwner));
            }

            if (string.IsNullOrWhiteSpace(issue.RepositoryName))
            {
                throw new ArgumentException("Repository name has to be non empty.", nameof(issue.RepositoryName));
            }
        }
        public Task <bool> TestLabel(IssueReference issue, string label)
        {
            Validation.ValidateIssue(issue);
            Validation.ValidateLabel(label);

            return(_issueLabelRepository.TestLabel(issue, label));
        }
        public async Task SubscribeToIssue(IssueReference issue, IReadOnlyCollection <LabelSubscriptionModel> labelSubscriptions)
        {
            var subscriptions = labelSubscriptions.GroupBy(ls => ls.Label, ls => ls.UserId).ToDictionary(g => g.Key, g => g.ToHashSet());

            var gitHubClient = await _gitHubClientFactory.GetGitHubInstallationClient();

            var comments = await gitHubClient.Issue.Comment.GetAllForIssue(issue.RepositoryOwner, issue.RepositoryName, issue.Number);

            var notifyComment = comments.FirstOrDefault(c => c.Body.Contains(CommentMark, StringComparison.InvariantCultureIgnoreCase));

            if (notifyComment == null)
            {
                var comment = FormatNotifyComment(subscriptions);
                await gitHubClient.Issue.Comment.Create(issue.RepositoryOwner, issue.RepositoryName, issue.Number, comment);
            }
            else
            {
                // syntax: |**label-name**|owner(s)|
                Regex labelSubscriptionPattern = new Regex(@"^\|\s*(?'label'[^,|]+)\s*\|(\s*@(?'users'[\w-/]+))+\s*\|", RegexOptions.Singleline);
                using (StringReader sr = new StringReader(notifyComment.Body))
                {
                    string?line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        var match = labelSubscriptionPattern.Match(line);
                        if (match.Success)
                        {
                            var label = match.Groups["label"].Captures[0].Value;

                            if (!subscriptions.TryGetValue(label, out var subscribers))
                            {
                                subscribers          = new HashSet <string>();
                                subscriptions[label] = subscribers;
                            }

                            foreach (Capture?capture in match.Groups["users"].Captures)
                            {
                                if (capture == null)
                                {
                                    continue;
                                }

                                subscribers.Add(capture.Value);
                            }
                        }
                    }
                }

                var comment = FormatNotifyComment(subscriptions);
                await gitHubClient.Issue.Comment.Update(issue.RepositoryOwner, issue.RepositoryName, notifyComment.Id, comment);
            }
        }
        public async Task <bool> TryRemoveLabel(IssueReference issue, string label, DateTime at)
        {
            Validation.ValidateIssue(issue);
            Validation.ValidateLabel(label);

            bool removed = await _issueLabelRepository.TrySetLabel(issue, label, false, at);

            if (removed)
            {
                var labelSubscriptions = await _userLabelRepository.GetLabelSubscriptions(issue.RepositoryOwner, issue.RepositoryName, label);

                if (labelSubscriptions.Count > 0)
                {
                    await _issueSubscriber.UnsubscribeFromIssue(issue, labelSubscriptions);
                }
            }

            return(removed);
        }
        public async Task <bool> TryAddLabel(IssueReference issue, string label, DateTime at)
        {
            Validation.ValidateIssue(issue);
            Validation.ValidateLabel(label);

            bool added = await _issueLabelRepository.TrySetLabel(issue, label, true, at);

            if (added)
            {
                var labelSubscriptions = await _userLabelRepository.GetLabelSubscriptions(issue.RepositoryOwner, issue.RepositoryName, label);

                if (labelSubscriptions.Count > 0)
                {
                    await _issueSubscriber.SubscribeToIssue(issue, labelSubscriptions);
                }
            }

            return(added);
        }
 public Task UnsubscribeFromIssue(IssueReference issue, IReadOnlyCollection <LabelSubscriptionModel> labelSubscriptions)
 {
     // we do not support unsubscribe as it is not possible to do so by issue comments
     return(Task.CompletedTask);
 }
Ejemplo n.º 8
0
        protected IssueReference IssueReference()
        {
            var issueReference = new IssueReference(RepositoryOwner, RepositoryName, Issue.Number, Issue.Id, Issue.NodeId);

            return(issueReference);
        }