def test_get_current_commit(caplog): with caplog.at_level(logging.WARNING): with tempfile.TemporaryDirectory() as target_dir: get_current_commit(target_dir) for record in caplog.records: assert "Failed getting current git commit" in record flexmock(subprocess).should_receive("run").and_return(subprocess.CompletedProcess(1, 0, "some_hash")) assert get_current_commit(".") == "some_hash"
def test_get_current_commit(caplog, tmpdir): with caplog.at_level(logging.WARNING): target_dir = tmpdir.mkdir("target_dir") get_current_commit(target_dir) for record in caplog.records: assert "Failed getting current git commit" in record flexmock(subprocess).should_receive("run").and_return( subprocess.CompletedProcess(1, 0, "some_hash")) assert get_current_commit(".") == "some_hash"
def run(self): created_branches = {} cmd_result = 0 languages = self.args.languages or self.config.languages commit_msg = "Regenerate client from commit {} of spec repo".format( get_current_commit(self.args.spec_repo_dir)) commit_msg = self.args.push_commit_msg or commit_msg for lang_name, lang_config in self.config.language_configs.items(): # Skip any languages not specified by the user if lang_name not in languages: continue gen_dir = self.get_generated_lang_dir(lang_name) # Assumes all generated changes are in the gen_dir directory # This is done by default in the `generate` command. with change_cwd(gen_dir): repo = "{}/{}".format(lang_config.github_org, lang_config.github_repo) branch_name = '{}/{}'.format(lang_name, time.time()) try: run_command(['git', 'checkout', '-b', branch_name]) run_command(['git', 'add', '-A']) run_command(['git', 'commit', '-a', '-m', commit_msg]) run_command(['git', 'push', 'origin', 'HEAD']) created_branches[repo] = branch_name except subprocess.CalledProcessError as e: log.error("Error running git commands: {}".format(e)) cmd_result += 1 continue log.info('Apigentools created the following branches:') log.info('\n'.join('{} : {}'.format(key, value) for key, value in created_branches.items())) return cmd_result
def write_dot_apigentools_info(self, language_config, version): """Write a record for language/version in .apigentools-info file in the top-level directory of the language :param language_config: Config of language to write .apigentools-info for :type language: ``LanguageConfig`` :param version: Version to write .apigentools-info record for :type version: ``str`` """ outfile = os.path.join(language_config.generated_lang_dir, ".apigentools-info") loaded = {} if os.path.exists(outfile): with open(outfile) as f: try: loaded = json.load(f) except json.JSONDecodeError: log.debug("Couldn't read .apigentools-info, will overwrite") if str(loaded.get("info_version")) == "1": log.info("Detected .apigentools-info version 1, will rewrite") loaded = {} info = { "additional_stamps": self.args.get("additional_stamp"), "info_version": "2", } loaded.update(info) loaded.setdefault("spec_versions", {}) loaded["spec_versions"][version] = { "apigentools_version": __version__, "regenerated": str(datetime.datetime.utcnow()), "spec_repo_commit": get_current_commit("."), } with open(outfile, "w") as f: json.dump(loaded, f, indent=4)
def run(self): created_branches = {} cmd_result = 0 languages = self.args.languages or self.config.languages commit_msg = "Regenerate client from commit {} of spec repo".format( get_current_commit(self.args.spec_repo_dir)) commit_msg = self.args.push_commit_msg or commit_msg for lang_name, lang_config in self.config.language_configs.items(): # Skip any languages not specified by the user if lang_name not in languages: continue log.info("Running push for language {}".format(lang_name)) gen_dir = self.get_generated_lang_dir(lang_name) # Assumes all generated changes are in the gen_dir directory # This is done by default in the `generate` command. with change_cwd(gen_dir): repo = "{}/{}".format(lang_config.github_org, lang_config.github_repo) branch_name = self.get_push_branch(lang_name) try: if self.args.skip_if_no_changes and self.git_status_empty( ): log.info( "Only .apigentools file changed for language {}, skipping" .format(lang_name)) continue # Update git config for this repository to use the provided author's email/name # If not specified, use the setup from the system/global if self.args.git_email: run_command([ 'git', 'config', 'user.email', self.args.git_email ], dry_run=self.args.dry_run) if self.args.git_name: run_command( ['git', 'config', 'user.name', self.args.git_name], dry_run=self.args.dry_run) run_command(['git', 'checkout', '-b', branch_name], dry_run=self.args.dry_run) run_command(['git', 'add', '-A'], dry_run=self.args.dry_run) run_command(['git', 'commit', '-a', '-m', commit_msg], dry_run=self.args.dry_run) run_command(['git', 'push', 'origin', 'HEAD'], dry_run=self.args.dry_run) created_branches[repo] = branch_name except subprocess.CalledProcessError as e: log.error("Error running git commands: {}".format(e)) cmd_result += 1 continue log.info('Apigentools created the following branches:') log.info('\n'.join('{} : {}'.format(key, value) for key, value in created_branches.items())) return cmd_result
def run(self): created_branches = {} cmd_result = 0 languages = self.args.get("languages") or self.config.languages commit_msg = "Regenerate client from commit {} of spec repo".format( get_current_commit()) commit_msg = self.args.get("push_commit_msg") or commit_msg for lang_name, lang_config in self.config.languages.items(): # Skip any languages not specified by the user if lang_name not in languages: continue log.info("Running push for language {}".format(lang_name)) gen_dir = lang_config.generated_lang_dir # Assumes all generated changes are in the gen_dir directory # This is done by default in the `generate` command. with change_cwd(gen_dir): repo = "{}/{}".format(lang_config.github_org, lang_config.github_repo) branch_name = self.get_push_branch(lang_name) try: if self.args.get( "skip_if_no_changes") and self.git_status_empty(): log.info( "Only .apigentools file changed for language {}, skipping" .format(lang_name)) continue self.setup_git_config() run_command( ["git", "checkout", "-b", branch_name], dry_run=self.args.get("dry_run"), ) run_command(["git", "add", "-A"], dry_run=self.args.get("dry_run")) run_command( ["git", "commit", "-a", "-m", commit_msg], dry_run=self.args.get("dry_run"), ) run_command( ["git", "push", "origin", "HEAD"], dry_run=self.args.get("dry_run"), ) created_branches[repo] = branch_name except subprocess.CalledProcessError as e: log.error("Error running git commands: {}".format(e)) cmd_result += 1 continue log.info("Apigentools created the following branches:") log.info("\n".join("{} : {}".format(key, value) for key, value in created_branches.items())) return cmd_result
def get_stamp(self): """Get string for "stamping" files for trackability :return: Stamp, for example: "Generated with: apigentools version X.Y.Z (image: apigentools:X.Y.Z); spec repo commit abcd123" :rtype: ``str`` """ stamp = "Generated with: apigentools version {version}".format( version=__version__) spec_repo_commit = get_current_commit(".") stamp = (stamp, ) if spec_repo_commit: stamp + ("spec repo commit {commit}".format( commit=spec_repo_commit), ) return "; ".join(stamp + (self.args.get("additional_stamp", ())))
def get_stamp(self): """ Get string for "stamping" files for trackability :return: Stamp, for example: "Generated with: apigentools version X.Y.Z (image: apigentools:X.Y.Z); spec repo commit abcd123" :rtype: ``str`` """ stamp = "Generated with: apigentools version {version}".format(version=__version__) if self.get_image_name() is None: stamp += " (non-container run)" else: stamp += " (image: '{image}')".format(image=self.get_image_name()) spec_repo_commit = get_current_commit(self.args.spec_repo_dir) stamp = [stamp] if spec_repo_commit: stamp.append("spec repo commit {commit}".format(commit=spec_repo_commit)) stamp.append("codegen version {v}".format(v=self.get_codegen_version())) return "; ".join(stamp + self.args.additional_stamp)
def write_dot_apigentools_info(self, language): """ Write .apigentools-info file in the top-level directory of the given language :param language: Language to write .apigentools-info for :type language: ``str`` """ outfile = os.path.join( self.get_generated_lang_dir(language), ".apigentools-info", ) info = { "additional_stamps": self.args.additional_stamp, "apigentools_version": __version__, "codegen_version": self.get_codegen_version(), "info_version": "1", "image": self.args.generated_with_image, "spec_repo_commit": get_current_commit(self.args.spec_repo_dir), } with open(outfile, "w") as f: json.dump(info, f, indent=4)