Skip to content

Commit e67e575

Browse files
authored
🚸 feat: can be started when gitlabconfig is not set in DCC (#425)
* 🚸 feat: can be started when gitlabconfig is not set in DCC * format
1 parent 530e33f commit e67e575

6 files changed

+83
-69
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the Apache License. See LICENSE.txt in the project root for license information.
3+
4+
using NGitLab;
5+
using NGitLab.Models;
6+
7+
namespace MASA.PM.Web.Docs;
8+
9+
public class GitLabClientWrapper
10+
{
11+
private readonly Lazy<Task<Project>> _project;
12+
private IRepositoryClient? _repositoryClient;
13+
14+
public GitLabClient? GitLabClient { get; }
15+
16+
public GitLabClientWrapper()
17+
{
18+
}
19+
20+
public GitLabClientWrapper(string hostUrl, string apiToken, string pathWithNamespace)
21+
{
22+
GitLabClient = new GitLabClient(hostUrl, apiToken);
23+
24+
_project = new Lazy<Task<Project>>(async () =>
25+
{
26+
if (GitLabClient is null)
27+
{
28+
throw new InvalidOperationException("The configuration for GitLab is missing.");
29+
}
30+
31+
return await GitLabClient.Projects.GetByNamespacedPathAsync(pathWithNamespace);
32+
});
33+
}
34+
35+
public async Task<Project> GetCurrentProjectAsync()
36+
{
37+
if (_project is null)
38+
{
39+
throw new InvalidOperationException("The configuration for GitLab is missing.");
40+
}
41+
42+
var project = await _project.Value;
43+
44+
return project;
45+
}
46+
47+
public async Task<IRepositoryClient> GetCurrentRepositoryClientAsync()
48+
{
49+
if (_repositoryClient is not null)
50+
{
51+
return _repositoryClient;
52+
}
53+
54+
var project = await GetCurrentProjectAsync();
55+
_repositoryClient = GitLabClient!.GetRepository(project.Id);
56+
57+
return _repositoryClient;
58+
}
59+
}

src/Web/MASA.PM.Web.Docs/PMGitLabClient.cs

-56
This file was deleted.

src/Web/MASA.PM.Web.Docs/Pages/Docs.razor

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@page "/docs"
22
@using NGitLab.Models
33
@using MASA.PM.Web.Docs.Models
4-
@inject PMGitLabClient GitLabClient
4+
@inject GitLabClientWrapper GitLabClientWrapper
55
@inherits MasaComponentBase
66

77
<div class="d-flex mb-4">
@@ -68,7 +68,7 @@
6868

6969
try
7070
{
71-
var repo = await GitLabClient.GetCurrentRepositoryClientAsync();
71+
var repo = await GitLabClientWrapper.GetCurrentRepositoryClientAsync();
7272
var tree = repo.GetTree(new RepositoryGetTreeOptions()
7373
{
7474
Recursive = true

src/Web/MASA.PM.Web.Docs/Pages/DocsNew.razor

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@page "/docs/new/{path?}"
22
@using NGitLab.Models
33
@using System.Web
4-
@inject PMGitLabClient GitLabClient
4+
@inject GitLabClientWrapper GitLabClientWrapper
55
@inherits MasaComponentBase
66

77
<PageTitle>@Title</PageTitle>
@@ -133,7 +133,7 @@
133133
_directory = split[0];
134134
_fileName = split[1];
135135

136-
var repo = await GitLabClient.GetCurrentRepositoryClientAsync();
136+
var repo = await GitLabClientWrapper.GetCurrentRepositoryClientAsync();
137137
var file = await repo.Files.GetAsync($"{path}.md", "main");
138138
_markdownValue = file.DecodedContent;
139139
_commitMessage = $"Update {_fileName}.md";
@@ -157,7 +157,7 @@
157157
_commiting = true;
158158
StateHasChanged();
159159

160-
var repo = await GitLabClient.GetCurrentRepositoryClientAsync();
160+
var repo = await GitLabClientWrapper.GetCurrentRepositoryClientAsync();
161161
var pathWithoutExtension = $"{_directory}/{_fileName}";
162162
var path = $"{pathWithoutExtension}.md";
163163
var fileUpsert = new FileUpsert()

src/Web/MASA.PM.Web.Docs/Pages/DocsPreview.razor

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
@using NGitLab.Models
33
@using System.Web
44
@using NGitLab
5-
@inject PMGitLabClient GitLabClient
5+
@inject GitLabClientWrapper GitLabClientWrapper
66
@inherits MasaComponentBase
77

88
<MContainer Fluid Class="white rounded-xl pa-6">
@@ -120,7 +120,7 @@
120120

121121
try
122122
{
123-
var repo = await GitLabClient.GetCurrentRepositoryClientAsync();
123+
var repo = await GitLabClientWrapper.GetCurrentRepositoryClientAsync();
124124
_file = await repo.Files.GetAsync(_fullPath, Branch);
125125
}
126126
catch (Exception e)
@@ -143,7 +143,7 @@
143143
{
144144
try
145145
{
146-
var repo = await GitLabClient.GetCurrentRepositoryClientAsync();
146+
var repo = await GitLabClientWrapper.GetCurrentRepositoryClientAsync();
147147
repo.Files.Delete(new FileDelete()
148148
{
149149
Branch = Branch,
@@ -169,7 +169,7 @@
169169
_fetchCommits = true;
170170
StateHasChanged();
171171

172-
var repo = await GitLabClient.GetCurrentRepositoryClientAsync();
172+
var repo = await GitLabClientWrapper.GetCurrentRepositoryClientAsync();
173173
_commits = repo.GetCommits(new GetCommitsRequest()
174174
{
175175
Path = _fullPath,
@@ -191,7 +191,7 @@
191191
else
192192
{
193193
_commitId = sha1;
194-
var repo = await GitLabClient.GetCurrentRepositoryClientAsync();
194+
var repo = await GitLabClientWrapper.GetCurrentRepositoryClientAsync();
195195
_diffs = repo.GetCommitDiff(sha1);
196196
}
197197
}

src/Web/MASA.PM.Web.Docs/ServiceCollectionExtensions.cs

+14-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,20 @@ public static class ServiceCollectionExtensions
1010
{
1111
public static void AddDocs(this IServiceCollection services, IConfiguration configuration)
1212
{
13-
var gitLabClient = new PMGitLabClient(configuration["gitlabconfig:HostUrl"], configuration["gitlabconfig:ApiToken"],
14-
configuration["gitlabconfig:FullPath"]);
13+
var hostUrl = configuration.GetSection("gitlabconfig:HostUrl").Value;
14+
var apiToken = configuration.GetSection("gitlabconfig:ApiToken").Value;
15+
var fullPath = configuration.GetSection("gitlabconfig:FullPath").Value;
1516

16-
services.AddSingleton<PMGitLabClient>(_ => gitLabClient);
17+
GitLabClientWrapper? wrapper;
18+
if (hostUrl is null || apiToken is null || fullPath is null)
19+
{
20+
wrapper = new GitLabClientWrapper();
21+
}
22+
else
23+
{
24+
wrapper = new GitLabClientWrapper(hostUrl, apiToken, fullPath);
25+
}
26+
27+
services.AddSingleton<GitLabClientWrapper>(_ => wrapper);
1728
}
1829
}

0 commit comments

Comments
 (0)