-
Notifications
You must be signed in to change notification settings - Fork 1
/
ghnotify.sh
executable file
·1332 lines (1111 loc) · 41.1 KB
/
ghnotify.sh
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#
# Shell script to parse github.com using only the GitHub API for a
# configurable selection of repositories and branches, emailing a
# summary of new commits.
#
# No email is sent when there have been no new commits.
#
# Requires: qprint (apt-get install qprint).
#
# Reads a list of repositories to be monitored from ./ghnotify.conf:
#
# <owner/repository/branch> <name of monitored repository>
#
# Example:.
#
# raspberrypi/firmware/master Raspberry Pi Firmware
#
# Writes the latest SHA of each monitored repository to ./ghnotify.commits.
# Writes the latest PR number of each monitored repository to ./ghnotify.pulls.
#
# Uses HTML template fragment files ./ghnotify.template.main and
# ./ghnotify.template.sub to generate the HTML message, which is then
# converted to quoted-printable format using the qprint utility.
#
# To authenticate with github.com, create a file named ~/.git.conf.
# Specify the username and password used for GitHub access to avoid GitHub
# rate limits. Authentication is optional, and may be unecessary depending on
# often you query GitHub, and how many repositories are being monitored. See
# GitHub API for details on rate limiting: https://developer.github.com/v3/rate_limit
#
# Also specify your email address in ~/.git.conf:
#
# GIT_USERNAME="username"
# GIT_PASSWORD="password"
# EMAILTO="email@address.com"
#
# You can also use ~/.git.conf to override other variables, such as which email binary
# to use when sending email (the script will attempt find a suitable MTA).
#
# (c) Neil MacLeod 2014-present :: ghnotify@nmacleod.com :: https://github.com/MilhouseVH/ghnotify
#
VERSION="v0.2.3"
BIN=$(readlink -f $(dirname $0))
# Avoid running more than one instance
PIDFILE="/tmp/$(basename $0).pid"
[ -f "${PIDFILE}" ] && exit 1
GHNOTIFY_CONF=ghnotify.conf
GHNOTIFY_DATA=ghnotify.dat
GHNOTIFY_CDATA=ghnotify.commits
GHNOTIFY_PDATA=ghnotify.pulls
GHNOTIFY_RDATA=ghnotify.releases
[ -z "${GHNOTIFY_GITDIR}" ] && GHNOTIFY_GITDIR=git
GHNOTIFY_WORKDIR=$(mktemp -d)
GHNOTIFY_CTEMP=${GHNOTIFY_WORKDIR}/.ctemp
GHNOTIFY_PTEMP=${GHNOTIFY_WORKDIR}/.ptemp
GHNOTIFY_RTEMP=${GHNOTIFY_WORKDIR}/.rtemp
GHNOTIFY_LOCKDIR=${GHNOTIFY_WORKDIR}/.locks
TMPFILE=${GHNOTIFY_WORKDIR}/.tmpfile
WEBWORKFILE=${GHNOTIFY_WORKDIR}/init.webrequest
USEPYTHON=${USEPYTHON:-python}
trap 'rm -rf -- "${PIDFILE}" "${GHNOTIFY_WORKDIR}"' EXIT
echo $$ > $PIDFILE
DEFAULTMAXJOBS=$(($(nproc) * 10))
MAXJOBS=${MAXJOBS:-${DEFAULTMAXJOBS}}
# Stop sending emails if the specified number of days have elapsed since
# the last modification to the CHECK_FILE.
CHECK_INTERVAL_DAYS=14
CHECK_FILE=${BIN}/patches.dat
PY_COMMIT_PR='
import os, sys, json, datetime, codecs, re, io
if sys.version_info >= (3, 0):
import urllib.request as urllib2
else:
import urllib2
DEBUGGING=os.environ.get("DEBUG")
DEFAULT_AVATAR="https://assets-cdn.github.com/images/gravatars/gravatar-user-420.png"
NOW_DATE=datetime.datetime.utcnow()
NOW_YEAR=NOW_DATE.strftime("%Y")
if sys.version_info >= (3, 1):
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
sys.stderr = codecs.getwriter("utf-8")(sys.stderr.detach())
else:
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
sys.stderr = codecs.getwriter("utf-8")(sys.stderr)
def debug(msg):
if DEBUGGING:
sys.stdout.write("%s\n" % msg)
def whendelta(when):
when_date = datetime.datetime.strptime(when, "%Y-%m-%dT%H:%M:%SZ")
when_year = when[:4]
delta = NOW_DATE - when_date
debug("UTC Now: %s, When: %s, Delta: %s" % (NOW_DATE, when_date, delta))
if delta.days > 30:
if NOW_YEAR != when_year:
return "on %s" % when_date.strftime("%d %b %Y")
else:
return "on %s" % when_date.strftime("%d %b")
if delta.days > 0:
return "%s day%s ago" % (delta.days, "s"[delta.days==1:])
if delta.seconds >= 3600:
hours = int(round(float(delta.seconds) / 3600))
if hours > 0:
if hours == 1:
return "an hour ago"
else:
return "%s hours ago" % (hours)
else:
mins = int(round(float(delta.seconds) / 60))
return "%s minute%s ago" % (mins, "s"[mins==1:])
def setavatar(list, creator):
id = creator.get("login", creator.get("name", ""))
if id and id not in list:
url = creator.get("avatar_url", DEFAULT_AVATAR)
url = url[:-1] if url[-1:] == "?" else url
list[id] = {"avatar": url, "gravatar": creator.get("gravatar_id", None) }
return
def getavatar(list, creator, size=20, enable_gravatar=False):
id = creator.get("login", creator.get("name", ""))
if id and id in list:
avatar = list[id]["avatar"]
gravatar = list[id]["gravatar"]
if gravatar and enable_gravatar:
return "https://1.gravatar.com/avatar/%s?d=%s&r=x&s=%d" % (gravatar, urllib2.quote(avatar, "()"), size)
else:
return "%s?s=%d" % (avatar, size)
else:
return "%s?s=%d" % (DEFAULT_AVATAR, size)
def htmlsafe(input):
if input:
tmp = input.replace("&", "&").replace("<", "<").replace(">", ">")
else:
tmp = input
return toUnicode(tmp)
def toUnicode(data):
if sys.version_info >= (3, 0): return data
if isinstance(data, basestring):
if not isinstance(data, unicode):
try:
data = unicode(data, encoding="utf-8", errors="ignore")
except UnicodeDecodeError:
pass
return data
data=[]
for line in sys.stdin: data.append(line)
jdata = json.loads("".join(data))
dfile = sys.argv[1]
ditem = sys.argv[2]
dtype = sys.argv[3]
output = codecs.open(dfile, "w", encoding="utf-8")
if "message" in jdata:
output.write(u"ERROR\n")
output.close()
sys.exit(0)
if dtype == "commits" and "commits" in jdata:
debug("%d commits loaded for %s" % (len(jdata["commits"]), jdata["url"]))
try:
PULL_URL = re.sub("compare/[a-z0-9]*...[a-z0-9]*$", "pull/", jdata["html_url"])
RE_PULL = re.compile("#([0-9]+)")
avatars = {}
for c in jdata["commits"]:
if c["author"]: setavatar(avatars, c["author"])
if c["committer"]: setavatar(avatars, c["committer"])
for c in reversed(jdata["commits"]):
if c["author"]:
avatar_url = getavatar(avatars, c["author"])
author = c["author"]["login"]
else:
avatar_url = getavatar(avatars, c["commit"]["author"])
author = c["commit"]["author"]["name"]
commitdata = "%s authored %s" % (author, whendelta(c["commit"]["author"]["date"]))
if c["commit"]["committer"] and c["commit"]["author"]:
if c["commit"]["committer"]["name"] != c["commit"]["author"]["name"] or \
c["commit"]["committer"]["email"] != c["commit"]["author"]["email"]:
commitdata = "%s (%s committed %s)" % (commitdata, c["commit"]["committer"]["name"], whendelta(c["commit"]["committer"]["date"]))
message = htmlsafe(c["commit"]["message"].split("\n")[0])
message = RE_PULL.sub("<a href=\"%s\\1\">#\\1</a>" % PULL_URL, message)
output.write(u"%s %s %s\n" % (avatar_url, htmlsafe(commitdata.replace(" ", "\001")), message))
debug(" Message : %s" % message)
debug(" Avatar : %s" % avatar_url)
debug(" Who/When: %s" % commitdata)
except:
raise
sys.exit(1)
elif dtype == "pulls":
debug("%d pull requests loaded" % len(jdata))
try:
avatars = {}
for c in jdata:
if c["user"]: setavatar(avatars, c["user"])
lastpr = 0
if len(sys.argv) == 5:
tmp = sys.argv[4]
lastpr = int(tmp) if tmp and tmp != "unknown" else 0
if len(jdata) != 0:
output.write(u"%s\n" % jdata[0]["number"])
for pr in [x for x in jdata if x["number"] > lastpr]:
avatar_url = getavatar(avatars, pr["user"])
author = pr["user"]["login"]
pulldata = "%s authored %s" % (author, whendelta(pr["created_at"]))
message = "<a href=\"%s\">#%s</a> %s" % (pr["html_url"], pr["number"], htmlsafe(pr["title"]))
output.write(u"%s %s %s\n" % (avatar_url, htmlsafe(pulldata.replace(" ", "\001")), message))
debug(" Message : %s" % message)
debug(" Avatar : %s" % avatar_url)
debug(" Who/When: %s" % pulldata)
except:
raise
output.close()
'
PY_COMMIT_GIT='
import os, sys, json, datetime, codecs, re, io
if sys.version_info >= (3, 0):
import urllib.request as urllib2
else:
import urllib2
DEBUGGING=os.environ.get("DEBUG")
DEFAULT_AVATAR="https://assets-cdn.github.com/images/gravatars/gravatar-user-420.png"
NOW_DATE=datetime.datetime.utcnow()
NOW_YEAR=NOW_DATE.strftime("%Y")
if sys.version_info >= (3, 1):
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
sys.stderr = codecs.getwriter("utf-8")(sys.stderr.detach())
else:
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
sys.stderr = codecs.getwriter("utf-8")(sys.stderr)
def debug(msg):
if DEBUGGING:
sys.stdout.write("%s\n" % msg)
def whendelta(when):
when_date = datetime.datetime.strptime(when, "%Y-%m-%dT%H:%M:%SZ")
when_year = when[:4]
delta = NOW_DATE - when_date
debug("UTC Now: %s, When: %s, Delta: %s" % (NOW_DATE, when_date, delta))
if delta.days > 30:
if NOW_YEAR != when_year:
return "on %s" % when_date.strftime("%d %b %Y")
else:
return "on %s" % when_date.strftime("%d %b")
if delta.days > 0:
return "%s day%s ago" % (delta.days, "s"[delta.days==1:])
if delta.seconds >= 3600:
hours = int(round(float(delta.seconds) / 3600))
if hours > 0:
if hours == 1:
return "an hour ago"
else:
return "%s hours ago" % (hours)
else:
mins = int(round(float(delta.seconds) / 60))
return "%s minute%s ago" % (mins, "s"[mins==1:])
def getavatar(size=20):
return "%s?s=%d" % (DEFAULT_AVATAR, size)
def htmlsafe(input):
if input:
tmp = input.replace("&", "&").replace("<", "<").replace(">", ">")
else:
tmp = input
return toUnicode(tmp)
def toUnicode(data):
if sys.version_info >= (3, 0): return data
if isinstance(data, basestring):
if not isinstance(data, unicode):
try:
data = unicode(data, encoding="utf-8", errors="ignore")
except UnicodeDecodeError:
pass
return data
def epoch2date(epoch):
return datetime.datetime.fromtimestamp(int(epoch.split(" ")[0])).strftime("%Y-%m-%dT%H:%M:%SZ")
dfile = sys.argv[1]
ditem = sys.argv[2]
output = codecs.open(dfile, "w", encoding="utf-8")
jdata={"commits": []}
for line in sys.stdin:
fields = line.split("\t")
jdata["commits"].append({
"author": {"name": fields[0], "date": epoch2date(fields[1]), "email": fields[2]},
"committer": {"name": fields[3], "date": epoch2date(fields[4]), "email": fields[5]},
"message": fields[6]
})
debug("%d commits loaded" % len(jdata["commits"]))
try:
for c in jdata["commits"]:
avatar_url = getavatar()
author = c["author"]["name"] if c["author"]["name"] else c["committer"]["name"]
commitdata = "%s authored %s" % (author, whendelta(c["author"]["date"]))
if c["committer"]["name"] and c["author"]["name"]:
if c["committer"]["name"] != c["author"]["name"] or \
c["committer"]["email"] != c["author"]["email"]:
commitdata = "%s (%s committed %s)" % (commitdata, c["committer"]["name"], whendelta(c["committer"]["date"]))
message = htmlsafe(c["message"].split("\n")[0])
output.write(u"%s %s %s\n" % (avatar_url, htmlsafe(commitdata.replace(" ", "\001")), message))
debug(" Message : %s" % message)
debug(" Avatar : %s" % avatar_url)
debug(" Who/When: %s" % commitdata)
except:
raise
sys.exit(1)
output.close()
'
PY_JSON_DATA='
import sys, json, datetime
data=[]
for line in sys.stdin: data.append(line)
jdata = json.loads("".join(data))
variable = sys.argv[1]
tdata = jdata
for v in variable.split("."):
if v in tdata:
tdata = tdata[v]
print(tdata)
'
warn() {
local fmt="$1"
shift
printf "ghnotify.sh: $fmt\n" "$@" >&2
}
die() {
local st="$?"
if [[ "$1" != *[^0-9]* ]]; then
st="$1"
shift
fi
warn "$@ - terminating"
exit "$st"
}
logger() {
local item=$1
while IFS= read -r line; do
printf "[%s] %s\n" ${item} "${line}"
done
}
getcomponent()
{
local field="$1" string="$2" remove="$3"
if [ -n "${remove}" ]; then
echo "${string}" | cut -d/ -f${field} | sed "s/${remove}//"
else
echo "${string}" | cut -d/ -f${field}
fi
}
getlatestsha()
{
local item="$1" output="$2" owner_repo_branch="$3" gitrepodir="$4"
local URL
if [ -n "${gitrepodir}" ]; then
git --git-dir="${gitrepodir}/.git" rev-parse HEAD > "${output}" && return 0 || return 1
fi
URL="${GITAPI}/$(getcomponent 1 "${owner_repo_branch}")/$(getcomponent 2 "${owner_repo_branch}")/commits?per_page=1&sha=$(getcomponent 3- "${owner_repo_branch}")"
webrequest "${URL}" || return 1
cat ${WEBWORKFILE} | ${USEPYTHON} -c '
import sys, json
data=[]
for line in sys.stdin: data.append(line)
jdata = json.loads("".join(data))
for item in jdata:
if "sha" in item:
print("%s" % item["sha"])
break
' > "${output}"
return 0
}
getalltags()
{
local item="$1" output="$2" owner_repo_branch="$3" gitrepodir="$4"
local URL
if [ -n "${gitrepodir}" ]; then
(
cd ${gitrepodir}
echo "['$(git describe --tags $(git rev-list --tags --max-count=1 2>/dev/null) 2>/dev/null)']" > "${output}"
) && return 0 || return 1
fi
URL="${GITAPI}/$(getcomponent 1 "${owner_repo_branch}")/$(getcomponent 2 "${owner_repo_branch}")/tags"
webrequest "${URL}" Y || return 1
cat ${WEBWORKFILE} | ${USEPYTHON} -c '
import sys, json
data=[]
for line in sys.stdin: data.append(line)
jdata = json.loads("".join(data))
tags = []
for page in jdata:
for item in page:
tags.append(item["name"])
print(json.dumps(sorted(tags)))
'> "${output}"
return 0
}
getnewtagdetails()
{
local item="$1" output="$2" owner_repo_branch="$3" gitrepodir="$4" lastvalue="$5" crntvalue="$6"
${USEPYTHON} -c '
from __future__ import print_function
import sys, json
DEFAULT_AVATAR="https://assets-cdn.github.com/images/gravatars/gravatar-user-420.png"
last=json.loads(sys.argv[1].replace(chr(39),"\""))
crnt=json.loads(sys.argv[2].replace(chr(39),"\""))
for tag in crnt:
if tag and tag not in last:
print("%s %s" % (DEFAULT_AVATAR, tag))
' "${lastvalue}" "${crntvalue}" > "${output}"
}
getcommitdetails()
{
local item="$1" output="$2" owner_repo_branch="$3" gitrepodir="$4" lastvalue="$5" crntvalue="$6"
local URL
if [ -n "${gitrepodir}" ]; then
[ "${lastvalue}" == "0" ] && lastvalue="${crntvalue}"
git --git-dir="${gitrepodir}/.git" \
log ${lastvalue}...${crntvalue} \
--date="raw" \
--pretty="tformat:%an%x09%ad%x09%ae%x09%cn%x09%cd%x09%ce%x09%s" 2>/dev/null | \
${USEPYTHON} -c "${PY_COMMIT_GIT}" "${output}" "${item}"
else
URL="${GITAPI}/$(getcomponent 1 "${owner_repo_branch}")/$(getcomponent 2 "${owner_repo_branch}")/compare/${lastvalue}...${crntvalue}"
webrequest "${URL}" || return 1
[ "${DEBUG}" == "Y" ] && cat ${WEBWORKFILE} >${BIN}/dbg_commits_$(echo "${owner_repo_branch}"|sed "s#/#_#g")
cat ${WEBWORKFILE} | ${USEPYTHON} -c "${PY_COMMIT_PR}" "${output}" "${item}" commits
fi
return $?
}
getpulldetails()
{
local item="$1" output="$2" owner_repo_branch="$3" lastvalue="$4"
local URL
URL="${GITAPI}/$(getcomponent 1 "${owner_repo_branch}")/$(getcomponent 2 "${owner_repo_branch}")/pulls"
webrequest "${URL}" || return 1
[ "${DEBUG}" == "Y" ] && cat ${WEBWORKFILE} >${BIN}/dbg_pulls_$(echo "${owner_repo_branch}"|sed "s#/#_#g")
cat ${WEBWORKFILE} | ${USEPYTHON} -c "${PY_COMMIT_PR}" "${output}" "${item}" pulls "${lastvalue}"
return $?
}
getcommitsurl()
{
local url="$1" branch=
if [[ ${url} =~ ^git@github.com: ]]; then
url="${url/git@github.com:/}"
echo "https://github.com/$(getcomponent 1 "${url}")/$(getcomponent 2 "${url}" "\.git$")/commits/$(getcomponent 3 "${url}")"
elif [[ ${url} =~ ^git:// ]]; then
if [[ ${url} =~ ^git://github.com ]]; then
url="${url/git:\/\/github.com\//}"
echo "https://github.com/$(getcomponent 1 "${url}")/$(getcomponent 2 "${url}" "\.git$")/commits/$(getcomponent 3 "${url}")"
else
branch="${url##*/}"
url=${url%/*}
if [ -n "${branch}" ]; then
echo "${url/git:/https:}/log/?h=${branch}"
else
echo "${url/git:/https:}/log"
fi
fi
else
echo "https://github.com/$(getcomponent 1 "${url}")/$(getcomponent 2 "${url}")/commits/$(getcomponent 3 "${url}")"
fi
}
getpullsurl()
{
echo "https://github.com/$(getcomponent 1 "$1")/$(getcomponent 2 "$1")/pulls"
}
gettagurl()
{
local url="$1"
if [[ ${url} =~ ^git@github.com: ]]; then
url="${url/git@github.com:/}"
echo "https://github.com/$(getcomponent 1 "${url}")/$(getcomponent 2 "${url}" "\.git$")/tags"
elif [[ ${url} =~ ^git:// ]]; then
if [[ ${url} =~ ^git://github.com ]]; then
url="${url/git:\/\/github.com\//}"
echo "https://github.com/$(getcomponent 1 "${url}")/$(getcomponent 2 "${url}" "\.git$")/tags"
else
url=${url%/*}
echo "${url/git:/https:}"
fi
else
echo "https://github.com/$(getcomponent 1 "${url}")/$(getcomponent 2 "${url}")/tags"
fi
}
get_rate_limit()
{
local limit remaining reset startedwith
local workfile=${GHNOTIFY_WORKDIR}/.startedwith
webrequest "https://api.github.com/rate_limit"
limit="$(cat ${WEBWORKFILE} | ${USEPYTHON} -c "${PY_JSON_DATA}" "rate.limit")"
remaining="$(cat ${WEBWORKFILE} | ${USEPYTHON} -c "${PY_JSON_DATA}" "rate.remaining")"
reset="$(cat ${WEBWORKFILE} | ${USEPYTHON} -c "${PY_JSON_DATA}" "rate.reset")"
if [ -f ${workfile} ]; then
startedwith=$(cat ${workfile})
else
startedwith=${remaining}
echo ${startedwith} >${workfile}
fi
echo "limit=${limit} remaining=${remaining} used=$((startedwith - remaining)) reset=$(date --date=@${reset} "+%Y-%m-%d %H:%M:%S")"
}
webrequest()
{
local url="$1" pages=${2:-N}
local response result=0 curl page=1
# Escape +
url="${url//+/%2B}"
curl="curl --location --silent --show-error --retry 6 ${AUTHENTICATION} --connect-timeout 30"
[ "${DIAGNOSTICS}" == "Y" ] && echo "WEB REQUEST : ${curl} \"${url}\""
rm -f ${WEBWORKFILE} ${WEBWORKFILE}.dmp
if [ ${pages} == N ]; then
response="$(${curl} "${url}" -o ${WEBWORKFILE} -D ${WEBWORKFILE}.dmp 2>&1)" || result=1
else
echo "[" >> ${WEBWORKFILE}
while [ ${result} -eq 0 ]; do
rm -f ${WEBWORKFILE}.tmp ${WEBWORKFILE}.dmp
response="$(${curl} "${url}?page=${page}&per_page=100" -o ${WEBWORKFILE}.tmp -D ${WEBWORKFILE}.dmp 2>&1)" || result=1
if [ ${result} -eq 0 ]; then
[ ${page} -ne 1 ] && echo "," >> ${WEBWORKFILE}
cat ${WEBWORKFILE}.tmp >> ${WEBWORKFILE}
grep -q "^Link: .* rel=\"last\"" ${WEBWORKFILE}.dmp || break
page=$((page + 1))
fi
done
echo "]" >> ${WEBWORKFILE}
fi
touch ${WEBWORKFILE}
if [ "${DIAGNOSTICS}" == "Y" ]; then
echo "RESPONSE: $(cat ${WEBWORKFILE})"
echo "PAGES : ${page}"
echo "RESULT : ${result}"
fi
if [ ${result} -ne 0 ]; then
warn "REQUEST: ${url}"
warn "ERROR : ${response}"
fi
return ${result}
}
htmlsafe()
{
local html="$1"
html="${html//&/&}"
html="${html//</<}"
html="${html//>/>}"
echo "${html}"
}
lock_repo()
{
local lockfile="${GHNOTIFY_LOCKDIR}/$(basename "${1}").lock"
mkdir -p ${GHNOTIFY_LOCKDIR}
touch ${lockfile}
exec 99<${lockfile}
while ! flock --nonblock --exclusive 99; do
sleep 1
done
}
unlock_repo()
{
flock --unlock 99 2>/dev/null
}
clone_refresh_repo ()
{
local item="${1}" repodir="${2}" repourl="${3%/*}" repobranch="${3##*/}"
# Lock this repo to prevent concurrent access (ie. two different branches of same repo)
lock_repo "${repodir}"
(
# Discard output if not logging
[ "${DEBUG}" == "Y" -o "${DIAGNOSTICS}" == "Y" ] || exec 1>/dev/null
if [ -d ${repodir} ]; then
cd ${repodir}
git checkout ${repobranch} 2>&1 && git pull 2>&1 && exit 0
fi
cd ${GHNOTIFY_GITDIR}
rm -fr ${repodir}
git clone ${repourl} ${repodir} 2>&1
)
return ${PIPESTATUS[0]}
}
HTML_MAIN="$(cat <<EOF
<html lang="en-US" dir="LTR">
<head>
<meta charset="UTF-8" />
<title>GitHub Updates</title>
</head>
<body dir="LTR" text="#141414" bgcolor="#f0f0f0" link="#176093" alink="#176093" vlink="#176093" style="padding: 10px">
<table cellpadding="0" cellspacing="0" border="0" dir="LTR" style="
background-color: #f0f7fc;
border: 1px solid #a5cae4;
border-radius: 5px;
direction: LTR;">
<tr>
<td style="
background-color: #d7edfc;
padding: 5px 10px;
border-bottom: 1px solid #a5cae4;
border-top-left-radius: 4px;
border-top-right-radius: 4px;
font-family: 'Trebuchet MS', Helvetica, Arial, sans-serif;
font-size: 11px;
line-height: 1.231;">
<div style="color: #176093; text-decoration:none">GitHub Updates: @@REPO.SUMMARY@@</div>
</td>
</tr>
@@BODY.DETAIL@@
<tr>
<td style="
background-color: #f0f7fc;
padding: 5px 10px;
border-top: 1px solid #d7edfc;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px">
<table style="font-family: 'Trebuchet MS', Helvetica, Arial, sans-serif;
font-size: 9px;
color: #176093;
text-decoration:none;
line-height: 1.231;
width:100%">
<tr>
<td>@@SCRIPT.STATUS@@</td>
<td align="right" style="text-align:right;vertical-align:bottom">@@SCRIPT.VERSION@@</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
EOF
)"
HTML_SUB="$(cat <<EOF
<tr>
<td style="
background-color: #fcfcff;
color: #141414;
font-family: 'Trebuchet MS', Helvetica, Arial, sans-serif;
font-size: 13px;
line-height: 1.231;">
<h2 style="font-size: 15pt; font-weight: normal; margin: 10px 10px 0 10px">@@ITEM.TYPE@@: <a href="@@ITEM.URL@@" style="color: #176093; text-decoration: none">@@ITEM.SUBJECT@@</a></h2>
<hr style="height: 1px; margin: 10px 0; border: 0; color: #d7edfc; background-color: #d7edfc" />
<table cellpadding="0" cellspacing="0" border="0" width="100%" style="margin: 10px 0 10px">
<tr valign="top">
<td width="100%">
<table cellpadding="0" cellspacing="0" border="0" width="100%" style="font-size: 9pt; line-height: 1.4">
@@ITEM.ROWS@@
</table>
</td>
</tr>
</table>
</td>
</tr>
EOF
)"
GITAPI="https://api.github.com/repos"
FIELDSEP=$'\001'
NEWLINE=$'\012'
DEBUG=N
DIAGNOSTICS=N
NOEMAIL=N
COMMITS=
PULLREQ=
RELEASES=
FILTER=,
SHOWLIST=N
for arg in $@; do
case "${arg}" in
debug) NOEMAIL=Y; export DEBUG=Y;;
diags) DIAGNOSTICS=Y;;
noemail) NOEMAIL=Y;;
commits) COMMITS=Y;;
pulls) PULLREQ=Y;;
releases|tags) RELEASES=Y;;
item=*|items=*|filter=*|filters=*)
FILTER=${FILTER}${arg#*=},;;
list) SHOWLIST=Y;;
noclean) trap 'rm -rf -- "${PIDFILE}"' EXIT;;
esac
done
if [ -z "${COMMITS}${PULLREQ}${RELEASES}" ]; then
COMMITS=Y
PULLREQ=Y
RELEASES=N
fi
COMMITS=${COMMITS:-N}
PULLREQ=${PULLREQ:-N}
RELEASES=${RELEASES:-N}
# Try and find a usable mail transfer agent (MTA).
#
# A list of possible MTA clients is tested for, and when multiple MTA clients are
# present the last one will be used, so order them in ascending (left-to-right)
# order of priority.
#
# msmtp_safe is a personalised wrapper for msmpt that will retry transmission
# up to 10 times in the event of msmpt timing out.
#
for emailclient in sendmail ssmtp msmtp msmtp_safe; do
command -v ${emailclient} 2>&1 >/dev/null && BIN_MTA="$(command -v ${emailclient})"
done
# Use a default email address if available
EMAILTO="$(grep MAILTO /etc/crontab 2>/dev/null | awk -F= '{print $2}')"
# Fixup config and data paths
# Legacy config file
[ -f ~/${GHNOTIFY_DATA} ] && mv ~/${GHNOTIFY_DATA} ~/${GHNOTIFY_CDATA}
[ -f ${BIN}/${GHNOTIFY_DATA} ] && mv ${BIN}/${GHNOTIFY_DATA} ${BIN}/${GHNOTIFY_CDATA}
# Absolute paths
[ -f ${BIN}/${GHNOTIFY_CONF} ] && GHNOTIFY_CONF="${BIN}/${GHNOTIFY_CONF}" || GHNOTIFY_CONF=~/${GHNOTIFY_CONF}
GHNOTIFY_CDATA=$(dirname "${GHNOTIFY_CONF}")/${GHNOTIFY_CDATA}
GHNOTIFY_PDATA=$(dirname "${GHNOTIFY_CONF}")/${GHNOTIFY_PDATA}
GHNOTIFY_RDATA=$(dirname "${GHNOTIFY_CONF}")/${GHNOTIFY_RDATA}
# If an absolute path then use it if it exists
if ! [ "${GHNOTIFY_GITDIR:0:1}" == "/" -a -d "${GHNOTIFY_GITDIR}" ]; then
[ -d ${BIN}/${GHNOTIFY_GITDIR} ] && GHNOTIFY_GITDIR="${BIN}/${GHNOTIFY_GITDIR}" || GHNOTIFY_GITDIR=~/${GHNOTIFY_GITDIR}
mkdir -p ${GHNOTIFY_GITDIR}
fi
# Optionally load GIT authentication and override settings, eg. EMAILTO, GHNOTIFY_?DATA etc.
[ -f ~/.git.conf ] && source ~/.git.conf
[ -n "${GIT_USERNAME}" -a -n "${GIT_PASSWORD}" ] && AUTHENTICATION="-u ${GIT_USERNAME}:${GIT_PASSWORD}"
[ ! -f ${GHNOTIFY_CONF} ] && die 1 "Cannot find configuration file [${GHNOTIFY_CONF}]"
[ ! -x ${BIN_MTA} ] && die 1 "Email client not found [${BIN_MTA}]"
[ -d ${GHNOTIFY_GITDIR} ] || die 1 "Unable to locate git directory ${GHNOTIFY_GITDIR}"
#Stop reporting new commits if there has been no build activity for longer than the specified period
if [ -f ${CHECK_FILE} -a ${CHECK_INTERVAL_DAYS} -ne 0 ]; then
DELTA=$(($(date +%s) - $(stat -c%Y ${CHECK_FILE})))
[ ${DELTA} -ge $((${CHECK_INTERVAL_DAYS} * 24 * 60 * 60)) ] && die 0 "Exceeded check interval ${CHECK_INTERVAL_DAYS} days"
fi
if [ "${DEBUG}" == "Y" ]; then
echo "Commits : ${COMMITS}" | logger init
echo "Pull Requests : ${PULLREQ}" | logger init
echo "Releases : ${RELEASES}" | logger init
echo "Using Python : ${USEPYTHON}" | logger init
echo "Config File : ${GHNOTIFY_CONF}" | logger init
echo "Commit DB : ${GHNOTIFY_CDATA}" | logger init
echo "Pull Req DB : ${GHNOTIFY_PDATA}" | logger init
echo "Release DB : ${GHNOTIFY_RDATA}" | logger init
echo "Git Directory : ${GHNOTIFY_GITDIR}" | logger init
echo "Temp Directory: ${GHNOTIFY_WORKDIR}" | logger init
echo "MTA Path : ${BIN_MTA}" | logger init
echo "Github Limits : $(get_rate_limit)" | logger init
fi
if [ "${COMMITS}" == "Y" ]; then
[ ! -f ${GHNOTIFY_CDATA} ] && touch ${GHNOTIFY_CDATA}
cp ${GHNOTIFY_CDATA} ${GHNOTIFY_CTEMP}
fi
if [ "${PULLREQ}" == "Y" ]; then
[ ! -f ${GHNOTIFY_PDATA} ] && touch ${GHNOTIFY_PDATA}
cp ${GHNOTIFY_PDATA} ${GHNOTIFY_PTEMP}
fi
if [ "${RELEASES}" == "Y" ]; then
[ ! -f ${GHNOTIFY_RDATA} ] && touch ${GHNOTIFY_RDATA}
cp ${GHNOTIFY_RDATA} ${GHNOTIFY_RTEMP}
fi
HISTORY_OWNER_REPO=()
findinlist()
{
local item="${1}" key
for key in "${!HISTORY_OWNER_REPO[@]}"; do
[ "${HISTORY_OWNER_REPO[$key]}" == "${item}" ] && return 0
done
return 1
}
getworkqueue()
{
local owner_repo_branch owner_repo name safe_name isduplicate
local plast pstatus clast cstatus rlast rstatus
local item=0 izeros
while read -r owner_repo_branch name; do
[ -z ${owner_repo_branch} ] && continue
item=$((item+1))
izeros="$(printf "%04d" ${item})"
# Ignore any item not in FILTER
[ "${FILTER}" != "," -a "${FILTER/,${item},/}" == "${FILTER}" -a "${FILTER/,${izeros},/}" == "${FILTER}" ] && continue
safe_name="$(htmlsafe "${name}")"
owner_repo="${owner_repo_branch%/*}"
if findinlist "${owner_repo}"; then
isduplicate=Y
else
isduplicate=N
HISTORY_OWNER_REPO+=("${owner_repo}")
fi
if [ "${PULLREQ}" == "Y" -a "${isduplicate}" == "N" ] && ! [[ ${owner_repo_branch} =~ ^git[@:] ]]; then
pstatus=Y
plast="$(grep "^${owner_repo_branch} " ${GHNOTIFY_PTEMP} | tail -1 | awk '{ print $2 }')"
[ -z "${plast}" ] && echo "${owner_repo_branch} 0" >> ${GHNOTIFY_PTEMP}
else
plast=
pstatus=N
fi
if [ "${COMMITS}" == "Y" ]; then
cstatus=Y
clast="$(grep "^${owner_repo_branch} " ${GHNOTIFY_CTEMP} | tail -1 | awk '{ print $2 }')"
[ -z "${clast}" ] && echo "${owner_repo_branch} 0" >> ${GHNOTIFY_CTEMP}
[ "${clast}" = "0" ] && clast=
else
clast=
cstatus=N
fi
if [ "${RELEASES}" == "Y" -a "${isduplicate}" == "N" ]; then
rstatus=Y
rlast="$(grep "^${owner_repo_branch} " ${GHNOTIFY_RTEMP} | tail -1 | cut -d' ' -f2-)"
[ -z "${rlast}" ] && echo "${owner_repo_branch} []" >> ${GHNOTIFY_RTEMP}
else
rlast=
rstatus=N
fi
printf "%s|%s|%s|%s|%s|%s|%s|%s|%s|\n" "${izeros}" "${plast:-unknown}" "${clast:-unknown}" "${rlast:-unknown}" "${pstatus}" "${cstatus}" "${rstatus}" "${owner_repo_branch}" "${safe_name}"
#[ ${item} -ge 1 ] && break
done <<< "$(grep -v "^#" ${GHNOTIFY_CONF})"
}
process_work_items()
{
local qitem qplast qclast qrlast qpstatus qcstatus qrstatus qownerrepobranch qname
local jobn=0
while IFS="|" read -r qitem qplast qclast qrlast qpstatus qcstatus qrstatus qownerrepobranch qname; do
jobn=$((jobn + 1))
echo "Processing ${qname}" | logger ${qitem}
if [ ${SHOWLIST} != Y ]; then
if [ "${DEBUG}" == "Y" -o "${DIAGNOSTICS}" == "Y" ]; then
( process_work_item "${qitem}" "${qownerrepobranch}" "${qname}" "${qplast}" "${qclast}" "${qrlast}" "${qpstatus}" "${qcstatus}" "${qrstatus}" | logger "${qitem}" ) &
else
process_work_item "${qitem}" "${qownerrepobranch}" "${qname}" "${qplast}" "${qclast}" "${qrlast}" "${qpstatus}" "${qcstatus}" "${qrstatus}" &
fi
if [ ${jobn} -ge ${MAXJOBS} ]; then
wait
jobn=0
fi
fi
done <<< "$(getworkqueue)"
wait
if [ $(ls -1 ${GHNOTIFY_WORKDIR}/*.err 2>/dev/null | wc -l) -ne 0 ]; then
cat ${GHNOTIFY_WORKDIR}/*.err >&2
die 1 "An error has occurred"
fi
}
process_work_item()
{
local item="$1" owner_repo_branch="$2" name="$3" plast="$4" clast="$5" rlast="$6" pstatus="$7" cstatus="$8" rstatus="$9"
local workfile=${GHNOTIFY_WORKDIR}/${item}
local githash gitrepodir
WEBWORKFILE=${workfile}.webrequest
local pulls=${workfile}.pull comms=${workfile}.commit rels=${workfile}.release
local pdata=${pulls}.dat perror=${pulls}.err punavailable=${pulls}.unavailable pupdate=${pulls}.update pitem=${pulls}.item
local cdata=${comms}.dat cerror=${comms}.err cunavailable=${comms}.unavailable cupdate=${comms}.update citem=${comms}.item
local rdata=${rels}.dat rerror=${rels}.err runavailable=${rels}.unavailable rupdate=${rels}.update ritem=${rels}.item
local pcrnt pactual pnodata pneedupdate=N