@@ -21,17 +21,21 @@ public class GitHubDevHosting : IDevHosting
21
21
private readonly ISimpleLogger _log ;
22
22
private readonly string _url ;
23
23
private readonly string _apiToken ;
24
+ private readonly string _gistApiToken ;
24
25
private readonly GitHubClient _client ;
25
-
26
- public GitHubDevHosting ( ISimpleLogger log , DevHostingConfiguration hostingConfiguration , string apiToken , string apiTokenUsage )
26
+ private readonly GitHubClient _clientGist ;
27
+
28
+ public GitHubDevHosting ( ISimpleLogger log , DevHostingConfiguration hostingConfiguration , string apiToken , string apiTokenUsage , string ? gistApiToken = null )
27
29
{
28
30
Logger = log ;
29
31
Configuration = hostingConfiguration ;
30
32
_log = log ;
31
33
_url = hostingConfiguration . Base ;
32
34
_apiToken = apiToken ;
35
+ _gistApiToken = gistApiToken ?? apiToken ;
33
36
ApiTokenUsage = apiTokenUsage ;
34
37
_client = new GitHubClient ( new ProductHeaderValue ( nameof ( ReleaserApp ) ) , new Uri ( hostingConfiguration . Api ) ) ;
38
+ _clientGist = new GitHubClient ( new ProductHeaderValue ( nameof ( ReleaserApp ) ) , new Uri ( hostingConfiguration . Api ) ) ;
35
39
}
36
40
37
41
public ISimpleLogger Logger { get ; }
@@ -44,7 +48,7 @@ public GitHubDevHosting(ISimpleLogger log, DevHostingConfiguration hostingConfig
44
48
45
49
public async Task < bool > Connect ( )
46
50
{
47
- var tokenAuth = new Credentials ( _apiToken ) ; // NOTE: not real token
51
+ var tokenAuth = new Credentials ( _apiToken ) ;
48
52
_client . Credentials = tokenAuth ;
49
53
50
54
_log . Info ( $ "Connecting to GitHub ({ ApiTokenUsage } )") ;
@@ -57,6 +61,13 @@ public async Task<bool> Connect()
57
61
_log . Error ( $ "Unable to connect GitHub ({ ApiTokenUsage } ). Reason: { ex . Message } ") ;
58
62
return false ;
59
63
}
64
+
65
+ _clientGist . Credentials = tokenAuth ;
66
+ if ( _gistApiToken != _apiToken )
67
+ {
68
+ _clientGist . Credentials = new Credentials ( _gistApiToken ) ;
69
+ }
70
+
60
71
return true ;
61
72
}
62
73
@@ -76,7 +87,17 @@ public async Task<List<ReleaseVersion>> GetAllReleaseTags(string user, string re
76
87
77
88
public async Task CreateOrUpdateGist ( string gistId , string fileName , string content )
78
89
{
79
- var gist = await _client . Gist . Get ( gistId ) ;
90
+ Gist gist ;
91
+ try
92
+ {
93
+ gist = await _clientGist . Gist . Get ( gistId ) ;
94
+ }
95
+ catch ( Exception ex )
96
+ {
97
+ _log . Error ( $ "Unable to get the gist { gistId } . Reason: { ex . Message } ") ;
98
+ return ;
99
+ }
100
+
80
101
if ( gist is null )
81
102
{
82
103
_log . Error ( $ "The gist { gistId } for code coverage was not found") ;
@@ -96,81 +117,11 @@ public async Task CreateOrUpdateGist(string gistId, string fileName, string cont
96
117
GistUpdate gistUpdate = new GistUpdate ( ) ;
97
118
//gistUpdate.Files.Add();
98
119
gistUpdate . Files . Add ( fileName , new GistFileUpdate ( ) { NewFileName = fileName , Content = content } ) ;
99
- await _client . Gist . Edit ( gistId , gistUpdate ) ;
120
+ await _clientGist . Gist . Edit ( gistId , gistUpdate ) ;
100
121
}
101
122
else
102
123
{
103
- if ( ! gist . GitPushUrl . StartsWith ( "https://" ) )
104
- {
105
- _log . Warn ( $ "The gist URL { gist . GitPushUrl } is not a standard gist URL and cannot be updated.") ;
106
- return ;
107
- }
108
-
109
- var uri = new Uri ( gist . GitPushUrl ) ;
110
- var gitCloneUrl = $ "git@{ uri . Host } :{ uri . PathAndQuery . TrimStart ( '/' ) } ";
111
-
112
- var gistTempDirectory = Directory . CreateTempSubdirectory ( "dotnet-releaser-gist" ) ;
113
- try
114
- {
115
- var result = await GitRunner . Run ( "clone" , new [ ] { gitCloneUrl , gistTempDirectory . FullName } ) ;
116
-
117
- if ( result . HasErrors )
118
- {
119
- _log . Error ( $ "Unable to clone the gist { gistId } to { gistTempDirectory . FullName } . ExitCode: { result . CommandResult . ExitCode } , Output: { result . Output } ") ;
120
- return ;
121
- }
122
-
123
- var gistFile = Path . Combine ( gistTempDirectory . FullName , fileName ) ;
124
- await File . WriteAllTextAsync ( gistFile , content ) ;
125
-
126
- result = await GitRunner . Run ( "config" , new [ ] { "--local" , "user.email" , "[email protected] " } , gistTempDirectory . FullName ) ;
127
- if ( result . HasErrors )
128
- {
129
- _log . Error ( $ "Unable to set the user.email for the git repository. ExitCode: { result . CommandResult . ExitCode } , Output: { result . Output } ") ;
130
- return ;
131
- }
132
-
133
- result = await GitRunner . Run ( "config" , new [ ] { "--local" , "user.name" , "GitHub Action" } , gistTempDirectory . FullName ) ;
134
- if ( result . HasErrors )
135
- {
136
- _log . Error ( $ "Unable to set the user.name for the git repository. ExitCode: { result . CommandResult . ExitCode } , Output: { result . Output } ") ;
137
- return ;
138
- }
139
-
140
- result = await GitRunner . Run ( "add" , new [ ] { "." } , gistTempDirectory . FullName ) ;
141
- if ( result . HasErrors )
142
- {
143
- _log . Error ( $ "Unable to add the file { fileName } to the git repository. ExitCode: { result . CommandResult . ExitCode } , Output: { result . Output } ") ;
144
- return ;
145
- }
146
-
147
- result = await GitRunner . Run ( "commit" , new [ ] { "-m" , $ "Upload new file { fileName } " } , gistTempDirectory . FullName ) ;
148
- if ( result . HasErrors )
149
- {
150
- _log . Error ( $ "Unable to commit the file { fileName } to the git repository. ExitCode: { result . CommandResult . ExitCode } , Output: { result . Output } ") ;
151
- return ;
152
- }
153
-
154
- result = await GitRunner . Run ( "push" , Array . Empty < string > ( ) , gistTempDirectory . FullName ) ;
155
- if ( result . HasErrors )
156
- {
157
- _log . Error ( $ "Unable to push the file { fileName } to the git repository. ExitCode: { result . CommandResult . ExitCode } , Output: { result . Output } ") ;
158
- return ;
159
- }
160
- }
161
- finally
162
- {
163
- try
164
- {
165
- gistTempDirectory . Delete ( true ) ;
166
- }
167
- catch
168
- {
169
- // ignore
170
- }
171
- }
172
-
173
- _log . Warn ( $ "New file uploaded to gist { gistId } ") ;
124
+ _log . Error ( $ "The gist { gistId } does not contain a file { fileName } ") ;
174
125
}
175
126
}
176
127
0 commit comments