public static CardType CalculateLeanKitCardType(BoardMapping project, GitHubIssues.Issue issue)
        {
            // NOTE: GitHub does not use types for issues. It uses labels.
            // Default labels are: bug, duplicate, enhancement, invalid, question, wont fix
            // of those bug and enhancement are the ones that fit a type the best.
            // bug could be mapped to bug/issue in LeanKit and enhancement mapped to improvement/feature in LeanKit

            var defaultCardType = project.ValidCardTypes.FirstOrDefault(x => x.IsDefault);

            if (issue != null && issue.Labels != null && issue.Labels.Any())
            {
                foreach (var label in issue.Labels)
                {
                    var mappedWorkType = project.Types.FirstOrDefault(x => x.Target.ToLowerInvariant() == label.Name.ToLowerInvariant());
                    if (mappedWorkType != null)
                    {
                        var definedVal = project.ValidCardTypes.FirstOrDefault(x => x.Name.ToLowerInvariant() == mappedWorkType.LeanKit.ToLowerInvariant());
                        if (definedVal != null)
                        {
                            return(definedVal);
                        }
                    }
                    var implicitVal = project.ValidCardTypes.FirstOrDefault(x => x.Name.ToLowerInvariant() == label.Name.ToLowerInvariant());
                    if (implicitVal != null)
                    {
                        return(implicitVal);
                    }
                }
            }
            return(defaultCardType);
        }
        public static long?CalculateLeanKitAssignedUserId(long boardId, GitHubIssues.Issue issue, ILeanKitApi leanKit)
        {
            if (issue == null)
            {
                return(null);
            }

            if (issue.Assignee != null && !string.IsNullOrEmpty(issue.Assignee.Login))
            {
                var lkUser = leanKit.GetBoard(boardId).BoardUsers.FirstOrDefault(x => x != null &&
                                                                                 (((!string.IsNullOrEmpty(x.EmailAddress)) && x.EmailAddress.ToLowerInvariant() == issue.Assignee.Login.ToLowerInvariant()) ||
                                                                                  ((!string.IsNullOrEmpty(x.FullName)) && x.FullName.ToLowerInvariant() == issue.Assignee.Login.ToLowerInvariant()) ||
                                                                                  ((!string.IsNullOrEmpty(x.UserName)) && x.UserName.ToLowerInvariant() == issue.Assignee.Login.ToLowerInvariant())));
                if (lkUser != null)
                {
                    return(lkUser.Id);
                }
            }

            return(null);
        }
        public static int CalculateLeanKitPriority(GitHubIssues.Issue issue)
        {
            // NOTE: GitHub does not use priorities for issues. The only thing we could do is use labels.
            // However the default labels of bug, duplicate, enhancement, invalid, question and wont fix
            // do not map well to priorities. We can look for custom labels but that would be up to the
            // GitHub admin to create the custom labels
            // If you would like to use different priorities/labels than the OOTB LeanKit priorities
            // then you will have to alter this method

            const int lkPriority = 1;             // default to 1 - Normal

            // LK Priority: 0 = Low, 1 = Normal, 2 = High, 3 = Critical
            if (issue != null && issue.Labels != null && issue.Labels.Any())
            {
                foreach (var label in issue.Labels)
                {
                    switch (label.Name.ToLowerInvariant())
                    {
                    case "critical":
                        return(3);

                    case "high":
                        return(2);

                    case "normal":
                        return(1);

                    case "low":
                        return(0);
                    }
                }
            }

            // else just set it to default of Normal
            return(lkPriority);
        }
 public static long?LeanKitAssignedUserId(this GitHubIssues.Issue issue, long boardId, ILeanKitApi leanKit)
 {
     return(CalculateLeanKitAssignedUserId(boardId, issue, leanKit));
 }
 public static CardType LeanKitCardType(this GitHubIssues.Issue issue, BoardMapping project)
 {
     return(CalculateLeanKitCardType(project, issue));
 }
		protected override void OnArrange()
		{
			base.OnArrange();

			MockLeanKitApi.Setup(x => x.GetBoard(It.IsAny<long>())).Returns(_testBoard);
			
			var serializer = new JsonSerializer<List<GitHubIssues.Issue>>();

			var issue1 = new GitHubIssues.Issue()
				{
					Id = 1,
					Number = 1,
					State = "Open", 
					Body = "New Issue 1", 
					Title = "New Issue 1"
				};

			var issue2 = new GitHubIssues.Issue()
			{
				Id = 2,
				Number = 2,
				State = "Open",
				Body = "New Issue 2",
				Title = "New Issue 2"
			};

			var issue3 = new GitHubIssues.Issue()
			{
				Id = 3,
				Number = 3,
				State = "Open",
				Body = "New Issue 3",
				Title = "New Issue 3"
			};

			var restResponse1 = new RestResponse()
				{
					Content = serializer.SerializeToString(new List<GitHubIssues.Issue>() { issue1 }),
					StatusCode = HttpStatusCode.OK
				};

			MockRestClient.Setup(x => x.Execute(It.Is<RestRequest>(y => y.Resource.Contains("/1/issues") && y.Method == Method.GET))).Returns(restResponse1);
			MockLeanKitApi.Setup(x => x.GetCardByExternalId(1, It.IsAny<string>())).Returns((Card)null);
			MockLeanKitApi.Setup(x => x.AddCard(1, It.IsAny<Card>(), It.IsAny<string>())).Returns(_testCardAddResult1);
			
			MockRestClient.Setup(x => x.Execute(It.Is<RestRequest>(y => y.Resource.Contains("/2/issues") && y.Method == Method.GET))).Returns(restResponse1);
			MockLeanKitApi.Setup(x => x.GetCardByExternalId(2, It.IsAny<string>())).Returns((Card)null);
			MockLeanKitApi.Setup(x => x.AddCard(2, It.IsAny<Card>(), It.IsAny<string>())).Returns(_testCardAddResult1);
			
			var restResponse3 = new RestResponse()
				{
					Content = serializer.SerializeToString(new List<GitHubIssues.Issue>() {issue1, issue2, issue3}),
					StatusCode = HttpStatusCode.OK
				};

			MockRestClient.Setup(x => x.Execute(It.Is<RestRequest>(y => y.Resource.Contains("/3/issues") && y.Method == Method.GET))).Returns(restResponse3);
			MockLeanKitApi.Setup(x => x.GetCardByExternalId(3, It.IsAny<string>())).Returns((Card)null);
			MockLeanKitApi.Setup(x => x.AddCard(3, It.IsAny<Card>(), It.IsAny<string>())).Returns(_testCardAddResult1);
			
			MockRestClient.Setup(x => x.Execute(It.Is<RestRequest>(y => y.Resource.Contains("/4/issues") && y.Method == Method.GET))).Returns(restResponse1);
			MockLeanKitApi.Setup(x => x.GetCardByExternalId(4, It.IsAny<string>())).Returns(new Card() { Id = 4, ExternalSystemName = "GitHub"});
			MockLeanKitApi.Setup(x => x.AddCard(4, It.IsAny<Card>(), It.IsAny<string>())).Returns(_testCardAddResult1);

			MockRestClient.Setup(x => x.Execute(It.Is<RestRequest>(y => y.Resource.Contains("/5/issues") && y.Method == Method.GET))).Returns(restResponse1);
			MockLeanKitApi.Setup(x => x.GetCardByExternalId(5, It.IsAny<string>())).Returns(new Card() { Id = 4, ExternalSystemName = "GitHubby" });
			MockLeanKitApi.Setup(x => x.AddCard(5, It.IsAny<Card>(), It.IsAny<string>())).Returns(_testCardAddResult1);
		}
 public static int LeanKitPriority(this GitHubIssues.Issue issue)
 {
     return(CalculateLeanKitPriority(issue));
 }
		protected override void OnArrange() 
		{
			base.OnArrange();

			MockLeanKitApi.Setup(x => x.GetBoard(It.IsAny<long>())).Returns(_testBoard);

			var serializer = new JsonSerializer<GitHubIssues.Issue>();

			var issue1 = new GitHubIssues.Issue()
				{
					Id = 1,
					Number = 1,
					State = "Open"
				};

			var restResponse1 = new RestResponse() { Content = serializer.SerializeToString(issue1), StatusCode = HttpStatusCode.OK };

			MockRestClient.Setup(x => x.Execute(It.Is<RestRequest>(y => y.Resource.Contains("issues/1") && y.Method == Method.GET))).Returns(restResponse1);
			MockRestClient.Setup(x => x.Execute(It.Is<RestRequest>(y => y.Resource.Contains("issues/1") && y.Method == Method.PATCH))).Returns(restResponse1);

			var issue2 = new GitHubIssues.Issue()
			{
				Id = 2,
				Number = 2,
				State = "Closed"
			};

			var restResponse2 = new RestResponse() { Content = serializer.SerializeToString(issue2), StatusCode = HttpStatusCode.OK };

			MockRestClient.Setup(x => x.Execute(It.Is<RestRequest>(y => y.Resource.Contains("issues/2") && y.Method == Method.GET))).Returns(restResponse2);
			MockRestClient.Setup(x => x.Execute(It.Is<RestRequest>(y => y.Resource.Contains("issues/2") && y.Method == Method.PATCH))).Returns(restResponse2);

			var errorSerializer = new JsonSerializer<GitHubIssues.ErrorMessage>();
			var errorResponse = new RestResponse() { Content = errorSerializer.SerializeToString(new GitHubIssues.ErrorMessage() { Message = "Error" }), StatusCode = HttpStatusCode.NotFound };

			MockRestClient.Setup(x => x.Execute(It.Is<RestRequest>(y => y.Resource.Contains("issues/3") && y.Method == Method.GET))).Returns(errorResponse);
			MockRestClient.Setup(x => x.Execute(It.Is<RestRequest>(y => y.Resource.Contains("issues/3") && y.Method == Method.PATCH))).Returns(errorResponse);
		}
		protected override void OnArrange() 
		{
			base.OnArrange();

			MockLeanKitApi.Setup(x => x.GetBoard(It.IsAny<long>())).Returns(_testBoard);

			var serializer = new JsonSerializer<GitHubIssues.Issue>();

			var issue1 = new GitHubIssues.Issue()
			{
				Id = 1,
				Number = 1,
				Title = "Issue 1", 
				Body = "Issue 1",
				State = "Open"
			};

			var restResponse1 = new RestResponse() { Content = serializer.SerializeToString(issue1), StatusCode = HttpStatusCode.OK };

			MockRestClient.Setup(x => x.Execute(It.Is<RestRequest>(y => y.Resource.Contains("issues/1") && y.Method == Method.GET))).Returns(restResponse1);
			MockRestClient.Setup(x => x.Execute(It.Is<RestRequest>(y => y.Resource.Contains("issues/1") && y.Method == Method.PATCH))).Returns(restResponse1);

			var issue2 = new GitHubIssues.Issue()
			{
				Id = 2,
				Number = 2,
				Title = "Issue 2",
				Body = "Issue 2",
				State = "Open"
			};

			var restResponse2 = new RestResponse() { Content = serializer.SerializeToString(issue2), StatusCode = HttpStatusCode.OK };

			MockRestClient.Setup(x => x.Execute(It.Is<RestRequest>(y => y.Resource.Contains("issues/2") && y.Method == Method.GET))).Returns(restResponse2);
			MockRestClient.Setup(x => x.Execute(It.Is<RestRequest>(y => y.Resource.Contains("issues/2") && y.Method == Method.PATCH))).Returns(restResponse2);

			var issue3 = new GitHubIssues.Issue()
			{
				Id = 3,
				Number = 3,
				Title = "Issue 3",
				Body = "Issue 3",
				State = "Open"
			};

			var restResponse3 = new RestResponse() { Content = serializer.SerializeToString(issue3), StatusCode = HttpStatusCode.OK };

			MockRestClient.Setup(x => x.Execute(It.Is<RestRequest>(y => y.Resource.Contains("issues/3") && y.Method == Method.GET))).Returns(restResponse3);
			MockRestClient.Setup(x => x.Execute(It.Is<RestRequest>(y => y.Resource.Contains("issues/3") && y.Method == Method.PATCH))).Returns(restResponse3);

			var issue4 = new GitHubIssues.Issue()
			{
				Id = 4,
				Number = 4,
				Title = "Issue 4",
				Body = "Issue 4",
				State = "Open"
			};

			var restResponse4 = new RestResponse() { Content = serializer.SerializeToString(issue4), StatusCode = HttpStatusCode.OK };

			MockRestClient.Setup(x => x.Execute(It.Is<RestRequest>(y => y.Resource.Contains("issues/4") && y.Method == Method.GET))).Returns(restResponse4);
			MockRestClient.Setup(x => x.Execute(It.Is<RestRequest>(y => y.Resource.Contains("issues/4") && y.Method == Method.PATCH))).Returns(restResponse4);		
		}
		public void It_should_return_mapped_card_type_if_issue_has_matching_label() 
		{
			var issue = new GitHubIssues.Issue() { Labels = new List<GitHubIssues.Label>() { new GitHubIssues.Label() { Name = "Roger" } } };
			GithubConversionExtensions.CalculateLeanKitCardType(_mapping, issue).Id.ShouldEqual(_testBoard.CardTypes.FirstOrDefault(x => x.Name == "Willy").Id);
		}
		public void It_should_return_default_card_type_if_issue_has_no_matching_labels() 
		{
			var issue = new GitHubIssues.Issue() { Labels = new List<GitHubIssues.Label>() { new GitHubIssues.Label() { Name = "Bob"}}};
			GithubConversionExtensions.CalculateLeanKitCardType(_mapping, issue).Id.ShouldEqual(_testBoard.CardTypes.Last().Id);
		}
		public void It_should_return_default_card_type_if_issue_has_no_labels()
		{
			var issue = new GitHubIssues.Issue();
			GithubConversionExtensions.CalculateLeanKitCardType(_mapping, (GitHubIssues.Issue)null).Id.ShouldEqual(_testBoard.CardTypes.Last().Id);
			GithubConversionExtensions.CalculateLeanKitCardType(_mapping, issue).Id.ShouldEqual(_testBoard.CardTypes.Last().Id);
		}