Пример #1
0
        private static async Task <string> ProcessMarketplacePurchaseAsync(Hook hook, CloudTable marketplaceTable, ILogger logger)
        {
            switch (hook.action)
            {
            case "purchased":
                await marketplaceTable.ExecuteAsync(TableOperation.InsertOrMerge(new Marketplace(hook.marketplace_purchase.account.id, hook.marketplace_purchase.account.login)
                {
                    AccountType = hook.marketplace_purchase.account.type,
                    PlanId      = hook.marketplace_purchase.plan.id,
                    SenderId    = hook.sender.id,
                    SenderLogin = hook.sender.login,
                }));

                logger.LogInformation("ProcessMarketplacePurchaseAsync/purchased for {Owner}", hook.marketplace_purchase.account.login);

                return("purchased");

            case "cancelled":
                await marketplaceTable.DropRow(hook.marketplace_purchase.account.id, hook.marketplace_purchase.account.login);

                logger.LogInformation("ProcessMarketplacePurchaseAsync/cancelled for {Owner}", hook.marketplace_purchase.account.login);
                return("cancelled");

            default:
                return(hook.action);
            }
        }
Пример #2
0
        private static async Task <string> ProcessInstallationAsync(Hook hook, CloudQueue routerMessages, CloudTable installationTable, ILogger logger)
        {
            switch (hook.action)
            {
            case "created":
                foreach (var repo in hook.repositories)
                {
                    await routerMessages.AddMessageAsync(new CloudQueueMessage(JsonConvert.SerializeObject(new RouterMessage
                    {
                        InstallationId = hook.installation.id,
                        Owner = hook.installation.account.login,
                        RepoName = repo.name,
                        CloneUrl = $"https://github.com/{repo.full_name}",
                    })));

                    logger.LogInformation("ProcessInstallationAsync/created: Added RouterMessage for {Owner}/{RepoName}", repo.owner, repo.name);
                }

                break;

            case "added":
                foreach (var repo in hook.repositories_added)
                {
                    await routerMessages.AddMessageAsync(new CloudQueueMessage(JsonConvert.SerializeObject(new RouterMessage
                    {
                        InstallationId = hook.installation.id,
                        Owner = hook.installation.account.login,
                        RepoName = repo.name,
                        CloneUrl = $"https://github.com/{repo.full_name}",
                    })));

                    logger.LogInformation("ProcessInstallationAsync/added: Added RouterMessage for {Owner}/{RepoName}", repo.owner, repo.name);
                }

                break;

            case "removed":
                foreach (var repo in hook.repositories_removed)
                {
                    await installationTable.DropRow(hook.installation.id.ToString(), repo.name);

                    logger.LogInformation("ProcessInstallationAsync/removed: DropRow for {InstallationId} :: {RepoName}", hook.installation.id, repo.name);
                }

                break;

            case "deleted":
                await installationTable.DropPartitionAsync(hook.installation.id.ToString());

                logger.LogInformation("ProcessInstallationAsync/deleted: DropPartition for {InstallationId}", hook.installation.id);
                break;
            }

            return("truth");
        }
