@@ -330,6 +330,9 @@ export async function isGlabAuthenticated(options = {}) {
330330/**
331331 * Get GitLab username from authenticated user
332332 *
333+ * Note: This function parses the JSON response in JavaScript rather than using
334+ * glab's --jq flag, as the --jq flag is not available in all glab versions.
335+ *
333336 * @param {Object } options - Options
334337 * @param {string } options.hostname - GitLab hostname (optional)
335338 * @param {boolean } options.verbose - Enable verbose logging
@@ -342,7 +345,7 @@ export async function getGitLabUsername(options = {}) {
342345
343346 log . debug ( 'Getting GitLab username...' ) ;
344347
345- const args = [ 'api' , 'user' , '--jq' , '.username' ] ;
348+ const args = [ 'api' , 'user' ] ;
346349 if ( hostname ) {
347350 args . push ( '--hostname' , hostname ) ;
348351 }
@@ -353,7 +356,23 @@ export async function getGitLabUsername(options = {}) {
353356 throw new Error ( `Failed to get GitLab username: ${ result . stderr } ` ) ;
354357 }
355358
356- const username = result . stdout . trim ( ) ;
359+ // Parse JSON response in JavaScript (glab's --jq flag is not available in all versions)
360+ let userData ;
361+ try {
362+ userData = JSON . parse ( result . stdout . trim ( ) ) ;
363+ } catch ( parseError ) {
364+ throw new Error (
365+ `Failed to parse GitLab user data: ${ parseError . message } . Raw output: ${ result . stdout } `
366+ ) ;
367+ }
368+
369+ const username = userData . username ;
370+ if ( ! username ) {
371+ throw new Error (
372+ 'No username found in GitLab user data. Please ensure your GitLab account has a username.'
373+ ) ;
374+ }
375+
357376 log . debug ( `GitLab username: ${ username } ` ) ;
358377
359378 return username ;
@@ -362,6 +381,9 @@ export async function getGitLabUsername(options = {}) {
362381/**
363382 * Get primary email from GitLab user
364383 *
384+ * Note: This function parses the JSON response in JavaScript rather than using
385+ * glab's --jq flag, as the --jq flag is not available in all glab versions.
386+ *
365387 * @param {Object } options - Options
366388 * @param {string } options.hostname - GitLab hostname (optional)
367389 * @param {boolean } options.verbose - Enable verbose logging
@@ -374,7 +396,7 @@ export async function getGitLabEmail(options = {}) {
374396
375397 log . debug ( 'Getting GitLab primary email...' ) ;
376398
377- const args = [ 'api' , 'user' , '--jq' , '.email' ] ;
399+ const args = [ 'api' , 'user' ] ;
378400 if ( hostname ) {
379401 args . push ( '--hostname' , hostname ) ;
380402 }
@@ -385,7 +407,17 @@ export async function getGitLabEmail(options = {}) {
385407 throw new Error ( `Failed to get GitLab email: ${ result . stderr } ` ) ;
386408 }
387409
388- const email = result . stdout . trim ( ) ;
410+ // Parse JSON response in JavaScript (glab's --jq flag is not available in all versions)
411+ let userData ;
412+ try {
413+ userData = JSON . parse ( result . stdout . trim ( ) ) ;
414+ } catch ( parseError ) {
415+ throw new Error (
416+ `Failed to parse GitLab user data: ${ parseError . message } . Raw output: ${ result . stdout } `
417+ ) ;
418+ }
419+
420+ const email = userData . email ;
389421
390422 if ( ! email ) {
391423 throw new Error (
@@ -401,17 +433,60 @@ export async function getGitLabEmail(options = {}) {
401433/**
402434 * Get GitLab user information (username and primary email)
403435 *
436+ * Note: This function makes a single API call and parses both username and email
437+ * from the response, which is more efficient than calling getGitLabUsername and
438+ * getGitLabEmail separately.
439+ *
404440 * @param {Object } options - Options
405441 * @param {string } options.hostname - GitLab hostname (optional)
406442 * @param {boolean } options.verbose - Enable verbose logging
407443 * @param {Object } options.logger - Custom logger
408444 * @returns {Promise<{username: string, email: string}> } User information
409445 */
410446export async function getGitLabUserInfo ( options = { } ) {
411- const [ username , email ] = await Promise . all ( [
412- getGitLabUsername ( options ) ,
413- getGitLabEmail ( options ) ,
414- ] ) ;
447+ const { hostname, verbose = false , logger = console } = options ;
448+ const log = createDefaultLogger ( { verbose, logger } ) ;
449+
450+ log . debug ( 'Getting GitLab user information...' ) ;
451+
452+ const args = [ 'api' , 'user' ] ;
453+ if ( hostname ) {
454+ args . push ( '--hostname' , hostname ) ;
455+ }
456+
457+ const result = await $ `glab ${ args } ` . run ( { capture : true } ) ;
458+
459+ if ( result . code !== 0 ) {
460+ throw new Error ( `Failed to get GitLab user info: ${ result . stderr } ` ) ;
461+ }
462+
463+ // Parse JSON response in JavaScript (glab's --jq flag is not available in all versions)
464+ let userData ;
465+ try {
466+ userData = JSON . parse ( result . stdout . trim ( ) ) ;
467+ } catch ( parseError ) {
468+ throw new Error (
469+ `Failed to parse GitLab user data: ${ parseError . message } . Raw output: ${ result . stdout } `
470+ ) ;
471+ }
472+
473+ const username = userData . username ;
474+ const email = userData . email ;
475+
476+ if ( ! username ) {
477+ throw new Error (
478+ 'No username found in GitLab user data. Please ensure your GitLab account has a username.'
479+ ) ;
480+ }
481+
482+ if ( ! email ) {
483+ throw new Error (
484+ 'No email found on GitLab account. Please set a primary email in your GitLab settings.'
485+ ) ;
486+ }
487+
488+ log . debug ( `GitLab username: ${ username } ` ) ;
489+ log . debug ( `GitLab primary email: ${ email } ` ) ;
415490
416491 return { username, email } ;
417492}
0 commit comments