コード例 #1
0
        static async Task <IActionResult> CleanupOldApp(HttpRequest req, dynamic data)
        {
            var        repData = data.repository;
            var        id      = Database.CleanseName(repData.full_name.ToString());
            GithubRepo repo;

            try{
                repo = await Database.GetRepo(id);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                repo = new GithubRepo
                {
                    Id        = id,
                    Owner     = repData.owner.login,
                    RepoName  = repData.Name,
                    CloneUrl  = repData.clone_url,
                    IsPrivate = repData.@private,
                    AzureData =
                    {
                        Location = "southcentralus",
                    }
                };
                await Database.CreateRepo(repo);
            }

            var    pullRequest = data.pull_request;
            string statusUrl   = pullRequest.statuses_url;

            var isAuthenticated = await AzureApi.Authenticate(repo);

            if (!isAuthenticated)
            {
                var paring = await Database.GetOrCreatePairingRequestByRepoId(id);

                var orgUrl = req.Host.Value;
                var url    = $"{req.Scheme}://{orgUrl}/api/RegisterRepo?id={paring.Id}";
                await GithubApi.PostStatus(repo, statusUrl, false, url, $"You need to log into Azure before auto deploy will work. {url}");

                return(new BadRequestObjectResult($"Please go to {url} to register the app with azure."));
            }

            if (repo.Builds == null)
            {
                repo.Builds = new List <Build>();
            }
            var number = (int)data.number;
            var build  = repo.Builds?.FirstOrDefault(x => x.PullRequestId == number);

            if (!string.IsNullOrWhiteSpace(build?.AzureAppId))
            {
                await AzureApi.DeleteWebApp(repo, build);

                await AzureApi.DeleteAppService(repo, build);
            }
            return(new OkResult());
        }
コード例 #2
0
        static async Task <IActionResult> BuildPullRequest(HttpRequest req, dynamic data)
        {
            var        repData = data.repository;
            var        id      = Database.CleanseName(repData.full_name.ToString());
            GithubRepo repo;

            try{
                repo = await Database.GetRepo(id);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                repo = new GithubRepo
                {
                    Id        = id,
                    Owner     = repData.owner.login,
                    RepoName  = repData.Name,
                    CloneUrl  = repData.clone_url,
                    IsPrivate = repData.@private,
                    AzureData =
                    {
                        Location = "southcentralus",
                    }
                };
                await Database.CreateRepo(repo);
            }

            var    pullRequest           = data.pull_request;
            string statusUrl             = pullRequest.statuses_url;
            var    isGithubAuthenticated = await GithubApi.Authenticate(repo);

            if (!isGithubAuthenticated)
            {
                var paring = await Database.GetOrCreatePairingRequestByRepoId(id);

                var orgUrl = req.Host.Value;
                var url    = $"{req.Scheme}://{orgUrl}/api/RegisterRepo?id={paring.Id}";
                return(new BadRequestObjectResult($"Please go to {url} to register the app with Github."));
            }

            var isAuthenticated = await AzureApi.Authenticate(repo);

            if (!isAuthenticated)
            {
                var paring = await Database.GetOrCreatePairingRequestByRepoId(id);

                var orgUrl = req.Host.Value;
                var url    = $"{req.Scheme}://{orgUrl}/api/RegisterRepo?id={paring.Id}";
                await GithubApi.PostStatus(repo, statusUrl, false, url, $"You need to log into Azure before auto deploy will work. {url}");

                return(new BadRequestObjectResult($"Please go to {url} to register the app with azure."));
            }

            if (repo.Builds == null)
            {
                repo.Builds = new List <Build>();
            }
            var number = (int)data.number;
            var build  = repo.Builds?.FirstOrDefault(x => x.PullRequestId == number);

            if (build == null)
            {
                build = new Build {
                    PullRequestId = number,
                };
                repo.Builds.Add(build);
            }
            build.CommitHash = pullRequest.head.sha;
            build.Branch     = pullRequest.head.@ref;
            build.StatusUrl  = statusUrl;
            string buildUrl = pullRequest.head.repo.clone_url;

            if (repo.CloneUrl != buildUrl)
            {
                build.GitUrl = buildUrl;
            }
            //Make sure we have a resource group!
            repo.AzureData.ResourceGroup = await AzureApi.GetOrCreateResourceGroup(repo);

            bool shouldSave = string.IsNullOrWhiteSpace(build.AzureAppId);

            build.AzureAppId = await AzureApi.CreateAppService(repo, build);

            if (shouldSave)
            {
                await Database.Save(repo);
            }
            await AzureApi.CreateWebApp(repo, build);

            build.DeployedUrl = $"http://{build.AzureAppId}.azurewebsites.net";
            var success = await AzureApi.PublishPullRequst(repo, build);

            await GithubApi.PostStatus(repo, statusUrl, success, build.DeployedUrl, success?$"Deployment was successful! {build.DeployedUrl}" : "There was an error deploying");

            await Database.Save(repo);

            return(new OkResult());
        }