2017-10-18 07:53:10 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# internal use only
|
|
|
|
# called by CI jobs when it uses a project related to IDF
|
|
|
|
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import argparse
|
|
|
|
import subprocess
|
2019-11-18 23:06:27 -05:00
|
|
|
import re
|
2017-10-18 07:53:10 -04:00
|
|
|
|
|
|
|
|
2019-11-18 23:06:27 -05:00
|
|
|
IDF_GIT_DESCRIBE_PATTERN = re.compile(r"^v(\d)\.(\d)")
|
|
|
|
|
|
|
|
|
|
|
|
def target_branch_candidates(proj_name):
|
|
|
|
"""
|
|
|
|
:return: a list of target branch candidates, from highest priority to lowest priority.
|
|
|
|
"""
|
|
|
|
candidates = [
|
|
|
|
# branch name (or tag name) of current IDF
|
|
|
|
os.getenv("CI_COMMIT_REF_NAME"),
|
|
|
|
# CI_MERGE_REQUEST_TARGET_BRANCH_NAME
|
|
|
|
os.getenv("CI_MERGE_REQUEST_TARGET_BRANCH_NAME"),
|
|
|
|
]
|
|
|
|
# revision defined in bot message
|
|
|
|
customized_project_revisions = os.getenv("BOT_CUSTOMIZED_REVISION")
|
|
|
|
if customized_project_revisions:
|
|
|
|
customized_project_revisions = json.loads(customized_project_revisions)
|
2017-10-18 07:53:10 -04:00
|
|
|
try:
|
2019-11-18 23:06:27 -05:00
|
|
|
ref_to_use = customized_project_revisions[proj_name.lower()]
|
|
|
|
# highest priority, insert to head of list
|
|
|
|
candidates.insert(0, ref_to_use)
|
2017-10-18 07:53:10 -04:00
|
|
|
except (KeyError, TypeError):
|
2019-11-18 23:06:27 -05:00
|
|
|
pass
|
|
|
|
# branch name read from IDF
|
2017-10-18 07:53:10 -04:00
|
|
|
try:
|
2020-09-28 23:20:05 -04:00
|
|
|
git_describe = subprocess.check_output(["git", "describe", "HEAD"])
|
2019-11-18 23:06:27 -05:00
|
|
|
match = IDF_GIT_DESCRIBE_PATTERN.search(git_describe.decode())
|
|
|
|
if match:
|
|
|
|
major_revision = match.group(1)
|
|
|
|
minor_revision = match.group(2)
|
|
|
|
# release branch
|
|
|
|
candidates.append("release/v{}.{}".format(major_revision, minor_revision))
|
|
|
|
# branch to match all major branches, like v3.x or v3
|
|
|
|
candidates.append("release/v{}.x".format(major_revision))
|
|
|
|
candidates.append("release/v{}".format(major_revision))
|
2017-10-18 07:53:10 -04:00
|
|
|
except subprocess.CalledProcessError:
|
2019-11-18 23:06:27 -05:00
|
|
|
# this should not happen as IDF should have describe message
|
|
|
|
pass
|
|
|
|
|
|
|
|
return [c for c in candidates if c] # filter out null value
|
2017-10-18 07:53:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("project",
|
|
|
|
help="the name of project")
|
2019-11-18 23:06:27 -05:00
|
|
|
parser.add_argument("project_relative_path",
|
|
|
|
help="relative path of project to IDF repository directory")
|
2017-10-18 07:53:10 -04:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2019-11-18 23:06:27 -05:00
|
|
|
candidate_branches = target_branch_candidates(args.project)
|
|
|
|
|
|
|
|
# change to project dir for checkout
|
|
|
|
os.chdir(args.project_relative_path)
|
2017-10-18 07:53:10 -04:00
|
|
|
|
2019-11-18 23:06:27 -05:00
|
|
|
for candidate in candidate_branches:
|
|
|
|
try:
|
|
|
|
subprocess.check_call(["git", "checkout", candidate])
|
|
|
|
print("CI using ref {} for project {}".format(candidate, args.project))
|
|
|
|
break
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
print("using default branch")
|