Пример #3
0
        private static async Task <string> ProcessMarketplacePurchaseAsync(Hook hook, CloudTable marketplaceTable, ILogger logger)
        {
            switch (hook.action)
            {
            case "changed":
            case "purchased":
                int?allowedPrivate = null;
                var limitedPlans   = KnownGitHubs.Plans.Keys.Where(k => KnownGitHubs.Plans[k] >= KnownGitHubs.SmallestLimitPaidPlan);
                if (limitedPlans.Contains(hook.marketplace_purchase.plan.id))
                {
                    allowedPrivate = KnownGitHubs.Plans[hook.marketplace_purchase.plan.id];
                }

                await marketplaceTable.ExecuteAsync(TableOperation.InsertOrMerge(new Marketplace(hook.marketplace_purchase.account.id, hook.marketplace_purchase.account.login)
                {
                    AccountType = hook.marketplace_purchase.account.type,
                    SenderEmail = hook.sender.email,
                    OrganizationBillingEmail = hook.marketplace_purchase.account.organization_billing_email,
                    PlanId         = hook.marketplace_purchase.plan.id,
                    SenderId       = hook.sender.id,
                    SenderLogin    = hook.sender.login,
                    AllowedPrivate = allowedPrivate,
                    UsedPrivate    = 0,
                }));

                logger.LogInformation("ProcessMarketplacePurchaseAsync/purchased {PlanId} for {Owner}", hook.marketplace_purchase.plan.id, hook.marketplace_purchase.account.login);

                return(hook.action);

            case "cancelled":
                await marketplaceTable.DropRow(hook.marketplace_purchase.account.id, hook.marketplace_purchase.account.login);

                logger.LogInformation("ProcessMarketplacePurchaseAsync/cancelled {PlanId} for {Owner}", hook.marketplace_purchase.plan.id, hook.marketplace_purchase.account.login);
                return("cancelled");

            default:
                return(hook.action);
            }
        }
Пример #4
0
        private static async Task <string> ProcessInstallationAsync(Hook hook, CloudTable marketplaceTable, CloudQueue routerMessages, CloudTable installationTable, ILogger logger)
        {
            var isPrivateEligible = false;

            if (hook.repositories?.Any(x => x.@private) == true || hook.repositories_added?.Any(x => x.@private) == true)
            {
                isPrivateEligible = await IsPrivateEligible(marketplaceTable, hook.installation.account.login);
            }

            switch (hook.action)
            {
            case "created":
                foreach (var repo in hook.repositories)
                {
                    if (repo.@private && !isPrivateEligible)
                    {
                        logger.LogError("ProcessInstallationAsync/added: Plan mismatch for {Owner}/{RepoName}", hook.installation.account.login, repo.name);
                        continue;
                    }

                    await routerMessages.AddMessageAsync(new CloudQueueMessage(JsonConvert.SerializeObject(new RouterMessage
                    {
                        InstallationId = hook.installation.id,
                        Owner = hook.installation.account.login,
                        RepoName = repo.name,
                        CloneUrl = $"https://github.com/{repo.full_name}",
                    })));

                    logger.LogInformation("ProcessInstallationAsync/created: Added RouterMessage for {Owner}/{RepoName}", hook.installation.account.login, repo.name);
                }

                break;

            case "added":
                foreach (var repo in hook.repositories_added)
                {
                    if (repo.@private && !isPrivateEligible)
                    {
                        logger.LogError("ProcessInstallationAsync/added: Plan mismatch for {Owner}/{RepoName}", hook.installation.account.login, repo.name);
                        continue;
                    }

                    await routerMessages.AddMessageAsync(new CloudQueueMessage(JsonConvert.SerializeObject(new RouterMessage
                    {
                        InstallationId = hook.installation.id,
                        Owner = hook.installation.account.login,
                        RepoName = repo.name,
                        CloneUrl = $"https://github.com/{repo.full_name}",
                    })));

                    logger.LogInformation("ProcessInstallationAsync/added: Added RouterMessage for {Owner}/{RepoName}", hook.installation.account.login, repo.name);
                }

                break;

            case "removed":
                foreach (var repo in hook.repositories_removed)
                {
                    await installationTable.DropRow(hook.installation.id.ToString(), repo.name);

                    logger.LogInformation("ProcessInstallationAsync/removed: DropRow for {InstallationId} :: {RepoName}", hook.installation.id, repo.name);
                }

                break;

            case "deleted":
                await installationTable.DropPartitionAsync(hook.installation.id.ToString());

                logger.LogInformation("ProcessInstallationAsync/deleted: DropPartition for {InstallationId}", hook.installation.id);
                break;
            }

            return("truth");
        }
