Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: trigger behat tests ci build #199

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions mdk/ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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"""
Expand Down
44 changes: 44 additions & 0 deletions mdk/commands/behat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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'],
{
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions mdk/config-dist.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand Down