1
0
Fork 0
mirror of https://gitlab.wikimedia.org/ladsgroup/Phabricator-maintenance-bot synced 2024-12-26 12:30:55 +01:00

gerrit.py changes

Avoid using ssh and the mess around it
This commit is contained in:
Amir Sarabadani 2022-08-13 16:06:30 +02:00
parent 731184f2be
commit 260ec45d9d

View file

@ -21,20 +21,12 @@ import tempfile
import urllib
from contextlib import contextmanager
import requests
dir_path = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(dir_path, 'gerrit-creds.json'), 'r') as f:
creds = json.loads(f.read())
def load_ssh_key():
mixin = ShellMixin()
dir_path = os.path.dirname(os.path.realpath(__file__))
mixin.check_call(
['ssh-add', os.path.join(dir_path, 'private_key')])
@contextmanager
def cd(dirname):
cwd = os.getcwd()
@ -45,9 +37,12 @@ def cd(dirname):
os.chdir(cwd)
def gerrit_url(repo: str, user=None, ssh=False) -> str:
def gerrit_url(repo: str, user=None, ssh=False, password=None) -> str:
if user is not None:
prefix = user + '@'
prefix = user
if password is not None:
prefix += ':' + password
prefix += '@'
else:
prefix = ''
if ssh:
@ -60,7 +55,8 @@ class ShellMixin:
def check_call(self, args: list, stdin='', env=None,
ignore_returncode=False) -> str:
debug = self.log if hasattr(self, 'log') else print
#debug('$ ' + ' '.join(args))
#debug('$ ' + ' '.join(args) + ' env: ' + str(env))
env = None
res = subprocess.run(
args,
input=stdin.encode(),
@ -68,7 +64,7 @@ class ShellMixin:
stderr=subprocess.STDOUT,
env=env
)
# debug(res.stdout.decode())
debug(res.stdout.decode())
if not ignore_returncode:
res.check_returncode()
return res.stdout.decode()
@ -80,13 +76,15 @@ class ShellMixin:
self.check_call(['git', 'config', 'user.name', creds['name']])
self.check_call(['git', 'config', 'user.email', creds['email']])
self.check_call(['git', 'submodule', 'update', '--init'])
load_ssh_key()
self.check_call(['scp', '-p', '-P', '29418', creds['name'] +
'@gerrit.wikimedia.org:hooks/commit-msg', '.git/hooks/'])
self.check_call(['mkdir', '-p', '.git/hooks'])
self.check_call(['curl', '-Lo', '.git/hooks/commit-msg', 'https://gerrit.wikimedia.org/r/tools/hooks/commit-msg'])
self.check_call(['chmod', '+x', '.git/hooks/commit-msg'])
def build_push_command(self, options: dict) -> list:
per = '%topic=new-wikis-patches'
for hashtag in options['hashtags']:
def build_push_command(self, options: dict, ticket='') -> list:
per = '%topic=lsc'
if ticket:
per += '-' + ticket
for hashtag in options.get('hashtags', []):
per += ',t=' + hashtag
if options.get('vote'):
per += ',l=' + options['vote']
@ -95,7 +93,7 @@ class ShellMixin:
per += ',m=' + urllib.parse.quote_plus(options['message'])
branch = options.get('branch', 'master')
return ['git', 'push',
gerrit_url(options['repo'], creds['name'], ssh=True),
gerrit_url(options['repo'], creds['name'], password=creds['password']),
'HEAD:refs/for/' + branch + per]
@ -128,11 +126,13 @@ class GerritBot(ShellMixin):
with open('.git/COMMIT_EDITMSG', 'w') as f:
f.write(self.commit_message)
self.check_call(['git', 'commit', '-F', '.git/COMMIT_EDITMSG'])
self.check_call(self.build_push_command(
{'hashtags': ['automated-wiki-creation'], 'repo': self.name}))
self.check_call(
self.build_push_command({'repo': self.name})
)
if __name__ == "__main__":
gerritbot = GerritBot('mediawiki/extensions/WikimediaMessages',
"Order entries by alphabetical order\n\nThis would make creating automated patches easier")
gerritbot = GerritBot(
'mediawiki/extensions/WikimediaMessages',
"Order entries by alphabetical order\n\nThis would make creating automated patches easier")
gerritbot.run()