Пример #1
0
        private Task ExecuteRunAsync(OpenPrMessage openPrMessage, Installation installation, long prId, out ILogger logger)
        {
            logger = Substitute.For <ILogger>();

            var context = Substitute.For <ExecutionContext>();

            context.FunctionDirectory = "data/functiondir";

            var installationTokenProvider = Substitute.For <IInstallationTokenProvider>();

            installationTokenProvider
            .GenerateAsync(Arg.Any <InstallationTokenParameters>(), Arg.Any <string>())
            .Returns(Task.FromResult(new InstallationToken
            {
                Token     = "token",
                ExpiresAt = "12345"
            }));

            var pullRequest = Substitute.For <IPullRequest>();

            pullRequest.OpenAsync(Arg.Any <GitHubClientParameters>()).Returns(x => Task.FromResult(new Pr(installation.Owner)
            {
                Id = prId
            }));

            var settingsTable = Substitute.For <CloudTable>(new Uri("https://myaccount.table.core.windows.net/Tables/settings"));

            var prs = Substitute.For <ICollector <Pr> >();

            return(OpenPr.RunAsync(openPrMessage, installation, prs, settingsTable, installationTokenProvider, pullRequest, logger, context));
        }
Пример #2
0
        public static async Task Run(
            [QueueTrigger("openprmessage")] OpenPrMessage openPrMessage,
            [Table("installation", "{InstallationId}", "{RepoName}")] Installation installation,
            ILogger logger,
            ExecutionContext context)
        {
            if (installation == null)
            {
                logger.LogError("No installation found for {InstallationId}", installation.InstallationId);
                throw new Exception($"No installation found for InstallationId: {installation.InstallationId}");
            }

            var installationTokenParameters = new InstallationTokenParameters
            {
                AccessTokensUrl = string.Format(KnownGitHubs.AccessTokensUrlFormat, installation.InstallationId),
                AppId           = KnownGitHubs.AppId,
            };

            var installationToken = await InstallationToken.GenerateAsync(
                installationTokenParameters,
                File.OpenText(Path.Combine(context.FunctionDirectory, $"../{KnownGitHubs.AppPrivateKey}")));

            logger.LogInformation("OpenPrFunction: Opening pull request for {Owner}/{RepoName}", installation.Owner, installation.RepoName);
            var id = await PullRequest.OpenAsync(new PullRequestParameters
            {
                Password  = installationToken.Token,
                RepoName  = installation.RepoName,
                RepoOwner = installation.Owner,
            });

            if (id > 0)
            {
                logger.LogInformation("OpenPrFunction: Successfully opened pull request (#{PullRequestId}) for {Owner}/{RepoName}", id, installation.Owner, installation.RepoName);
            }
        }
Пример #3
0
        public static async Task Run(
            [QueueTrigger("openprmessage")] OpenPrMessage openPrMessage,
            [Table("installation", "{InstallationId}", "{RepoName}")] Installation installation,
            TraceWriter log,
            ExecutionContext context)
        {
            if (installation == null)
            {
                throw new Exception($"No installation found for InstallationId: {installation.InstallationId}");
            }

            var installationTokenParameters = new InstallationTokenParameters
            {
                AccessTokensUrl = installation.AccessTokensUrl,
                AppId           = KnownGitHubs.AppId,
            };

            var installationToken = await InstallationToken.GenerateAsync(
                installationTokenParameters,
                File.OpenText(Path.Combine(context.FunctionDirectory, $"../{KnownGitHubs.AppPrivateKey}")));

            await PullRequest.OpenAsync(new PullRequestParameters
            {
                Password  = installationToken.Token,
                RepoName  = installation.RepoName,
                RepoOwner = installation.Owner,
            });
        }
Пример #4
0
        public static async Task RunAsync(
            OpenPrMessage openPrMessage,
            Installation installation,
            IInstallationTokenProvider installationTokenProvider,
            IPullRequest pullRequest,
            ILogger logger,
            ExecutionContext context)
        {
            if (installation == null)
            {
                logger.LogError("No installation found for {InstallationId}", openPrMessage.InstallationId);
                throw new Exception($"No installation found for InstallationId: {openPrMessage.InstallationId}");
            }

            var installationToken = await installationTokenProvider.GenerateAsync(
                new InstallationTokenParameters
            {
                AccessTokensUrl = string.Format(KnownGitHubs.AccessTokensUrlFormat, installation.InstallationId),
                AppId           = KnownGitHubs.AppId,
            },
                KnownEnvironmentVariables.APP_PRIVATE_KEY);

            logger.LogInformation("OpenPrFunction: Opening pull request for {Owner}/{RepoName}", installation.Owner, installation.RepoName);
            var id = await pullRequest.OpenAsync(new GitHubClientParameters
            {
                Password  = installationToken.Token,
                RepoName  = installation.RepoName,
                RepoOwner = installation.Owner,
            });

            if (id > 0)
            {
                logger.LogInformation("OpenPrFunction: Successfully opened pull request (#{PullRequestId}) for {Owner}/{RepoName}", id, installation.Owner, installation.RepoName);
            }
        }
