Replace the name of the "branch" parameter in "scm.get_latest" to "ref"

This commit is contained in:
mprahl
2017-11-11 09:28:48 -05:00
parent d1548a7049
commit 1622494b17
6 changed files with 28 additions and 27 deletions

View File

@@ -178,15 +178,16 @@ class SCM(object):
raise RuntimeError("checkout: Unhandled SCM scheme.")
return self.sourcedir
def get_latest(self, branch='master'):
"""Get the latest commit ID.
def get_latest(self, ref='master'):
""" Get the latest commit hash based on the provided git ref
:returns: str -- the latest commit ID, e.g. the git $BRANCH HEAD
:param ref: a string of a git ref (either a branch or commit hash)
:returns: a string of the latest commit hash
:raises: RuntimeError
"""
if self.scheme == "git":
log.debug("Getting/verifying commit hash for %s" % self.repository)
# check all branches on the remote
# get all the branches on the remote
output = SCM._run(["git", "ls-remote", "--exit-code", self.repository])[1]
# pair branch names and their latest refs into a dict. The output of the above command
# is multiple lines of "bf028e573e7c18533d89c7873a411de92d4d913e refs/heads/master".
@@ -200,14 +201,14 @@ class SCM(object):
cur_ref = branch_and_ref.split("\t")[0]
branches[cur_branch] = cur_ref
# first check if the branch name is in the repo
if branch in branches:
return branches[branch]
if ref in branches:
return branches[ref]
# if the branch is not in the repo it may be a ref.
else:
# if the ref does not exist in the repo, _run will raise and UnprocessableEntity
# error.
SCM._run(["git", "fetch", "--dry-run", self.repository, branch])
return branch
# if the ref does not exist in the repo, _run will raise an UnprocessableEntity
# exception
SCM._run(["git", "fetch", "--dry-run", self.repository, ref])
return ref
else:
raise RuntimeError("get_latest: Unhandled SCM scheme.")

View File

@@ -614,7 +614,7 @@ def _scm_get_latest(pkg):
# to the specific commit available at the time of
# submission (now).
pkgref = module_build_service.scm.SCM(
pkg.repository).get_latest(branch=pkg.ref)
pkg.repository).get_latest(pkg.ref)
except Exception as e:
log.exception(e)
return {

View File

@@ -79,8 +79,8 @@ class FakeSCM(object):
return self.sourcedir
def get_latest(self, branch='master'):
return branch
def get_latest(self, ref='master'):
return ref
def get_module_yaml(self):
return path.join(self.sourcedir, self.name + ".yaml")

View File

@@ -53,7 +53,7 @@ class TestSCMModule(unittest.TestCase):
def test_local_get_latest_is_sane(self):
""" See that a hash is returned by scm.get_latest. """
scm = module_build_service.scm.SCM(repo_path)
latest = scm.get_latest(branch='master')
latest = scm.get_latest('master')
target = '5481faa232d66589e660cc301179867fb00842c9'
assert latest == target, "%r != %r" % (latest, target)
@@ -66,7 +66,7 @@ class TestSCMModule(unittest.TestCase):
assert scm.scheme == 'git', scm.scheme
fname = tempfile.mktemp(suffix='mbs-scm-test')
try:
scm.get_latest(branch='master; touch %s' % fname)
scm.get_latest('master; touch %s' % fname)
except UnprocessableEntity:
assert not os.path.exists(fname), "%r exists! Vulnerable." % fname
@@ -121,22 +121,22 @@ class TestSCMModule(unittest.TestCase):
@raises(UnprocessableEntity)
def test_get_latest_incorect_component_branch(self):
scm = module_build_service.scm.SCM(repo_path)
scm.get_latest(branch='foobar')
scm.get_latest('foobar')
def test_get_latest_component_branch(self):
ref = "5481faa232d66589e660cc301179867fb00842c9"
branch = "master"
scm = module_build_service.scm.SCM(repo_path)
commit = scm.get_latest(branch=branch)
commit = scm.get_latest(branch)
assert commit == ref
def test_get_latest_component_ref(self):
ref = "5481faa232d66589e660cc301179867fb00842c9"
scm = module_build_service.scm.SCM(repo_path)
commit = scm.get_latest(branch=ref)
commit = scm.get_latest(ref)
assert commit == ref
@raises(UnprocessableEntity)
def test_get_latest_incorect_component_ref(self):
scm = module_build_service.scm.SCM(repo_path)
scm.get_latest(branch='15481faa232d66589e660cc301179867fb00842c9')
scm.get_latest('15481faa232d66589e660cc301179867fb00842c9')

View File

@@ -68,8 +68,8 @@ class FakeSCM(object):
return self.sourcedir
def get_latest(self, branch='master'):
return self.commit if self.commit else branch
def get_latest(self, ref='master'):
return self.commit if self.commit else ref
def get_module_yaml(self):
return path.join(self.sourcedir, self.name + ".yaml")
@@ -240,8 +240,8 @@ class TestUtils(unittest.TestCase):
'f25': '76f9d8c8e87eed0aab91034b01d3d5ff6bd5b4cb'}
original_refs = ["f23", "f24", "f25"]
def mocked_get_latest(branch="master"):
return hashes_returned[branch]
def mocked_get_latest(ref="master"):
return hashes_returned[ref]
mocked_scm.return_value.get_latest = mocked_get_latest
mmd = modulemd.ModuleMetadata()
@@ -639,8 +639,8 @@ class TestUtils(unittest.TestCase):
'f25': '4ceea43add2366d8b8c5a622a2fb563b625b9abf',
'f24': 'fbed359411a1baa08d4a88e0d12d426fbf8f602c'}
def mocked_get_latest(branch="master"):
return hashes_returned[branch]
def mocked_get_latest(ref="master"):
return hashes_returned[ref]
mocked_scm.return_value.get_latest = mocked_get_latest

View File

@@ -105,8 +105,8 @@ class FakeSCM(object):
return self.sourcedir
def get_latest(self, branch='master'):
return hashlib.sha1(branch).hexdigest()[:10]
def get_latest(self, ref='master'):
return hashlib.sha1(ref).hexdigest()[:10]
def get_module_yaml(self):
return path.join(self.sourcedir, self.name + ".yaml")