/// <summary> /// 執行每一 Command 後,依據設定暫停若干毫秒 /// </summary> /// <param name="gitCommandList">The git command list.</param> /// <returns></returns> public GitCommandLog RunScript(IEnumerable <GitCommandDto> gitCommandList) { _gitCommandLog = new GitCommandLog(); _bashProcess.StartInfo.WorkingDirectory = string.Format(@"{0}", CurrentWorkingDirectory); _bashProcess.Start(); _bashProcess.BeginErrorReadLine(); _bashProcess.BeginOutputReadLine(); foreach (GitCommandDto dto in gitCommandList) { _gitCommandLog.InputCommand.AppendLine(dto.GitCommand); _bashProcess.StandardInput.WriteLine(dto.GitCommand); _bashProcess.StandardInput.WriteLine("echo $?"); if (dto.SleepMilliSecond > 0) { System.Threading.Thread.Sleep(dto.SleepMilliSecond); } } _bashProcess.StandardInput.WriteLine("exit"); _bashProcess.StandardInput.Close(); _bashProcess.WaitForExit(); _gitCommandLog.TotalInputCommand = gitCommandList.Where(c => c.IsReturnZeroAfterExecute).Count(); _gitCommandLog.TotalSucceedCommand = this.GetTotalReturnZeroCommand(); _bashProcess.CancelErrorRead(); _bashProcess.CancelOutputRead(); return(_gitCommandLog); }
public GitCommandLog FetchRepo(string repoName) { GitCommandLog log = null; this.SwitchRepo(repoName); log = base.RunScript("git fetch --all"); return(log); }
public GitCommandLog VerifyRemoteTag(string gitAccount, string gitServer, string gitName, string tagName, string commitID) { this.SwitchToRoot(); string tagPrefix = "refs/tags/"; if (tagName.StartsWith(tagPrefix)) { tagName = tagName.Replace(tagPrefix, ""); } GitCommandLog log = base.RunScript(string.Format("git ls-remote ssh://{0}@{1}/{2} refs/tags/{3}^{{}}", gitAccount, gitServer, gitName, tagName)); log.OverallResult = log.DataMessage.ToString().Contains(commitID); return(log); }
public GitCommandLog VerifyLocalTag(string repoName, string tagName, string commitID) { this.SwitchToRoot(); string tagPrefix = "refs/tags/"; if (tagName.StartsWith(tagPrefix)) { tagName = tagName.Replace(tagPrefix, ""); } GitCommandLog log = base.RunScript(string.Format("git ls-remote {0} refs/tags/{1}^{{}}", repoName, tagName)); log.OverallResult = log.DataMessage.ToString().Contains(commitID); return(log); }
public GitCommandLog SetRepoConfig(string repoName, string gitAccountName, string gitAccountEmail) { GitCommandLog log = null; this.SwitchRepo(repoName); IList <string> script = new List <string>(); script.Add(string.Format("git config user.name {0}", gitAccountName)); script.Add(string.Format("git config user.email {0}", gitAccountEmail)); script.Add("git config core.autocrlf input"); script.Add("git config core.safecrlf true"); log = base.RunScript(script); return(log); }
public GitCommandLog GC(string repoName) { GitCommandLog log = null; this.SwitchRepo(repoName); IList <string> script = new List <string>(); script.Add("git gc"); script.Add("git prune"); script.Add("git fetch -p --all"); log = base.RunScript(script); return(log); }
public GitCommandLogModel Save(GitCommandLog log, string systemShortName, string taskTableName = "", long?taskFK = null) { GitCommandLogModel logModel = new GitCommandLogModel(); logModel.Task = systemShortName; logModel.TaskTableName = taskTableName; logModel.TaskFK = taskFK; logModel.InputCommand = log.InputCommand.ToString(); logModel.DataMessage = log.DataMessage.ToString(); logModel.ErrMessage = log.ErrMessage.ToString(); logModel.TotalInputCommand = log.TotalInputCommand; logModel.TotalSucceedCommand = log.TotalSucceedCommand; logModel.OverallResult = log.OverallResult; logModel.CreateTime = DateTime.Now; return(base.Save(logModel)); }
public GitCommandLog TagCommitID(string repoName, string tagName, string commitID, string comment) { GitCommandLog log = null; this.SwitchRepo(repoName); string tagPrefix = "refs/tags/"; if (tagName.StartsWith(tagPrefix)) { tagName = tagName.Replace(tagPrefix, ""); } IList <GitCommandDto> script = new List <GitCommandDto>(); //Remove local tag script.Add(new GitCommandDto() { GitCommand = string.Format("git tag -d {0}", tagName) }); //Add local tag script.Add(new GitCommandDto() { GitCommand = string.Format("git tag -a {0} {1} -m \"{2}\"", tagName, commitID, comment) }); //Push specific tag to remote site script.Add(new GitCommandDto() { GitCommand = string.Format("git push origin refs/tags/{0} -f", tagName), SleepMilliSecond = 2000//暫停2秒 }); //Push all tag to remote site //script.Add(new GitCommandDto() // { // GitCommand = "git push origin --tag", // SleepMilliSecond = 2000//暫停2秒 // }); log = base.RunScript(script); return(log); }
public void Execute() { try { IList <T> ReleaseDataList = this.GetReleaseRecordList(); foreach (T model in ReleaseDataList) { GitCommandLog log = Execute(model); if (log != null) { this.UpdateReleaseRecord(model.ID, log.OverallResult); this.RecordAndNotify(log, model.GitName, model.ID); } } } catch (Exception ex) { if (_exceptionHandler != null) { _exceptionHandler.Handle(ex); } } }
public GitCommandLog CloneRepo(string gitAccount, string gitServer, string gitName, string branchName = "") { GitCommandLog log = null; this.SwitchToRoot(); if (!string.IsNullOrEmpty(branchName)) { log = base.RunScript( string.Format("git clone ssh://{0}@{1}/{2} -b {3} {2}", gitAccount, gitServer, gitName, branchName)); } else { log = base.RunScript( string.Format("git clone ssh://{0}@{1}/{2} {2}", gitAccount, gitServer, gitName)); } return(log); }
private GitCommandLog Execute(T model) { GitCommandLog log = new GitCommandLog(); try { string processName = System.Diagnostics.Process.GetCurrentProcess().ProcessName; string repoName = model.GitName; bool isRepoExist = _gitBash.RepoExist(repoName); if (string.IsNullOrEmpty(repoName)) { throw new Exception("[Warn] RepoName is empty."); } if (!isRepoExist) { log.Merge(_gitBash.CloneRepo(_gitAccount, _gitServer, model.GitName)); if (!log.OverallResult) { throw new GenericModelException <GitCommandLog>(log, string.Format("[Warn] CloneRepo {0} failed", repoName)); } log.Merge(_gitBash.SetRepoConfig(repoName, _gitAccount, _gitAccountEmail)); if (!log.OverallResult) { throw new GenericModelException <GitCommandLog>(log, string.Format("[Warn] SetRepoConfig {0} failed", repoName)); } } else { log.Merge(_gitBash.FetchRepo(repoName)); if (!log.OverallResult) { throw new GenericModelException <GitCommandLog>(log, string.Format("[Warn] FetchRepo {0} failed", repoName)); } } ////string tagName = TagMaker(model); if (string.IsNullOrEmpty(model.TagName)) { throw new GenericModelException <GitCommandLog>(log, "[Warn] TagName is empty."); } else { log.Merge(_gitBash.TagCommitID(repoName, model.TagName, model.ReleaseCommitID, string.Format("{0} tool tagged", processName))); if (!log.OverallResult) { throw new GenericModelException <GitCommandLog>(log, string.Format("[Fatal] {0} tagging failed", repoName)); } } //check local tag log.Merge(_gitBash.VerifyLocalTag(repoName, model.TagName, model.ReleaseCommitID)); if (!log.OverallResult) { throw new GenericModelException <GitCommandLog>(log, "[Fatal] VerifyLocalTag failed."); } //check remote tag log.Merge(_gitBash.VerifyRemoteTag(_gitAccount, _gitServer, model.GitName, model.TagName, model.ReleaseCommitID)); if (!log.OverallResult) { throw new GenericModelException <GitCommandLog>(log, "[Fatal] VerifyRemoteTag failed."); } } catch (Exception ex) { if (_exceptionHandler != null) { _exceptionHandler.Handle(ex, model); } } return(log); }
/// <summary> /// 實作依據傳入的model製作出來Tag. /// </summary> /// <param name="model">The model.</param> /// <returns></returns> ////protected abstract string TagMaker(T model); /// <summary> /// 實作處理(紀錄 或 通知) /// 執行git command所產生的log /// </summary> /// <param name="log">The log.</param> /// <param name="git">The git.</param> protected abstract void RecordAndNotify(GitCommandLog log, string git, long taskFK);
public AbstractGitCommandLogMail(GitCommandLog gitCommandLog) : base(BaseHtmlSmtpMail.GenMailObjByAppConfig()) { this._gitCommandLog = gitCommandLog; }
public GitCommandLogMail(GitCommandLog log, string mailSubject, string mailReceivers) : base(log) { this._mailSubject = mailSubject; this._mailReceivers = mailReceivers; }