public TranslateCommitAttemptResult Execute(CommitAttempt attempt)
        {
            var body = GetDecodedBody(attempt.RawBody);

            dynamic root = JObject.Parse(body);

            var commits = new List<CommitMessage>();

            // TODO: make a copy instead of parse if can be done...
            dynamic rootWithoutCommits = root.DeepClone();
            rootWithoutCommits.commits.RemoveAll();

            foreach (var commitItem in root.commits)
            {
                var commit = new CommitMessage
                                 {
                                     MessageId = commitItem.id,
                                     Author = commitItem.author.name + " <" + commitItem.author.email + ">",
                                     Comment = commitItem.message,
                                     Date = commitItem.timestamp,
                                 };
                dynamic fullContextCommit = rootWithoutCommits.DeepClone();
                fullContextCommit.commits.Add(commitItem);
                commit.SourceCommit = fullContextCommit.ToString();
                commits.Add(commit);
            }

            return new TranslateCommitAttemptResult
            {
                Success = true,
                Commits = commits
            };
        }
 public TranslatorException(ITranslateCommitAttempt translator, CommitAttempt attempt) 
     : base(string.Format("Translator of type '{0}' was unable to process incoming CommitAttempt starting with body: {1}",
                          translator.GetType(), attempt.RawBody.Substring(0, attempt.RawBody.Length < 50 ? attempt.RawBody.Length : 50)))
 {
     Translator = translator;
     Attempt = attempt;
 }
		private XDocument DecodeContentAndCreateDocument(CommitAttempt attempt)
		{
			string content = HttpUtility.HtmlDecode(attempt.RawBody);
			Match match = _eventPattern.Match(content);
			var document = XDocument.Parse(match.Value);
			return document;
		}
        private TranslateCommitAttemptResult Translate(CommitAttempt attempt)
        {
            foreach (var translator in _translators)
            {
                try
                {
                    if (!translator.CanProcess(attempt))
                        continue;

                    var result = translator.Execute(attempt);

                    if (result.Success)
                        return result;

                    throw new TranslatorException(translator, attempt);
                }
                catch (Exception ex)
                {
                    throw new TranslatorException(translator, attempt, ex);
                }
            }

            //return null;
            throw new TranslatorNotFoundException(attempt);
        }
        public void Execute_succeeds_for_valid_CommitAttempt()
        {
            var commitAttempt = new CommitAttempt() { RawBody = TestData.FullyFormedAttemptBody };
            var result = subject.Execute(commitAttempt);

            Assert.IsTrue(result.Success);
            Assert.AreEqual(2, result.Commits.Count);
        }
 public TranslateCommitAttemptResult Execute(CommitAttempt attempt)
 {
     return new TranslateCommitAttemptResult
                {
                    Success = true,
                    Commits = new [] { new CommitMessage() { Name = "BitBucket", SourceCommit = attempt.ToString() } }
                };
 }
		public bool CanProcess(CommitAttempt attempt)
		{
			if (string.IsNullOrEmpty(attempt.RawBody))
				return false;

			if (attempt.UserAgent.StartsWith("Team Foundation"))
				return true;

			return false;
		}
        public bool CanProcess(CommitAttempt attempt)
        {
            if (string.IsNullOrEmpty(attempt.RawBody))
            {
                return false;
            }

            var isMatch = _taster.IsMatch(attempt.RawBody);

            return isMatch;
        }
		public TranslateCommitAttemptResult Execute(CommitAttempt attempt)
		{
			XDocument document = DecodeContentAndCreateDocument(attempt);
			var checkIn = ParseXmlDocument(document);

			return new TranslateCommitAttemptResult
			{
				Commits = new List<CommitMessage> { checkIn },
				Success = true
			};
		}
		public void SetUp()
		{
			var validSampleData = File.ReadAllText("ValidSample.xml");
			_validAttempt = new CommitAttempt()
			{
				RawBody = validSampleData,
				UserAgent = "Team Foundation (TfsJobAgent.exe, 10.0.40219.1)"
			};

			var inValidSampleData = File.ReadAllText("InValidSample.xml");
			_invalidAttempt = new CommitAttempt()
			{
				RawBody = inValidSampleData,
				UserAgent = "Team Foundation (TfsJobAgent.exe, 10.0.40219.1)"
			};
		}
        // TODO: what about exceptions, and what about exceptions when partial success, but not all??
        public object Any(CommitAttempt request)
        {
            var translationResult = Translate(request);

            if (translationResult != null)
            {
                var redisClient = new RedisClient();
                using (var commitsStore = redisClient.As<CommitMessage>())
                {
                    foreach (var commit in translationResult.Commits)
                    {
                        commit.Id = commitsStore.GetNextSequence();
                        commitsStore.Store(commit);
                        // Send to connected clients
                        EventStream.Publish<CommitMessage>(commit);
                    }
                }

                return new CommitAcknowledge { CanProcess = true };
            }

            return new CommitAcknowledge { CanProcess = false };
        }
        private bool RunCanProcessTest(string attemptBody)
        {
            var attemptMessage = new CommitAttempt { RawBody = attemptBody };

            var result = subject.CanProcess(attemptMessage);

            return result;
        }
 public TranslatorNotFoundException(CommitAttempt attempt)
     : base(string.Format("Unable to find a translator for incoming CommitAttempt starting with body: {0}",
                          attempt.RawBody.Substring(0, attempt.RawBody.Length < 50 ? attempt.RawBody.Length : 50)))
 {
     Attempt = attempt;
 }
        public void Execute_creates_three_valid_CommitMessages_from_single_CommitAttempt_with_three_commits()
        {
            var attempt = new CommitAttempt() {RawBody = TestData.ThreeValidCommitsFragment};

            var result = subject.Execute(attempt);

            Assert.IsTrue(result.Success);
            Assert.True(IsCommitValid(TestData.ExpectedValidCommitMessage1, result.Commits[0]));
            Assert.True(IsCommitValid(TestData.ExpectedValidCommitMessage2, result.Commits[1]));
            Assert.True(IsCommitValid(TestData.ExpectedValidCommitMessage3, result.Commits[2]));
        }
 public void GitHubNewFormat()
 {
     var attempt = new CommitAttempt() {RawBody = TestData.GitHubNov28};
     var result = subject.CanProcess(attempt);
 }