Commit e67e575 1 parent 530e33f commit e67e575 Copy full SHA for e67e575
File tree 6 files changed +83
-69
lines changed
6 files changed +83
-69
lines changed Original file line number Diff line number Diff line change
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
+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
1
@page " /docs"
2
2
@using NGitLab .Models
3
3
@using MASA .PM .Web .Docs .Models
4
- @inject PMGitLabClient GitLabClient
4
+ @inject GitLabClientWrapper GitLabClientWrapper
5
5
@inherits MasaComponentBase
6
6
7
7
<div class =" d-flex mb-4" >
68
68
69
69
try
70
70
{
71
- var repo = await GitLabClient .GetCurrentRepositoryClientAsync ();
71
+ var repo = await GitLabClientWrapper .GetCurrentRepositoryClientAsync ();
72
72
var tree = repo .GetTree (new RepositoryGetTreeOptions ()
73
73
{
74
74
Recursive = true
Original file line number Diff line number Diff line change 1
1
@page " /docs/new/{path?}"
2
2
@using NGitLab .Models
3
3
@using System .Web
4
- @inject PMGitLabClient GitLabClient
4
+ @inject GitLabClientWrapper GitLabClientWrapper
5
5
@inherits MasaComponentBase
6
6
7
7
<PageTitle >@Title </PageTitle >
133
133
_directory = split [0 ];
134
134
_fileName = split [1 ];
135
135
136
- var repo = await GitLabClient .GetCurrentRepositoryClientAsync ();
136
+ var repo = await GitLabClientWrapper .GetCurrentRepositoryClientAsync ();
137
137
var file = await repo .Files .GetAsync ($" {path }.md" , " main" );
138
138
_markdownValue = file .DecodedContent ;
139
139
_commitMessage = $" Update {_fileName }.md" ;
157
157
_commiting = true ;
158
158
StateHasChanged ();
159
159
160
- var repo = await GitLabClient .GetCurrentRepositoryClientAsync ();
160
+ var repo = await GitLabClientWrapper .GetCurrentRepositoryClientAsync ();
161
161
var pathWithoutExtension = $" {_directory }/{_fileName }" ;
162
162
var path = $" {pathWithoutExtension }.md" ;
163
163
var fileUpsert = new FileUpsert ()
Original file line number Diff line number Diff line change 2
2
@using NGitLab .Models
3
3
@using System .Web
4
4
@using NGitLab
5
- @inject PMGitLabClient GitLabClient
5
+ @inject GitLabClientWrapper GitLabClientWrapper
6
6
@inherits MasaComponentBase
7
7
8
8
<MContainer Fluid Class =" white rounded-xl pa-6" >
120
120
121
121
try
122
122
{
123
- var repo = await GitLabClient .GetCurrentRepositoryClientAsync ();
123
+ var repo = await GitLabClientWrapper .GetCurrentRepositoryClientAsync ();
124
124
_file = await repo .Files .GetAsync (_fullPath , Branch );
125
125
}
126
126
catch (Exception e )
143
143
{
144
144
try
145
145
{
146
- var repo = await GitLabClient .GetCurrentRepositoryClientAsync ();
146
+ var repo = await GitLabClientWrapper .GetCurrentRepositoryClientAsync ();
147
147
repo .Files .Delete (new FileDelete ()
148
148
{
149
149
Branch = Branch ,
169
169
_fetchCommits = true ;
170
170
StateHasChanged ();
171
171
172
- var repo = await GitLabClient .GetCurrentRepositoryClientAsync ();
172
+ var repo = await GitLabClientWrapper .GetCurrentRepositoryClientAsync ();
173
173
_commits = repo .GetCommits (new GetCommitsRequest ()
174
174
{
175
175
Path = _fullPath ,
191
191
else
192
192
{
193
193
_commitId = sha1 ;
194
- var repo = await GitLabClient .GetCurrentRepositoryClientAsync ();
194
+ var repo = await GitLabClientWrapper .GetCurrentRepositoryClientAsync ();
195
195
_diffs = repo .GetCommitDiff (sha1 );
196
196
}
197
197
}
Original file line number Diff line number Diff line change @@ -10,9 +10,20 @@ public static class ServiceCollectionExtensions
10
10
{
11
11
public static void AddDocs ( this IServiceCollection services , IConfiguration configuration )
12
12
{
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 ;
15
16
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 ) ;
17
28
}
18
29
}
You can’t perform that action at this time.
0 commit comments