2010-03-06 14:05:51 -08:00
|
|
|
|
|
|
|
"""
|
|
|
|
hookscripts - ready to use hook scripts for gitzilla.
|
|
|
|
|
|
|
|
These pick up configuration values from the environment.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
2010-07-02 14:44:54 -07:00
|
|
|
import re
|
2010-03-06 14:05:51 -08:00
|
|
|
import gitzilla.hooks
|
|
|
|
import logging
|
|
|
|
import ConfigParser
|
2010-03-13 13:56:53 -08:00
|
|
|
import bugz
|
2010-03-06 14:05:51 -08:00
|
|
|
|
|
|
|
def get_or_default(conf, section, option, default=None):
|
|
|
|
if conf.has_option(section, option):
|
|
|
|
return conf.get(section, option)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2010-03-13 13:56:53 -08:00
|
|
|
def bz_auth_from_config(config, sRepo):
|
|
|
|
sBZUser = None
|
|
|
|
sBZPasswd = None
|
|
|
|
|
|
|
|
if config.has_section(sRepo):
|
|
|
|
if config.has_option(sRepo, "bugzilla_user") and config.has_option(sRepo, "bugzilla_password"):
|
|
|
|
sBZUser = config.get(sRepo, "bugzilla_user")
|
|
|
|
sBZPasswd = config.get(sRepo, "bugzilla_password")
|
|
|
|
|
|
|
|
return (sBZUser, sBZPasswd)
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-03-06 14:05:51 -08:00
|
|
|
def get_bz_data(siteconfig, userconfig):
|
|
|
|
sRepo = os.getcwd()
|
|
|
|
|
2010-03-13 13:56:53 -08:00
|
|
|
bAllowDefaultAuth = False
|
|
|
|
|
2010-03-06 14:05:51 -08:00
|
|
|
try:
|
|
|
|
sBZUrl = siteconfig.get(sRepo, "bugzilla_url")
|
|
|
|
except:
|
|
|
|
print "missing/incomplete bugzilla conf"
|
|
|
|
sys.exit(1)
|
|
|
|
|
2010-03-13 13:56:53 -08:00
|
|
|
sUserOption = get_or_default(siteconfig, sRepo, "user_config", "allow")
|
|
|
|
sUserOption = {"deny": "deny", "force": "force"}.get(sUserOption, "allow")
|
|
|
|
|
|
|
|
(sBZUser, sBZPasswd) = bz_auth_from_config(userconfig, sRepo)
|
|
|
|
|
|
|
|
# ignore auth from site-config if "force"
|
|
|
|
if sUserOption == "force":
|
|
|
|
bAllowDefaultAuth = False
|
2010-03-06 14:05:51 -08:00
|
|
|
|
2010-03-13 13:56:53 -08:00
|
|
|
# for 'allow', get the auth from user config but allow fallback
|
|
|
|
if sUserOption == "allow":
|
|
|
|
bAllowDefaultAuth = True
|
|
|
|
|
|
|
|
# ignore auth from user config is "deny"
|
|
|
|
if sUserOption == "deny":
|
|
|
|
(sBZUser, sBZPasswd) = bz_auth_from_config(siteconfig, sRepo)
|
|
|
|
if None in (sBZUser, sBZPasswd):
|
|
|
|
raise ValueError("No default Bugzilla auth found. Cannot use user-auth because user_config is set to 'deny'")
|
|
|
|
|
|
|
|
return (sBZUrl, sBZUser, sBZPasswd, bAllowDefaultAuth)
|
2010-03-06 14:05:51 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_logger(siteconfig):
|
|
|
|
sRepo = os.getcwd()
|
|
|
|
logger = None
|
|
|
|
if siteconfig.has_option(sRepo, "logfile"):
|
|
|
|
logger = logging.getLogger("gitzilla")
|
|
|
|
logger.addHandler(logging.FileHandler(siteconfig.get(sRepo, "logfile")))
|
|
|
|
# default to debug, but switch to info if asked.
|
|
|
|
sLogLevel = get_or_default(siteconfig, sRepo, "loglevel", "debug")
|
|
|
|
logger.setLevel({"info": logging.INFO}.get(sLogLevel, logging.DEBUG))
|
|
|
|
|
|
|
|
return logger
|
|
|
|
|
|
|
|
|
|
|
|
def get_bug_regex(siteconfig):
|
|
|
|
sRepo = os.getcwd()
|
|
|
|
oBugRegex = None
|
|
|
|
if siteconfig.has_option(sRepo, "bug_regex"):
|
|
|
|
oBugRegex = re.compile(siteconfig.get(sRepo, "bug_regex"),
|
|
|
|
re.MULTILINE | re.DOTALL | re.IGNORECASE)
|
|
|
|
|
|
|
|
return oBugRegex
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-03-13 13:56:53 -08:00
|
|
|
def make_bz_init(siteconfig, bAllowDefaultAuth):
|
|
|
|
# return a bz_init function which does the right thing.
|
|
|
|
|
|
|
|
def bz_init(sBZUrl, sBZUser, sBZPasswd):
|
|
|
|
# if username/passwd are none, then modify the Bugz instance so that
|
|
|
|
# Bugz.get_input and getpass.getpass get the username and passwd
|
|
|
|
# from the siteconfig.
|
|
|
|
if sBZUrl is None:
|
|
|
|
raise ValueError("No Bugzilla URL specified")
|
|
|
|
|
|
|
|
sSiteUser = sBZUser
|
|
|
|
sSitePasswd = sBZPasswd
|
|
|
|
|
|
|
|
sRepo = os.getcwd()
|
|
|
|
|
|
|
|
if None in (sBZUser, sBZPasswd):
|
|
|
|
if bAllowDefaultAuth:
|
|
|
|
# get data from siteconfig
|
|
|
|
(sSiteUser, sSitePasswd) = bz_auth_from_config(siteconfig, sRepo)
|
|
|
|
|
|
|
|
oBZ = bugz.bugzilla.Bugz(sBZUrl, user=sBZUser, password=sBZPasswd)
|
|
|
|
oBZ.get_input = lambda prompt: sSiteUser
|
|
|
|
import getpass
|
|
|
|
getpass.getpass = lambda: sSitePasswd
|
|
|
|
return oBZ
|
|
|
|
|
|
|
|
return bz_init
|
|
|
|
|
2010-03-06 14:05:51 -08:00
|
|
|
|
|
|
|
def post_receive():
|
|
|
|
"""
|
|
|
|
The gitzilla-post-receive hook script.
|
|
|
|
|
|
|
|
The configuration is picked up from /etc/gitzillarc and ~/.gitzillarc
|
|
|
|
|
|
|
|
The user specific configuration is allowed to override the bugzilla
|
|
|
|
username and password.
|
|
|
|
"""
|
2010-07-02 14:50:16 -07:00
|
|
|
siteconfig = ConfigParser.RawConfigParser()
|
2010-03-06 14:05:51 -08:00
|
|
|
siteconfig.readfp(file("/etc/gitzillarc"))
|
|
|
|
sRepo = os.getcwd()
|
|
|
|
|
|
|
|
if not siteconfig.has_section(sRepo):
|
|
|
|
print "No %s section found in /etc/gitzillarc" % (sRepo,)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2010-07-02 14:50:16 -07:00
|
|
|
userconfig = ConfigParser.RawConfigParser()
|
2010-03-06 14:05:51 -08:00
|
|
|
userconfig.read(os.path.expanduser("~/.gitzillarc"))
|
|
|
|
|
2010-03-13 13:56:53 -08:00
|
|
|
(sBZUrl, sBZUser, sBZPasswd, bAllowDefaultAuth) = get_bz_data(siteconfig, userconfig)
|
2010-03-06 14:05:51 -08:00
|
|
|
|
|
|
|
logger = get_logger(siteconfig)
|
|
|
|
oBugRegex = get_bug_regex(siteconfig)
|
|
|
|
sSeparator = get_or_default(siteconfig, sRepo, "separator")
|
|
|
|
sFormatSpec = get_or_default(siteconfig, sRepo, "formatspec")
|
2010-09-16 01:41:22 -07:00
|
|
|
sChangeLogCommand = get_or_default(siteconfig, sRepo, "changelogcmd")
|
2010-03-06 14:05:51 -08:00
|
|
|
|
2010-03-13 13:56:53 -08:00
|
|
|
bz_init = make_bz_init(siteconfig, bAllowDefaultAuth)
|
|
|
|
|
2010-03-06 14:05:51 -08:00
|
|
|
gitzilla.hooks.post_receive(sBZUrl, sBZUser, sBZPasswd, sFormatSpec,
|
2010-09-16 01:41:22 -07:00
|
|
|
sChangeLogCommand,
|
2010-03-13 13:56:53 -08:00
|
|
|
oBugRegex, sSeparator, logger, bz_init)
|
2010-03-06 14:05:51 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update():
|
|
|
|
"""
|
|
|
|
The gitzilla-update hook script.
|
|
|
|
|
|
|
|
The configuration is picked up from /etc/gitzillarc and ~/.gitzillarc
|
|
|
|
|
|
|
|
The user specific configuration is allowed to override the bugzilla
|
|
|
|
username and password.
|
|
|
|
"""
|
2010-07-02 14:50:16 -07:00
|
|
|
siteconfig = ConfigParser.RawConfigParser()
|
2010-03-06 14:05:51 -08:00
|
|
|
siteconfig.readfp(file("/etc/gitzillarc"))
|
|
|
|
sRepo = os.getcwd()
|
|
|
|
|
|
|
|
if not siteconfig.has_section(sRepo):
|
|
|
|
print "No %s section found in /etc/gitzillarc" % (sRepo,)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
logger = get_logger(siteconfig)
|
|
|
|
oBugRegex = get_bug_regex(siteconfig)
|
|
|
|
sSeparator = get_or_default(siteconfig, sRepo, "separator")
|
|
|
|
sFormatSpec = get_or_default(siteconfig, sRepo, "formatspec")
|
|
|
|
|
|
|
|
asAllowedStatuses = None
|
|
|
|
if siteconfig.has_option(sRepo, "allowed_bug_states"):
|
|
|
|
asAllowedStatuses = map(lambda x: x.strip(),
|
|
|
|
siteconfig.get(sRepo, "allowed_bug_states").split(","))
|
|
|
|
|
2010-03-13 13:56:53 -08:00
|
|
|
# and the bugzilla info.
|
2010-07-02 14:50:16 -07:00
|
|
|
userconfig = ConfigParser.RawConfigParser()
|
2010-03-13 13:56:53 -08:00
|
|
|
userconfig.read(os.path.expanduser("~/.gitzillarc"))
|
|
|
|
(sBZUrl, sBZUser, sBZPasswd, bAllowDefaultAuth) = get_bz_data(siteconfig, userconfig)
|
2010-03-06 14:05:51 -08:00
|
|
|
|
2010-03-13 13:56:53 -08:00
|
|
|
bz_init = make_bz_init(siteconfig, bAllowDefaultAuth)
|
2010-03-06 14:05:51 -08:00
|
|
|
|
|
|
|
gitzilla.hooks.update(oBugRegex, asAllowedStatuses, sSeparator,
|
2010-03-13 13:56:53 -08:00
|
|
|
sBZUrl, sBZUser, sBZPasswd, logger, bz_init)
|
2010-03-06 14:05:51 -08:00
|
|
|
|
|
|
|
|