From cc33e8ace9174f417e0960d65fc3320b4d919455 Mon Sep 17 00:00:00 2001 From: Silvan <61540155+ni507@users.noreply.github.com> Date: Thu, 1 Apr 2021 14:29:05 +0200 Subject: [PATCH] fix: issue search in --- .../Internal/Queries/IssuesQueryBuilder.cs | 18 ++++++++ .../Issues/Requests/IssuesQueryOptions.cs | 7 ++- .../Models/Issues/Requests/SearchIn.cs | 9 ++++ .../Queries/IssuesQueryBuilderTest.cs | 4 ++ test/GitLabApiClient.Test/IssuesClientTest.cs | 44 +++++++++++++++++++ 5 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/GitLabApiClient/Models/Issues/Requests/SearchIn.cs diff --git a/src/GitLabApiClient/Internal/Queries/IssuesQueryBuilder.cs b/src/GitLabApiClient/Internal/Queries/IssuesQueryBuilder.cs index 83415695..b83abe58 100644 --- a/src/GitLabApiClient/Internal/Queries/IssuesQueryBuilder.cs +++ b/src/GitLabApiClient/Internal/Queries/IssuesQueryBuilder.cs @@ -44,6 +44,9 @@ protected override void BuildCore(Query query, IssuesQueryOptions options) if (!options.Filter.IsNullOrEmpty()) query.Add("search", options.Filter); + if (options.In != SearchIn.TitleAndDescription) + query.Add("in", GetSearchInQueryValue(options.In)); + if (options.IsConfidential) query.Add("confidential", true); @@ -87,5 +90,20 @@ private static string GetIssuesOrderQueryValue(IssuesOrder order) throw new NotSupportedException($"Order {order} is not supported"); } } + + private static string GetSearchInQueryValue(SearchIn searchIn) + { + switch (searchIn) + { + case SearchIn.Title: + return "title"; + case SearchIn.Description: + return "description"; + case SearchIn.TitleAndDescription: + return "title,description"; + default: + throw new NotSupportedException($"SearchIn {searchIn} is not supported"); + } + } } } diff --git a/src/GitLabApiClient/Models/Issues/Requests/IssuesQueryOptions.cs b/src/GitLabApiClient/Models/Issues/Requests/IssuesQueryOptions.cs index bc91cc68..3313b41e 100644 --- a/src/GitLabApiClient/Models/Issues/Requests/IssuesQueryOptions.cs +++ b/src/GitLabApiClient/Models/Issues/Requests/IssuesQueryOptions.cs @@ -18,7 +18,7 @@ internal IssuesQueryOptions() { } public IssueState State { get; set; } /// - /// List of label names, issues must have all labels to be returned. + /// List of label names, issues must have all labels to be returned. /// No+Label lists all issues with no labels. /// public IList Labels { get; set; } = new List(); @@ -75,6 +75,11 @@ internal IssuesQueryOptions() { } /// public string Filter { get; set; } + /// + /// Modify the scope of the search attribute. Title, Description, or TitleAndDescription. Default is TitleAndDescription + /// + public SearchIn In { get; set; } + /// /// Return issues created after the given time (inclusive) /// diff --git a/src/GitLabApiClient/Models/Issues/Requests/SearchIn.cs b/src/GitLabApiClient/Models/Issues/Requests/SearchIn.cs new file mode 100644 index 00000000..9ba951ab --- /dev/null +++ b/src/GitLabApiClient/Models/Issues/Requests/SearchIn.cs @@ -0,0 +1,9 @@ +namespace GitLabApiClient.Models.Issues.Requests +{ + public enum SearchIn + { + TitleAndDescription, + Title, + Description, + } +} diff --git a/test/GitLabApiClient.Test/Internal/Queries/IssuesQueryBuilderTest.cs b/test/GitLabApiClient.Test/Internal/Queries/IssuesQueryBuilderTest.cs index 8c739e1e..668799af 100644 --- a/test/GitLabApiClient.Test/Internal/Queries/IssuesQueryBuilderTest.cs +++ b/test/GitLabApiClient.Test/Internal/Queries/IssuesQueryBuilderTest.cs @@ -30,6 +30,7 @@ public void NonDefaultQueryBuilt() Order = IssuesOrder.UpdatedAt, SortOrder = SortOrder.Ascending, Filter = "filter", + In = SearchIn.Description, CreatedAfter = new DateTime(1991, 11, 11, 1, 1, 1), CreatedBefore = new DateTime(1991, 12, 12, 2, 2, 2), UpdatedAfter = new DateTime(1991, 4, 4, 4, 4, 4), @@ -50,6 +51,7 @@ public void NonDefaultQueryBuilt() "order_by=updated_at&" + "sort=asc&" + "search=filter&" + + "in=description&" + "confidential=true&" + "created_before=1991-12-12T02%3a02%3a02.0000000&" + "created_after=1991-11-11T01%3a01%3a01.0000000&" + @@ -76,6 +78,7 @@ public void NonDefaultQueryBuiltWithUserNames() Order = IssuesOrder.UpdatedAt, SortOrder = SortOrder.Ascending, Filter = "filter", + In = SearchIn.Title, CreatedAfter = new DateTime(1991, 11, 11, 1, 1, 1), CreatedBefore = new DateTime(1991, 12, 12, 2, 2, 2), UpdatedAfter = new DateTime(1991, 4, 4, 4, 4, 4), @@ -96,6 +99,7 @@ public void NonDefaultQueryBuiltWithUserNames() "order_by=updated_at&" + "sort=asc&" + "search=filter&" + + "in=title&" + "confidential=true&" + "created_before=1991-12-12T02%3a02%3a02.0000000&" + "created_after=1991-11-11T01%3a01%3a01.0000000&" + diff --git a/test/GitLabApiClient.Test/IssuesClientTest.cs b/test/GitLabApiClient.Test/IssuesClientTest.cs index f333e13d..9413796c 100644 --- a/test/GitLabApiClient.Test/IssuesClientTest.cs +++ b/test/GitLabApiClient.Test/IssuesClientTest.cs @@ -190,5 +190,49 @@ public async Task CreateIssueWithTasks() i.TaskCompletionStatus.Completed == 1 && i.TimeStats != null); } + + [Fact] + public async Task CreatedIssueCanBeRetrievedByTitleOrDescription() + { + //arrange + string title = Guid.NewGuid().ToString(); + string description = "Description"; + var issue = await _sut.CreateAsync(TestProjectTextId, new CreateIssueRequest(title) + { + Description = description + }); + + //act + var issueSearchedInTitle = (await _sut.GetAllAsync(TestProjectTextId, options: o => + { + o.In = SearchIn.Title; + o.Filter = title; + })) + .FirstOrDefault(); + + var issueSearchedInDescription = (await _sut.GetAllAsync(TestProjectTextId, options: o => + { + o.In = SearchIn.Description; + o.Filter = description; + })) + .FirstOrDefault(); + + var issueSearchedInTitleAndDescription = (await _sut.GetAllAsync(TestProjectTextId, options: o => + { + o.In = SearchIn.TitleAndDescription; + o.Filter = description; + })) + .FirstOrDefault(); + + //assert + issue.Should().Match(i => + i.ProjectId == TestProjectTextId && + i.Title == title && + i.Description == description); + + issueSearchedInTitle.Should().BeEquivalentTo(issue, o => o.Excluding(s => s.UpdatedAt)); + issueSearchedInDescription.Should().BeEquivalentTo(issue, o => o.Excluding(s => s.UpdatedAt)); + issueSearchedInTitleAndDescription.Should().BeEquivalentTo(issue, o => o.Excluding(s => s.UpdatedAt)); + } } }