Github issue is suitable to write technical blog, at least I think so. To backup the valuable issues automatically is first and foremost. To make things simple, in this article, only export the issues of a repository without their comments.
export issues
According to the List repository issues api, use the command as below manually:
curl \
-H "Authorization: token your-token" \
https://api.github.com/repos/your-username/your-repo/issues
If the correct result is returned, then we can go to next step. We can use launchd or cron to do some automatic jobs on macOS. Because cron has no output or error message, so I choose launchd. Before writing the launchd configuration file, we need to write the shell script which does the exporting and transformation work first.
Prepare the shell script
- First, we need to save the output of above api to a file which I named
output.jsonas the output format is json. - Then, we need to transform the data from json to csv to make the data more readable. Here, I found a good package called
json2csv
The final shell script as below:
curl -s $repo --header "Authorization: token "$token >$jsonPathName
$nodeBin $json2csvBin -i $jsonPathName -f $fields -o $csvPathName
echo $msg$(date +" %d-%m-%Y-%H:%M:%S")
The shell variable and its meaning as below table:
| variable | meaning |
|---|---|
| repo | your repo name |
| token | your github api token used to do this exporting work |
| jsonPathName | the file to receive the output of exporting |
| nodeBin | node executable file |
| json2csvBin | json2csv script file |
| fields | preferred output fields |
| csvPathName | the transformed csv file |
| msg | the message when all the works done |
Run the shell script manually, you will see the csv file in the specified location and the successful message in the terminal.
Configure the automatic part
Here is an example as below, the parameters meaning can be found here.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>test.ssaw.issue-export</string>
<key>Program</key>
<string>/Users/your-username/issue-export2csv.sh</string>
<key>StandardErrorPath</key>
<string>/tmp/test.ssaw.issue-export.err</string>
<key>StandardOutPath</key>
<string>/tmp/test.ssaw.issue-export.out</string>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>17</integer>
<key>Minute</key>
<integer>29</integer>
</dict>
</dict>
</plist>
Save the plist file in the ~/Library/LaunchAgents/.
Using launchctl to load the timed job
launchctl load ~/Library/LaunchAgents/test.ssaw.issue-export.plist
Enjoy the happiness of technology!