Skip to content

Pipeline Job Get SCM Element Bug Fix #892

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

Open
wants to merge 8 commits 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
26 changes: 19 additions & 7 deletions jenkinsapi/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,14 +531,26 @@ def load_config(self):
def get_scm_type(self):
element_tree = self._get_config_element_tree()
scm_element = element_tree.find("scm")
multibranch_scm_prefix = "properties/org.jenkinsci.plugins.\
workflow.multibranch.BranchJobProperty/branch/"
pipeline_scm_prefix = "definition/"
alt_scm_elems = [
{
"prefix": multibranch_scm_prefix, # multibranch pipeline.
"path": multibranch_scm_prefix + "scm",
},
{
"prefix": pipeline_scm_prefix, # standard pipeline.
"path": pipeline_scm_prefix + "scm",
},
]
if not scm_element:
multibranch_scm_prefix = "properties/org.jenkinsci.plugins.\
workflow.multibranch.BranchJobProperty/branch/"
multibranch_path = multibranch_scm_prefix + "scm"
scm_element = element_tree.find(multibranch_path)
if scm_element:
# multibranch pipeline.
self._scm_prefix = multibranch_scm_prefix
for scm_elm_conf in alt_scm_elems:
scm_element = element_tree.find(scm_elm_conf["path"])
if scm_element:
# SCM Element found.
self._scm_prefix = scm_elm_conf["prefix"]
break
scm_class = scm_element.get("class") if scm_element else None
scm = self._scm_map.get(scm_class)
if not scm:
Expand Down
24 changes: 24 additions & 0 deletions jenkinsapi_tests/systests/job_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,27 @@
</buildWrappers>
</project>
""".strip()

PIPELINE_SCM_CONF_TEST_PARAMS = {
"scm_class": "hudson.plugins.git.GitSCM",
"git_url": "https://example.com/sairk/pipeline-test.git",
}

PIPELINE_SCM_JOB = f"""<?xml version='1.1' encoding='UTF-8'?>
<flow-definition>
<definition class="org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition">
<scm class="{PIPELINE_SCM_CONF_TEST_PARAMS['scm_class']}">
<configVersion>2</configVersion>
<userRemoteConfigs>
<hudson.plugins.git.UserRemoteConfig>
<url>{PIPELINE_SCM_CONF_TEST_PARAMS['git_url']}</url>
</hudson.plugins.git.UserRemoteConfig>
</userRemoteConfigs>
<branches>
<hudson.plugins.git.BranchSpec>
<name>main</name>
</hudson.plugins.git.BranchSpec>
</branches>
</scm>
</definition>
</flow-definition>"""
18 changes: 18 additions & 0 deletions jenkinsapi_tests/systests/test_job_pipeline_scm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from jenkinsapi_tests.systests.job_configs import (
PIPELINE_SCM_CONF_TEST_PARAMS,
PIPELINE_SCM_JOB,
)
from jenkinsapi_tests.test_utils.random_strings import random_string


def test_pipeline_scm(jenkins):
"""
Can we extract scm info from a pipeline scm job?
"""
job_name = random_string()
job = jenkins.create_job(job_name, PIPELINE_SCM_JOB)
assert (
job.get_scm_type()
== job._scm_map[PIPELINE_SCM_CONF_TEST_PARAMS["scm_class"]]
)
assert job.get_scm_url()[0] == PIPELINE_SCM_CONF_TEST_PARAMS["git_url"]
Loading