2017-10-18 11:53:10 +00: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-19 04:06:27 +00:00
|
|
|
import re
|
2017-10-18 11:53:10 +00:00
|
|
|
|
|
|
|
|
2019-11-19 04:06:27 +00: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 11:53:10 +00:00
|
|
|
try:
|
2019-11-19 04:06:27 +00: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 11:53:10 +00:00
|
|
|
except (KeyError, TypeError):
|
2019-11-19 04:06:27 +00:00
|
|
|
pass
|
|
|
|
# branch name read from IDF
|
2017-10-18 11:53:10 +00:00
|
|
|
try:
|
2019-11-19 04:06:27 +00:00
|
|
|
git_describe = subprocess.check_output(["git", "describe", "--tags", "HEAD"])
|
|
|
|
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 11:53:10 +00:00
|
|
|
except subprocess.CalledProcessError:
|
2019-11-19 04:06:27 +00: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 11:53:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("project",
|
|
|
|
help="the name of project")
|
2019-11-19 04:06:27 +00:00
|
|
|
parser.add_argument("project_relative_path",
|
|
|
|
help="relative path of project to IDF repository directory")
|
2017-10-18 11:53:10 +00:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2019-11-19 04:06:27 +00:00
|
|
|
candidate_branches = target_branch_candidates(args.project)
|
|
|
|
|
|
|
|
# change to project dir for checkout
|
|
|
|
os.chdir(args.project_relative_path)
|
2017-10-18 11:53:10 +00:00
|
|
|
|
2019-11-19 04:06:27 +00: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")
|