Skip to content

Commit

Permalink
Add capability to create issue form files
Browse files Browse the repository at this point in the history
  • Loading branch information
raffaele-oplabs committed Dec 4, 2024
1 parent cf94eca commit a5bfb34
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
35 changes: 35 additions & 0 deletions github_utility/github_cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,41 @@
from typing import Optional # Import Optional for type hinting


def create_issue_from_string(
github: Github,
repo: str,
issue_title: str,
issue_body: str,
issue_labels: Optional[list] = None,
assignees: Optional[list] = None
) -> any:
"""Create an issue with the given title, body, labels, and assignees."""
if not all([repo, issue_title, issue_body, github]):
raise ValueError("All parameters must be provided and valid.")

repository = github.get_repo(repo)

# Prepare issue data
issue_data = {
"title": issue_title,
"body": issue_body,
}

# Add labels if provided
if issue_labels:
issue_data["labels"] = [label.strip()
for label in issue_labels.split(",")]

# Add assignees if provided
if assignees:
issue_data["assignees"] = [assignee.strip()
for assignee in assignees.split(",")]

# Create the issue
issue = repository.create_issue(**issue_data)
return issue


def post_pr_comment(
github: Github,
repo: str,
Expand Down
76 changes: 76 additions & 0 deletions github_utility/github_cli/main.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,93 @@
import typer
from github import Github
from typing import Optional # Import Optional for type hinting
from pathlib import Path # For file operations
from pprint import pprint # For pretty printing
from commands import (
post_pr_comment,
delete_pr_comment,
process_issues,
process_pull_requests,
get_comments_ids,
get_pr_base_sha,
create_issue_from_string,
)
app = typer.Typer(help="CLI tool for GitHub operations.")


@app.command("create-issue-from-file")
def cli_create_issue_from_file(
github_token: str = typer.Option(...,
help="GitHub token with permissions to create issues."),
repo: str = typer.Option(...,
help="GitHub repository in the format 'owner/repo'."),
file_path: Path = typer.Option(...,
help="Path to the file containing issue content."),
issue_title: str = typer.Option(..., help="Title of the issue to create."),
issue_labels: Optional[str] = typer.Option(
None, help="Comma-separated list of labels for the issue."),
assignees: Optional[str] = typer.Option(
None, help="Comma-separated list of GitHub usernames to assign."),
):
"""
Create an issue from a file, with optional labels and assignees.
"""
try:
# Read file content
if not file_path.exists() or not file_path.is_file():
raise FileNotFoundError(
f"The file '{file_path}' does not exist or is not a valid file.")

with file_path.open("r") as file:
issue_body = file.read().strip()

if not issue_body:
raise ValueError(
"The file is empty. Provide a file with valid issue content.")

# Connect to GitHub
github = Github(github_token)
issue = create_issue_from_string(
github, repo, issue_title, issue_body, issue_labels, assignees)

typer.echo(f"Issue #{issue.number} created successfully: {
issue.html_url}")

except Exception as e:
typer.echo(f"Error: {e}", err=True)
raise typer.Exit(code=1)


@app.command("create-issue-from-string")
def cli_create_issue_from_string(
github_token: str = typer.Option(...,
help="GitHub token with permissions to create issues."),
repo: str = typer.Option(...,
help="GitHub repository in the format 'owner/repo'."),
issue_body: str = typer.Option(..., help="Content of the issue."),
issue_title: str = typer.Option(..., help="Title of the issue to create."),
issue_labels: Optional[str] = typer.Option(
None, help="Comma-separated list of labels for the issue."),
assignees: Optional[str] = typer.Option(
None, help="Comma-separated list of GitHub usernames to assign."),
):
"""
Create an issue from a string, with optional labels and assignees.
"""
try:
# Connect to GitHub
github = Github(github_token)
issue = create_issue_from_string(
github, repo, issue_title, issue_body, issue_labels, assignees)

typer.echo(f"Issue #{issue.number} created successfully: {
issue.html_url}")

except Exception as e:
typer.echo(f"Error: {e}", err=True)
raise typer.Exit(code=1)


@app.command("post-pr-comment")
def cli_post_pr_comment(
github_token: str = typer.Option(...,
Expand Down
2 changes: 1 addition & 1 deletion github_utility/github_cli/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
PyGithub==2.5.0
python_dateutil==2.9.0.post0
typer==0.13.1
typer==0.15.0

0 comments on commit a5bfb34

Please sign in to comment.