-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Closed
Labels
enhancementa request to improve CLIa request to improve CLIgh-reporelating to the gh repo commandrelating to the gh repo commandhelp wantedContributions welcomeContributions welcome
Description
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.2Steps 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 outputCalling 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 outputScreenshot:
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:
Lines 212 to 225 in c789b56
| 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
Labels
enhancementa request to improve CLIa request to improve CLIgh-reporelating to the gh repo commandrelating to the gh repo commandhelp wantedContributions welcomeContributions welcome