def get_modules_changed(path, ref='HEAD'): '''Get modules changed from git diff-index {ref} :param path: String path of git repo :param ref: branch or remote/branch or sha to compare :return: List of paths of modules changed ''' git_run_obj = GitRun(os.path.join(path, '.git')) if ref != 'HEAD': fetch_ref = ref if ':' not in fetch_ref: # to force create branch fetch_ref += ':' + fetch_ref git_run_obj.run(['fetch'] + fetch_ref.split('/', 1)) items_changed = git_run_obj.get_items_changed(ref) folders_changed = set([ item_changed.split('/')[0] for item_changed in items_changed if '/' in item_changed] ) modules = set(get_modules(path)) modules_changed = list(modules & folders_changed) modules_changed_path = [ os.path.join(path, module_changed) for module_changed in modules_changed] return modules_changed_path
def version_validate(version, dir): if not version and dir: repo_path = os.path.join(dir, '.git') branch_name = GitRun(repo_path).get_branch_name() version = (branch_name.replace('_', '-').split('-')[:1] if branch_name else False) version = version[0] if version else None if not version: print( travis_helpers.yellow('Undefined environment variable' ' `VERSION`.\nSet `VERSION` for ' 'compatibility with guidelines by version.')) return version
def __init__(self): self._git = GitRun(os.path.join(os.getcwd(), '.git'), True) self.branch = os.environ.get("TRAVIS_BRANCH", self._git.get_branch_name()) remote = self._git.run(["ls-remote", "--get-url", "origin"]) name = remote.replace(':', '/') name = re.sub('.+@', '', name) name = re.sub('.git$', '', name) name = re.sub('^https://', '', name) name = re.sub('^http://', '', name) match = re.search( r'(?P<host>[^/]+)/(?P<owner>[^/]+)/(?P<repo>[^/]+)', name) if match: name = ("%(host)s:%(owner)s/%(repo)s (%(branch)s)" % dict(match.groupdict(), branch=self.branch)) self.repo_name = name self.wl_api = WeblateApi() self.gh_api = GitHubApi() self._travis_home = os.environ.get("HOME", "~/") self._travis_build_dir = os.environ.get("TRAVIS_BUILD_DIR", "../..") self._odoo_version = os.environ.get("VERSION") self._odoo_branch = os.environ.get("ODOO_BRANCH") self._langs = (parse_list(os.environ.get("LANG_ALLOWED")) if os.environ.get("LANG_ALLOWED", False) else []) self._odoo_full = os.environ.get("ODOO_REPO", "odoo/odoo") self._server_path = get_server_path(self._odoo_full, (self._odoo_branch or self._odoo_version), self._travis_home) self._addons_path = get_addons_path(self._travis_home, self._travis_build_dir, self._server_path) self._database = os.environ.get('MQT_TEST_DB', 'openerp_test') self._connection_context = context_mapping.get( self._odoo_version, Odoo10Context) self._apply_patch_odoo() self._get_modules_installed()
def __init__(self, git_project, revision, command_format='docker', docker_user=None, root_path=None, default_docker_image=None, remotes=None, exclude_after_success=None, run_extra_args=None, include_cleanup=None, build_extra_args=''): """ Method Constructor @fname_dockerfile: str name of file dockerfile to save. @format_cmd: str name of format of command. bash: Make a bash script. docker: Make a dockerfile script. """ if root_path is None: self.root_path = os.path.join( gettempdir(), os.path.splitext(os.path.basename(__file__))[0], ) else: self.root_path = os.path.expandvars(os.path.expanduser(root_path)) self.default_docker_image = default_docker_image self.git_project = git_project self.revision = revision git_path = self.get_repo_path(self.root_path) self.git_obj = GitRun(git_project, git_path) self.travis_data = self.load_travis_file(revision) self.sha = self.git_obj.get_sha(revision) self.exclude_after_success = exclude_after_success self.run_extra_args = run_extra_args self.include_cleanup = include_cleanup self.build_extra_args = build_extra_args if not self.travis_data: raise Exception("Make sure you have access to repository" " " + git_project + " and that the file .travis.yml" " exists in branch or revision " + revision) self.travis2docker_section = [ # ('build_image', 'build_image'), ('python', 'python'), ('env', 'env'), ('install', 'run'), ('script', 'script'), ('after_success', 'script'), ] self.travis2docker_section_dict = dict(self.travis2docker_section) self.scripts_root_path = self.get_script_path(self.root_path) env_regex_str = r"(?P<var>[\w]*)[ ]*[\=][ ]*[\"\']{0,1}" + \ r"(?P<value>[\w\.\-\_/\$\{\}\:,\(\)\#\* ]*)[\"\']{0,1}" export_regex_str = r"^(?P<export>export|EXPORT)( )+" + env_regex_str self.env_regex = re.compile(env_regex_str, re.M) self.export_regex = re.compile(export_regex_str, re.M) self.extra_env_from_run = "" self.command_format = command_format self.docker_user = docker_user or 'root' self.remotes = remotes
def translate_gitlab_env(): """ This sets Travis CI's variables using GitLab CI's variables. This makes possible to run scripts which depend on Travis CI's variables when running under Gitlab CI. For documentation on these environment variables, please check: - Predefined environment variables on Travis CI: https://docs.travis-ci.com/user/environment-variables#Default-Environment-Variables - Predefined environment variables on GitLab CI: https://docs.gitlab.com/ee/ci/variables/#predefined-variables-environment-variables """ if not os.environ.get("GITLAB_CI"): return False build_dir = os.environ.get("CI_PROJECT_DIR", ".") git_run_obj = GitRun(repo_path=os.path.join(build_dir, ".git")) commit_message = git_run_obj.run(["log", "-1", "--pretty=%b"]) head_branch = os.environ.get("CI_COMMIT_REF_NAME") # This is a guess target_branch = os.environ.get("VERSION") # Set environment variables os.environ.update({ # Predefined values "TRAVIS": "true", "CONTINUOUS_INTEGRATION": "true", "HAS_JOSH_K_SEAL_OF_APPROVAL": "true", "RAILS_ENV": "test", "RACK_ENV": "test", "MERB_ENV": "test", "TRAVIS_ALLOW_FAILURE": "false", "TRAVIS_JOB_NUMBER": "1", "TRAVIS_TEST_RESULT": "0", "TRAVIS_COMMIT_RANGE": "unknown", # Dinamic values "TRAVIS_BRANCH": target_branch, "TRAVIS_COMMIT_MESSAGE": commit_message, "TRAVIS_OS_NAME": sys.platform, "TRAVIS_SUDO": "true" if getuser() == "root" else "false", }) # Set variables whose values are already directly set in other variables equivalent_vars = [ ("TRAVIS_BUILD_DIR", "CI_PROJECT_DIR"), ("TRAVIS_BUILD_ID", "CI_BUILD_ID"), ("TRAVIS_BUILD_NUMBER", "CI_JOB_ID"), ("TRAVIS_COMMIT", "CI_COMMIT_SHA"), ("TRAVIS_EVENT_TYPE", "CI_PIPELINE_SOURCE"), ("TRAVIS_JOB_ID", "CI_JOB_ID"), ("TRAVIS_REPO_SLUG", "CI_PROJECT_PATH"), ("TRAVIS_BUILD_STAGE_NAME", "CI_BUILD_STAGE"), ] os.environ.update({ travis_var: os.environ.get(gitlab_var) for travis_var, gitlab_var in equivalent_vars if gitlab_var in os.environ }) # If within an MR is_mr = target_branch != head_branch if is_mr: os.environ.update({ # there's no way to know the MR number. For more info, see: # https://gitlab.com/gitlab-org/gitlab-ce/issues/15280 "TRAVIS_PULL_REQUEST": "unknown", "TRAVIS_PULL_REQUEST_BRANCH": head_branch, "TRAVIS_PULL_REQUEST_SHA": os.environ.get("CI_COMMIT_SHA"), "TRAVIS_PULL_REQUEST_SLUG": os.environ.get("CI_PROJECT_PATH"), }) else: os.environ.update({ "TRAVIS_PULL_REQUEST": "false", "TRAVIS_PULL_REQUEST_BRANCH": "", "TRAVIS_PULL_REQUEST_SHA": "", "TRAVIS_PULL_REQUEST_SLUG": "", }) return True