public async Task CreateWorkItemAsync_OnWorkItemWithFeatureIssueAndTextTypes_SetsIssueTypeLabel(string codePlexWorkItemType, string gitHubLabel)
        {
            // Arrange
            var issuesMock = new GitHubIssueMock();
            var ctorArgs   = new CtorArgs {
                Issues = issuesMock.Issues
            };
            GitHubRepoIssueReaderWriter target          = CreateTarget(ctorArgs);
            WorkItemDetails             workItemDetails = CreateSampleWorkItemDetails(type: codePlexWorkItemType);

            // Act
            await target.WriteWorkItemAsync(workItemDetails);

            // Assert
            if (gitHubLabel != null)
            {
                Assert.Equal(2, issuesMock.UpdateIssueArgs.IssueUpdate.Labels.Count);
                Assert.Contains(gitHubLabel, issuesMock.UpdateIssueArgs.IssueUpdate.Labels);
            }
            else
            {
                Assert.Single(issuesMock.UpdateIssueArgs.IssueUpdate.Labels);
            }

            Assert.Contains(GitHubLabels.CodePlexMigrated, issuesMock.UpdateIssueArgs.IssueUpdate.Labels);
        }
        public async Task CreateWorkItemAsync_OnDuplicateWorkItem_SetsDuplicateLabel(bool isIssueDuplicate)
        {
            // Arrange
            var issuesMock = new GitHubIssueMock();
            var ctorArgs   = new CtorArgs {
                Issues = issuesMock.Issues
            };
            GitHubRepoIssueReaderWriter target          = CreateTarget(ctorArgs);
            WorkItemDetails             workItemDetails = CreateSampleWorkItemDetails(isDuplicate: isIssueDuplicate);

            // Act
            await target.WriteWorkItemAsync(workItemDetails);

            // Assert
            if (isIssueDuplicate)
            {
                Assert.Equal(2, issuesMock.UpdateIssueArgs.IssueUpdate.Labels.Count);
                Assert.Contains(GitHubLabels.Duplicate, issuesMock.UpdateIssueArgs.IssueUpdate.Labels);
            }
            else
            {
                Assert.Single(issuesMock.UpdateIssueArgs.IssueUpdate.Labels);
            }

            Assert.Contains(GitHubLabels.CodePlexMigrated, issuesMock.UpdateIssueArgs.IssueUpdate.Labels);
        }
        public async Task UpdateWorkItemAsync_IfWorkItemExists_Succeeds(bool hasAttachments)
        {
            // Arrange
            Issue           issue           = CreateSampleIssue(label: "AnyLabel");
            WorkItemDetails workItemDetails = CreateSampleWorkItemDetails(hasAttachments: hasAttachments);

            var      issuesMock = new GitHubIssueMock();
            var      searchMock = new GitHubSearchInBodyMock(searchResults: new[] { issue });
            CtorArgs ctorArgs   = new CtorArgs {
                Search = searchMock.Search, Issues = issuesMock.Issues
            };

            GitHubRepoIssueReaderWriter target = CreateTarget(ctorArgs);

            // Act
            await target.UpdateWorkItemAsync(workItemDetails);

            // Assert: Search
            Assert.Contains(workItemDetails.WorkItem.Id.ToString(), searchMock.SearchIssuesRequest.Term);
            Assert.Equal(IssueTypeQualifier.Issue, searchMock.SearchIssuesRequest.Type);
            Assert.Single(searchMock.SearchIssuesRequest.Repos);
            Assert.Equal(ctorArgs.FullRepoName, searchMock.SearchIssuesRequest.Repos[0]);
            Assert.Contains(searchMock.SearchIssuesRequest.In, q => q == IssueInQualifier.Body);
            searchMock.VerifySearchCallCount(callCount: 1);

            // Assert: Update -- Owner + Repo + Issue number
            Assert.Equal(ctorArgs.RepoOwner, issuesMock.UpdateIssueArgs.Owner);
            Assert.Equal(ctorArgs.Repo, issuesMock.UpdateIssueArgs.Name);
            Assert.Equal(issue.Number, issuesMock.UpdateIssueArgs.Number);

            // Assert: Update -- Title + Body
            Assert.Equal(workItemDetails.WorkItem.Summary, issuesMock.UpdateIssueArgs.IssueUpdate.Title);
            Assert.Contains(TextUtilities.GetFormattedWorkItemBody(workItemDetails.WorkItem, workItemDetails.FileAttachments), issuesMock.UpdateIssueArgs.IssueUpdate.Body);

            // Assert: Update -- Labels
            Assert.Single(issuesMock.UpdateIssueArgs.IssueUpdate.Labels);
            Assert.Contains(issuesMock.UpdateIssueArgs.IssueUpdate.Labels, label => label == GitHubLabels.CodePlexMigrated);

            // Assert: Update -- Attachments
            if (hasAttachments)
            {
                Assert.Contains(Resources.Attachments, issuesMock.UpdateIssueArgs.IssueUpdate.Body);
            }
            else
            {
                Assert.DoesNotContain(Resources.Attachments, issuesMock.UpdateIssueArgs.IssueUpdate.Body);
            }

            issuesMock.VerifyIssuesCallCount(methodName: nameof(IIssuesClient.Update), callCount: 2);
        }
        public async Task CreateWorkItemAsync_OnNonNullWorkItemDetailsAndWorkItem_CreatesIssue(bool isIssueClosed, bool hasAttachments)
        {
            // Arrange
            var issuesMock = new GitHubIssueMock();
            var ctorArgs   = new CtorArgs {
                Issues = issuesMock.Issues
            };
            GitHubRepoIssueReaderWriter target          = CreateTarget(ctorArgs);
            WorkItemDetails             workItemDetails = CreateSampleWorkItemDetails(hasAttachments: hasAttachments, isClosed: isIssueClosed);

            // Act
            await target.WriteWorkItemAsync(workItemDetails);

            // Assert: Owner + Repo
            Assert.NotNull(issuesMock.CreateIssueArgs.NewIssue);
            Assert.Equal(ctorArgs.RepoOwner, issuesMock.CreateIssueArgs.Owner);
            Assert.Equal(ctorArgs.Repo, issuesMock.CreateIssueArgs.Name);

            // Assert: Title + Body
            Assert.Equal(workItemDetails.WorkItem.Summary, issuesMock.CreateIssueArgs.NewIssue.Title);
            Assert.Contains(TextUtilities.GetFormattedWorkItemBody(workItemDetails.WorkItem, workItemDetails.FileAttachments), issuesMock.CreateIssueArgs.NewIssue.Body);

            // Assert: Labels
            Assert.Single(issuesMock.CreateIssueArgs.NewIssue.Labels);
            Assert.Contains(issuesMock.CreateIssueArgs.NewIssue.Labels, label => label == GitHubLabels.CodePlexMigrationInitiated);
            Assert.Single(issuesMock.UpdateIssueArgs.IssueUpdate.Labels);
            Assert.Contains(issuesMock.UpdateIssueArgs.IssueUpdate.Labels, label => label == GitHubLabels.CodePlexMigrated);

            // Assert: Attachments
            if (hasAttachments)
            {
                Assert.Contains(Resources.Attachments, issuesMock.CreateIssueArgs.NewIssue.Body);
            }
            else
            {
                Assert.DoesNotContain(Resources.Attachments, issuesMock.CreateIssueArgs.NewIssue.Body);
            }

            // Assert: Issue state: closed/open
            Assert.Equal(isIssueClosed ? ItemState.Closed : (ItemState?)null, issuesMock.UpdateIssueArgs.IssueUpdate.State);

            issuesMock.VerifyIssuesCallCount(methodName: nameof(IIssuesClient.Create), callCount: 1);
            issuesMock.VerifyIssuesCallCount(methodName: nameof(IIssuesClient.Update), callCount: 1);
            issuesMock.VerifyCommentCallCount(methodName: nameof(IIssueCommentsClient.Create), callCount: 0);
            issuesMock.VerifyCommentCallCount(methodName: nameof(IIssueCommentsClient.Delete), callCount: 0);
        }
        public async Task UpdateWorkItemAsync_IfWorkItemHasComments_DropsAndRecreatesComments()
        {
            // Arrange
            int   issueCommentCount = 10;
            Issue issue             = CreateSampleIssue(label: "AnyLabel");

            IssueComment[]  issueComments   = Enumerable.Range(0, issueCommentCount).Select(i => CreateSampleComment(issue.Number)).ToArray();
            WorkItemDetails workItemDetails = CreateSampleWorkItemDetails(hasComments: true);

            var issuesMock = new GitHubIssueMock();

            issuesMock.SetCommentsForIssue(issue.Number, issueComments);

            var      searchMock = new GitHubSearchInBodyMock(searchResults: new[] { issue });
            CtorArgs ctorArgs   = new CtorArgs {
                Search = searchMock.Search, Issues = issuesMock.Issues
            };

            GitHubRepoIssueReaderWriter target = CreateTarget(ctorArgs);

            // Act
            await target.UpdateWorkItemAsync(workItemDetails);

            // Assert: Search
            searchMock.VerifySearchCallCount(callCount: 1);

            // Assert: Delete comments
            Assert.True(issuesMock.DeleteCommentArgs.All(args => args.Owner == ctorArgs.RepoOwner));
            Assert.True(issuesMock.DeleteCommentArgs.All(args => args.Name == ctorArgs.Repo));
            Assert.Equal(issueComments.Select(c => c.Id), issuesMock.DeleteCommentArgs.Select(args => args.Id));
            issuesMock.VerifyCommentCallCount(nameof(IIssueCommentsClient.Delete), callCount: issueCommentCount);

            // Assert: Create comments
            Assert.True(issuesMock.CreateCommentArgs.All(args => args.Owner == ctorArgs.RepoOwner));
            Assert.True(issuesMock.CreateCommentArgs.All(args => args.Name == ctorArgs.Repo));
            Assert.True(issuesMock.CreateCommentArgs.All(args => args.Number == issue.Number));

            string[] createdComments = issuesMock.CreateCommentArgs.Select(c => c.NewComment).ToArray();
            foreach (WorkItemComment comment in workItemDetails.Comments)
            {
                Assert.Contains(createdComments, c => c.Contains(comment.Message));
            }

            issuesMock.VerifyCommentCallCount(nameof(IIssueCommentsClient.Create), callCount: workItemDetails.Comments.Count());
        }
        public async Task CreateWorkItemAsync_OnWorkItemWithReleaseComponentAndPriority_SetsTextLabels()
        {
            // Arrange
            var issuesMock = new GitHubIssueMock();
            var ctorArgs   = new CtorArgs {
                Issues = issuesMock.Issues
            };
            GitHubRepoIssueReaderWriter target          = CreateTarget(ctorArgs);
            WorkItemDetails             workItemDetails = CreateSampleWorkItemDetails(hasSpecialProperties: true);

            // Act
            await target.WriteWorkItemAsync(workItemDetails);

            // Assert
            Assert.Equal(4, issuesMock.UpdateIssueArgs.IssueUpdate.Labels.Count);
            Assert.Contains(workItemDetails.WorkItem.PlannedForRelease, issuesMock.UpdateIssueArgs.IssueUpdate.Labels);
            Assert.Contains(workItemDetails.WorkItem.AffectedComponent.DisplayName, issuesMock.UpdateIssueArgs.IssueUpdate.Labels);
            Assert.Contains(issuesMock.UpdateIssueArgs.IssueUpdate.Labels, label => label.Contains(GitHubLabels.Impact) && label.Contains(workItemDetails.WorkItem.Priority.Name));
            Assert.Contains(GitHubLabels.CodePlexMigrated, issuesMock.UpdateIssueArgs.IssueUpdate.Labels);
        }
        public async Task CreateWorkItemAsync_OnWorkItemWithComments_CreatesCommentsOnIssue()
        {
            // Arrange
            var issuesMock = new GitHubIssueMock();
            var ctorArgs   = new CtorArgs {
                Issues = issuesMock.Issues
            };
            GitHubRepoIssueReaderWriter target          = CreateTarget(ctorArgs);
            WorkItemDetails             workItemDetails = CreateSampleWorkItemDetails(hasComments: true);

            // Act
            await target.WriteWorkItemAsync(workItemDetails);

            // Assert
            Assert.True(issuesMock.CreateCommentArgs.All(argSet => argSet.Name == ctorArgs.Repo && argSet.Owner == ctorArgs.RepoOwner));
            issuesMock.VerifyCommentCallCount(methodName: nameof(IIssueCommentsClient.Create), callCount: workItemDetails.Comments.Count());
            foreach (WorkItemComment comment in workItemDetails.Comments)
            {
                Assert.Contains(issuesMock.CreateCommentArgs, argSet => argSet.NewComment.Contains(comment.Message));
            }
        }