@@ -7,12 +7,19 @@ import { ChildProcessSpawner } from "effect/unstable/process";
77
88import * as VcsProcess from "../vcs/VcsProcess.ts" ;
99import * as GitHubCli from "./GitHubCli.ts" ;
10+ import { parseGitHubAuthStatus } from "./gitHubAuthStatus.ts" ;
1011import * as GitHubSourceControlProvider from "./GitHubSourceControlProvider.ts" ;
1112
12- const processResult = ( stdout : string ) : VcsProcess . VcsProcessOutput => ( {
13- exitCode : ChildProcessSpawner . ExitCode ( 0 ) ,
13+ const processResult = (
14+ stdout : string ,
15+ options ?: {
16+ readonly stderr ?: string ;
17+ readonly exitCode ?: ChildProcessSpawner . ExitCode ;
18+ } ,
19+ ) : VcsProcess . VcsProcessOutput => ( {
20+ exitCode : options ?. exitCode ?? ChildProcessSpawner . ExitCode ( 0 ) ,
1421 stdout,
15- stderr : "" ,
22+ stderr : options ?. stderr ?? "" ,
1623 stdoutTruncated : false ,
1724 stderrTruncated : false ,
1825} ) ;
@@ -157,3 +164,178 @@ it.effect("creates GitHub PRs through provider-neutral input names", () =>
157164 } ) ;
158165 } ) ,
159166) ;
167+
168+ it ( "accepts active authenticated GitHub accounts when another account fails" , ( ) => {
169+ const auth = GitHubSourceControlProvider . discovery . parseAuth (
170+ processResult (
171+ JSON . stringify ( {
172+ hosts : {
173+ "github.com" : [
174+ {
175+ state : "success" ,
176+ active : true ,
177+ host : "github.com" ,
178+ login : "active-user" ,
179+ tokenSource : "keyring" ,
180+ gitProtocol : "ssh" ,
181+ } ,
182+ {
183+ state : "error" ,
184+ active : false ,
185+ host : "github.com" ,
186+ login : "stale-user" ,
187+ tokenSource : "keyring" ,
188+ gitProtocol : "ssh" ,
189+ error : "The token in keyring is invalid." ,
190+ } ,
191+ ] ,
192+ } ,
193+ } ) ,
194+ ) ,
195+ ) ;
196+
197+ assert . deepStrictEqual (
198+ {
199+ status : auth . status ,
200+ account : auth . account ,
201+ host : auth . host ,
202+ } ,
203+ {
204+ status : "authenticated" ,
205+ account : Option . some ( "active-user" ) ,
206+ host : Option . some ( "github.com" ) ,
207+ } ,
208+ ) ;
209+ } ) ;
210+
211+ it ( "parses GitHub auth JSON from stdout when stderr has warnings" , ( ) => {
212+ const auth = GitHubSourceControlProvider . discovery . parseAuth (
213+ processResult (
214+ JSON . stringify ( {
215+ hosts : {
216+ "github.com" : [
217+ {
218+ state : "success" ,
219+ active : true ,
220+ host : "github.com" ,
221+ login : "active-user" ,
222+ tokenSource : "keyring" ,
223+ gitProtocol : "ssh" ,
224+ } ,
225+ ] ,
226+ } ,
227+ } ) ,
228+ { stderr : "warning: ignored diagnostic from gh\n" } ,
229+ ) ,
230+ ) ;
231+
232+ assert . deepStrictEqual (
233+ {
234+ status : auth . status ,
235+ account : auth . account ,
236+ host : auth . host ,
237+ } ,
238+ {
239+ status : "authenticated" ,
240+ account : Option . some ( "active-user" ) ,
241+ host : Option . some ( "github.com" ) ,
242+ } ,
243+ ) ;
244+ } ) ;
245+
246+ it ( "parses GitHub auth status accounts by host and active state" , ( ) => {
247+ assert . deepStrictEqual (
248+ parseGitHubAuthStatus (
249+ JSON . stringify ( {
250+ hosts : {
251+ "github.com" : [
252+ {
253+ state : "success" ,
254+ active : true ,
255+ host : "github.com" ,
256+ login : "active-user" ,
257+ tokenSource : "keyring" ,
258+ gitProtocol : "ssh" ,
259+ } ,
260+ {
261+ state : "error" ,
262+ active : false ,
263+ host : "github.com" ,
264+ login : "stale-user" ,
265+ tokenSource : "keyring" ,
266+ gitProtocol : "ssh" ,
267+ } ,
268+ ] ,
269+ "github.example.test" : [
270+ {
271+ state : "success" ,
272+ active : false ,
273+ host : "github.example.test" ,
274+ login : "enterprise-user" ,
275+ tokenSource : "keyring" ,
276+ gitProtocol : "ssh" ,
277+ } ,
278+ ] ,
279+ } ,
280+ } ) ,
281+ ) . accounts ,
282+ [
283+ {
284+ host : "github.com" ,
285+ account : "active-user" ,
286+ authenticated : true ,
287+ active : true ,
288+ error : null ,
289+ } ,
290+ {
291+ host : "github.com" ,
292+ account : "stale-user" ,
293+ authenticated : false ,
294+ active : false ,
295+ error : null ,
296+ } ,
297+ {
298+ host : "github.example.test" ,
299+ account : "enterprise-user" ,
300+ authenticated : true ,
301+ active : false ,
302+ error : null ,
303+ } ,
304+ ] ,
305+ ) ;
306+ } ) ;
307+
308+ it ( "reports unauthenticated when GitHub JSON has accounts but none are valid" , ( ) => {
309+ const auth = GitHubSourceControlProvider . discovery . parseAuth (
310+ processResult (
311+ JSON . stringify ( {
312+ hosts : {
313+ "github.com" : [
314+ {
315+ state : "error" ,
316+ active : true ,
317+ host : "github.com" ,
318+ login : "stale-user" ,
319+ tokenSource : "keyring" ,
320+ gitProtocol : "ssh" ,
321+ error : "The token in keyring is invalid." ,
322+ } ,
323+ ] ,
324+ } ,
325+ } ) ,
326+ ) ,
327+ ) ;
328+
329+ assert . deepStrictEqual (
330+ {
331+ status : auth . status ,
332+ host : auth . host ,
333+ detail : auth . detail ,
334+ } ,
335+ {
336+ status : "unauthenticated" ,
337+ host : Option . some ( "github.com" ) ,
338+ detail : Option . some ( "The token in keyring is invalid." ) ,
339+ } ,
340+ ) ;
341+ } ) ;
0 commit comments