Skip to content

Instantly share code, notes, and snippets.

@HeinrichHartmann
Created May 5, 2021 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HeinrichHartmann/9c8b9647322e041fe161598073e46387 to your computer and use it in GitHub Desktop.
Save HeinrichHartmann/9c8b9647322e041fe161598073e46387 to your computer and use it in GitHub Desktop.
Pull issues into local text files via GH CLI
#!/usr/bin/env python
"""
SYNOPSIS
gh-issue-fetch [-R <repository url>] [-d <output directory>]
DESCRIPTION
Fetch all issues from a GH repository into local text files.
By default issues are placed in ./issues/*, this can be
changed by using the -d flag.
DEPENDENCIES
Depends on gh and jq commands being available on $PATH.
- gh https://cli.github.com/
- jq https://stedolan.github.io/jq/
"""
import click
import sys
import json
import re
import subprocess as sp
from jinja2 import Template
from pathlib import Path
name_template = Template("{{ repo }} - {% if closed %}[closed] {% endif %}#{{ number }} {{ title }}.md")
file_template = Template("""\
# #{{ number }} -- {{ title }}
- author: {{ author.login }}
- date: {{ createdAt }}
- url: {{ url }}
{{body | indent(4, first=true) }}
# Comments
{% for c in comments %}
* {{ c.createdAt }} @{{ c.author.login }}:
{{ c.body | wordwrap(100) | indent(2, first=true) }}
{% endfor %}
""")
@click.command()
@click.option('-R','--repository')
@click.option('-d','--output-directory', default="./issues")
def main(repository, output_directory):
gh_cmd = ['gh']
if repository: gh_cmd += ['-R', repository ]
gh_cmd += 'issue list --state all --json author,title,closed,number,url,body,comments,createdAt'.split()
p_gh = sp.Popen(gh_cmd, stdout=sp.PIPE)
p_jq = sp.Popen(('jq', '-c', '.[]'), stdin=p_gh.stdout, stdout=sp.PIPE)
d_out : Path = Path(output_directory)
d_out.mkdir(parents=True, exist_ok=True)
for line in p_jq.stdout:
line : str
line = line.strip()
if len(line) == 0:
continue
o : dict = json.loads(line)
o["org"] = o['url'].split("/")[3]
o["repo"] = o['url'].split("/")[4]
path = Path()
name : str = name_template.render(o)
name = name.replace("/", "-")
p_out = d_out / name
with open(p_out, "w") as fh:
fh.write(file_template.render(o))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment