Ejemplo n.º 1
0
        public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req, TraceWriter log)
        {
            try{
                string id = req.Query["state"];
                if (id == null)
                {
                    return(new BadRequestObjectResult("Invalid state"));
                }
                string code = req.Query["code"];
                if (code == null)
                {
                    return(new BadRequestObjectResult("Invalid code"));
                }

                var pairing = await Database.GetPairingRequest(id);

                var repo = await Database.GetRepo(pairing.RepoId);

                var orgRedirect = new Uri(Microsoft.AspNetCore.Http.Extensions.UriHelper.GetEncodedUrl(req));
                var redirectUrl = $"{req.Scheme}://{req.Host.Value}/api/{nameof(SignInRedirect)}";
                var success     = await AzureApi.Authenticate(repo, code, redirectUrl);

                return(success ? (IActionResult) new RedirectResult($"{nameof(RegistrationSuccess)}?state={id}") :new BadRequestObjectResult("There was an error logging in. Please try again."));
            }
            catch (Exception e)
            {
                return(new BadRequestObjectResult(e));
            }
        }
Ejemplo n.º 2
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());
        }
Ejemplo n.º 3
0
        public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req, TraceWriter log)
        {
            try
            {
                string id = req.Query["id"];
                if (id == null)
                {
                    return(new BadRequestObjectResult("Invalid ID"));
                }

                var pairing = await Database.GetPairingRequest(id);

                if (pairing == null)
                {
                    return(new BadRequestObjectResult("Invalid ID"));
                }
                var repo = await Database.GetRepo(pairing.RepoId);

                if (string.IsNullOrWhiteSpace(repo.AzureAccount.Token))
                {
                    var redirectUrl = $"{req.Scheme}://{req.Host.Value}/api/{nameof(SignInRedirect)}";
                    redirectUrl = HttpUtility.UrlEncode(redirectUrl);
                    var url = AzureApi.AuthUrl(id, redirectUrl);
                    return(new RedirectResult(url));
                }

                if (string.IsNullOrWhiteSpace(repo.GithubAccount.Token))
                {
                    var redirectUrl = $"{req.Scheme}://{req.Host.Value}/api/{nameof(GithubSignIn)}";
                    redirectUrl = HttpUtility.UrlEncode(redirectUrl);
                    var url = GithubApi.AuthUrl(id, redirectUrl);
                    return(new RedirectResult(url));
                }

                if (string.IsNullOrWhiteSpace(repo.AzureData.Subscription))
                {
                    var redirectUrl = $"{req.Scheme}://{req.Host.Value}/api/{nameof(Settings)}?id={id}&AzureSubscriptionId=YourSubscriptionId";
                    return(new OkObjectResult($"You still need to set your Azure Subscription. Edit the URL to include your Azure subscription ID {redirectUrl} and navigate there."));
                }
                return(new RedirectResult($"{nameof(RegistrationSuccess)}?state={id}"));
            }
            catch (Exception ex)
            {
                return(new BadRequestObjectResult(ex));
            }
        }
Ejemplo n.º 4
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());
        }