Пример #5
0
        private static async Task <string> ProcessInstallationAsync(Hook hook, CloudTable marketplaceTable, CloudQueue routerMessages, CloudTable installationTable, ILogger logger)
        {
            var isPrivateEligible = false;
            var isOnAddedPlan     = false;
            int?usedPrivate       = 0;
            int?allowedPrivate    = 0;
            var privateRepo       = hook.repositories?.Any(x => x.@private) == true || hook.repositories_added?.Any(x => x.@private) == true;

            if (privateRepo)
            {
                (isOnAddedPlan, allowedPrivate, usedPrivate) = await IsOnAddedPlan(marketplaceTable, hook.installation.account.login);
            }

            if (!isOnAddedPlan && privateRepo)
            {
                isPrivateEligible = await IsPrivateEligible(marketplaceTable, hook.installation.account.login);
            }

            switch (hook.action)
            {
            case "created":

                foreach (var repo in hook.repositories)
                {
                    if (repo.@private && !isPrivateEligible && !isOnAddedPlan)
                    {
                        logger.LogError("ProcessInstallationAsync/added: Plan mismatch for {Owner}/{RepoName}", hook.installation.account.login, repo.name);
                        continue;
                    }

                    var compress = true;
                    if (repo.@private && isOnAddedPlan)
                    {
                        compress = false;
                        if (usedPrivate < allowedPrivate)
                        {
                            usedPrivate++;
                            await marketplaceTable.ExecuteAsync(TableOperation.InsertOrMerge(new Marketplace(hook.installation.account.id, hook.installation.account.login)
                            {
                                UsedPrivate = usedPrivate,
                            }));

                            compress = true;
                        }
                    }

                    await routerMessages.AddMessageAsync(new CloudQueueMessage(JsonConvert.SerializeObject(new RouterMessage
                    {
                        InstallationId = hook.installation.id,
                        Owner = hook.installation.account.login,
                        RepoName = repo.name,
                        CloneUrl = $"https://github.com/{repo.full_name}",
                        IsPrivate = repo.@private,
                        Compress = compress,
                    })));

                    logger.LogInformation("ProcessInstallationAsync/created: Added RouterMessage for {Owner}/{RepoName}", hook.installation.account.login, repo.name);
                }

                break;

            case "added":

                foreach (var repo in hook.repositories_added)
                {
                    if (repo.@private && !isPrivateEligible && !isOnAddedPlan)
                    {
                        logger.LogError("ProcessInstallationAsync/added: Plan mismatch for {Owner}/{RepoName}", hook.installation.account.login, repo.name);
                        continue;
                    }

                    var compress = true;
                    if (repo.@private && isOnAddedPlan)
                    {
                        compress = false;
                        if (usedPrivate < allowedPrivate)
                        {
                            usedPrivate++;
                            await marketplaceTable.ExecuteAsync(TableOperation.InsertOrMerge(new Marketplace(hook.installation.account.id, hook.installation.account.login)
                            {
                                UsedPrivate = usedPrivate,
                            }));

                            compress = true;
                        }
                    }

                    await routerMessages.AddMessageAsync(new CloudQueueMessage(JsonConvert.SerializeObject(new RouterMessage
                    {
                        InstallationId = hook.installation.id,
                        Owner = hook.installation.account.login,
                        RepoName = repo.name,
                        CloneUrl = $"https://github.com/{repo.full_name}",
                        IsPrivate = repo.@private,
                        Compress = compress,
                    })));

                    logger.LogInformation("ProcessInstallationAsync/added: Added RouterMessage for {Owner}/{RepoName}", hook.installation.account.login, repo.name);
                }

                break;

            case "removed":

                foreach (var repo in hook.repositories_removed)
                {
                    await installationTable.DropRow(hook.installation.id.ToString(), repo.name);

                    logger.LogInformation("ProcessInstallationAsync/removed: DropRow for {InstallationId} :: {RepoName}", hook.installation.id, repo.name);
                }

                break;

            case "deleted":

                await installationTable.DropPartitionAsync(hook.installation.id.ToString());

                logger.LogInformation("ProcessInstallationAsync/deleted: DropPartition for {InstallationId}", hook.installation.id);
                break;
            }

            return("truth");
        }
