Skip to content

Commit

Permalink
Add authenticate, star/unstar actions
Browse files Browse the repository at this point in the history
  • Loading branch information
joelpurra committed Jan 26, 2018
1 parent 0fc27f1 commit e1e6e57
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 10 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ ln -s "$PWD/ghd" "$HOME/bin/ghd"

## Usage

Make sure you have loaded relevant github credentials by setting the following environment variables. Get your own credentials by [registering a new github oauth application](https://github.com/settings/applications/new). This is a bit of a hack, as the oauth part won't be used. The values for application name, homepage url, application description, and authorization callback url do not matter for `ghd`; pick your own values.
Make sure you have loaded relevant github credentials by setting the following environment variables. Get your own credentials by [registering a new github oauth application](https://github.com/settings/applications/new). The values for application name, homepage url, application description, and authorization callback url do not matter for `ghd`; pick your own values.


- `GITHUB_CLIENT_ID`
- `GITHUB_CLIENT_SECRET`

Authenticate yourself. If enabled, you will be asked for a 2-factor authentication code.

```bash
ghd authenticate <username>
```

Now execute the main command.

```bash
Expand All @@ -32,6 +38,7 @@ ghd
Usage: ghd <action>
Source folder: .../github-data-cli
Actions available in the source folder:
authenticate
contributors
diff-contributors
diff-help
Expand All @@ -42,7 +49,9 @@ Actions available in the source folder:
repositories-json
repositories-list
repositories
star
stargazers
unstar
```


Expand All @@ -57,4 +66,4 @@ Actions available in the source folder:



Copyright &copy; 2016, 2017 [Joel Purra](https://joelpurra.com/). Released under [GNU General Public License version 3.0 (GPL-3.0)](https://www.gnu.org/licenses/gpl.html).
Copyright &copy; 2016, 2017, 2018 [Joel Purra](https://joelpurra.com/). Released under [GNU General Public License version 3.0 (GPL-3.0)](https://www.gnu.org/licenses/gpl.html).
17 changes: 17 additions & 0 deletions ghd-authenticate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

set -e
set -u
set -o pipefail

source "${BASH_SOURCE%/*}/shared/functions.sh"
source "${BASH_SOURCE%/*}/shared/functionality.sh"
source "${BASH_SOURCE%/*}/shared/github/functionality.sh"
source "${BASH_SOURCE%/*}/shared/github/functionality-online.sh"
source "${BASH_SOURCE%/*}/shared/github/functionality-write.sh"

function main {
githubAuthenticate "$configUsername"
}

main "$@"
17 changes: 17 additions & 0 deletions ghd-star.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

set -e
set -u
set -o pipefail

source "${BASH_SOURCE%/*}/shared/functions.sh"
source "${BASH_SOURCE%/*}/shared/functionality.sh"
source "${BASH_SOURCE%/*}/shared/github/functionality.sh"
source "${BASH_SOURCE%/*}/shared/github/functionality-online.sh"
source "${BASH_SOURCE%/*}/shared/github/functionality-write.sh"

function main {
starGithubRepository "$configUsername" "$configRepository"
}

main "$@"
17 changes: 17 additions & 0 deletions ghd-unstar.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

set -e
set -u
set -o pipefail

source "${BASH_SOURCE%/*}/shared/functions.sh"
source "${BASH_SOURCE%/*}/shared/functionality.sh"
source "${BASH_SOURCE%/*}/shared/github/functionality.sh"
source "${BASH_SOURCE%/*}/shared/github/functionality-online.sh"
source "${BASH_SOURCE%/*}/shared/github/functionality-write.sh"

function main {
unstarGithubRepository "$configUsername" "$configRepository"
}

main "$@"
4 changes: 3 additions & 1 deletion shared/functionality.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ set -o pipefail

declare PROJECT_PREFIX="ghd"

readonly configOutputPrefix="${HOME}/.ghd/output";
readonly CONFIG_PREFIX="${HOME}/.ghd";
readonly CONFIG_OUTPUT_PREFIX="${CONFIG_PREFIX}/output";
readonly CONFIG_AUTHORIZATION_FILE="${CONFIG_PREFIX}/authorization.json"
readonly executionStartTimestamp=$(getUTCDatestamp)
7 changes: 7 additions & 0 deletions shared/functions/basic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ function errorMsg {
}

function debugMsg {
set +u
if [[ "$GHD_DEBUG" != "1" ]];
then
return 0
fi
set -u

echo -E "${PROJECT_PREFIX}: DEBUG $@" >&2
}

Expand Down
25 changes: 20 additions & 5 deletions shared/github/functionality.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,35 @@ set -e
set -u
set -o pipefail

function checkInput {
function checkUsernameInput {
set +u
if [[ -z "$1" ]];
then
die "Missing username; provide it on the command line"
fi
set -u

return 0
}

function checkRepositoryInput {
# NOTE: repository is not used much yet.
set +u
if [[ -z "$1" ]];
then
return 1
else
return 0
fi
set -u
}

checkInput "$@"
# TODO: use getopts or similar?
checkUsernameInput "$@" && readonly configUsername="$1" && shift

readonly configUsername="$1"
shift
# TODO: use getopts or similar?
checkRepositoryInput "$@" && readonly configRepository="$1" && shift

readonly configOutdir="${configOutputPrefix}/${configUsername}"
readonly configOutdir="${CONFIG_OUTPUT_PREFIX}/${configUsername}"

readonly configOutdirDaily="${configOutdir}/${executionStartTimestamp}"
78 changes: 76 additions & 2 deletions shared/github/github.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ readonly REPOSITORIES_FILE="repositories.json"
readonly SOURCES_FILE="sources.json"
readonly CONTRIBUTORS_FILE="contributors.json"

function fetchGithubCurl() {
curl --silent --header "User-Agent: ${PROJECT_PREFIX}" --header 'Accept: application/vnd.github.v3+json' "$@"
}

function fetchGithub {
local -a fetchArgs=( "$@" )
local url="${fetchArgs[-1]}"
Expand All @@ -11,7 +15,29 @@ function fetchGithub {
local -a urlParts
IFS='?' read -r -a urlParts <<< "$url"
local urlWithCredentials="${urlParts[0]}?client_id=${GITHUB_CLIENT_ID}&client_secret=${GITHUB_CLIENT_SECRET}&${urlParts[1]:-}"
curl --silent --header "User-Agent: ${PROJECT_PREFIX}" "${fetchArgs[@]}" "$urlWithCredentials"

fetchGithubCurl "${fetchArgs[@]}" "$urlWithCredentials"
}

function getLoggedInUsername() {
jq --raw-output '.username' "$CONFIG_AUTHORIZATION_FILE"
}

function getLoggedInToken() {
jq --raw-output '.token' "$CONFIG_AUTHORIZATION_FILE"
}

function loggedInFetchGithub {
local -a fetchArgs=( "$@" )
local url="${fetchArgs[-1]}"
debugMsg "$url"
unset fetchArgs[${#fetchArgs[@]}-1]

local -r loggedInToken="$(getLoggedInToken)"
fetchArgs+=("--header")
fetchArgs+=("Authorization: token ${loggedInToken}")

fetchGithubCurl "${fetchArgs[@]}" "$url"
}

function getNextPageLink {
Expand Down Expand Up @@ -42,7 +68,7 @@ function fetchPage {

if [[ ! -s "$outfile.${pageNumber}~" ]]
then
local page=$(fetchGithub --dump-header "$HEADERS_FILE" "${url}?page=${pageNumber}")
local page=$(fetchGithub "${url}?page=${pageNumber}")
local numberOfEntries=$(echo "$page" | getArrayLength)

if (( numberOfEntries > 0 ));
Expand Down Expand Up @@ -181,6 +207,36 @@ function checkGithubPrerequisites {
checkGithubRateLimit
}

function githubAuthenticate {
local -r username="$1"
shift

echo "Username: ${username}"

local password
read -e -s -p "Password (not saved): " password
echo

local -r timestamp=$(getUTCDatestamp)

local token

token="$(fetchGithub --data "{\"scopes\":[\"public_repo\"],\"note\":\"ghd ${timestamp}\"}" --dump-header "$HEADERS_FILE" "https://${username}:${password}@api.github.com/authorizations" | jq --raw-output '.token')"

local -r needs2fa="$(grep 'X-GitHub-OTP: required;' "$HEADERS_FILE")"

if [[ ! -z "$needs2fa" ]];
then
local token2fa
read -e -s -p "Two-factor authentication code: " token2fa
echo

token="$(fetchGithub --data "{\"scopes\":[\"public_repo\"],\"note\":\"ghd ${timestamp}\"}" --dump-header "$HEADERS_FILE" --header "X-GitHub-OTP: ${token2fa}" "https://${username}:${password}@api.github.com/authorizations" | tee ".debug.json~" | jq --raw-output '.token')"
fi

echo "{\"username\":\"${username}\",\"token\":\"${token}\"}" | jq '.' > "${CONFIG_AUTHORIZATION_FILE}"
}

function fetchGithubRepositories {
local -r username="$1"
shift
Expand All @@ -191,3 +247,21 @@ function fetchGithubRepositories {
function extractGithubSources {
cat "${configOutdirDaily}/${REPOSITORIES_FILE}" | jq 'map(select((.private or .fork or (.mirror_url | type) != "null") | not))' >"${configOutdirDaily}/${SOURCES_FILE}"
}

function starGithubRepository {
local -r username="$1"
shift
local -r repository="$1"
shift

loggedInFetchGithub --request PUT "https://api.github.com/user/starred/${username}/${repository}"
}

function unstarGithubRepository {
local -r username="$1"
shift
local -r repository="$1"
shift

loggedInFetchGithub --request DELETE "https://api.github.com/user/starred/${username}/${repository}"
}

0 comments on commit e1e6e57

Please sign in to comment.