summary refs log tree commit diff
path: root/bin/gh
blob: 66b7767277e91ae43198df86ce64fb75dc5d455b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/bin/bash

set -e -o pipefail

CURL='curl -sSL'
JQ='jq -r'

GH_ACCESS_TOKEN=${GH_ACCESS_TOKEN:-`cat $DOT_DIR/GH_TOKEN_GH_SCRIPT`}

usage() {
    printf "usage: gh [-v] [commands] [options]\n"
    printf "\ncommands:\n"
    printf " release-latest REPO\t\t prints the latest version tag of REPO\n"
    printf " release-url REPO TAG\t prints the url to the release TAG of REPO\n"
    printf " tag-latest REPO\t\t prints the latest version tag of REPO\n"
    printf " gql QUERY\t\t sends a query to the v4 graphql API\n"
    printf "\n"
}

err() {
    echo >&2 "error: $@"
}

has_cmd() {
    if ! command -v $1 1,2&>/dev/null; then
        err "$s not found"
        return 1
    fi
}

gql() {
    local q=$1
    local res=`$CURL https://api.github.com/graphql \
        -X POST \
        -H "Authorization: bearer $GH_ACCESS_TOKEN" \
        -H "Content-Type: application/json" \
        -d "{\"query\":\"query{ $q }\"}"`

    [[ $? -ne 0 ]] && {
        err `echo $res | $JQ '.message'`
        err "request was" "{\"query\":\"query{ $q }\"}"
        exit 1
    }
    [[ -z "$res" ]] && {
        err "empty response"
        exit 1
    }
    local errors=`echo $res | $JQ '.errors?'`
    [[ $errors -ne "null" ]] && {
        err "$errors"
        err "request was" "{\"query\":\"query{ $q }\"}"
        exit 1
    }
    echo $res
}

if [ -z "$GH_ACCESS_TOKEN" ]; then
    err "no github access token"
    exit 1
fi

has_cmd jq

cmd="$1"; opt="$2"
case $cmd in

    gql)
        if [[ -z $opt ]]; then
            err "no QUERY found"
            exit 1
        fi
        gql "$opt" | jq '.'
        exit 0
        ;;

    release-latest)
        if [[ -z $opt ]]; then
            err "no REPO found"
            exit 1
        fi
        opt=(${opt//\// })
        gql "repository(owner:\\\"${opt[0]}\\\",name:\\\"${opt[1]}\\\"){ releases(first:1, orderBy:{field:CREATED_AT, direction:DESC}){ nodes{tag{name}}} }" | $JQ '.data.repository.releases.nodes[].tag.name'
        exit 0
        ;;

    tag-latest)
        if [[ -z $opt ]]; then
            err "no REPO found"
            exit 1
        fi
        opt=(${opt//\// })
        gql "repository(owner:\\\"${opt[0]}\\\",name:\\\"${opt[1]}\\\"){ refs(first: 1, orderBy:{field:TAG_COMMIT_DATE, direction:DESC}, refPrefix:\\\"refs/tags/\\\"){ nodes{name}} }" | $JQ '.data.repository.refs.nodes[].name'
        exit 0
        ;;


    release-url)
        if [[ -z $opt ]]; then
            err "no REPO found"
            exit 1
        fi
        if [[ -z $3 ]]; then
            err "no TAG found"
        fi
        opt=(${opt//\// })
        gql "repository(owner:\\\"${opt[0]}\\\",name:\\\"${opt[1]}\\\"){release(tagName:\\\"$3\\\"){url}}" | $JQ '.data.repository.release.url'
        exit 0
        ;;

    *)
        usage
        exit 0
        ;;
esac