Skip to content

gh pr comment --edit-last now creates if no previous comment #10580

Description

@broksonic21

Describe the bug

Prior to #10427, --edit-last didn't comment if there wasn't a previous comment. That seems to have changed with this, and now it creates (by default) if no comment.

This means we can no longer use it or expect it to fail if there's no previous comment, which is breaking many of our flows from github actions - it's commenting when we wouldn't expect

At least in 26.8.1

Affected version

gh version 2.68.1 (2025-03-06) https://github.com/cli/cli/releases/tag/v2.68.1

Steps to reproduce the behavior

  1. create a PR
  2. type this: `gh pr comment PR_ID_OR_URL --edit-last --body "test from gh pr"
  3. See comment is added

Try again with a previous gh version, it doesn't do this (as per the recommendation here for why it's asking for a different feature for this: #6790

Expected vs actual behavior

--edit-last behaviour shouldn't have changed, or else we need a flag to not have it create a comment

CC: @latzskim and @BagToad as the mergers of that PR. Sorry for the ping; if there's an alternate workaround to get back old behavior, let us know.

Activity

  1. BagToad commented on Mar 11, 2025

    @BagToad
    Member

    👋 Hey @broksonic21, thanks for the ping ❤ I'm discussing this more with the team.

    Sorry for this friction; I don't think we were aware of that issue from the previous maintainers when we accepted this feature request.

    This means we can no longer use it or expect it to fail if there's no previous comment, which is breaking many of our flows from github actions - it's commenting when we wouldn't expect

    In the meantime, can you talk more about how you use this feature? Why do you have workflows that you expect to fail?

    A sample workflow YAML that demonstrates this use-case would be helpful, if possible! 🙏

  2. GriceTurrble commented on Mar 11, 2025

    @GriceTurrble

    I think the issue stems from the change here: https://github.com/cli/cli/pull/10427/files#diff-f547f67beffa35c15f5f45b8e35f1a774abf6a6459949b9a2065cfa3ff1e54b6R96-R113

    Previously: if EditLast flag is used, return the result of updateComment. Simple. If this errors out for any reason, no comment is created.

    Now:

    • If EditLast flag is used, first check if the error returned is the "no user comments" error. If not that error, return whatever it is.
    • If the error is "no user comments", and if in non-interactive mode, the block will drop out and use createComment automatically.

    This is a change in behavior, which will automatically create a new comment if --edit-last is used in non-interactive mode. To match previous behavior, it should simply error out because the new --create-if-none flag was not supplied. In other words, the --create-if-none flag is logically defaulting to true in non-interactive mode.

  3. broksonic21 commented on Mar 11, 2025

    @broksonic21
    Author

    @BagToad here's our use case:

    We have some scenarios where we check some logic, then:

    • If some action to take:
      ** a) comment on the PR always (either edit old comment or write new comment)
    • If no action to take (i.e. all clean):
      ** b) If old comment for that PR, update the old comment to say thanks.
      ** c) If no old comment, don't say anything - no value in commenting, and it's noise

    We had been using --edit-last for the b/c cases - as it updated if there, but didn't do anything if nothing there. that's what broke now and we started getting a ton of comments on our PRs because of that

    We can work around this with some gh pr view --json comments and some jq magic, then some if logic, but it turns a one-liner into a 10 line script each time.

    For example, here's a sample bash:

    #!/bin/bash
    
    set -x # Enable echoing of commands
    
    # --- Configuration ---
    PR_NUMBER="$1"
    REPO_OWNER="$3"
    REPO_NAME="$4"
    
    if [ -z "$PR_NUMBER" ] || [ -z "$REPO_OWNER" ] || [ -z "$REPO_NAME" ]; then
      echo "Usage: $0 <PR_NUMBER> <REPO_OWNER> <REPO_NAME>"
      exit 1
    fi
    
    REPO="$REPO_OWNER/$REPO_NAME"
    
    # --- Get PR Description ---
    PR_DESCRIPTION=$(gh pr view "$PR_NUMBER" --repo "$REPO" --json title -q .title)
    
    # --- Check Description ---
    if [[ "$PR_DESCRIPTION" =~ ^(chore|fix|feature)\(.*\) ]]; then
      echo "PR description matches conventional commit format."
    
      # This is what broke - it now always comments, whereas it didn't before
      gh pr comment "$PR_NUMBER" --repo "$REPO" --body "Thanks for using conventional commits!" --edit-last || echo "No previous comment to edit"
    # end this
    
    else
      echo "PR description does not match conventional commit format."
    
      # could use --create-if-none instead of the || format here
      gh pr comment "$PR_NUMBER" --repo "$REPO" --body "You need to use conventional commit format." --edit-last || gh pr comment "$PR_NUMBER" --repo "$REPO" --body "You need to use conventional commit format."
    
    fi
    

    (or in an action)

    The part / example that broke is where I wrote # This is what broke - it now always comments, whereas it didn't before

    I agree the create-if-none field is nice to be able to fix up the else clause, that said instead.

  4. andyfeller commented on Mar 17, 2025

    @andyfeller
    Contributor

    Acceptance Criteria

    There shouldn't be any new AC than what was originally captured in #10370 (comment) outside of what was implied about issues and PRs with comments:

    1. Given I have no comments on an existing PR
      when I run gh pr comment [<number> | <url> | <branch>] --edit-last --create-if-none --body <body> (non-interactively)
      then a comment is created instead of erroring

    2. Given I have no comments on an existing PR
      when I run gh pr comment [<number> | <url> | <branch>] --edit-last (interactively)
      then I am prompted that no comment exists and whether or not I'd like to create a new comment. If I select Y, then I have the same experience as though I ran gh pr comment [<number> | <url> | <branch>]

    3. Given I have no comments on an existing PR
      when I run gh pr comment [<number> | <url> | <branch>] --edit-last --create-if-none (interactively)
      then I am told that no comments exist and I am creating a new comment. Then I have the same experience as though I ran gh pr comment [<number> | <url> | <branch>]

    4. Given I have no comments on an existing issue
      when I run any of the above scenarios using gh issue comment
      then I have get the same behavior as I would running gh pr comment
      (i.e. make sure gh issue comment gets the same new functionality that's added to gh pr comment by the above AC)

    Existing gh issue comment and gh pr comment experiences

    The following is the same for both commands:

    $ gh pr comment https://github.com/tinyfists/gh-nonsense-internal/pull/39            
    - Press Enter to draft your comment in vim... 
    ? Submit? Yes
    https://github.com/tinyfists/gh-nonsense-internal/pull/39#issuecomment-2729974029
    
    
    $ gh pr comment https://github.com/tinyfists/gh-nonsense-internal/pull/39 --edit-last
    - Press Enter to draft your comment in vim... 
    ? Submit? Yes
    https://github.com/tinyfists/gh-nonsense-internal/pull/39#issuecomment-2729974029
    
    
    $ gh pr view https://github.com/tinyfists/gh-nonsense-internal/pull/39 --comments
    Andyfeller/236 public content tinyfists/gh-nonsense-internal#39
    Open • andyfeller wants to merge 5 commits into main from andyfeller/236-public-content • about 4 months ago
    +126 -28 • No checks
    Reviewers: copilot-pull-request-reviewer (Commented)
    
    
      No description provided
    
    
    copilot-pull-request-reviewer commented • Nov 15, 2024
    
      Copilot reviewed 5 out of 8 changed files in this pull request and generated 1 suggestion.                              
                                                                                                                              
      • .github/workflows/promote-release.sh: Language not supported                                                          
      • public/docs/README.md: Evaluated as low risk                                                                          
      • .github/workflows/promote-release.yml: Evaluated as low risk                                                          
                                                                                                                              
      Tip: Turn on automatic Copilot reviews for this repository to get quick feedback on every pull request.  Learn more     
      https://gh.io/copilot-code-reviews-docs                                                                                 
    
    andyfeller (Member) • 0m • Edited • Newest comment
    
      This is the first PR comment ... edited                                                                                 
    
    View the full review: https://github.com/tinyfists/gh-nonsense-internal/pull/39#issuecomment-2729974029
    
    
    View this pull request on GitHub: https://github.com/tinyfists/gh-nonsense-internal/pull/39

    Additionally, here's an example around the --body flag, which is not the same as --body-file:

    $ COMMENT=$(cat << EOF
    This is a cmdsubst heredoc based comment
    EOF
    )
    
    
    $ echo $COMMENT
    This is a cmdsubst heredoc based comment
    andyfeller@Andys-MBP:~ $ gh pr comment https://github.com/tinyfists/gh-nonsense-internal/pull/39 --edit-last --body $COMMENT 
    https://github.com/tinyfists/gh-nonsense-internal/pull/39#issuecomment-2729974029
    
    
    $ gh pr view https://github.com/tinyfists/gh-nonsense-internal/pull/39 --comments                    
    Andyfeller/236 public content tinyfists/gh-nonsense-internal#39
    Open • andyfeller wants to merge 5 commits into main from andyfeller/236-public-content • about 4 months ago
    +126 -28 • No checks
    Reviewers: copilot-pull-request-reviewer (Commented)
    
    
      No description provided
    
    
    copilot-pull-request-reviewer commented • Nov 15, 2024
    
      Copilot reviewed 5 out of 8 changed files in this pull request and generated 1 suggestion.                              
                                                                                                                              
      • .github/workflows/promote-release.sh: Language not supported                                                          
      • public/docs/README.md: Evaluated as low risk                                                                          
      • .github/workflows/promote-release.yml: Evaluated as low risk                                                          
                                                                                                                              
      Tip: Turn on automatic Copilot reviews for this repository to get quick feedback on every pull request.  Learn more     
      https://gh.io/copilot-code-reviews-docs                                                                                 
    
    andyfeller (Member) • 27m • Edited • Newest comment
    
      This is a cmdsubst heredoc based comment                                                                                
    
    View the full review: https://github.com/tinyfists/gh-nonsense-internal/pull/39#issuecomment-2729974029
    
    
    View this pull request on GitHub: https://github.com/tinyfists/gh-nonsense-internal/pull/39
  5. added
    priority-2Affects more than a few users but doesn't prevent core functions
    and removed on Mar 17, 2025
  6. self-assigned this
    on Mar 17, 2025
  7. andyfeller commented on Mar 18, 2025

    @andyfeller
    Contributor

    @broksonic21 @GriceTurrble : just made #10625 ready for review with fairly extensive testing changes around this

  8. broksonic21 commented on Mar 19, 2025

    @broksonic21
    Author

    Thank you @andyfeller - tests match what I'd expect!

  9. Milanhe92 commented on Apr 14, 2025

    @Milanhe92

    andyfeller/10580-edit-last-regression "features": {
    "ghcr.io/devcontainers/features/github-cli:1": {}
    }gh pr checkout 28065holiman:vuln_2023connmanager

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinggh-prrelating to the gh pr commandpriority-2Affects more than a few users but doesn't prevent core functions

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions