public MonitoredPath Get(MonitoredPathConfig mpc, string monitoredPathName, string repoName, string branchName, int days) { MonitoredPath newmonitoredPath = new MonitoredPath(); try { if (string.IsNullOrWhiteSpace(monitoredPathName)) { monitoredPathName = "default"; } foreach (MonitoredPath mp in mpc.MonitoredPaths) { if (mp.Name.ToLower() == monitoredPathName.ToLower()) { newmonitoredPath = this.GetMonitoredPath(mpc, mp, repoName, branchName, days); } } } catch (Exception ex) { this.locallogger.LogError("Bad - ", ex); return(null); } return(newmonitoredPath); }
private static GitRepository TryGetRepo(MonitoredPath monitoredPath, string directoryName) { GitRepository r = new GitRepository { Name = directoryName, FriendlyName = directoryName }; if (monitoredPath.Repositories.Any()) { foreach (var repo in monitoredPath.Repositories) { if (string.Compare(repo.Name, directoryName, StringComparison.OrdinalIgnoreCase) == 0) { r = new GitRepository { AllowFetch = repo.AllowFetch, CommitUrl = repo.CommitUrl, FriendlyName = string.IsNullOrWhiteSpace(repo.FriendlyName) ? directoryName : repo.FriendlyName, Name = directoryName, }; } } } return(r); }
public MonitoredPath Get(MonitoredPathConfig mpc, string monitoredPathName, string repoName, string branchName, string commitId) { MonitoredPath newmonitoredPath = new MonitoredPath(); try { if (string.IsNullOrWhiteSpace(monitoredPathName)) { monitoredPathName = "default"; } foreach (MonitoredPath mp in mpc.MonitoredPaths) { if (mp.Name.ToLower() == monitoredPathName.ToLower()) { DirectoryInfo[] directoriesToScan; if (!string.IsNullOrWhiteSpace(repoName)) { directoriesToScan = new DirectoryInfo(mp.Path).GetDirectories(repoName, SearchOption.TopDirectoryOnly); } else { if (mp.AllFolders) { directoriesToScan = new DirectoryInfo(mp.Path).GetDirectories("*", SearchOption.TopDirectoryOnly); } else { directoriesToScan = new DirectoryInfo[mp.Repositories.Count]; int i = 0; foreach (var dir in mp.Repositories) { directoriesToScan[i++] = new DirectoryInfo(Path.Combine(mp.Path, dir.Name)); } } } newmonitoredPath = this.GetMonitoredPath(commitId, mp, directoriesToScan, branchName); } } } catch (Exception ex) { this.locallogger.LogError("Bad - ", ex); return(null); } return(newmonitoredPath); }
private MonitoredPath GetMonitoredPath(string commitId, MonitoredPath monitoredPath, DirectoryInfo[] directoriesToScan, string branchName) { MonitoredPath newmonitoredPath = new MonitoredPath(); List <GitCommit> commits = new List <GitCommit>(); foreach (DirectoryInfo dir in directoriesToScan) { try { GitRepository gitrepo = this.TryGetRepo(monitoredPath, dir.Name); using (Repository repo = new Repository(dir.FullName)) { try { string branch = repo.Info.IsBare ? branchName : $"origin/{branchName}"; gitrepo.Branch = branch; int commitCount = 0; Commit com = repo.Lookup <Commit>(commitId); if (com != null) { var comFilter = new CommitFilter { IncludeReachableFrom = branch, ExcludeReachableFrom = com }; var coms = repo.Commits.QueryBy(comFilter).OrderBy(s => s.Author.When); string repositoryUrl = string.Empty; if (repo.Network.Remotes?["origin"] != null) { repositoryUrl = repo.Network.Remotes["origin"].Url; } foreach (Commit cm in coms) { if (!monitoredPath.IncludeMergeCommits) { // filter out merge commits if (com.Parents.Count() > 1) { continue; } } commits.Add(new GitCommit { Author = cm.Author.Name, AuthorEmail = string.IsNullOrWhiteSpace(com.Author.Email) ? string.Empty : cm.Author.Email, AuthorWhen = cm.Author.When.UtcDateTime, Committer = cm.Committer.Name, CommitterEmail = string.IsNullOrWhiteSpace(com.Committer.Email) ? string.Empty : cm.Committer.Email, CommitterWhen = cm.Committer.When.UtcDateTime, Sha = cm.Sha, Message = cm.Message, RepositoryFriendlyName = gitrepo.FriendlyName, RepositoryName = dir.Name, RepositoryUrl = repositoryUrl, CommitUrl = string.IsNullOrWhiteSpace(gitrepo.CommitUrl) ? string.Empty : string.Format($"{gitrepo.CommitUrl}{cm.Sha}"), IsMerge = com.Parents.Count() > 1 }); commitCount++; } gitrepo.CommitCount = commitCount; newmonitoredPath.Repositories.Add(gitrepo); } } catch (Exception ex) { this.locallogger.LogError("GetMonitoredPath Bad - ", ex); } newmonitoredPath.Name = monitoredPath.Name; newmonitoredPath.AllowFetch = monitoredPath.AllowFetch; newmonitoredPath.AllFolders = monitoredPath.AllFolders; newmonitoredPath.Path = monitoredPath.Path; newmonitoredPath.CommitCount = commits.Count; newmonitoredPath.Commits = commits; } } catch (Exception ex) { this.locallogger.LogError("GetMonitoredPath Bad - ", ex); } } return(newmonitoredPath); }
private MonitoredPath GetMonitoredPath(MonitoredPathConfig monitoredPathConfig, MonitoredPath monitoredPath, string repository, string branchName, int days) { List <GitCommit> commits = new List <GitCommit>(); DirectoryInfo[] directoriesToScan; if (!string.IsNullOrWhiteSpace(repository)) { directoriesToScan = new DirectoryInfo(monitoredPath.Path).GetDirectories(repository, SearchOption.TopDirectoryOnly); } else { if (monitoredPath.AllFolders) { directoriesToScan = new DirectoryInfo(monitoredPath.Path).GetDirectories("*", SearchOption.TopDirectoryOnly); } else { directoriesToScan = new DirectoryInfo[monitoredPath.Repositories.Count]; int i = 0; foreach (var dir in monitoredPath.Repositories) { directoriesToScan[i++] = new DirectoryInfo(Path.Combine(monitoredPath.Path, dir.Name)); } } } if (days == 0) { days = monitoredPath.Days == 0 ? Convert.ToInt32(monitoredPathConfig.DefaultDays) : monitoredPath.Days; } if (days > 0) { days = days * -1; } MonitoredPath newmonitoredPath = new MonitoredPath(); foreach (var dir in directoriesToScan) { try { GitRepository gitrepo = this.TryGetRepo(monitoredPath, dir.Name); using (var repo = new Repository(dir.FullName)) { DateTime startDate = DateTime.Now.AddDays(days); int commitCount = 0; if (string.IsNullOrEmpty(branchName)) { branchName = "master"; } string branch = repo.Info.IsBare ? branchName : $"origin/{branchName}"; gitrepo.Branch = branch; foreach (Commit com in repo.Branches[branch].Commits.Where(s => s.Committer.When >= startDate).OrderByDescending(s => s.Author.When)) { if (!monitoredPath.IncludeMergeCommits) { // filter out merge commits if (com.Parents.Count() > 1) { continue; } } string[] nameexclusions = monitoredPathConfig.DefaultUserNameExcludeFilter.Split(','); if (nameexclusions.Any(name => com.Author.Name.Contains(name))) { continue; } string url = string.IsNullOrWhiteSpace(gitrepo.CommitUrl) ? string.Empty : string.Format($"{gitrepo.CommitUrl}{com.Sha}"); string repositoryUrl = string.Empty; if (repo.Network.Remotes?["origin"] != null) { repositoryUrl = repo.Network.Remotes["origin"].Url; } commits.Add(new GitCommit { Author = com.Author.Name, AuthorEmail = string.IsNullOrWhiteSpace(com.Author.Email) ? string.Empty : com.Author.Email, AuthorWhen = com.Author.When.UtcDateTime, Committer = com.Committer.Name, CommitterEmail = string.IsNullOrWhiteSpace(com.Committer.Email) ? string.Empty : com.Committer.Email, CommitterWhen = com.Committer.When.UtcDateTime, Sha = com.Sha, Message = com.Message, RepositoryFriendlyName = gitrepo.FriendlyName, RepositoryName = dir.Name, RepositoryUrl = repositoryUrl, CommitUrl = url, IsMerge = com.Parents.Count() > 1 }); commitCount++; } gitrepo.CommitCount = commitCount; newmonitoredPath.Repositories.Add(gitrepo); newmonitoredPath.AllowFetch = monitoredPath.AllowFetch; } newmonitoredPath.Name = monitoredPath.Name; newmonitoredPath.AllowFetch = monitoredPath.AllowFetch; newmonitoredPath.AllFolders = monitoredPath.AllFolders; newmonitoredPath.Days = days; newmonitoredPath.Path = monitoredPath.Path; newmonitoredPath.CommitCount = commits.Count; newmonitoredPath.Commits = commits; newmonitoredPath.Commits.Sort((x, y) => - DateTime.Compare(x.CommitterWhen, y.CommitterWhen)); } catch (Exception ex) { this.locallogger.LogError("GetMonitoredItem Bad - ", ex); } } return(newmonitoredPath); }
public PathMonitor(MonitoredPath path) { ResourcePath = path ?? throw new ArgumentNullException(nameof(path) + " is null."); }