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