From a2ad3587cd53db11d26734d8e2962c968db525c9 Mon Sep 17 00:00:00 2001 From: Dongsheng Cai Date: Sat, 12 Jun 2021 21:23:08 +1000 Subject: [PATCH] feature: trigger behat tests ci build --- mdk/ci.py | 19 +++++++++++++++++-- mdk/commands/behat.py | 44 +++++++++++++++++++++++++++++++++++++++++++ mdk/config-dist.json | 7 +++++++ 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/mdk/ci.py b/mdk/ci.py index 85cf6eae..c5cf1ba0 100644 --- a/mdk/ci.py +++ b/mdk/ci.py @@ -43,9 +43,10 @@ class CI(object): url = None token = None - def __init__(self, url=None, token=None, load=True): + def __init__(self, url=None, username=None, token=None, load=True): self.url = url or C.get('ci.url') self.token = token or C.get('ci.token') + self.username = username or C.get('ci.username') if load: self.load() @@ -64,7 +65,21 @@ def load(self): logger.setLevel(logging.WARNING) # Loads the jenkins object. - self._jenkins = jenkins.Jenkins(self.url, requester=CrumbRequester(baseurl=self.url)) + requester = CrumbRequester(baseurl=self.url, username=self.username, password=self.token) + self._jenkins = jenkins.Jenkins(self.url, username=self.username, password=self.token, requester=requester) + + def runBuild(self, jobname, params, block=False, delay=5): + try: + job = self.jenkins.get_job(jobname) + queueitem = job.invoke(build_params=params, delay=delay, block=block) + except JenkinsAPIException: + raise CIException('Failed to invoke the build, check your permissions.') + + queueitem.block_until_building() + buildnumber = queueitem.get_build_number() + build = queueitem.get_build() + + return (buildnumber, build.get_build_url()) def precheckRemoteBranch(self, remote, branch, integrateto, issue=None): """Runs the precheck job and returns the outcome""" diff --git a/mdk/commands/behat.py b/mdk/commands/behat.py index f8a87a83..8355ff68 100644 --- a/mdk/commands/behat.py +++ b/mdk/commands/behat.py @@ -31,6 +31,7 @@ from time import sleep from ..command import Command from ..tools import process, ProcessInThread, downloadProcessHook, question, natural_sort_key +from ..ci import CI, CIException class BehatCommand(Command): @@ -43,6 +44,13 @@ class BehatCommand(Command): 'help': 'run the tests' } ), + ( + ['--ci'], + { + 'action': 'store_true', + 'help': 'run test tests on ci server instead of local' + } + ), ( ['-d', '--disable'], { @@ -140,7 +148,43 @@ class BehatCommand(Command): ] _description = 'Initialise Behat' + def runInCi(self, args): + M = self.Wp.resolve() + currentBranch = M.currentBranch() + username = self.C.get('behatci.username') + token = self.C.get('behatci.token') + ciurl = self.C.get('behatci.url') + if ciurl is None: + raise Exception('behat ci url must be set.') + if username is None or token is None: + raise Exception('Username and token must be set to access behatci.') + + self.ci = CI( + url=ciurl, + username=username, + token=token, + ) + jobname = self.C.get('behatci.jobname') + gitrepo = self.C.get('repositoryUrl') + print('{0:<20}: {1}'.format('Jenkin job name', jobname)) + print('{0:<20}: {1}'.format('Git repository', gitrepo)) + print('{0:<20}: {1}'.format('Git branch', currentBranch)) + print('Request to run behat tests on %s...' % ciurl) + params = { + 'REPOSITORY': gitrepo, + 'BRANCH': currentBranch, + 'NAME': args.testname, + 'TAGS': args.tags + } + + (buildnumber, buildurl) = self.ci.runBuild(jobname, params) + print('{0:<20}: #{1}'.format('Build number', buildnumber)) + print('{0:<20}: {1}'.format('Build url', buildurl)) + def run(self, args): + if args.ci and args.run: + self.runInCi(args) + return # Loading instance M = self.Wp.resolve(args.name) diff --git a/mdk/config-dist.json b/mdk/config-dist.json index 199c9245..19439302 100644 --- a/mdk/config-dist.json +++ b/mdk/config-dist.json @@ -96,6 +96,13 @@ "token": null }, + "behatci": { + "jobname": "DEV.01 - Developer-requested Behat", + "url": "https://ci.moodle.org", + "username": null, + "token": null + }, + // The information for integrating MDK with Jira "tracker": { "url": "https://tracker.moodle.org/",