Esempio n. 1
0
        /// <summary>
        /// Takes the github message and saves it to the db.
        /// </summary>
        /// <param name="message">The github message that was sent.</param>
        /// <returns>An actionresult depending upon how it went.</returns>
        public ActionResult AddGithubMessage(GithubPush message)
        {
            using (var transaction = _buildDBContext.Database.BeginTransaction())
            {
                try
                {
                    var commit = new Database.Entities.Github.Commit
                    {
                        CommitMessage = JsonConvert.SerializeObject(message)
                    };

                    _buildDBContext.Add(commit);
                    _buildDBContext.SaveChanges();

                    transaction.Commit();
                    return(new OkObjectResult(new { message = "200 OK", currentDate = DateTime.Now }));
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    return(new BadRequestObjectResult(new { message = "500 Server Error", currentDate = DateTime.Now }));
                }
            }
        }
Esempio n. 2
0
 public ActionResult Update([FromBody] GithubPush githubPush)
 {
     return(_githubService.AddGithubMessage(githubPush));
 }
Esempio n. 3
0
        public static bool CreateAssembly(GithubPush payload, DateTime time)
        {
            var    buildref = payload.Ref;
            string branch   = buildref.Remove(0, 11);

            var asmBaseDir = Environment.GetEnvironmentVariable(AsmDirVar);
            var msbuildDir = Environment.GetEnvironmentVariable(AsmMSBuildVar);
            var gitPath    = Environment.GetEnvironmentVariable(AsmGitVar);

            if (asmBaseDir == null || msbuildDir == null || gitPath == null)
            {
                return(false);
            }

            var asmWorkingDir = Path.Combine(asmBaseDir, "asmbuilds");
            var asmStorageDir = Path.Combine(asmBaseDir, "downloads");

            string pullDirectory = GetBuild(asmWorkingDir, branch, time, gitPath);

            if (pullDirectory == null)
            {
                return(false);
            }
            try
            {
                if (!CopyDependencies(asmWorkingDir, pullDirectory) ||
                    !CompileProgram(msbuildDir, pullDirectory, "Assembly", asmWorkingDir) ||
                    !CompileProgram(msbuildDir, pullDirectory, "AssemblyUpdateManager", asmWorkingDir) ||
                    !AddVersionInfo(pullDirectory, time, branch) ||
                    !CleanupLocalizations(pullDirectory) ||
                    !BuildPackage(pullDirectory, time, branch, asmStorageDir, "Assembly", "Assembly", false) ||
                    !BuildPackage(pullDirectory, time, branch, asmStorageDir, "Assembly", "AssemblyUpdateManager", true))
                {
                    return(false);
                }

                var changelog = payload.Created ? "Initial Commit" : ParseChangelog(pullDirectory, payload.Compare, gitPath);
                if (changelog == null)
                {
                    return(false);
                }

                lock (typeof(AssemblyBuilder))
                {
                    using (var db = new DatabaseContext())
                    {
                        var friendlyVersion   = String.Format(time.ToString("yyyy.MM.dd.HH.mm.ss") + "-{0}", branch);
                        var internalVersion   = GetInternalVersion(pullDirectory);
                        var repoTree          = String.Format("{0}/tree/{1}", "Assembly", branch);
                        var applicationBranch = db.ApplicationBranches.FirstOrDefault(b => b.Ref == buildref && b.Application.RepoName == "Assembly");

                        if (applicationBranch != null && (applicationBranch.InternalVersion == null || new Version(applicationBranch.InternalVersion).CompareTo(new Version(internalVersion)) < 0))
                        {
                            var application = applicationBranch.Application;

#if AZURE
                            var buildLink = String.Format("https://xbcapi.blob.core.windows.net/assembly/{0}/builds/{1}.zip", branch,
                                                          time.ToFileTimeUtc());
                            var updaterLink = String.Format("https://xbcapi.blob.core.windows.net/assembly/{0}/updaters/{1}.zip", branch,
                                                            time.ToFileTimeUtc());
#else
                            var buildLink = String.Format("http://tj.ngrok.com/downloads/{0}/{1}.zip",
                                                          string.Format("{0}/tree/{1}/builds", "Assembly", branch), time.ToFileTimeUtc());
                            var updaterLink = String.Format("http://tj.ngrok.com/downloads/{0}/{1}.zip",
                                                            string.Format("{0}/tree/{1}/updaters", "Assembly", branch), time.ToFileTimeUtc());
#endif

                            var appBranch = new ApplicationBranch
                            {
                                Application     = application,
                                Name            = branch,
                                Ref             = buildref,
                                RepoTree        = repoTree,
                                BuildDownload   = buildLink,
                                UpdaterDownload = updaterLink,
                                FriendlyVersion = friendlyVersion,
                                InternalVersion = internalVersion,
                            };

                            db.ApplicationBranches.AddOrUpdate(b => b.RepoTree, appBranch);
                            db.SaveChanges();
                        }
                        else
                        {
                            File.Delete(Path.Combine(asmStorageDir, "Assembly", "tree", branch, "builds", time.ToFileTimeUtc() + ".zip"));
                            File.Delete(Path.Combine(asmStorageDir, "Assembly", "tree", branch, "updaters", time.ToFileTimeUtc() + ".zip"));
                        }

                        var changelogOwner =
                            db.ApplicationBranches.FirstOrDefault(a => a.RepoTree == repoTree && a.Application.RepoName == "Assembly");
                        if (changelogOwner != null)
                        {
                            var changes = new Changelog()
                            {
                                Changes         = changelog,
                                FriendlyVersion = friendlyVersion,
                                InternalVersion = internalVersion,
                                Branch          = changelogOwner
                            };
                            db.Changelogs.AddOrUpdate(changes);
                            db.SaveChanges();
                        }
                    }
                }
                return(true);
            }
            finally
            {
                CleanupExtraZips(asmStorageDir, branch, "Assembly");
                DirectoryUtility.DeleteDirectory(pullDirectory, true);
            }
        }