Skip to content

Add lock and unlock commands for issues and pull requests - #5333

Merged
vilmibm merged 37 commits into
cli:trunkfrom
chemotaxis:gh-issue-lock
Dec 21, 2022
Merged

vilmibm merged 37 commits into
cli:trunkfrom
chemotaxis:gh-issue-lock

Conversation

@chemotaxis

@chemotaxis chemotaxis commented Mar 21, 2022 •

Copy link
Copy Markdown
Contributor

Fixes #5020.

This pull request adds lock and unlock to issues and pull requests.

unlock will unlock a conversation if it was previously locked. Otherwise, it will do nothing.

lock will lock a conversation if it was previously unlocked. You can optionally specify a reason for locking from a set of fixed reasons.

If the issue or pull request is already locked, gh will prompt you to confirm if you want to "relock" a conversation or abort the "relock" if you forgot or didn't know the conversation was already locked. The primary use for relocking will probably be to change the lock reason.

I tried just sending a lock request again with an updated reason, but nothing changes. You first have to unlock the conversation, then lock it with the updated reason.

The error messages are parameterized on several variables including the parent command (issue or pr), what the intended action was (lock or unlock), the reason for locking, etc. For example, if issue #1 exists, but we used the command gh pr lock 1, the error message will say that issue #1 exists, but not pull request #1. It then suggests to use gh issue lock 1 instead.

Lock will lock and unlock both issues and pull requests
As originally designed in the issue discussion, a single function
`NewCmdLock()` with a parameter to lock or unlock was proposed.
However, after playing around with a couple different designs, it seems
best to create two separate public functions and one private function to
do the common work.

Using two public functions seems to make sense because the api for
locking is different from the api for unlocking.  Therefore, the
documentation for both are different and keeping them in separate
functions would make it easier to maintain the documentation.
- Changed function to method
- Moved additional common options to method
- Remove redundant documentation
    - Cobra sets documentation in the Command struct.
These are needed to know if an issue is an actual issue or a pull
request.
Add queries for "ActiveLockReason" and "Locked"
@chemotaxis
chemotaxis force-pushed the gh-issue-lock branch 2 times, most recently from b88ffa2 to c1f98dc Compare April 11, 2022 08:50
- Fix error if found an issue while using `gh pr lock/unlock` or vice versa
- Added additional types
- Used githubv4 types
- Added "relock" state
    - If the conversation is already locked you have two choices: try to
      lock it again or do nothing.  Do nothing is easy.  But, if you
      want to change the lock reason, you need to first unlock the
      conversation and then lock it again.
- Added survey to confirm if you want to relock
- Added formatted print statements
- Switch to underscores
- Revise error message
Rather than saving the intended lock state and calling a method
depending on the lock state, just call the method directly.  By the time
you need to the padlock state, you already know which method to use; no
need to first change the lock state than call the method.

Also, refactored print/error messages that are conditional.
I noticed that PadlockState didn't really have anything to do with the
LockOptions and it was easy to call an incorrect locking function that
didn't match the PadlockState.

Now, you pass in the state as an argument and you simply call the
appropriate function instead of setting PadlockState and then calling
the correct function.

- Other touch ups and refactoring
Use switch statements for regular branching code.
As much as I like keeping statements as flat as possible, this
not-as-flat revision just seems easier to read.
@chemotaxis

chemotaxis commented Apr 29, 2022 •

Copy link
Copy Markdown
Contributor Author

I apologize if the history on this pull request is a little messy, but I went through several drafts before arriving at this current version. I haven't written any tests yet, but I wanted to see if the overall behavior was acceptable.

Some notes:

There seem to be several methods with overlapping functionality in issue/shared and pr/shared, so it was a little confusing at first to find the most appropriate function to request information about an issue or pull request.

It was also somewhat tricky to address two separate functions (lock/unlock) and two separate types (issues/pull requests) simultaneously. Hopefully my current solution addresses everything fairly cleanly.

Finally, since the current version of gh doesn't support querying/mutating the lock state or active lock reason, I needed to add a couple new fields to the Issue type. Consequently, gh doesn't display the locked state or lock reason. I plan to create a new issue in order to add lock-related information to issue view, pr view, at least.

@chemotaxis
chemotaxis marked this pull request as ready for review April 29, 2022 05:35
@chemotaxis
chemotaxis requested a review from a team as a code owner April 29, 2022 05:35
@chemotaxis
chemotaxis requested review from mislav and removed request for a team April 29, 2022 05:35
@cliAutomation cliAutomation added the external pull request originating outside of the CLI core team label Apr 29, 2022
Update a somewhat stale feature branch with the latest stable commits.
@vilmibm vilmibm assigned vilmibm and unassigned mislav Aug 22, 2022
@vilmibm
vilmibm self-requested a review August 22, 2022 16:16

@vilmibm vilmibm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great start. I'm cool with the UX and appreciate the effort put into making this work across issues and prs.

Comment thread pkg/cmd/issue/lock/lock.go Outdated
Comment thread pkg/cmd/issue/lock/lock.go Outdated
Comment thread pkg/cmd/issue/lock/lock.go Outdated
Comment thread pkg/cmd/issue/lock/lock_test.go
Add recent changes, especially changes to `api`.
While I usually like explicitly setting all values, getting rid of
setting the empty string streamlines map construction and testing.

The reasonsMap will return the nil value because the empty string is not
in the map.
Make sure reasons were added in the correct order.

@Upgradeddevil05 Upgradeddevil05 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vilmibm

vilmibm commented Dec 20, 2022

Copy link
Copy Markdown
Contributor

I've:

  • added an interactive picker for reason if TTY and none selected
  • simplified error messages when stdout is not TTY
  • tweaked some wording
  • added a full test suite

@vilmibm
vilmibm enabled auto-merge December 20, 2022 23:50
@vilmibm
vilmibm self-requested a review December 20, 2022 23:53
@vilmibm
vilmibm merged commit 1f85a92 into cli:trunk Dec 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external pull request originating outside of the CLI core team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support to manage issue/pull request/discussion locks

5 participants