Skip to content

Instantly share code, notes, and snippets.

@jaydanielian
Created February 13, 2014 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaydanielian/8977887 to your computer and use it in GitHub Desktop.
Save jaydanielian/8977887 to your computer and use it in GitHub Desktop.
GitService.cs - used for parsing the commits between the previous tag and the current one that our jenkins build is using. We grab all the commits between these two points, and parse the commit comments
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LibGit2Sharp;
using LibGit2Sharp.Core;
using LibGit2Sharp.Handlers;
using System.Text.RegularExpressions;
namespace Dublabs.CircleBack.PivotalTrackGit
{
class GitService
{
private string _gitPath = string.Empty;
public GitService(string pathToGitRepo)
{
_gitPath = pathToGitRepo;
}
public string GetPreviousTag(string currentTag)
{
string previousTag = string.Empty;
using (var repo = new Repository(_gitPath))
{
var tags = repo.Tags;
var filteredTags = tags.Where(x => x.Annotation != null && x.Name != currentTag).OrderByDescending(x => x.Annotation.Tagger.When).Take(1);
if (filteredTags != null && filteredTags.Count() == 1)
{
previousTag = filteredTags.FirstOrDefault().Name;
}
return previousTag;
}
}
public List<string> GetStoryIdsForTag(string currentTagName, string previousTagName)
{
List<string> stories = new List<string>();
using (var repo = new Repository(_gitPath))
{
var filter = new CommitFilter { Since = repo.Tags[currentTagName], Until = repo.Tags[previousTagName] };
var commits = repo.Commits.QueryBy(filter);
foreach (var c in commits)
{
string pattern = @"^\[(.*?)\]";
string input = c.MessageShort.Trim();
Match regMatch = Regex.Match(input, pattern);
if (regMatch.Success)
{
string innerText = regMatch.Value.Replace("[", "").Replace("]", "");
char[] splitter = { Convert.ToChar(" ") };
var chunks = innerText.Split(splitter, StringSplitOptions.RemoveEmptyEntries);
foreach (var chunk in chunks)
{
string story = chunk.Replace("#", "");
int storyNumber = 0;
if (int.TryParse(story, out storyNumber))
{
stories.Add(story);
}
}
}
}
}
return stories.Distinct().ToList();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment