Skip to content

Commit

Permalink
deploy githooks for all cms repos
Browse files Browse the repository at this point in the history
  • Loading branch information
smuzaffar committed Jan 31, 2018
1 parent 5ac9431 commit 32bdc96
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 31 deletions.
32 changes: 8 additions & 24 deletions create-github-hooks
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ from github import Github
from os.path import expanduser , exists, join, dirname, abspath
from os import environ
from optparse import OptionParser
from github_hooks_config import GITHUB_HOOKS as hk_conf
from github_hooks_config import REPO_HOOK_MAP
from github_hooks_config import get_repository_hooks
from github_utils import api_rate_limits
import hashlib, re
from categories import EXTERNAL_REPOS, CMSSW_REPOS, CMSDIST_REPOS
Expand All @@ -25,7 +24,7 @@ def get_secret(hook_name):
def match_config(new,old):
if new["active"] != old.active:
return False
elif new["events"] != old.events:
elif set(new["events"]) != set(old.events):
return False
for key in new["config"]:
if (not key in old.config) or (key!='secret' and new["config"][key] != old.config[key]):
Expand Down Expand Up @@ -88,29 +87,13 @@ if __name__ == "__main__":
gh = Github(login_or_token=open(expanduser(repo_config.GH_TOKEN)).read().strip())
repo_name = repo_config.GH_REPO_FULLNAME
print "Checking for repo ",repo_name
hooks = []
for r in REPO_HOOK_MAP:
if re.match(r[0],repo_name):
if opts.hook and opts.hook in r[1]:
hooks.append(opts.hook)
break
elif not opts.hook:
for h in r[1]: hooks.append(h)
break
hk_conf = get_repository_hooks (repo_name, opts.hook)
hooks = hk_conf.keys()
if not hooks:
print "==>Warning: No hook found for repository",repo_name
error = 1
continue

err = 0
for h in hooks:
if not h in hk_conf:
print "==>Error: No hook name ",h," found for repository ",repo_name
err = 1
if err:
error = 1
continue

print "Found hooks:",hooks
repo = repos[repo_name]
if not repo: repo = gh.get_repo(repo_name)
Expand All @@ -119,6 +102,7 @@ if __name__ == "__main__":
if "name" in hook.config:
repo_hooks_all[ hook.config['name'] ] = hook
api_rate_limits(gh)
print "Dryrun:",opts.dryRun
for hook in hooks:
print "checking for web hook" , hook
hook_conf = hk_conf[hook]
Expand All @@ -133,12 +117,12 @@ if __name__ == "__main__":
if not opts.dryRun:
old_hook.edit(**hook_conf)
api_rate_limits(gh)
print "hook updated"
print "hook updated",hook
else:
print "Hook configuration is same"
print "Hook configuration is same",hook
else:
if not opts.dryRun:
repo.create_hook(**hook_conf)
api_rate_limits(gh)
print "Hook created in github.....success"
print "Hook created in github.....success",hook
exit(error)
20 changes: 13 additions & 7 deletions github_hooks_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@

#First repository name matches wins
REPO_HOOK_MAP = []
REPO_HOOK_MAP.append(["cms-sw/cmssdt-ib", ["Jenkins_Github_Hook_Push"]])
REPO_HOOK_MAP.append(["cms-sw/cmssdt-web", ["Jenkins_Github_Hook_Push"]])
REPO_HOOK_MAP.append(["cms-sw/cms-bot", ["Jenkins_Github_Hook_Push"]])
REPO_HOOK_MAP.append(["cms-sw/cms-docker", ["Jenkins_Github_Hook_Push"]])
REPO_HOOK_MAP.append(["cms-sw/cmssw", ["Jenkins_Github_Hook", "Jenkins_Github_Hook_Push"]])
REPO_HOOK_MAP.append(["cms-sw/cmsdist", ["Jenkins_Github_Hook", "Jenkins_Github_Hook_Push"]])
REPO_HOOK_MAP.append([".+", ["Jenkins_Github_Hook"]])
REPO_HOOK_MAP.append([".+", ["Jenkins_Github_Hook","Jenkins_Github_Hook_Push"]])

def get_repository_hooks(repo_name, hook=None):
import re
hooks = {}
for r in REPO_HOOK_MAP:
if re.match(r[0],repo_name):
if not hook:
for h in r[1]: hooks[h]=GITHUB_HOOKS[h]
elif hook in r[1]:
hooks[hook] = GITHUB_HOOKS[hook]
break
return hooks

0 comments on commit 32bdc96

Please sign in to comment.