Пример #6
0
        private static async Task <string> ProcessMarketplacePurchaseAsync(Hook hook, CloudTable marketplaceTable, CloudQueue backupMessages, ILogger logger)
        {
            switch (hook.action)
            {
            case "changed":
            case "purchased":
                int?allowedPrivate = null;
                var limitedPlans   = KnownGitHubs.Plans.Keys.Where(k => KnownGitHubs.Plans[k] >= KnownGitHubs.SmallestLimitPaidPlan);
                if (limitedPlans.Contains(hook.marketplace_purchase.plan.id))
                {
                    allowedPrivate = KnownGitHubs.Plans[hook.marketplace_purchase.plan.id];
                }

                if (hook.marketplace_purchase.on_free_trial != true && KnownGitHubs.Plans.ContainsKey(hook.marketplace_purchase.plan.id) && KnownGitHubs.Plans[hook.marketplace_purchase.plan.id] != 0)
                {
                    var mktplc = await marketplaceTable.ExecuteAsync(
                        TableOperation.Retrieve <Marketplace>(hook.marketplace_purchase.account.id.ToString(), hook.marketplace_purchase.account.login));

                    var recurrentSale = "new";

                    if (mktplc?.Result != null)
                    {
                        if ((mktplc.Result as Marketplace)?.FreeTrial == true)
                        {
                            recurrentSale = "trial_upgrade";
                        }
                        else
                        {
                            var previousPlan = (mktplc.Result as Marketplace)?.PlanId ?? 0;

                            if (previousPlan != 0 && KnownGitHubs.Plans.ContainsKey(previousPlan) &&
                                KnownGitHubs.Plans[previousPlan] != 0)
                            {
                                recurrentSale = "recurrent";
                                if (KnownGitHubs.Plans[previousPlan] <
                                    KnownGitHubs.Plans[hook.marketplace_purchase.plan.id])
                                {
                                    recurrentSale = "upgrade_" + previousPlan.ToString();
                                }
                            }
                        }
                    }

                    await backupMessages.AddMessageAsync(new CloudQueueMessage(JsonConvert.SerializeObject(
                                                                                   new BackupMessage
                    {
                        PlanId = hook.marketplace_purchase.plan.id,
                        Price = hook.marketplace_purchase.plan.monthly_price_in_cents / 100,
                        SaleType = recurrentSale
                    })));
                }

                await marketplaceTable.ExecuteAsync(TableOperation.InsertOrMerge(new Marketplace(hook.marketplace_purchase.account.id, hook.marketplace_purchase.account.login)
                {
                    AccountType = hook.marketplace_purchase.account.type,
                    SenderEmail = hook.sender.email,
                    OrganizationBillingEmail = hook.marketplace_purchase.account.organization_billing_email,
                    PlanId         = hook.marketplace_purchase.plan.id,
                    SenderId       = hook.sender.id,
                    SenderLogin    = hook.sender.login,
                    AllowedPrivate = allowedPrivate,
                    UsedPrivate    = 0,
                    FreeTrial      = hook.marketplace_purchase.on_free_trial,
                }));

                logger.LogInformation("ProcessMarketplacePurchaseAsync/purchased {PlanId} for {Owner}", hook.marketplace_purchase.plan.id, hook.marketplace_purchase.account.login);

                return(hook.action);

            case "cancelled":
                await marketplaceTable.DropRow(hook.marketplace_purchase.account.id, hook.marketplace_purchase.account.login);

                logger.LogInformation("ProcessMarketplacePurchaseAsync/cancelled {PlanId} for {Owner}", hook.marketplace_purchase.plan.id, hook.marketplace_purchase.account.login);
                return("cancelled");

            default:
                return(hook.action);
            }
        }
Пример #7
0
 public static Task <bool> DropRow(this CloudTable table, int partitionKey, string rowKey)
 {
     return(table.DropRow(partitionKey.ToString(), rowKey));
 }