[CLI] Add CLI for Community features (Discussions & Pull Requests) (#3855)
* feat: add CLI for community features (discussions and pull requests)
Add two new command groups to the hf CLI:
- `hf discussion` (alias: `discussions`) - Manage discussions on Hub repos
- list/ls, view, create, comment, close, reopen, rename
- `hf pr` (alias: `prs`) - Manage pull requests on Hub repos
- list/ls, view, create, comment, close, reopen, merge, diff
Inspired by the GitHub CLI (gh issue / gh pr) patterns:
- Consistent --state/-s filtering (open/closed/all for discussions; +merged/draft for PRs)
- --comments/-c flag on view to show all comments
- --web/-w flag to open in browser
- --format table/json and --quiet/-q for scriptability
- --comment/-c on close/reopen/merge for inline comments
- Color-coded status indicators in terminal output
Co-authored-by: Lucain <Wauplin@users.noreply.github.com>
* refactor: consolidate into single 'hf discussions' command group
Merge all discussion and PR commands into a unified 'hf discussions'
command group (no separate 'hf pr'):
- list/ls: added --kind filter (discussion, pull_request, all) and
--state now supports merged/draft for PRs
- create: added --pull-request/--pr flag to create PRs
- view: added --diff flag (for pull requests)
- merge: merge a pull request
- diff: show PR diff
- comment, close, reopen, rename: work on both discussions and PRs
Table output now includes a KIND column to distinguish PRs from
discussions.
Removed src/huggingface_hub/cli/prs.py.
Co-authored-by: Lucain <Wauplin@users.noreply.github.com>
* fix: align CLI flag names with API parameter names
- create: --body → --description (API: description)
- comment: --body → --comment (API: comment)
- rename: --title → --new-title (API: new_title)
- list: --state → --status (API: discussion_status)
- list: --kind → --discussion-type (API: discussion_type)
- Rename enums: DiscussionState → DiscussionStatus, DiscussionKind → DiscussionType
- Update all examples and help text accordingly
Co-authored-by: Lucain <Wauplin@users.noreply.github.com>
* fix: use proper Literal types for discussion_status and discussion_type
Fix mypy error: argument 'discussion_status' expects
Optional[Literal['all', 'open', 'closed']], not Optional[str].
Same fix applied to discussion_type.
Co-authored-by: Lucain <Wauplin@users.noreply.github.com>
* refactor: adjust CLI flag naming per review feedback
- create: --description → --body/-b (revert)
- comment: --comment/-c → --body/-b (revert)
- rename: --new-title option → positional argument NEW_TITLE
- list: --discussion-type → --type (--repo-type only for repo type
in this command to avoid collision)
- list: --status kept as-is
Co-authored-by: Lucain <Wauplin@users.noreply.github.com>
* refactor: address all review feedback on discussions CLI
2.1: Use print_list_output with api_object_to_dict instead of manual
print_as_table + separate quiet/json code paths
2.3: Rename --type back to --kind/-k to avoid collision with --type
(repo type); restore standard RepoTypeOpt
2.6: New ViewFormat enum (text/json) for view command instead of
reusing OutputFormat.table
3.1: Remove --web option entirely
3.2: Add --body-file for create/comment (supports file path and
'-' for stdin)
3.3: Add --yes/-y confirmation prompt on close, reopen, merge
(following buckets delete pattern)
4.1: Remove all -c and -b short aliases
4.5: Add --no-color flag on view (sets NO_COLOR env var, already
respected by the ANSI utility)
5.2: Use api_object_to_dict for all JSON output instead of manual
dict construction
5.3: Add comments explaining why merged/draft resolve to
api_status=None
Co-authored-by: Lucain <Wauplin@users.noreply.github.com>
* feat: add tests and documentation for discussions CLI
- Add tests/test_cli_discussions.py following test_buckets_cli.py patterns:
list (table/quiet/json/kind filter/status filter), view (text/json/no-color),
create (discussion/PR/body/body-file/conflict), comment (body/body-file),
close/reopen (with --yes and confirmation prompt), rename, merge, diff
- Update docs/source/en/guides/cli.md with hf discussions section
- Update docs/source/en/guides/community.md with CLI usage section
Co-authored-by: Lucain <Wauplin@users.noreply.github.com>
* fix: test failures — use shlex.split for quoted args, fix merge test
- Use shlex.split() instead of str.split(' ') in the cli() test
helper to properly handle quoted arguments like --title "My title"
- Fix test_merge_pr: use upload_file(create_pr=True) to create a
non-draft PR (draft PRs cannot be merged)
Co-authored-by: Lucain <Wauplin@users.noreply.github.com>
* fix: use tmp_path fixture instead of NamedTemporaryFile for Windows compat
On Windows, NamedTemporaryFile doesn't allow other processes to open
the file while the handle is still open. Switch to pytest's tmp_path
fixture which creates real files that are accessible cross-platform.
Co-authored-by: Lucain <Wauplin@users.noreply.github.com>
* fix: pass file paths via extra_args to avoid shlex backslash issues on Windows
shlex.split uses POSIX mode by default which interprets backslashes
in Windows paths (C:\Users\...) as escape characters, mangling the
path. Pass file paths via extra_args to bypass shlex entirely.
Co-authored-by: Lucain <Wauplin@users.noreply.github.com>
* better guide
* Update src/huggingface_hub/cli/discussions.py
---------
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Lucain <Wauplin@users.noreply.github.com>