Thanks to visit codestin.com
Credit goes to github.com

Skip to content

When gh repo fork results in a newly created fork, there is no output to non-TTY terminal #10079

@kenodegard

Description

@kenodegard

Describe the bug

While writing a GitHub workflow that forks a repo I found that gh repo fork only outputs success text if the fork previously existed, not when the fork is newly created. This issue only appears if running the command from a non-TTY terminal.

GitHub Workflow Step

What I currently have to do to get the forked repo name:

- name: Create Fork
  # no-op if the repository is already forked
  shell: bash
  # since this runs from a non-TTY shell, `gh repo fork` doesn't output anything if the
  # fork is newly created, but outputs "owner/repo already exists" for existing forks
  run: |
    FORK=$(gh repo fork --clone=false --remote=false --default-branch-only 2>&1)
    if [ -z "${FORK}" ]; then
      sleep 60  # it takes a minute before a newly created repo is considered "existing"
      FORK=$(gh repo fork --clone=false --remote=false --default-branch-only 2>&1)
    fi
    echo FORK=$(echo "${FORK}" | grep -o '[^ ]*/[^ ]*' | head -n1 \) >> $GITHUB_ENV
  env:
    GH_TOKEN: ${{ inputs.fork-token }}

What I'd like to do instead:

- name: Create Fork
  # no-op if the repository is already forked
  shell: bash
  run: echo FORK=$(
    gh repo fork --clone=false --remote=false --default-branch-only |
    grep -o '[^ ]*/[^ ]*' |
    head -n1) >> $GITHUB_ENV
  env:
    GH_TOKEN: ${{ inputs.fork-token }}
$ gh --version
gh version 2.63.2 (2024-12-05)
https://github.com/cli/cli/releases/tag/v2.63.2

Steps to reproduce the behavior

Call this from a repo that has not been forked before:

$ bash <<HERE
gh repo fork --clone=false --default-branch-only --remote=false
HERE
# <-- no output

Calling this a second time (after ~60 seconds):

$ bash <<HERE
gh repo fork --clone=false --default-branch-only --remote=false
HERE
kenodegard/releases already exists  # <-- exists output

Screenshot:

Screenshot 2024-12-13 at 09 54 11

Expected vs actual behavior

I would expect the command to output the fork name in either scenarios whether or not an interactive terminal (TTY vs non-TTY).

Looking at the source code this is where the issue lies:

if createdAgo > time.Minute {
if connectedToTerminal {
fmt.Fprintf(stderr, "%s %s %s\n",
cs.Yellow("!"),
cs.Bold(ghrepo.FullName(forkedRepo)),
"already exists")
} else {
fmt.Fprintf(stderr, "%s already exists\n", ghrepo.FullName(forkedRepo))
}
} else {
if connectedToTerminal {
fmt.Fprintf(stderr, "%s Created fork %s\n", cs.SuccessIconWithColor(cs.Green), cs.Bold(ghrepo.FullName(forkedRepo)))
}
}

As I understand this, we would want the following:

if createdAgo > time.Minute {
	if connectedToTerminal {
		fmt.Fprintf(stderr, "%s %s %s\n",
			cs.Yellow("!"),
			cs.Bold(ghrepo.FullName(forkedRepo)),
			"already exists")
	} else {
		fmt.Fprintf(stderr, "%s already exists\n", ghrepo.FullName(forkedRepo))
	}
} else {
	if connectedToTerminal {
		fmt.Fprintf(stderr, "%s Created fork %s\n",
			cs.SuccessIconWithColor(cs.Green),
			cs.Bold(ghrepo.FullName(forkedRepo)))
	} else {
		fmt.Fprintf(stderr, "Created fork %s\n", ghrepo.FullName(forkedRepo))
	}
}

Logs

n/a

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementa request to improve CLIgh-reporelating to the gh repo commandhelp wantedContributions welcome

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions