Esempio n. 1
0
 private void SetAssignedUser(WorkItem workItem, long boardId, long userId)
 {
     try
     {
         var lkUser = LeanKit.GetBoard(boardId).BoardUsers.FirstOrDefault(x => x.Id == userId);
         if (lkUser != null)
         {
             var gss     = (IGroupSecurityService)_projectCollection.GetService(typeof(IGroupSecurityService));
             var sids    = gss.ReadIdentity(SearchFactor.AccountName, "Project Collection Valid Users", QueryMembership.Expanded);
             var users   = gss.ReadIdentities(SearchFactor.Sid, sids.Members, QueryMembership.None);
             var tfsUser = users.FirstOrDefault(x =>
                                                ((!string.IsNullOrEmpty(lkUser.EmailAddress)) && (!string.IsNullOrEmpty(x.MailAddress)) && lkUser.EmailAddress.ToLowerInvariant() == x.MailAddress.ToLowerInvariant()) ||
                                                ((!string.IsNullOrEmpty(lkUser.FullName)) && (!string.IsNullOrEmpty(x.DisplayName)) && lkUser.FullName.ToLowerInvariant() == x.DisplayName.ToLowerInvariant()) ||
                                                ((!string.IsNullOrEmpty(lkUser.UserName)) && (!string.IsNullOrEmpty(x.AccountName)) && lkUser.UserName.ToLowerInvariant() == x.AccountName.ToLowerInvariant()));
             if (tfsUser != null && tfsUser.DisplayName != null)
             {
                 workItem.Fields["System.AssignedTo"].Value = tfsUser.DisplayName;
             }
         }
     }
     catch (Exception ex)
     {
         Log.Error(string.Format("An error occurred: {0} - {1} - {2}", ex.GetType(), ex.Message, ex.StackTrace));
     }
 }
Esempio n. 2
0
 public long?CalculateAssignedUserId(long boardId, string assignedTo)
 {
     if (!String.IsNullOrEmpty(assignedTo))
     {
         if (_tfsUsers != null && _tfsUsers.Any())
         {
             var user = _tfsUsers.FirstOrDefault(x =>
                                                 x != null &&
                                                 x.DisplayName != null &&
                                                 !String.IsNullOrEmpty(x.DisplayName) &&
                                                 x.DisplayName.ToLowerInvariant() == assignedTo.ToLowerInvariant());
             if (user != null)
             {
                 var lkUser = LeanKit.GetBoard(boardId).BoardUsers.FirstOrDefault(x => x != null &&
                                                                                  (((!String.IsNullOrEmpty(x.EmailAddress)) && (!String.IsNullOrEmpty(user.MailAddress)) && x.EmailAddress.ToLowerInvariant() == user.MailAddress.ToLowerInvariant()) || ((!String.IsNullOrEmpty(x.FullName)) && (!String.IsNullOrEmpty(user.DisplayName)) && x.FullName.ToLowerInvariant() == user.DisplayName.ToLowerInvariant()) ||
                                                                                   ((!String.IsNullOrEmpty(x.FullName)) && (!string.IsNullOrEmpty(user.DisplayName)) && x.FullName.ToLowerInvariant() == user.DisplayName.ToLowerInvariant()) ||
                                                                                   ((!String.IsNullOrEmpty(x.UserName)) && (!String.IsNullOrEmpty(user.AccountName)) && x.UserName.ToLowerInvariant() == user.AccountName.ToLowerInvariant())));
                 if (lkUser != null)
                 {
                     return(lkUser.Id);
                 }
             }
         }
     }
     return(null);
 }
Esempio n. 3
0
        private void UpdateBoardVersion(long boardId, long?version = null)
        {
            if (!version.HasValue)
            {
                var board = LeanKit.GetBoard(boardId);
                if (board == null)
                {
                    return;
                }
                version = board.Version;
            }

            if (AppSettings.BoardVersions.ContainsKey(boardId))
            {
                AppSettings.BoardVersions[boardId] = version.Value;
            }
            else
            {
                AppSettings.BoardVersions.Add(boardId, version.Value);
            }
            LocalStorage.Save(AppSettings);
        }
