/// <summary> /// Executed by the external hook /// </summary> /// <param name="args">command line args (none)</param> static void Main(string[] args) { // get a reference to the hook calling the program var hook = new Mercurial.Hooks.MercurialChangeGroupHook(); // configure the logger ConfigureLogger(); Log.InfoFormat("MercurialChangeGroupHook: Triggered for repository [{0}]", hook.Repository); // get the current identity who committed the change group var windowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent(); if (windowsIdentity != null) { // log info to aide in debugging var user = windowsIdentity.Name; Log.InfoFormat("MercurialChangeGroupHook: Current user [{0}]", user); } try { // get all the change sets for the revision from the log var changesets = hook.Repository.Log(new LogCommand() .WithRevision(RevSpec.From(hook.FirstRevision))) .ToArray(); Log.InfoFormat("MercurialChangeGroupHook: Found [{0}] change sets", changesets.Length); // wire up the bugnet service var services = new WebServices.BugNetServices { CookieContainer = new System.Net.CookieContainer(), Url = AppSettings.BugNetServicesUrl }; if (Convert.ToBoolean(AppSettings.BugNetWindowsAuthentication)) { services.UseDefaultCredentials = true; } else { Log.Info("MercurialChangeGroupHook: Logging in to BugNET Web Services"); var result = services.LogIn(AppSettings.BugNetUsername, AppSettings.BugNetPassword); if (result) { Log.Info("MercurialChangeGroupHook: Login successful"); } else { Log.Error("MercurialChangeGroupHook: Unauthorized access, please check the user name and password settings"); Environment.Exit(1); } } // get the repository from the hook var repositoryName = IssueTrackerIntegration.GetRepositoryName(hook.Repository.ToString()); // loop the change sets from the log foreach (var changeset in changesets) { Log.InfoFormat("MercurialChangeGroupHook: Processing change set [{0}]", changeset.Hash); IssueTrackerIntegration.UpdateBugNetForChangeset(repositoryName, changeset, services); } Log.Info("MercurialChangeGroupHook: Processing complete"); } catch (Exception ex) { Log.FatalFormat("MercurialChangeGroupHook: An error occurred while processing: {0} \n\n {1}", ex.Message, ex.StackTrace); Environment.Exit(1); } }
/// <summary> /// Updates the issue tracker from the change set. /// </summary> /// <param name="repository"> </param> /// <param name="changeset"> </param> /// <param name="service"> </param> public static void UpdateBugNetForChangeset(string repository, Changeset changeset, WebServices.BugNetServices service) { var issuesAffectedList = new List <int>(); var regEx = new Regex(AppSettings.IssueIdRegEx, RegexOptions.IgnoreCase); var commitMessage = changeset.CommitMessage.Trim(); var matchResults = regEx.Match(commitMessage); if (!matchResults.Success) // none in the commit message { Log.Info("MercurialChangeGroupHook: Found no Issue Ids in change set"); return; } // capture the issues ids in the commit message // validate if the issue id is // change the commit message for each issue id (may be more to the commit) while (matchResults.Success) { var value = matchResults.Groups[1].Value.Trim(); var issueIdParts = value.Split(new[] { '-' }); if (issueIdParts.Length.Equals(2)) { var idString = issueIdParts[1]; int issueId; if (int.TryParse(idString, out issueId)) { if (service.ValidIssue(issueId)) // check the issue to make sure it exists { commitMessage = Regex.Replace(commitMessage, AppSettings.IssueIdRegEx, "<a href=\"IssueDetail.aspx?id=$2#top\"><b>$1</b></a>"); issuesAffectedList.Add(issueId); } } } matchResults = matchResults.NextMatch(); } if (issuesAffectedList.Count <= 0) { return; } var revisionNumber = changeset.RevisionNumber; var revision = changeset.Hash.Trim(); var author = changeset.AuthorName.Trim(); var dateTime = changeset.Timestamp.ToString(); var branch = changeset.Branch.Trim(); foreach (var id in issuesAffectedList) { try { Log.Info("MercurialChangeGroupHook: Creating new issue revision"); var success = service.CreateNewIssueRevision( revisionNumber, id, repository, author, dateTime, commitMessage, revision, branch); if (success) { Log.Info("MercurialChangeGroupHook: Successfully added new issue revision"); } else { Log.Warn("MercurialChangeGroupHook: Adding new issue revision failed"); } } catch (Exception ex) { Log.ErrorFormat("MercurialChangeGroupHook: An error occurred adding a new issue revision to BugNET: {0} \n\n {1}", ex.Message, ex.StackTrace); } } }