Skip to content

Commit

Permalink
fixed triggering flow by webhook, resolves #296
Browse files Browse the repository at this point in the history
  • Loading branch information
godfryd committed Sep 22, 2023
1 parent 84f5aaa commit 912ed5f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
14 changes: 6 additions & 8 deletions server/kraken/server/bg/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ def trigger_run(stage_id, flow_kind=consts.FLOW_KIND_CI, reason=None):
exec_utils.start_run(stage, flow, reason=reason, repo_data=repo_data)


def trigger_flow(project_id, trigger_data=None):
def trigger_flow(project_id, trigger_data):
log.reset_ctx()
app = _create_app('trigger_flow_%d' % project_id)

Expand Down Expand Up @@ -891,11 +891,15 @@ def trigger_flow(project_id, trigger_data=None):
except Exception:
log.warning('cannot parse trigger git url: %s', trigger_git_url)

# find stages that use repo from trigger
# find stages that use the repo that appears in trigger data
# and are root
matching_stages = []
for stage in branch.stages:
if stage.deleted:
continue
if stage.schema['parent'] != 'root':
continue

found = False
for job in stage.schema['jobs']:
for step in job['steps']:
Expand Down Expand Up @@ -931,12 +935,6 @@ def trigger_flow(project_id, trigger_data=None):
if not stage.enabled:
log.info('stage %s not started - disabled', stage)
continue
parent_name = stage.schema['parent']
if parent_name != 'root':
parent_stage = name_stages[parent_name]
if parent_stage.id not in run_stages:
# TODO: we should wait when parent is run and then trigger this stage
continue

if not last_flow.trigger_data:
last_flow.trigger_data = trigger_data
Expand Down
64 changes: 64 additions & 0 deletions server/tests/test_bg_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,3 +669,67 @@ def test_load_remote_tool():
finally:
del os.environ['MINIO_ROOT_USER']
del os.environ['MINIO_ROOT_PASSWORD']


@pytest.mark.db
def test_trigger_flow_1():
app = create_app()

with app.app_context():
initdb._prepare_initial_preferences()

#with patch('kraken.server.bg.jobs.log') as mylog, patch('kraken.server.kkrq.enq'):
# jobs._analyze_results_history(77777777)
# mylog.error.assert_called_with('got unknown run to analyze results history: %s', 77777777)

project = Project()
branch = Branch(project=project, branch_name='master')
stage = Stage(branch=branch,
schema={
'parent': 'root',
'jobs': [{
'steps': [{
'tool': 'git',
'checkout': 'https://github.com/Kraken-CI/kraken.git'
}]
}]
})
db.session.commit()

# bad trigger data
with patch('kraken.server.exec_utils.start_run') as sr, patch('kraken.server.exec_utils.create_a_flow') as caf:
trigger_data = dict(trigger='bad')
jobs.trigger_flow(project.id, trigger_data)

sr.assert_not_called()
caf.assert_not_called()

# trigger the first flow in a branch
with patch('kraken.server.exec_utils.start_run') as sr, patch('kraken.server.exec_utils.create_a_flow') as caf:
trigger_data = dict(trigger='github-push', ref='refs/heads/master')
jobs.trigger_flow(project.id, trigger_data)

sr.assert_not_called()
caf.assert_called()
assert type(caf.call_args.args[0]) == Branch
assert caf.call_args.args[0].id == branch.id
assert caf.call_args.args[1] == 'ci'
assert caf.call_args.args[2] == {}

# prepare prev flow
flow = Flow(branch=branch, kind=0)
db.session.commit()

# trigger the first flow in a branch
with patch('kraken.server.exec_utils.start_run') as sr, patch('kraken.server.exec_utils.create_a_flow') as caf:
trigger_data = dict(trigger='github-push', ref='refs/heads/master',
repo='https://github.com/Kraken-CI/kraken.git')
jobs.trigger_flow(project.id, trigger_data)

caf.assert_not_called()
sr.assert_called()
assert type(sr.call_args.args[0]) == Stage
# assert sr.call_args.args[0].id == stage.id
assert type(sr.call_args.args[1]) == Flow
# assert sr.call_args.args[1].id == flow.id
assert sr.call_args.kwargs['reason'] == {'reason': 'github push'}

0 comments on commit 912ed5f

Please sign in to comment.