Esempio n. 4
0
        public long?CalculateAssignedUserId(long boardId, Ticket ticket)
        {
            if (ticket == null)
            {
                return(null);
            }

            if (ticket.Assignee_Id > 0)
            {
                // http://mysubdomain.unfuddle.com/api/v1/people/{id}
                var request      = new RestRequest(string.Format("/api/v1/people/{0}", ticket.Assignee_Id), Method.GET);
                var unfuddleResp = _restClient.Execute(request);

                if (unfuddleResp.StatusCode != HttpStatusCode.OK)
                {
                    var serializer   = new JsonSerializer <ErrorMessage>();
                    var errorMessage = serializer.DeserializeFromString(unfuddleResp.Content);
                    Log.Warn(string.Format("Unable to get user from Unfuddle, Error: {0}", errorMessage.Message));
                }
                else
                {
                    var user = new JsonSerializer <Person>().DeserializeFromString(unfuddleResp.Content);

                    if (user != null)
                    {
                        var lkUser = LeanKit.GetBoard(boardId).BoardUsers.FirstOrDefault(x => x != null &&
                                                                                         ((!string.IsNullOrEmpty(x.EmailAddress)) && (!string.IsNullOrEmpty(user.Email)) && x.EmailAddress.ToLowerInvariant() == user.Email.ToLowerInvariant()) ||
                                                                                         ((!string.IsNullOrEmpty(x.FullName)) && (!string.IsNullOrEmpty(user.Last_Name)) && x.FullName.ToLowerInvariant() == (user.First_Name + " " + user.Last_Name).ToLowerInvariant()) ||
                                                                                         ((!string.IsNullOrEmpty(x.UserName)) && (!string.IsNullOrEmpty(user.Username)) && x.UserName.ToLowerInvariant() == user.Username.ToLowerInvariant()));
                        if (lkUser != null)
                        {
                            return(lkUser.Id);
                        }
                    }
                }
            }

            return(null);
        }
Esempio n. 5
0
        private void LoadBoardValues(BoardMapping boardMapping)
        {
            Board board = null;

            try
            {
                board = LeanKit.GetBoard(boardMapping.Identity.LeanKit);
            }
            catch (LeanKitAPIException ex)
            {
                Log.Error(ex, string.Format("Error getting Board: {0}", boardMapping.Identity.LeanKit));
            }

            if (board == null)
            {
                return;
            }

            if (board.CardTypes != null && board.CardTypes.Any())
            {
                boardMapping.ValidCardTypes = board.CardTypes;

                // check to make sure we have a default card type
                var defaultCard = boardMapping.ValidCardTypes.FirstOrDefault(x => x.IsDefault);
                if (defaultCard == null)
                {
                    // if we do not have a default card type then check
                    // to see if there is a Task card type and make that the default
                    var taskCardType = boardMapping.ValidCardTypes.FirstOrDefault(x => x.Name.ToLowerInvariant() == "task");
                    if (taskCardType != null)
                    {
                        boardMapping.ValidCardTypes.FirstOrDefault(x => x.Name.ToLowerInvariant() == "task").IsDefault = true;
                    }
                    else
                    {
                        // otherwise just set the first card type to be the default
                        boardMapping.ValidCardTypes.FirstOrDefault().IsDefault = true;
                    }
                }
            }

            if (board.ArchiveTopLevelLaneId.HasValue)
            {
                boardMapping.ArchiveLaneId = board.ArchiveTopLevelLaneId.Value;
            }
            else
            {
                var archive = board.Archive.FirstOrDefault(x => x.ParentLaneId == 0);
                if (archive != null && archive.Id.HasValue)
                {
                    boardMapping.ArchiveLaneId = archive.Id.Value;
                }

                if (boardMapping.ArchiveLaneId == 0)
                {
                    var allLanes = board.AllLanes();
                    archive = allLanes.FirstOrDefault(x => x.ClassType == LaneClassType.Archive && x.ParentLaneId == 0);
                    if (archive != null && archive.Id.HasValue)
                    {
                        boardMapping.ArchiveLaneId = archive.Id.Value;
                    }
                }
            }

            if (board.Lanes != null && board.Lanes.Any())
            {
                var validLanes = board.AllLanes().Where(x => x.ClassType != LaneClassType.Archive).OrderBy(x => x.Index).ToList();
                var maxIndex   = validLanes.Max(x => x.Index);

                // only use active lanes for the purpose of selecting the default drop lane
                var activeLanes       = board.Lanes.Where(x => x.ClassType == LaneClassType.Active).OrderBy(x => x.Index).ToList();
                var defaultDropLaneId = GetDefaultDropLane(activeLanes);

                boardMapping.ValidLanes = validLanes
                                          .Select(x => new Lane
                {
                    Id           = x.Id.Value,
                    Name         = x.Title,
                    IsFirst      = x.Id == defaultDropLaneId,
                    ChildLaneIds = (x.ChildLaneIds != null && x.ChildLaneIds.Any()) ? x.ChildLaneIds : null,
                    IsLast       = (boardMapping.ArchiveLaneId > 0) ? x.Id == boardMapping.ArchiveLaneId : x.Index == maxIndex
                })
                                          .ToList();

                if (boardMapping.ArchiveLaneId > 0)
                {
                    var archiveLane = board.GetLaneById(boardMapping.ArchiveLaneId);
                    if (archiveLane != null)
                    {
                        boardMapping.ValidLanes.Add(new Lane
                        {
                            Id      = boardMapping.ArchiveLaneId,
                            Name    = archiveLane.Title,
                            IsFirst = false,
                            IsLast  = true
                        });
                    }
                }
            }

            if (boardMapping.Types == null)
            {
                boardMapping.Types = new List <WorkItemType>();
            }

            // values in LaneToStatesMap are assumed to valid, as they were configured using valid values.
            if (boardMapping.LaneToStatesMap == null)
            {
                Log.Fatal("An unexpected error occurred -- there is no valid lane-to-states mapping.");
            }
        }
Esempio n. 6
0
        private void CheckForMissedCardMoves(BoardMapping mapping)
        {
            // if we have local storage, we have saved board versions and we have one for this board
            long boardId = mapping.Identity.LeanKit;

            if (AppSettings != null &&
                AppSettings.BoardVersions != null &&
                AppSettings.BoardVersions.Any() &&
                AppSettings.BoardVersions.ContainsKey(boardId))
            {
                var version = AppSettings.BoardVersions[boardId];
                Log.Debug(string.Format("Checking for any cards moved to mapped lanes on board [{0}] since service last ran, version [{1}].", boardId, version));
                try
                {
                    var events = LeanKit.GetBoardHistorySince(boardId, (int)version);
                    var board  = LeanKit.GetBoard(boardId);
                    if (board != null && events != null)
                    {
                        foreach (var ev in events)
                        {
                            // check for created cards
                            if (ev.EventType == "CardCreation")
                            {
                                var card = LeanKit.GetCard(board.Id, ev.CardId);
                                if (card != null && string.IsNullOrEmpty(card.ExternalCardID))
                                {
                                    try
                                    {
                                        CreateNewItem(card.ToCard(), mapping);
                                    }
                                    catch (Exception e)
                                    {
                                        Log.Error("Exception for CreateNewItem: " + e.Message);
                                    }
                                }
                            }
                            // only look for moved cards
                            else if (ev.ToLaneId != 0)
                            {
                                var lane = board.GetLaneById(ev.ToLaneId);
                                if (lane != null)
                                {
                                    if (lane.Id.HasValue && mapping.LaneToStatesMap.Any() && mapping.LaneToStatesMap.ContainsKey(lane.Id.Value))
                                    {
                                        if (mapping.LaneToStatesMap[lane.Id.Value] != null && mapping.LaneToStatesMap[lane.Id.Value].Count > 0)
                                        {
                                            // board.GetCard() only seems to get cards in active lanes
                                            // using LeanKitApi.GetCard() instead because it will get
                                            // cards in archive lanes
                                            var card = LeanKit.GetCard(board.Id, ev.CardId);
                                            if (card != null && !string.IsNullOrEmpty(card.ExternalCardID))
                                            {
                                                try {
                                                    UpdateStateOfExternalItem(card.ToCard(), mapping.LaneToStatesMap[lane.Id.Value], mapping);
                                                } catch (Exception e) {
                                                    Log.Error("Exception for UpdateStateOfExternalItem: " + e.Message);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        UpdateBoardVersion(board.Id, board.Version);
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(string.Format("An error occured: {0} - {1} - {2}", ex.GetType(), ex.Message, ex.StackTrace));
                }
            }
        }