Пример #5
0
        public static async Task Trigger(
            [QueueTrigger("openprmessage")] OpenPrMessage openPrMessage,
            [Table("installation", "{InstallationId}", "{RepoName}")] Installation installation,
            ILogger logger,
            ExecutionContext context)
        {
            var installationTokenProvider = new InstallationTokenProvider();
            var pullRequest = new PullRequest();

            await RunAsync(openPrMessage, installation, installationTokenProvider, pullRequest, logger, context);
        }
Пример #6
0
        public static async Task RunAsync(
            OpenPrMessage openPrMessage,
            Installation installation,
            ICollector <Pr> prs,
            CloudTable settingsTable,
            IInstallationTokenProvider installationTokenProvider,
            IPullRequest pullRequest,
            ILogger logger,
            ExecutionContext context)
        {
            if (installation == null)
            {
                logger.LogError("No installation found for {InstallationId}", openPrMessage.InstallationId);
                throw new Exception($"No installation found for InstallationId: {openPrMessage.InstallationId}");
            }

            var installationToken = await installationTokenProvider.GenerateAsync(
                new InstallationTokenParameters
            {
                AccessTokensUrl = string.Format(KnownGitHubs.AccessTokensUrlFormat, installation.InstallationId),
                AppId           = KnownGitHubs.AppId,
            },
                KnownEnvironmentVariables.APP_PRIVATE_KEY);

            logger.LogInformation("OpenPrFunction: Opening pull request for {Owner}/{RepoName}", installation.Owner, installation.RepoName);

            var settings = await SettingsHelper.GetSettings(settingsTable, installation.InstallationId, installation.RepoName);

            if (settings != null && !string.IsNullOrEmpty(settings.DefaultBranchOverride))
            {
                logger.LogInformation(
                    "OpenPrFunction: default branch override for {Owner}/{RepoName} is {DefaultBranchOverride}",
                    installation.Owner,
                    installation.RepoName,
                    settings.DefaultBranchOverride);
            }

            var result = await pullRequest.OpenAsync(
                new GitHubClientParameters
            {
                Password  = installationToken.Token,
                RepoName  = installation.RepoName,
                RepoOwner = installation.Owner,
            },
                openPrMessage.Update,
                settings);

            if (result?.Id > 0)
            {
                logger.LogInformation("OpenPrFunction: Successfully opened pull request (#{PullRequestId}) for {Owner}/{RepoName}", result.Id, installation.Owner, installation.RepoName);
                prs.Add(result);
            }
        }
Пример #7
0
 public static async Task Trigger(
     [QueueTrigger("openprmessage")]OpenPrMessage openPrMessage,
     [Table("installation", "{InstallationId}", "{RepoName}")] Installation installation,
     [Table("pull")] ICollector<Pr> prs,
     ILogger logger,
     ExecutionContext context)
 {
     var storageAccount = CloudStorageAccount.Parse(KnownEnvironmentVariables.AzureWebJobsStorage);
     var settingsTable = storageAccount.CreateCloudTableClient().GetTableReference("settings");
     var installationTokenProvider = new InstallationTokenProvider();
     var pullRequest = new PullRequest();
     await RunAsync(openPrMessage, installation, prs, settingsTable, installationTokenProvider, pullRequest, logger, context)
             .ConfigureAwait(false);
 }
Пример #8
0
        private Task ExecuteRunAsync(int installationId, string owner, string repoName, long prId, out ILogger logger)
        {
            var cloneUrl = $"https://github.com/{owner}/{repoName}";

            var openPrMessage = new OpenPrMessage
            {
                CloneUrl       = cloneUrl,
                InstallationId = installationId,
                RepoName       = repoName
            };

            var installation = new Installation
            {
                CloneUrl       = cloneUrl,
                InstallationId = installationId,
                RepoName       = repoName,
                PartitionKey   = installationId.ToString(),
                RowKey         = repoName,
                Owner          = owner
            };

            return(ExecuteRunAsync(openPrMessage, installation, prId, out logger));
        }
Пример #9
0
        private Task ExecuteRunAsync(OpenPrMessage openPrMessage, Installation installation, long prId, out ILogger logger)
        {
            logger = Substitute.For <ILogger>();

            var context = Substitute.For <ExecutionContext>();

            context.FunctionDirectory = "data/functiondir";

            var installationTokenProvider = Substitute.For <IInstallationTokenProvider>();

            installationTokenProvider
            .GenerateAsync(Arg.Any <InstallationTokenParameters>(), Arg.Any <StreamReader>())
            .Returns(Task.FromResult(new InstallationToken
            {
                Token     = "token",
                ExpiresAt = "12345"
            }));

            var pullRequest = Substitute.For <IPullRequest>();

            pullRequest.OpenAsync(Arg.Any <PullRequestParameters>()).Returns(x => Task.FromResult(prId));

            return(OpenPr.RunAsync(openPrMessage, installation, installationTokenProvider, pullRequest, logger, context));
        }