Esempio n. 1
0
        static void mergeNewGitLogFileToGitRepositoryInfo(GitRepositoryInfo gitRepoInfo, EnumBranch eBranch, string gitLogFile, string buildSessionId, string buildResult, string branchName, bool hasPackageFile, bool hasBuildFailFile)
        {
            if (!File.Exists(gitLogFile))
            {
                return;
            }

            //List<GitLogElems> newLogList = parseGitLogFileIntoGitLogElemsList(gitLogFile);
            List <GitLogElems> newLogList = parseGitLogFileIntoGitLogElemsListNew(gitLogFile);

            updateFirstCommitBuildResult(newLogList, buildSessionId, buildResult, hasPackageFile, hasBuildFailFile);

            if (eBranch == EnumBranch.Master)
            {
                gitRepoInfo.master.InsertRange(0, newLogList);
            }
            else if (eBranch == EnumBranch.Tag)
            {
                if (newLogList != null && newLogList.Count > 0)
                {
                    GitLogElems gle = newLogList[0];
                    gle.commitNumber = branchName;
                    gitRepoInfo.tags.Insert(0, gle);
                }
            }
        }
Esempio n. 2
0
        static void buildCodeAndMergeResult(GitRepositoryInfo gitRepoInfo,
                                            string vmName,
                                            EnumBranch eBranch,
                                            string gitLogFilePath,
                                            string buildLogFilePath,
                                            string buildResultFilePath,
                                            string branchName,
                                            List <string> notifyEmails)
        {
            string vmExecStatus                 = string.Empty;
            string vmExecStatuFilePath          = Path.Combine(_projectWorkRootDir, _vmExecStatusFileName);
            string vmExecStatusFinishedFilePath = Path.Combine(_projectWorkRootDir, _vmExecStatusFinished);
            string vmGetBuildLogBackupFilePath  = Path.Combine(_projectWorkRootDir, _vmGetBuildLogBackupFileName);

            string        buildScriptLogFilePath             = Path.Combine(_projectWorkRootDir, _buildScriptLogFileName);
            string        buildResult                        = string.Empty;
            string        projectSharedDir                   = string.Empty;
            string        backupProjectReleasePackageDirPath = string.Empty;
            List <string> copyFileExtensionList              = new List <string>();

            copyFileExtensionList.Add(".zip");

            //Project shared folder is the folder which the host computer shares VM for each project.
            projectSharedDir = Path.Combine(_ps.localSharedDir, gitRepoInfo.project_name);
            if (!Directory.Exists(projectSharedDir))
            {
                Directory.CreateDirectory(projectSharedDir);
            }

            //The path for backing up generated packages (Can be an independant folder path).
            backupProjectReleasePackageDirPath = Path.Combine(_ps.releasePackageDirPath, gitRepoInfo.project_name);
            if (!Directory.Exists(backupProjectReleasePackageDirPath))
            {
                Directory.CreateDirectory(backupProjectReleasePackageDirPath);
            }

            if (File.Exists(vmExecStatuFilePath))
            {
                File.Delete(vmExecStatuFilePath);
            }

            if (File.Exists(buildScriptLogFilePath))
            {
                File.Delete(buildScriptLogFilePath);
            }

            if (File.Exists(vmExecStatusFinishedFilePath))
            {
                File.Delete(vmExecStatusFinishedFilePath);
            }

            //After running the VM, the git log file and the build result file will be generated
            _vboxOp.StartVMByName(vmName);
            Logger.Write(string.Format("VM : {0} gets started", vmName));

            string prevVmExecStatus = string.Empty;
            //Keep reading the VM's execution status to see if it has finished the source code building.
            int indexOfDebug = 0;

            while (true)
            {
                if (indexOfDebug > 10000)
                {
                    indexOfDebug = 0;
                }

                System.Threading.Thread.Sleep(SCAN_SLEEP_TIME_SECONDS * 1000);

                Console.Clear();
                Console.WriteLine(string.Format("debug a {0} ({1})", (++indexOfDebug).ToString(), vmExecStatus));

                if (File.Exists(vmExecStatusFinishedFilePath))
                {
                    break;
                }

                Console.WriteLine(string.Format("debug b {0} ({1})", (++indexOfDebug).ToString(), vmExecStatus));
                if (File.Exists(vmExecStatuFilePath))
                {
                    Console.WriteLine(string.Format("debug c {0} ({1})", (++indexOfDebug).ToString(), vmExecStatus));
                    vmExecStatus = getFileText(vmExecStatuFilePath);
                    Console.WriteLine(string.Format("debug d {0} ({1})", (++indexOfDebug).ToString(), vmExecStatus));
                    vmExecStatus = vmExecStatus.Trim(new char[] { ' ', '\r', '\n' });
                    Console.WriteLine(string.Format("debug f {0} ({1})", (++indexOfDebug).ToString(), vmExecStatus));
                    if (prevVmExecStatus != vmExecStatus)
                    {
                        Logger.Write(vmExecStatus);
                        prevVmExecStatus = vmExecStatus;
                    }
                    Console.WriteLine(string.Format("debug g {0} ({1})", (++indexOfDebug).ToString(), vmExecStatus));
                }
                Console.WriteLine(string.Format("debug h {0} ({1})", (++indexOfDebug).ToString(), vmExecStatus));
                //System.Threading.Thread.Sleep(SCAN_SLEEP_TIME_SECONDS * 1000);
                //Console.WriteLine(string.Format("debug i {0} ({1})", (++indexOfDebug).ToString(), vmExecStatus));
            }

            _vboxOp.StopCurrentlyRunningVMs();
            Logger.Write("VM : Shut down all running VMs");

            if (File.Exists(buildResultFilePath))
            {
                buildResult = File.ReadAllText(buildResultFilePath);
                buildResult = buildResult.Trim(new char[] { ' ', '\r', '\n' });
            }

            List <GitLogElems> newLogList = new List <GitLogElems>();

            if (File.Exists(gitLogFilePath))
            {
                newLogList = parseGitLogFileIntoGitLogElemsListNew(gitLogFilePath);
            }

            //We will get a build session id after the build
            //The build session id for master branch -> master-{commit_number}
            //The build session id for a tag branch -> tag_number
            string buildSessionId = string.Empty;

            if (File.Exists(buildLogFilePath))
            {
                buildSessionId = File.ReadAllLines(buildLogFilePath)[0];
                buildSessionId = buildSessionId.Trim(new char[] { ' ', '\r', '\n' });
            }

            //Prepare the emails list that we're going to send. *start*
            if (newLogList.Count > 0)
            {
                MailMessager.SendToList.Add(newLogList[0].authorEmail);
            }

            List <GitLabOperation.UserInfo> projectUsers = GitLabOperation.GetProjectUsers(gitRepoInfo.project_name);

            if (projectUsers != null && projectUsers.Count > 0)
            {
                foreach (GitLabOperation.UserInfo u in projectUsers)
                {
                    MailMessager.SendToList.Add(u.email);
                }
            }

            if (notifyEmails != null && notifyEmails.Count > 0)
            {
                MailMessager.SendToList.AddRange(notifyEmails);
            }

            if (_ps.notifyCcEmailsList != null && _ps.notifyCcEmailsList.Count > 0)
            {
                MailMessager.SendCcList.AddRange(_ps.notifyCcEmailsList);
            }

            List <string> attachFileList = new List <string>();
            //Prepare the emails list that we're going to send. *end*

            string gitContentInEmail = "<br>The latest commit: <br>" +
                                       "====== " + newLogList[0].commitNumber + " =====<br>" +
                                       newLogList[0].comment;

            //Copy release package zip files to the backup project release package folder.
            if (buildResult == _buildSuccessMessage)
            {
                Logger.Write(string.Format("Project {0} {1} branch build code -> Success!", gitRepoInfo.project_name, branchName));
                copyFilesByExtensions(projectSharedDir, backupProjectReleasePackageDirPath, copyFileExtensionList);

                attachFileList.Add(buildLogFilePath);
#if EMAIL_NOTIFY
                MailMessager.SendMessage("Protech OS Build Server's Message(There is a SUCCESSFUL build)", "Good job! Your latest source code committed to the project of " + gitRepoInfo.project_name + " can be built up successfully.<br>" + gitContentInEmail, attachFileList);
#endif
            }
            else if (buildResult == _buildErrorMessage)
            {
                Logger.Write(string.Format("Project {0} {1} branch build code -> Fail!", gitRepoInfo.project_name, branchName));
                File.Copy(buildLogFilePath, Path.Combine(projectSharedDir, buildSessionId), true);

                attachFileList.Add(buildLogFilePath);
#if EMAIL_NOTIFY
                MailMessager.SendMessage("Protech OS Build Server's Message(There is a FAILED build)", "We are regret to inform you that your latest source committed to the project of " + gitRepoInfo.project_name + " cannot be successfully built.<br>" + gitContentInEmail, attachFileList);
#endif
            }

            //House keeping for release packages
            if (eBranch == EnumBranch.Master)
            {
                houseKeepingForReleasePackages(projectSharedDir, backupProjectReleasePackageDirPath);
            }

            bool hasPackageFile   = false;
            bool hasBuildFailFile = false;

            if (File.Exists(Path.Combine(projectSharedDir, string.Format("{0}.zip'", buildSessionId))))
            {
                hasPackageFile = true;
            }

            if (File.Exists(Path.Combine(projectSharedDir, string.Format("{0}'", buildSessionId))))
            {
                hasBuildFailFile = true;
            }

            mergeNewGitLogFileToGitRepositoryInfo(gitRepoInfo, eBranch, gitLogFilePath, buildSessionId, buildResult, branchName, hasPackageFile, hasBuildFailFile);
        }