Export Old Pull Requests from GitHub

For various (perhaps less than rational) reasons, I find myself needing to document my past work using Pull Requests. Since this is more about quantity than quality, I sought to automate the task. To simplify the process, I crafted a concise bash script that achieves precisely that, generating Markdown files.

#!/bin/bash
set -e

# Max number of PRs
LIMIT=500

# Check Output Folder
if [[ -z "${OUTPUT_FOLDER}" ]]; then
    # Set to default
    OUTPUT_FOLDER="ghexport"
# else
    # Folder is set
fi
mkdir -p $OUTPUT_FOLDER

PR_LIST_FILE="$OUTPUT_FOLDER/pr_list.txt"
gh pr list --json number --state closed --jq '.[].number' -L $LIMIT > $PR_LIST_FILE

lines=$(cat $PR_LIST_FILE)
for PR_NUMBER in $lines
do
    # Export PR into md file
    echo "Current PR: $PR_NUMBER "
    FILE_NAME="$OUTPUT_FOLDER/$PR_NUMBER.md"

    echo "Filename: $FILE_NAME"

    gh pr view $PR_NUMBER --json number,title,body,reviews,assignees,author,commits \
        --template   '{{printf "# %v" .number}} {{.title}}

Author: {{.author.name}} - {{.author.login}}

{{.body}}

## Commits
{{range .commits}}
- {{ .messageHeadline }} [ {{range .authors}}{{ .name }}{{end}} ]{{end}}

## Reviews

{{range .reviews}}{{ .body }}{{end}}


' > $FILE_NAME

done

# ## Assignees
# {{range .assignees}}{{.login .name}}{{end}}

The code can be accessed on GitHub: oliverscheer/github-export: Export Pull Requests and contributor information from GitHub projects.

https://github.com/oliverscheer/github-export

To be unequivocal, mandating developers to document their work through a set number of Pull Requests is among the least productive tasks managers can impose on their teams.