@@ -33,17 +33,97 @@ def grafana_9x(data, headers):
33
33
def github (data , headers ):
34
34
"""Pretty-print a github notification."""
35
35
# TODO: Write nice useful formatters. This is only an example.
36
- if headers ["X-GitHub-Event" ] == "push" :
37
- pusher , ref , a , b , c = (
38
- data [k ] for k in ["pusher" , "ref" , "after" , "before" , "compare" ]
36
+ #if headers["X-GitHub-Event"] == "push":
37
+ # pusher, ref, a, b, c = (
38
+ # data[k] for k in ["pusher", "ref", "after", "before", "compare"]
39
+ # )
40
+ # pusher = f"[@{pusher['name']}](https://github.com/{pusher['name']})"
41
+ # data["body"] = f"{pusher} pushed on {ref}: [{b} → {a}]({c}):\n\n"
42
+ # for commit in data["commits"]:
43
+ # data["body"] += f"- [{commit['message']}]({commit['url']})\n"
44
+ #else:
45
+ # data["body"] = "notification from github"
46
+ #data["digest"] = headers["X-Hub-Signature-256"].replace("sha256=", "")
47
+ #return data
48
+ return github_aerynos (data , headers )
49
+
50
+
51
+ def github_aerynos (data , headers ):
52
+ """Custom pretty-printer for GH notifications"""
53
+ repository = data ['repository' ]
54
+ # It doesn't make sense to show private commits in public, so turn that off
55
+ if repository ['private' ] and repository ['visibility' ] == "private" :
56
+ pass
57
+ # GH webhook will get a 400 return code w/missing body
58
+ elif headers ['X-GitHub-Event' ] == "push" :
59
+ pusher , ref , compare , created , deleted , forced = (
60
+ data [k ] for k in ["pusher" , "ref" , "compare" , "created" , "deleted" , "forced" ]
39
61
)
40
- pusher = f"[@{ pusher ['name' ]} ](https://github.com/{ pusher ['name' ]} )"
41
- data ["body" ] = f"{ pusher } pushed on { ref } : [{ b } → { a } ]({ c } ):\n \n "
42
- for commit in data ["commits" ]:
43
- data ["body" ] += f"- [{ commit ['message' ]} ]({ commit ['url' ]} )\n "
62
+ pusher_url = f"[@{ pusher ['name' ]} ](https://github.com/{ pusher ['name' ]} )"
63
+ # Since we use monorepos and use an org-wide webhook, let's add repo info too.
64
+ repo_url = f"[{ repository ['full_name' ]} ]({ repository ['html_url' ]} )"
65
+
66
+ if len (data ['commits' ]) == 0 :
67
+ # these are booleans
68
+ created , deleted , forced = (data [k ] for k in ["created" , "deleted" , "forced" ])
69
+ # `git push --tags` has empty commit field, but mentions refs/tags/<the-tag> in ref
70
+ if "refs/tags/" in ref :
71
+ tag = ref .split ("/" )[- 1 ]
72
+ tag_url = f"{ repository ['html_url' ]} /releases/tag/{ tag } "
73
+ data ['body' ] = f"{ repo_url } : { pusher_url } pushed tag [{ tag } ]({ tag_url } )\n "
74
+ elif created :
75
+ data ['body' ] = f"{ repo_url } : { pusher_url } created empty branch _{ ref } _\n "
76
+ elif deleted :
77
+ data ['body' ] = f"{ repo_url } : { pusher_url } deleted branch <del>{ ref } </del>\n "
78
+ elif forced :
79
+ branch = ref .split ("/" )[- 1 ]
80
+ branch_url = f"{ repository ['html_url' ]} /commits/{ branch } "
81
+ data ['body' ] = f"{ repo_url } : { pusher_url } force pushed on [{ ref } ]({ branch_url } )\n "
82
+ # Yet to be understood scenario
83
+ else :
84
+ pass
85
+ else :
86
+ # The commit shasum hashes are noisy, so just make the ref link to the full compare
87
+ data ['body' ] = f"{ repo_url } : { pusher_url } "
88
+ if forced :
89
+ data ['body' ] += "force "
90
+ data ['body' ] += f"pushed on [{ ref } ]({ compare } ):\n \n "
91
+
92
+ for idx , commit in enumerate (data ['commits' ]):
93
+ # Elide commit list once we go past a reasonable number of commits for readability
94
+ if idx >= 4 :
95
+ data ['body' ] += f"- (... { len (data ['commits' ]) - 4 } more commits ...)"
96
+ break
97
+ # We only really need the shortlog of each relevant commit
98
+ shortlog = commit ['message' ].strip ().split ("\n " )[0 ]
99
+ data ['body' ] += f"- [{ shortlog } ]({ commit ['url' ]} )\n "
100
+ elif headers ['X-GitHub-Event' ] == "pull_request" :
101
+ action , number , pr , sender = (
102
+ data [k ] for k in ["action" , "number" , "pull_request" , "sender" ]
103
+ )
104
+ # avoid PR spam and wasted CPU cycles
105
+ if action in ["opened" , "closed" , "reopened" , "ready for review" , "review requested" ]:
106
+ pr_title = pr ['title' ]
107
+ pr_url = pr ['html_url' ]
108
+ reponame = repository ['full_name' ]
109
+ repo_url = repository ['html_url' ]
110
+ # the user associated with the actual action, not just the PR
111
+ sender_user = sender ['login' ]
112
+ url_query = "pulls/"
113
+
114
+ if action == "closed" :
115
+ url_query = "pulls/?q=is%3Apr+is%3Aclosed"
116
+
117
+ data ['body' ] = f"PR#{ number } [{ pr_title } ]({ pr_url } )\n \n "
118
+ data ['body' ] += f"{ action } by [@{ sender_user } ](https://github.com/{ sender_user } ) "
119
+ data ['body' ] += f"in [{ reponame } ]({ repo_url } /{ url_query } )"
120
+ # endif
121
+ # GH webhook will get a 400 return code w/missing body if the action
122
+ # isn't in the allow-list
44
123
else :
45
- data ["body" ] = "notification from github"
46
- data ["digest" ] = headers ["X-Hub-Signature-256" ].replace ("sha256=" , "" )
124
+ event = headers ['X-GitHub-Event' ]
125
+ data ['body' ] = f"unsupported github event: '{ event } '"
126
+ data ['digest' ] = headers ['X-Hub-Signature-256' ].replace ("sha256=" , "" )
47
127
return data
48
128
49
129
0 commit comments