using System; using System.Collections.Generic; using System.Text; using P4API; using System.Diagnostics; namespace JobLabeler { class JobLabeler { private string _p4port = null; private string _p4user = null; private string _p4password = null; private string _labelTemplate = null; private string _p4counter = null; private string _labelNameFormat = null; private bool _useLogin = false; //Constructor... just store off the configuration settings. public JobLabeler(string P4Port, string P4User, string P4Password, string LabelTemplate, string P4Counter, string LabelNameFormat, bool UseLogin) { _p4port = P4Port; _p4user = P4User; _p4password = P4Password; _labelTemplate = LabelTemplate; _p4counter = P4Counter; _useLogin = UseLogin; _labelNameFormat = LabelNameFormat; } //Guts of the logic. This is where the app will dynamicall build the job labels public void RunLabeler(EventLog log) { P4Connection p4 = null; try { p4 = new P4Connection(); p4.Port = _p4port; p4.User = _p4user; if (!_useLogin) { p4.Password = _p4password; } p4.Connect(); if (_useLogin) { p4.Login(_p4password); } log.WriteEntry("shawn"); P4StringsResult counter = p4.Run_Strings("counter", _p4counter); int counterValue = int.Parse(counter[0]); P4TaggedResult loggers = p4.Run_Tags("logger", "-c", counterValue.ToString()); log.WriteEntry(loggers.ErrorMessage); int LastSequenceNumber = 0; //spin the results, and get a unique list of jobs List<string> JobList = new List<string>(); foreach (P4ResultRecord r in loggers) { if ((string)r["key"] == "job") { string JobName = (string)r["attr"]; if (!JobList.Contains(JobName)) JobList.Add(JobName); } LastSequenceNumber = int.Parse((string)r["sequence"]); } log.WriteEntry(LastSequenceNumber.ToString()); log.WriteEntry("was"); // Now spin all the jobs and build the label foreach (string JobName in JobList) { BuildLabel(JobName, p4, log); Trace.WriteLine(JobName); } log.WriteEntry("here"); //Now we update the counter to the last sequence number from logger. p4.Run_Strings("counter", _p4counter, LastSequenceNumber.ToString()); } catch (Exception e) { throw e; } finally { // All done here... time to disconnect. p4.Disconnect(); } } private void BuildLabel(string JobName, P4Connection p4, EventLog log) { string LabelName = string.Format(_labelNameFormat, JobName); // delete the label... it may not exist, but finding that out is worse performance // than just deleting and assuming it doesn't exist. P4StringsResult labelDel = p4.Run_Strings("label", "-f", "-d", LabelName); if (labelDel.HasErrors()) { Trace.WriteLine("Dude!"); } // Run a fixes to get all the changelists we need to add P4TaggedResult fixes = p4.Run_Tags("fixes", "-j", JobName); List<int> JobChanges = new List<int>(); foreach (P4ResultRecord fix in fixes) { JobChanges.Add(int.Parse((string)fix["Change"])); } JobChanges.Sort(); // only build the label if there are indeed fixes if (JobChanges.Count > 0) { //re-create the label P4Form labelForm = p4.Fetch_Form("label", "-t", _labelTemplate, LabelName); // make sure the form is unlocked labelForm["Options"] = "unlocked"; // make sure we're the owner labelForm["Owner"] = _p4user; p4.Save_Form(labelForm); int ChangesAdded = 0; //now need to labelsync to all latest changes foreach (int change in JobChanges) { // using tag here so a valid client spec is not needed. // for older servers, you could sub labelsync P4StringsResult ls = p4.Run_Strings("tag", "-l", LabelName, string.Format("@={0}", change)); if (ls.HasErrors()) { if (ls.ErrorMessage.StartsWith("Can't use a pending changelist number for this command.")) { // Nothing to worry about. p4 fixes returns fix records for pending changelists, // but we don't know that going into this. It's likely more performant to just // let this dude error out and ignore it. } else { // Something's gone ary. Should throw an Exception log.WriteEntry(ls.ErrorMessage); } Trace.WriteLine("Dude!"); } } // now lock the label labelForm["Options"] = "locked"; p4.Save_Form(labelForm); } } } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#7 | 5830 | Shawn Hladky | P4.Net: reorg to support release branches | ||
#6 | 5812 | Shawn Hladky | P4.Net: More documentation. | ||
#5 | 5801 | Shawn Hladky |
P4.Net. More misc changes |
||
#4 | 5798 | Shawn Hladky |
P4.Net... still not ready for beta Added license to all files Added several doc files Misc bugs |
||
#3 | 5433 | Shawn Hladky | P4.Net More refactoring | ||
#2 | 5431 | Shawn Hladky |
Refactoring... step 1. |
||
#1 | 5427 | Shawn Hladky | P4.Net -- several fixes and added sample application |