-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathgit-ci
executable file
·97 lines (79 loc) · 1.94 KB
/
git-ci
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
#!/bin/bash
command -v git >/dev/null 2>&1 || { echo >&2 "This command needs git installed and in PATH."; exit 1; }
command -v jq >/dev/null 2>&1 || { echo >&2 "This command needs jq installed and in PATH. Install with brew install jq"; exit 1; }
GREEN="\e[0;32;7m"
YELLOW="\e[0;33;7m"
RED="\e[0;31;7m"
NOCOLOR="\e[0m"
function usage {
read -d '' USAGE <<"BLOCK"
USAGE: git-ci [OPTION] [SHA1]
SYNOPSIS:
Display CI status for a GitHub commit
OPTIONS:
-o Open the target URL for the build
-d Debug, show the API response
BLOCK
echo "$USAGE"
}
while getopts ":odh" opt; do
case $opt in
o)
OPEN_TARGET=1
;;
d)
DEBUG=1
;;
h)
usage
exit 1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
# Clear all options and reset the command line
shift $(( OPTIND -1 ))
if [ -n "$1" ]; then
SHA="$1"
else
if [ -d .git ] || git rev-parse --git-dir > /dev/null 2>&1 ; then
SHA=$(git rev-parse HEAD)
else
echo "Please supply commit SHA or run from a git Repository"
echo ""
usage
exit 1
fi
fi
if [ -z "$SHA" ]; then
echo "Commit SHA not found"
echo ""
usage
exit 1
fi
REPO=$(git config remote.origin.url | cut -d/ -f4- | sed s/.git$//g)
REPO=$(git config --get remote.origin.url | perl -pe's/(git@|https:\/\/)?github.com(:|\/)([-\.\w]+)\/([-\w]+)(\.git)?/$3\/$4/')
API_URL="https://api.github.com/repos/$REPO/statuses/$SHA"
RESPONSE=$(curl -H"Authorization: bearer $GITHUB_TOKEN" $API_URL)
STATE=$(echo $RESPONSE | jq -r '.[0].state')
TARGET=$(echo $RESPONSE | jq -r '.[0].target_url')
case "$STATE" in
pending )
printf "$YELLOW%s$NOCOLOR %s\n" $STATE $TARGET
;;
failure )
printf "$RED%s$NOCOLOR %s\n" $STATE $TARGET
;;
success )
printf "$GREEN%s$NOCOLOR %s\n" $STATE $TARGET
;;
esac
if [[ -n $OPEN_TARGET ]] && [[ -n $TARGET ]]; then
open $TARGET;
fi
if [[ -n $DEBUG ]]; then
echo $API_URL
echo $RESPONSE
fi