Skip to content

Instantly share code, notes, and snippets.

@cpyle0819
Last active June 29, 2024 09:36
Show Gist options
  • Save cpyle0819/27758200253ac2480c13cfde1b887e53 to your computer and use it in GitHub Desktop.
Save cpyle0819/27758200253ac2480c13cfde1b887e53 to your computer and use it in GitHub Desktop.
Clone a GitHub Issue
import subprocess
import argparse
import json
# Must be executed from within a git repo.
def main():
parser = argparse.ArgumentParser(description='Clone a GitHub issue.')
parser.add_argument('issue_number', type=int, help='The issue number to clone.')
args = parser.parse_args()
fields = ["title", "labels", "assignees", "body"]
# Run gh issue view and capture output
issue = subprocess.run(
["gh", "issue", "view", str(args.issue_number), "--json", ','.join(fields)],
capture_output=True, text=True
).stdout
issue_parsed = json.loads(issue);
print(issue_parsed);
# Create new issue
new_issue = ["gh", "issue", "create"]
for key, value in issue_parsed.items():
if key == "assignees":
new_issue.append('--assignee')
new_issue.append(','.join([assignee['login'] for assignee in value]))
elif key == "labels":
new_issue.append('--label')
new_issue.append(','.join(value))
elif key == "body":
new_issue.append('--body')
new_issue.append(value or ' ')
else:
new_issue.append(f'--{key}')
new_issue.append(value)
result = subprocess.run(new_issue, capture_output=True, text=True)
print(result.stdout);
print(result.stderr);
if __name__ == "__main__":
main()
Copy link

ghost commented Oct 5, 2023

good script!

@cpyle0819
Copy link
Author

  • Output stderr.
  • Fixed issue where empty body causes a failure.
  • Appended value in the else like every other statement.

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