batcave: port the fedmsg-announce-commits script/hook to fedora-messaging
Signed-off-by: Pierre-Yves Chibon <pingou@pingoured.fr> Signed-off-by: Aurélien Bompard <aurelien@bompard.org>
This commit is contained in:
parent
394aacec19
commit
45ffb17efe
1 changed files with 57 additions and 34 deletions
|
@ -8,21 +8,26 @@ import sys
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
import pygit2
|
import pygit2
|
||||||
import six
|
|
||||||
|
|
||||||
import fedmsg
|
import fedora_messaging.config
|
||||||
import fedmsg.config
|
import fedora_messaging.api
|
||||||
|
import fedora_messaging.exceptions
|
||||||
|
|
||||||
|
fedora_messaging.config.conf.load_config(
|
||||||
|
'/etc/fedora-messaging/git-hooks-messaging.toml')
|
||||||
|
|
||||||
|
|
||||||
|
# Use $GIT_DIR to determine where this repo is.
|
||||||
|
abspath = os.path.abspath(os.environ['GIT_DIR'])
|
||||||
|
|
||||||
|
# This assumes git root dir is named "repo_name.git"
|
||||||
|
repo_name = os.path.splitext(os.path.basename(abspath))[0]
|
||||||
|
|
||||||
username = getpass.getuser()
|
username = getpass.getuser()
|
||||||
|
|
||||||
repo = pygit2.Repository(os.getcwd())
|
repo = pygit2.Repository(abspath)
|
||||||
repo_name = '.'.join(os.getcwd().split(os.path.sep)[-1].split('.')[:-1])
|
|
||||||
|
|
||||||
print("Emitting a message to the fedmsg bus.")
|
print("Emitting a message to the fedora-messaging message bus.")
|
||||||
config = fedmsg.config.load_config([], None)
|
|
||||||
config['active'] = True
|
|
||||||
config['endpoints']['relay_inbound'] = config['relay_inbound']
|
|
||||||
fedmsg.init(name='relay_inbound', cert_prefix='scm', **config)
|
|
||||||
|
|
||||||
|
|
||||||
def revs_between(head, base):
|
def revs_between(head, base):
|
||||||
|
@ -30,7 +35,7 @@ def revs_between(head, base):
|
||||||
|
|
||||||
# pygit2 can't do a rev-list yet, so we have to shell out.. silly.
|
# pygit2 can't do a rev-list yet, so we have to shell out.. silly.
|
||||||
cmd = '/usr/bin/git rev-list %s...%s' % (head.id, base.id)
|
cmd = '/usr/bin/git rev-list %s...%s' % (head.id, base.id)
|
||||||
proc = sp.Popen(cmd.split(), stdout=sp.PIPE, stderr=sp.PIPE)
|
proc = sp.Popen(cmd.split(), stdout=sp.PIPE, stderr=sp.PIPE, cwd=abspath)
|
||||||
stdout, stderr = proc.communicate()
|
stdout, stderr = proc.communicate()
|
||||||
if proc.returncode != 0:
|
if proc.returncode != 0:
|
||||||
raise IOError('git rev-list failed: %r, err: %r' % (stdout, stderr))
|
raise IOError('git rev-list failed: %r, err: %r' % (stdout, stderr))
|
||||||
|
@ -49,21 +54,20 @@ def build_stats(commit):
|
||||||
|
|
||||||
for diff in diffs:
|
for diff in diffs:
|
||||||
for patch in diff:
|
for patch in diff:
|
||||||
if hasattr(patch, 'new_file_path'):
|
if hasattr(patch, 'new_file_path'):
|
||||||
path = patch.new_file_path
|
path = patch.new_file_path
|
||||||
else:
|
else:
|
||||||
path = patch.delta.new_file.path
|
path = patch.delta.new_file.path
|
||||||
|
|
||||||
if hasattr(patch, 'additions'):
|
|
||||||
files[path]['additions'] += patch.additions
|
|
||||||
files[path]['deletions'] += patch.deletions
|
|
||||||
files[path]['lines'] += patch.additions + patch.deletions
|
|
||||||
else:
|
|
||||||
files[path]['additions'] += patch.line_stats[1]
|
|
||||||
files[path]['deletions'] += patch.line_stats[2]
|
|
||||||
files[path]['lines'] += patch.line_stats[1] \
|
|
||||||
+ patch.line_stats[2]
|
|
||||||
|
|
||||||
|
if hasattr(patch, 'additions'):
|
||||||
|
files[path]['additions'] += patch.additions
|
||||||
|
files[path]['deletions'] += patch.deletions
|
||||||
|
files[path]['lines'] += patch.additions + patch.deletions
|
||||||
|
else:
|
||||||
|
files[path]['additions'] += patch.line_stats[1]
|
||||||
|
files[path]['deletions'] += patch.line_stats[2]
|
||||||
|
files[path]['lines'] += patch.line_stats[1] \
|
||||||
|
+ patch.line_stats[2]
|
||||||
|
|
||||||
total = defaultdict(int)
|
total = defaultdict(int)
|
||||||
for name, stats in files.items():
|
for name, stats in files.items():
|
||||||
|
@ -77,6 +81,14 @@ def build_stats(commit):
|
||||||
|
|
||||||
seen = []
|
seen = []
|
||||||
|
|
||||||
|
|
||||||
|
def getlogin():
|
||||||
|
try:
|
||||||
|
return os.getlogin()
|
||||||
|
except Exception:
|
||||||
|
return os.environ['USER']
|
||||||
|
|
||||||
|
|
||||||
# Read in all the rev information git-receive-pack hands us.
|
# Read in all the rev information git-receive-pack hands us.
|
||||||
lines = [line.split() for line in sys.stdin.readlines()]
|
lines = [line.split() for line in sys.stdin.readlines()]
|
||||||
for line in lines:
|
for line in lines:
|
||||||
|
@ -97,7 +109,7 @@ for line in lines:
|
||||||
revs = [head.id]
|
revs = [head.id]
|
||||||
|
|
||||||
def _build_commit(rev):
|
def _build_commit(rev):
|
||||||
commit = repo.revparse_single(six.text_type(rev))
|
commit = repo.revparse_single(str(rev))
|
||||||
|
|
||||||
# Tags are a little funny, and vary between versions of pygit2, so we'll
|
# Tags are a little funny, and vary between versions of pygit2, so we'll
|
||||||
# just ignore them as far as fedmsg is concerned.
|
# just ignore them as far as fedmsg is concerned.
|
||||||
|
@ -116,11 +128,11 @@ for line in lines:
|
||||||
files=files,
|
files=files,
|
||||||
total=total,
|
total=total,
|
||||||
),
|
),
|
||||||
rev=six.text_type(rev),
|
rev=str(rev),
|
||||||
path=os.getcwd(),
|
path=abspath,
|
||||||
repo=repo_name,
|
repo=repo_name,
|
||||||
branch=branch,
|
branch=branch,
|
||||||
agent=os.getlogin(),
|
agent=getlogin(),
|
||||||
)
|
)
|
||||||
|
|
||||||
commits = map(_build_commit, revs)
|
commits = map(_build_commit, revs)
|
||||||
|
@ -141,8 +153,19 @@ for line in lines:
|
||||||
commit['seen'] = False
|
commit['seen'] = False
|
||||||
seen.append(commit['rev'])
|
seen.append(commit['rev'])
|
||||||
|
|
||||||
fedmsg.publish(
|
try:
|
||||||
topic="receive",
|
msg = fedora_messaging.api.Message(
|
||||||
msg=dict(commit=commit),
|
topic="git.receive",
|
||||||
modname="infragit",
|
body=dict(commit=commit)
|
||||||
)
|
)
|
||||||
|
fedora_messaging.api.publish(msg)
|
||||||
|
except fedora_messaging.exceptions.PublishReturned as exp:
|
||||||
|
print(
|
||||||
|
f"Fedora Messaging broker rejected message {msg.id}: {exp}"
|
||||||
|
)
|
||||||
|
except fedora_messaging.exceptions.ConnectionException as exp:
|
||||||
|
print(f"Error sending message {msg.id}: {exp}")
|
||||||
|
except Exception as exp:
|
||||||
|
print("Error sending fedora-messaging message")
|
||||||
|
print(exp)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue