/// <summary>
        ///  Subscriptions support notifying GitHub tags when non-batched dependency flow PRs fail checks.
        ///  Before inserting them into the database, we'll make sure they're either not a user's login or
        ///  that user is publicly a member of the Microsoft organization so we can store their login.
        /// </summary>
        /// <param name="pullRequestFailureNotificationTags"></param>
        /// <returns></returns>
        private async Task <bool> AllNotificationTagsValid(string pullRequestFailureNotificationTags)
        {
            string[] allTags = pullRequestFailureNotificationTags.Split(';', StringSplitOptions.RemoveEmptyEntries);

            // We'll only be checking public membership in the Microsoft org, so no token needed
            var  client  = _gitHubClientFactory.CreateGitHubClient(string.Empty);
            bool success = true;

            foreach (string tagToNotify in allTags)
            {
                // remove @ if it's there
                string tag = tagToNotify.TrimStart('@');

                try
                {
                    IReadOnlyList <Octokit.Organization> orgList = await client.Organization.GetAllForUser(tag);

                    success &= orgList.Any(o => o.Login?.Equals(RequiredOrgForSubscriptionNotification, StringComparison.InvariantCultureIgnoreCase) == true);
                }
                catch (Octokit.NotFoundException)
                {
                    // Non-existent user: Either a typo, or a group (we don't have the admin privilege to find out, so just allow it)
                }
            }

            return(success);
        }
Beispiel #2
0
 /// <summary>
 /// Construct a <see cref="GitHubManager"/>
 /// </summary>
 /// <param name="generalConfigurationOptions">The <see cref="IOptions{TOptions}"/> containing the value of <see cref="generalConfiguration"/></param>
 /// <param name="gitHubConfigurationOptions">The <see cref="IOptions{TOptions}"/> containing the value of <see cref="gitHubConfiguration"/></param>
 /// <param name="_databaseContext">The value of <see cref="databaseContext"/></param>
 /// <param name="_logger">The value of <see cref="logger"/></param>
 /// <param name="_gitHubClientFactory">The value of <see cref="gitHubClientFactory"/></param>
 public GitHubManager(IOptions <GeneralConfiguration> generalConfigurationOptions, IOptions <GitHubConfiguration> gitHubConfigurationOptions, IDatabaseContext _databaseContext, ILogger <GitHubManager> _logger, IGitHubClientFactory _gitHubClientFactory)
 {
     gitHubConfiguration  = gitHubConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(gitHubConfigurationOptions));
     generalConfiguration = generalConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(generalConfigurationOptions));
     logger              = _logger ?? throw new ArgumentNullException(nameof(_logger));
     databaseContext     = _databaseContext ?? throw new ArgumentNullException(nameof(_databaseContext));
     gitHubClientFactory = _gitHubClientFactory ?? throw new ArgumentNullException(nameof(_gitHubClientFactory));
     gitHubClient        = gitHubClientFactory.CreateGitHubClient(gitHubConfiguration.PersonalAccessToken);
     semaphore           = new SemaphoreSlim(1);
 }
Beispiel #3
0
        /// <inheritdoc />
        public async Task <User> GetUserLogin(string accessToken, CancellationToken cancellationToken)
        {
            logger.LogTrace("GetUserLogin. accessToken: {0}", accessToken);

            if (accessToken == null)
            {
                using (await SemaphoreSlimContext.Lock(semaphore, cancellationToken).ConfigureAwait(false))
                {
                    await CheckUser(false, cancellationToken).ConfigureAwait(false);

                    return(knownUser);
                }
            }

            return(await gitHubClientFactory.CreateGitHubClient(accessToken).User.Current().ConfigureAwait(false));
        }
Beispiel #4
0
        public async Task <EventHandlerResult> Execute(string requestBody)
        {
            if (!tryParsePayload(requestBody, out var webhookPayload, out var errorResult))
            {
                return(errorResult);
            }

            GitHubClient = await gitHubClientFactory.CreateGitHubClient(webhookPayload.Installation.Id, Logger);

            if (!await isEnabledForRepository(webhookPayload))
            {
                Logger.LogInformation("no ahk-monitor.yml or disabled");
                return(EventHandlerResult.Disabled("no ahk-monitor.yml or disabled"));
            }

            return(await executeCore(webhookPayload));
        }
        public async Task <EventHandlerResult> Execute(string requestBody)
        {
            if (!tryParsePayload(requestBody, out var webhookPayload, out var errorResult, out var organizationLogin))
            {
                return(errorResult);
            }

            GitHubClient = await gitHubClientFactory.CreateGitHubClient(webhookPayload.Installation.Id);

            var(repoSettings, repoSettingsErrorResult) = await tryGetRepositorySettings(webhookPayload, organizationLogin);

            if (repoSettings == null)
            {
                return(repoSettingsErrorResult);
            }

            if (!repoSettings.Enabled)
            {
                return(EventHandlerResult.Disabled($"ahk-monitor.yml disabled app for repository {webhookPayload.Repository.FullName}"));
            }

            return(await execute(webhookPayload, repoSettings));
        }
 public async Task <IGitHubClient> CreateGitHubClientAsync(string owner, string repo)
 {
     return(_clientFactory.CreateGitHubClient(await _tokenProvider.GetTokenForRepository(owner, repo)));
 }