mbohun
3/20/2015 - 1:32 AM

githubapi-get.sh notes

githubapi-get.sh notes

#!/bin/bash

set -e

if [ ${#@} -lt 2 ]; then
    echo "usage: $0 [your github token] [REST expression]"
    exit 1;
fi

GITHUB_TOKEN=$1
GITHUB_API_REST=$2

# TODO: we have to allow for passing new/custom Accept header-s form the cmd line,
#       and/or setup a lookup table of accept headers from the known headers?
#GITHUB_API_HEADER_ACCEPT="Accept: application/vnd.github.v3+json"
#GITHUB_API_HEADER_ACCEPT="Accept: application/vnd.github.sersi-preview+json"
GITHUB_API_HEADER_ACCEPT="Accept: application/vnd.github.drax-preview+json"

temp=`basename $0`
TMPFILE=`mktemp /tmp/${temp}.XXXXXX` || exit 1

# single page result-s (no pagination), have no Link: section, the grep result is empty
last_page=`curl -s -I "https://api.github.com${GITHUB_API_REST}" -H "${GITHUB_API_HEADER_ACCEPT}" -H "Authorization: token $GITHUB_TOKEN" | grep '^Link:' | sed -e 's/^Link:.*page=//g' -e 's/>.*$//g'`

# does this result use pagination?
if [ -z "$last_page" ]; then
    # no - this result has only one page
    curl -s "https://api.github.com${GITHUB_API_REST}" -H "${GITHUB_API_HEADER_ACCEPT}" -H "Authorization: token $GITHUB_TOKEN" >> $TMPFILE
    cat $TMPFILE

else
    # yes - this result is on multiple pages
    for p in `seq 1 $last_page`; do
	curl -s "https://api.github.com${GITHUB_API_REST}?page=$p" -H "${GITHUB_API_HEADER_ACCEPT}" -H "Authorization: token $GITHUB_TOKEN" | sed -e 's/^\[$//g' -e 's/^\]$/,/g' >> $TMPFILE
    done

    # return the multipage JSON result-s as a JSON array
    line_counter=`wc -l $TMPFILE | sed -e 's/[/a-zA-Z].*$//g'`

    echo "["
    head -n $(($line_counter - 1)) $TMPFILE
    echo "]"

fi
$ ./githubapi-get.sh $GITHUBTOKEN /licenses
[
  {
    "key": "agpl-3.0",
    "name": "GNU Affero General Public License v3.0",
    "url": "https://api.github.com/licenses/agpl-3.0"
  },
  {
    "key": "apache-2.0",
    "name": "Apache License 2.0",
    "url": "https://api.github.com/licenses/apache-2.0"
  },
  {
    "key": "artistic-2.0",
    "name": "Artistic License 2.0",
    "url": "https://api.github.com/licenses/artistic-2.0"
  },
  {
    "key": "bsd-2-clause",
    "name": "BSD 2-clause \"Simplified\" License",
    "url": "https://api.github.com/licenses/bsd-2-clause"
  },
  {
    "key": "bsd-3-clause",
    "name": "BSD 3-clause \"New\" or \"Revised\" License",
    "url": "https://api.github.com/licenses/bsd-3-clause"
  },
  {
    "key": "cc0-1.0",
    "name": "Creative Commons Zero v1.0 Universal",
    "url": "https://api.github.com/licenses/cc0-1.0"
  },
  {
    "key": "epl-1.0",
    "name": "Eclipse Public License 1.0",
    "url": "https://api.github.com/licenses/epl-1.0"
  },
  {
    "key": "gpl-2.0",
    "name": "GNU General Public License v2.0",
    "url": "https://api.github.com/licenses/gpl-2.0"
  },
  {
    "key": "gpl-3.0",
    "name": "GNU General Public License v3.0",
    "url": "https://api.github.com/licenses/gpl-3.0"
  },
  {
    "key": "isc",
    "name": "ISC License",
    "url": "https://api.github.com/licenses/isc"
  },
  {
    "key": "lgpl-2.1",
    "name": "GNU Lesser General Public License v2.1",
    "url": "https://api.github.com/licenses/lgpl-2.1"
  },
  {
    "key": "lgpl-3.0",
    "name": "GNU Lesser General Public License v3.0",
    "url": "https://api.github.com/licenses/lgpl-3.0"
  },
  {
    "key": "mit",
    "name": "MIT License",
    "url": "https://api.github.com/licenses/mit"
  },
  {
    "key": "mpl-2.0",
    "name": "Mozilla Public License 2.0",
    "url": "https://api.github.com/licenses/mpl-2.0"
  },
  {
    "key": "unlicense",
    "name": "The Unlicense",
    "url": "https://api.github.com/licenses/unlicense"
  }
]
$ ./githubapi-get.sh $GITHUBTOKEN /licenses/mit
{
  "key": "mit",
  "name": "MIT License",
  "url": "https://api.github.com/licenses/mit",
  "html_url": "http://choosealicense.com/licenses/mit/",
  "featured": true,
  "description": "A permissive license that is short and to the point. It lets people do anything with your code with proper attribution and without warranty.",
  "category": "MIT",
  "implementation": "Create a text file (typically named LICENSE or LICENSE.txt) in the root of your source code and copy the text of the license into the file. Replace [year] with the current year and [fullname] with the name (or names) of the copyright holders.",
  "required": [
    "include-copyright"
  ],
  "permitted": [
    "commercial-use",
    "modifications",
    "distribution",
    "sublicense",
    "private-use"
  ],
  "forbidden": [
    "no-liability"
  ],
  "body": "\n\nThe MIT License (MIT)\n\nCopyright (c) [year] [fullname]\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
}
$ ./githubapi-get.sh $GITHUBTOKEN /licenses/mit | python -m json.tool
{
    "body": "\n\nThe MIT License (MIT)\n\nCopyright (c) [year] [fullname]\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n",
    "category": "MIT",
    "description": "A permissive license that is short and to the point. It lets people do anything with your code with proper attribution and without warranty.",
    "featured": true,
    "forbidden": [
        "no-liability"
    ],
    "html_url": "http://choosealicense.com/licenses/mit/",
    "implementation": "Create a text file (typically named LICENSE or LICENSE.txt) in the root of your source code and copy the text of the license into the file. Replace [year] with the current year and [fullname] with the name (or names) of the copyright holders.",
    "key": "mit",
    "name": "MIT License",
    "permitted": [
        "commercial-use",
        "modifications",
        "distribution",
        "sublicense",
        "private-use"
    ],
    "required": [
        "include-copyright"
    ],
    "url": "https://api.github.com/licenses/mit"
}