private async Task EnsureLabelsAsync() { GitHubConnectionOptions options = _githubOptions.Value; IGitHubClient client = await _gitHubClientFactory.CreateGitHubClientAsync(options.Organization, options.Repository); await GitHubModifications.TryCreateAsync( () => client.Issue.Labels.Create( options.Organization, options.Repository, new NewLabel(_githubOptions.Value.RcaLabel, "009999")), _logger ); }
private async Task CloseExistingNotificationAsync(GrafanaNotification notification) { string org = _githubOptions.Value.Organization; string repo = _githubOptions.Value.Repository; IGitHubClient client = await GetGitHubClientAsync(org, repo); Issue issue = await GetExistingIssueAsync(client, notification); if (issue == null) { _logger.LogInformation("No active issue found for alert '{ruleName}', ignoring", notification.RuleName); return; } _logger.LogInformation( "Found existing issue {org}/{repo}#{issueNumber}, replacing {activeTag} with {inactiveTag}", org, repo, issue.Number, ActiveAlertLabel, InactiveAlertLabel); await GitHubModifications.TryRemoveAsync(() => client.Issue.Labels.RemoveFromIssue(org, repo, issue.Number, ActiveAlertLabel), _logger); await GitHubModifications.TryCreateAsync(() => client.Issue.Labels.AddToIssue(org, repo, issue.Number, new[] { InactiveAlertLabel }), _logger); _logger.LogInformation("Adding recurrence comment to {org}/{repo}#{issueNumber}", org, repo, issue.Number); IssueComment comment = await client.Issue.Comment.Create(org, repo, issue.Number, GenerateNewNotificationComment(notification)); _logger.LogInformation("Created comment {org}/{repo}#{issue}-issuecomment-{comment}", org, repo, issue.Id, comment.Id); }
private async Task OpenNewNotificationAsync(GrafanaNotification notification) { string org = _githubOptions.Value.Organization; string repo = _githubOptions.Value.Repository; _logger.LogInformation( "Alert state detected for {ruleUrl} in stage {ruleState}, porting to github repo {org}/{repo}", notification.RuleUrl, notification.State, org, repo); IGitHubClient client = await GetGitHubClientAsync(_githubOptions.Value.Organization, _githubOptions.Value.Repository); Issue issue = await GetExistingIssueAsync(client, notification); await EnsureLabelsAsync(client, org, repo); if (issue == null) { _logger.LogInformation("No existing issue found, creating new active issue with {label}", ActiveAlertLabel); issue = await client.Issue.Create(org, repo, GenerateNewIssue(notification)); _logger.LogInformation("Github issue {org}/{repo}#{issueNumber} created", org, repo, issue.Number); NotificationEpicOptions epic = _githubOptions.Value.NotificationEpic; if (epic != null) { (Repository epicRepoData, Repository issueRepoData) = await Task.WhenAll( client.Repository.Get(org, epic.Repository), client.Repository.Get(org, repo) ); _logger.LogInformation("Adding new issue to ZenHub..."); await _zenHub.AddIssueToEpicAsync( new ZenHubClient.IssueIdentifier(issueRepoData.Id, issue.Number), new ZenHubClient.IssueIdentifier(epicRepoData.Id, epic.IssueNumber) ); } else { _logger.LogInformation("No ZenHub epic configured, skipping..."); } } else { _logger.LogInformation( "Found existing issue {org}/{repo}#{issueNumber}, replacing {inactiveTag} with {activeTag}", org, repo, issue.Number, InactiveAlertLabel, ActiveAlertLabel); await GitHubModifications.TryRemoveAsync(() => client.Issue.Labels.RemoveFromIssue(org, repo, issue.Number, InactiveAlertLabel), _logger); await GitHubModifications.TryCreateAsync(() => client.Issue.Labels.AddToIssue(org, repo, issue.Number, new[] { ActiveAlertLabel }), _logger); _logger.LogInformation("Adding recurrence comment to {org}/{repo}#{issueNumber}", org, repo, issue.Number); IssueComment comment = await client.Issue.Comment.Create(org, repo, issue.Number, GenerateNewNotificationComment(notification)); _logger.LogInformation("Created comment {org}/{repo}#{issue}-issuecomment-{comment}", org, repo, issue.Id, comment.Id); } }