Beispiel #1
0
        public bool InitGogSource()
        {
            var     owner = this.Accounts.FirstOrDefault(x => x.AccessLevels != null && x.AccessLevels.Contains(AccessLevels.Owner.ToString()));
            Account accountAsOwner;

            if (owner == null)
            {
                this.EwhStatus = GlobalStatus.HaveNoAnOwner;
                return(false);
            }
            else
            {
                accountAsOwner = _accountRepository.Get(owner.AccountId);
                if (accountAsOwner == null)
                {
                    this.EwhStatus = GlobalStatus.HaveNoAnOwner;
                    return(false);
                }
            }

            var ewhSource = new EwhSource();

            // create source
            if (ewhSource.CreateRepository(accountAsOwner.UserName, this.RepositoryName))
            {
                this.Source = ewhSource.RepositoryAdded.Url;
                this.Save();
                return(true);
            }
            else
            {
                this.EwhStatus = GlobalStatus.UnSuccess;
            }
            return(false);
        }
        public IHttpActionResult ConfirmWebsite(string websiteId)
        {
            ew.common.EwhLogger.Common.Debug("START CONFIRM WEBSITE: " + websiteId);
            if (ModelState.IsValid)
            {
                var ewhWebsite = _websiteManager.GetEwhWebsite(websiteId);
                if (ewhWebsite == null)
                {
                    return(NotFound());
                }
                var githubManager = new GitHubManager();
                Octokit.Repository gitRepository = null;
                //create github repository
                if (string.IsNullOrEmpty(ewhWebsite.Git))
                {
                    if (githubManager.CreateRepository(ewhWebsite.RepositoryName, ewhWebsite.DisplayName).Result)
                    {
                        gitRepository  = githubManager.RepositoryAdded;
                        ewhWebsite.Git = githubManager.RepositoryAdded.CloneUrl;
                        ewhWebsite.Save();
                    }
                    else
                    {
                        EwhLogger.Common.Debug(string.Format("Create Repository Failed: {0} - {1}", ewhWebsite.RepositoryName, ewhWebsite.DisplayName));
                        return(NoOK("Can not create github repository"));
                    }
                }
                else
                {
                    gitRepository = githubManager.GetRepository(repoName: ewhWebsite.RepositoryName).Result;
                }

                if (string.IsNullOrEmpty(ewhWebsite.Source))
                {
                    ewhWebsite.InitGogSource();
                }
                if (string.IsNullOrEmpty(ewhWebsite.Source))
                {
                    return(NoOK("Source_Empty"));
                }

                // add web-hook to demo & production server
                var ewhGogsSource     = new EwhSource();
                var ewhAccountAsOwner = _accountManager.GetEwhAccount(ewhWebsite.GetOwnerId());
                if (ewhAccountAsOwner != null)
                {
                    ewhGogsSource.CreateWebHook(new EwhSource.CreateWebHookDto(ewhAccountAsOwner.UserName, ewhWebsite.RepositoryName));

                    ewhGogsSource.CreateWebHook(new EwhSource.CreateWebHookDto(ewhAccountAsOwner.UserName, ewhWebsite.RepositoryName, ew.config.ProductionServer.WebHookUrl, ew.config.ProductionServer.SecretKey));
                }

                // create sub domain
                var websiteDomain     = string.Empty;
                var subDomainName     = string.Format("{0}-{1}.{2}", ewhAccountAsOwner.UserName, ewhWebsite.Name, ew.config.CloudflareInfo.EwhDomain).ToLower();
                var cloudflareManager = new CloudflareManager();
                if (cloudflareManager.CreateDNSRecord(ew.config.CloudflareInfo.EwhCloudflareZoneId, new cloudflare_wrapper.Models.UpdateDNSRecordDto()
                {
                    Type = "CNAME", Name = subDomainName, Content = "easywebhub.github.io"
                }))
                {
                    websiteDomain = cloudflareManager.DNSRecordAdded.name;
                }
                else
                {
                }

                if (gitRepository != null)
                {
                    var gitUrlIncludePass  = githubManager.GetGitUrlIncludePassword(ewhWebsite.Git);
                    var sourceRepoUrl      = ewhWebsite.Source;
                    var ewhGitHookListener = new EwhGitHookListener();
                    var demoGitHook        = new git_hook_listener.Models.CreateGitHookListenerConfigDto()
                    {
                        GitHookListenerBaseUrl = ew.config.DemoServer.BaseUrl,
                        repoUrl     = sourceRepoUrl,
                        branch      = "gh-pages",
                        cloneBranch = "gh-pages",
                        path        = string.Format("repositories/{0}", gitRepository.Name),
                        args        = new List <string>(),
                        then        = new List <git_hook_listener.Models.RepoCommand>()
                    };
                    demoGitHook.then.Add(new git_hook_listener.Models.RepoCommand()
                    {
                        command = "git", args = new List <string>()
                        {
                            "remote", "add", "github", gitUrlIncludePass
                        }, options = new git_hook_listener.Models.RepoCommandOption()
                        {
                            cwd = demoGitHook.path
                        }
                    });
                    demoGitHook.then.Add(new git_hook_listener.Models.RepoCommand()
                    {
                        command = "git", args = new List <string>()
                        {
                            "push", "--force", "github", "HEAD:gh-pages"
                        }, options = new git_hook_listener.Models.RepoCommandOption()
                        {
                            cwd = demoGitHook.path
                        }
                    });

                    if (ewhGitHookListener.CreateGitHookListernerConfig(demoGitHook))
                    {
                        ewhWebsite.AddStagging(new UpdateDeploymentEnvironmentToWebsite()
                        {
                            Url = websiteDomain, Git = sourceRepoUrl, HostingFee = HostingFees.Free.ToString(), Name = "EasyWeb Environment"
                        });
                    }

                    var productionGitHook = new git_hook_listener.Models.CreateGitHookListenerConfigDto()
                    {
                        GitHookListenerBaseUrl = ew.config.ProductionServer.BaseUrl,
                        repoUrl     = sourceRepoUrl,
                        branch      = "master",
                        cloneBranch = "master",
                        path        = string.Format("repositories/{0}", gitRepository.Name)
                    };
                    productionGitHook.then.Add(new git_hook_listener.Models.RepoCommand()
                    {
                        command = "git", args = new List <string>()
                        {
                            "remote", "add", "github", gitUrlIncludePass
                        }, options = new git_hook_listener.Models.RepoCommandOption()
                        {
                            cwd = productionGitHook.path
                        }
                    });
                    productionGitHook.then.Add(new git_hook_listener.Models.RepoCommand()
                    {
                        command = "git", args = new List <string>()
                        {
                            "push", "--force", "github", "gh-pages"
                        }, options = new git_hook_listener.Models.RepoCommandOption()
                        {
                            cwd = productionGitHook.path
                        }
                    });

                    if (ewhGitHookListener.CreateGitHookListernerConfig(productionGitHook))
                    {
                        ewhWebsite.AddProduction(new UpdateDeploymentEnvironmentToWebsite()
                        {
                            Git = sourceRepoUrl, HostingFee = HostingFees.Basic.ToString(), Name = "Production Enviroment"
                        });
                    }
                }
                return(Ok(new WebsiteDetailDto(ewhWebsite)));
            }
            return(BadRequest());
        }