-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathProjectSearchClient.cs
More file actions
82 lines (69 loc) · 2.88 KB
/
ProjectSearchClient.cs
File metadata and controls
82 lines (69 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections.Generic;
using System.IO;
using NGitLab.Extensions;
using NGitLab.Mock.Internals;
using NGitLab.Models;
namespace NGitLab.Mock.Clients;
internal sealed class ProjectSearchClient : ClientBase, ISearchClient
{
private readonly ClientContext _context;
private readonly long _projectId;
public ProjectSearchClient(ClientContext context, ProjectId projectId)
: base(context)
{
_context = context;
_projectId = Server.AllProjects.FindProject(projectId.ValueAsString()).Id;
}
public GitLabCollectionResponse<SearchBlob> GetBlobsAsync(SearchQuery query)
{
if (query.Search.StartsWith("filename:", StringComparison.Ordinal))
return SearchByFilename(query);
if (query.Search.StartsWith("path:", StringComparison.Ordinal))
return SearchByPath(query);
throw new NotImplementedException();
}
private GitLabCollectionResponse<SearchBlob> SearchByPath(SearchQuery query)
{
var path = query.Search["path:".Length..];
var project = _context.Server.AllProjects.FindProject(_projectId.ToStringInvariant());
var rootUri = new Uri(project.HttpUrl.TrimEnd('/') + '/');
var expectedUri = new Uri(rootUri, path);
var expectedFilePath = expectedUri.LocalPath;
if (!System.IO.File.Exists(expectedFilePath))
return GitLabCollectionResponse.Create(Array.Empty<SearchBlob>());
return GitLabCollectionResponse.Create([
new SearchBlob
{
FileName = Path.GetFileName(expectedFilePath),
Ref = project.DefaultBranch,
ProjectId = project.Id,
Path = rootUri.MakeRelativeUri(expectedUri).ToString(),
Data = System.IO.File.ReadAllText(expectedFilePath),
},
]);
}
private GitLabCollectionResponse<SearchBlob> SearchByFilename(SearchQuery query)
{
var filename = query.Search["filename:".Length..];
var project = _context.Server.AllProjects.FindProject(_projectId.ToStringInvariant());
var rootUri = new Uri(project.HttpUrl.TrimEnd('/') + '/');
var files = Directory.GetFiles(rootUri.LocalPath, filename, searchOption: SearchOption.AllDirectories);
if (files.Length == 0)
return GitLabCollectionResponse.Create(Array.Empty<SearchBlob>());
var blobs = new List<SearchBlob>();
foreach (var file in files)
{
var fileUri = new Uri(file);
blobs.Add(new SearchBlob
{
FileName = Path.GetFileName(file),
Ref = project.DefaultBranch,
ProjectId = project.Id,
Path = rootUri.MakeRelativeUri(fileUri).ToString(),
Data = System.IO.File.ReadAllText(file),
});
}
return GitLabCollectionResponse.Create(blobs);
}
}