Skip to content

gh pr checks <pr> --required should not fail when there are no required checks #9682

Open

Description

@clarityflowers

Describe the feature or problem you’d like to solve

I have a script that wants to wait for required checks. If there are no required checks at all, that should count as an instant success and not a failure. AFAICT, it's a bit of a pain if I want to actually tell between "checks failed" or "no checks".

Proposed solution

Option 1: return a 0 exit code if there are no required checks.
Option 2: return a distinct (& documented!) exit code for the "no required checks" scenario so it can be specifically handled.

Activity

  1. added
    enhancement
    on Sep 30, 2024
  2. added
    needs-triage
    on Sep 30, 2024
  3. self-assigned this
    on Oct 1, 2024
  4. added
    gh-pr
    bug
    priority-3
    and removed
    needs-triage
    on Oct 1, 2024
  5. BagToad commented on Oct 1, 2024

    @BagToad
    Member

    👋 Hey @clarityflowers. Thank you for opening this ✨

    Option 1: return a 0 exit code if there are no required checks.

    This makes sense, and I think this might be a bug. I've done a bit of digging, and it seems like there is a design intention to exit 0 when there are no results:

    cli/cmd/gh/main.go

    Lines 140 to 144 in 3f3acde

    if cmdFactory.IOStreams.IsStdoutTTY() {
    fmt.Fprintln(stderr, noResultsError.Error())
    }
    // no results is not a command failure
    return exitOK

    Digging further, it looks like we currently return an error (which I understand exits as 1) when no checks are found, and this applies to gh pr checks with the --required flag and without:

    if len(statusCheckRollup.Nodes) == 0 {
    return nil, checkCounts{}, fmt.Errorf("no checks reported on the '%s' branch", pr.HeadRefName)
    }
    checks, counts := aggregateChecks(statusCheckRollup.Nodes, requiredChecks)
    if len(checks) == 0 && requiredChecks {
    return checks, counts, fmt.Errorf("no required checks reported on the '%s' branch", pr.HeadRefName)
    }
    return checks, counts, nil

    I suspect this is a bug for both cases.

    Acceptance Criteria

    • gh pr checks <pr> --required returns a 0 exit code when no checks are found
    • gh pr checks <pr> returns a 0 exit code when no checks are found

    I have opened a PR for the above: #9691

    New AC is here: #9682 (comment)

  6. williammartin commented on Oct 2, 2024

    @williammartin
    Member

    @BagToad Thanks for looking into this. I'm not sure this approach is going to work though, sorry.

    There are more details in this comment by Mislav. There seems to be an underlying issue that:

    The branch for the PR might not report any checks if the branch was just created, so before any Checks nor Statuses got registered by external services yet. Re-running the command a few seconds later could show that there are now checks registered as “pending”.

    If this is true, this is not a safe change to make with the consequences being that PRs that may have failing checks could be considered passing.

    We could adjust the exit code in this case to a distinct one as originally proposed. It could be argued that it's breaking but I'm content to say that the extent of the stability of our exit codes is zero/non-zero if not specified (e.g. 8 is for pending).

    In the meantime, @clarityflowers, just curious what you prefer about scripting against a distinct exit code vs checking the stderr for "no checks reported"? Cheers.

  7. added
    more-info-needed
    on Oct 2, 2024
  8. clarityflowers commented on Oct 2, 2024

    @clarityflowers
    Author

    In the meantime, @clarityflowers, just curious what you prefer about scripting against a distinct exit code vs checking the stderr for "no checks reported"? Cheers.

    @williammartin redirecting stderr and then comparing against it while also still having to check the exit code for an actual error is much more of a pain then just checking $? and moving on. It's also more robust, since you demonstrated in that PR that you're perfectly happy to update the wording of an error message! The crucial word here is "documented" – I'd like a way to check for this scenario that's guaranteed to stable against minor updates.

  9. removed
    more-info-needed
    on Oct 2, 2024
  10. williammartin commented on Oct 2, 2024

    @williammartin
    Member

    Thanks, that's what I imagined, and makes sense. I have an issue open somewhere to write a document about what the version stability guarantees of the CLI actually are because it doesn't seem to have ever been strictly semver and right now it's all a bit wibbly wobbly and likely to cause frustration in the future.

    Do you have any concerns about the API returning inconsistent view of checks under some circumstances (seems to be when the branch is newly created)? Is that something you are content to handle on your end?

  11. added
    more-info-needed
    and removed
    bug
    on Oct 2, 2024
  12. clarityflowers commented on Oct 2, 2024

    @clarityflowers
    Author

    It'd definitely be nice if it could tell the difference between "no checks" and "checks haven't started yet" but that feels like a separate issue from this one! My current scripts just throw a sleep in to make sure checks have the chance to load right now.

  13. removed
    more-info-needed
    priority-3
    on Oct 3, 2024
  14. BagToad commented on Oct 4, 2024

    @BagToad
    Member

    Acceptance Criteria

    • gh pr checks <pr> --required returns a unique non-zero exit code when no checks are found
    • gh pr checks <pr> returns a unique non-zero exit code when no checks are found
    • Documentation is updated to describe the new behavior.
  15. williammartin commented on Oct 17, 2024

    @williammartin
    Member

    As I think about this more, I get more and more concerned about the existing UX when it comes to checks that are not yet registered. We have special cased 0 checks as an error because there is a delay between the PR being created and first party or third party checks being registered against the PR. However, this delay is equally true for n checks. There's no way that I know of to guarantee that a slow third party hasn't yet registered a check. For example, we may have 4 checks that have all passed, but a slow third party has not registered the 5th. This is indistinguishable from it not existing.

    I think that instead of a separate status code, I would like this discrepancy to be called out and explicitly opted into via a flag. That way users can clearly understand that relying on this behaviour comes with risks. @clarityflowers do you think a flag like --succeed-on-no-checks would work for your case, with the behaviour being that we would exit 0 if this flag were provided and the API returned no checks.

  16. clarityflowers commented on Oct 17, 2024

    @clarityflowers
    Author

    Yeah, that would work fine!

  17. removed their assignment
    on Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementgh-pr

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions