def _get(self, name): """Read a config value with 'git config --get'.""" try: return git.config('--get', 'gerrit.%s' % name, _cwd=self.repodir).rstrip() except ErrorReturnCode_1: return None
def sync_branches_list(snap): """ Syncs the downstream snap branches to a yaml file for parsing in jobs """ click.echo(f"Checking: git+ssh://[email protected]/snap-{snap}") git_repo = f"git+ssh://[email protected]/snap-{snap}" snap_releases = remote_branches(git_repo) snap_releases.reverse() env = os.environ.copy() repo = f"https://{env['CDKBOT_GH_USR']}:{env['CDKBOT_GH_PSW']}@github.com/charmed-kubernetes/jenkins" with tempfile.TemporaryDirectory() as tmpdir: git.clone(repo, tmpdir) git.config("user.email", "*****@*****.**", _env=env, _cwd=tmpdir) git.config("user.name", "cdkbot", _env=env, _cwd=tmpdir) git.config("--global", "push.default", "simple", _cwd=tmpdir) output = Path(f"{tmpdir}/jobs/includes/k8s-snap-branches-list.inc") click.echo(f"Saving to {str(output)}") output.write_text(yaml.dump(snap_releases, default_flow_style=False, indent=2)) cmd_ok(f"git add {str(output)}", cwd=tmpdir) ret = cmd_ok( ["git", "commit", "-m", "Updating k8s snap branches list"], cwd=tmpdir ) if not ret.ok: return click.echo(f"Committing to {repo}.") ret = cmd_ok(["git", "push", repo, "master"], cwd=tmpdir) if not ret.ok: raise SystemExit("Failed to commit latest snap branches.")
def _cut_stable_release(layer_list, charm_list, ancillary_list, filter_by_tag, dry_run): """This will merge each layers master onto the stable branches. PLEASE NOTE: This step should come after each stable branch has been tagged and references a current stable bundle revision. layer_list: YAML spec containing git repos and their upstream/downstream properties charm_list: YAML spec containing git repos and their upstream/downstream properties """ layer_list = yaml.safe_load(Path(layer_list).read_text(encoding="utf8")) charm_list = yaml.safe_load(Path(charm_list).read_text(encoding="utf8")) ancillary_list = yaml.safe_load( Path(ancillary_list).read_text(encoding="utf8")) new_env = os.environ.copy() for layer_map in layer_list + charm_list + ancillary_list: for layer_name, repos in layer_map.items(): downstream = repos["downstream"] if not repos.get("needs_stable", True): continue tags = repos.get("tags", None) if tags: if not any(match in filter_by_tag for match in tags): continue log.info( f"Releasing :: {layer_name:^35} :: from: master to: stable") if not dry_run: downstream = f"https://{new_env['CDKBOT_GH_USR']}:{new_env['CDKBOT_GH_PSW']}@github.com/{downstream}" identifier = str(uuid.uuid4()) os.makedirs(identifier) for line in git.clone(downstream, identifier, _iter=True): log.info(line) git_rev_master = git("rev-parse", "origin/master", _cwd=identifier).stdout.decode() git_rev_stable = git("rev-parse", "origin/stable", _cwd=identifier).stdout.decode() if git_rev_master == git_rev_stable: log.info( f"Skipping :: {layer_name:^35} :: master == stable") continue git.config("user.email", "*****@*****.**", _cwd=identifier) git.config("user.name", "cdkbot", _cwd=identifier) git.config("--global", "push.default", "simple") git.checkout("-f", "stable", _cwd=identifier) git.merge("master", "--no-ff", _cwd=identifier) for line in git.push("origin", "stable", _cwd=identifier, _iter=True): log.info(line)
def _tag_stable_forks(layer_list, charm_list, k8s_version, bundle_rev, filter_by_tag, bugfix, dry_run): """Tags stable forks to a certain bundle revision for a k8s version layer_list: YAML spec containing git repos and their upstream/downstream properties bundle_rev: bundle revision to tag for a particular version of k8s git tag (ie. ck-{bundle_rev}), this would mean we tagged current stable branches for 1.14 with the latest charmed kubernetes(ck) bundle rev of {bundle_rev} TODO: Switch to different merge strategy git checkout master git checkout -b staging git merge stable -s ours git checkout stable git reset staging """ layer_list = yaml.safe_load(Path(layer_list).read_text(encoding="utf8")) charm_list = yaml.safe_load(Path(charm_list).read_text(encoding="utf8")) new_env = os.environ.copy() for layer_map in layer_list + charm_list: for layer_name, repos in layer_map.items(): tags = repos.get("tags", None) if tags: if not any(match in filter_by_tag for match in tags): continue downstream = repos["downstream"] if bugfix: tag = f"{k8s_version}+{bundle_rev}" else: tag = f"ck-{k8s_version}-{bundle_rev}" if not repos.get("needs_tagging", True): log.info(f"Skipping {layer_name} :: does not require tagging") continue log.info(f"Tagging {layer_name} ({tag}) :: {repos['downstream']}") if not dry_run: downstream = f"https://{new_env['CDKBOT_GH_USR']}:{new_env['CDKBOT_GH_PSW']}@github.com/{downstream}" identifier = str(uuid.uuid4()) os.makedirs(identifier) for line in git.clone(downstream, identifier, _iter=True): log.info(line) git.config("user.email", "*****@*****.**", _cwd=identifier) git.config("user.name", "cdkbot", _cwd=identifier) git.config("--global", "push.default", "simple") git.checkout("stable", _cwd=identifier) try: for line in git.tag("--force", tag, _cwd=identifier, _iter=True, _bg_exc=False): log.info(line) for line in git.push( "--force", "origin", tag, _cwd=identifier, _bg_exc=False, _iter=True, ): log.info(line) except sh.ErrorReturnCode as error: log.info( f"Problem tagging: {error.stderr.decode().strip()}, will skip for now.." )
#!/usr/bin/env python3 import os from sh.contrib import git print("== Git Setup for starters! ==") user = input('Your Name: ') mail = input('Your Email: ') print("Configure git...") git.config('--global', 'user.name', user) git.config('--global', 'user.email', mail) print("Generate SSH Key...") os.system('ssh-keygen -t ed25519 -C "'+mail+'"')
"prerelease": False} print("Releasing {}".format(release["tag_name"])) print(release["body"]) response = github.post("/repos/adafruit/" + bundle + "/releases", json=release) if not response.ok: print("Failed to create release") print(release) print(response.request.url) print(response.text) os.chdir(working_directory) if __name__ == "__main__": directory = os.path.abspath(".bundles") if "GITHUB_WORKSPACE" in os.environ: git.config("--global", "user.name", "adabot") git.config("--global", "user.email", os.environ["ADABOT_EMAIL"]) for bundle in bundles: bundle_path = os.path.join(directory, bundle) try: fetch_bundle(bundle, bundle_path) update_info = update_bundle(bundle_path) if update_info: commit_updates(bundle_path, update_info) push_updates(bundle_path) new_release(bundle, bundle_path) except RuntimeError as e: print("Failed to update and release:", bundle) print(e)
def _cut_stable_release(layer_list, charm_list, ancillary_list, filter_by_tag, dry_run): """This will merge each layers master onto the stable branches. PLEASE NOTE: This step should come after each stable branch has been tagged and references a current stable bundle revision. layer_list: YAML spec containing git repos and their upstream/downstream properties charm_list: YAML spec containing git repos and their upstream/downstream properties """ layer_list = yaml.safe_load(Path(layer_list).read_text(encoding="utf8")) charm_list = yaml.safe_load(Path(charm_list).read_text(encoding="utf8")) ancillary_list = yaml.safe_load( Path(ancillary_list).read_text(encoding="utf8")) new_env = os.environ.copy() failed_to_release = [] for layer_map in layer_list + charm_list + ancillary_list: for layer_name, repos in layer_map.items(): downstream = repos["downstream"] if not repos.get("needs_stable", True): continue tags = repos.get("tags", None) if tags: if not any(match in filter_by_tag for match in tags): continue auth = (new_env.get("CDKBOT_GH_USR"), new_env.get("CDKBOT_GH_PSW")) default_branch = repos.get("branch") or default_gh_branch( downstream, auth=auth) log.info( f"Releasing :: {layer_name:^35} :: from: {default_branch} to: stable" ) downstream = f"https://{':'.join(auth)}@github.com/{downstream}" identifier = str(uuid.uuid4()) os.makedirs(identifier) for line in git.clone(downstream, identifier, _iter=True): log.info(line) git_rev_default = (git("rev-parse", f"origin/{default_branch}", _cwd=identifier).stdout.decode().strip()) git_rev_stable = (git("rev-parse", "origin/stable", _cwd=identifier).stdout.decode().strip()) if git_rev_default == git_rev_stable: log.info( f"Skipping :: {layer_name:^35} :: {default_branch} == stable" ) continue log.info( f"Commits :: {layer_name:^35} :: {default_branch} != stable") log.info(f" {default_branch:10}= {git_rev_default:32}") log.info(f" {'stable':10}= {git_rev_stable:32}") for line in git("rev-list", f"origin/stable..origin/{default_branch}", _cwd=identifier): for line in git.show( "--format=%h %an '%s' %cr", "--no-patch", line.strip(), _cwd=identifier, ): log.info(" " + line.strip()) if not dry_run: git.config("user.email", "*****@*****.**", _cwd=identifier) git.config("user.name", "cdkbot", _cwd=identifier) git.config("--global", "push.default", "simple") git.checkout("-f", "stable", _cwd=identifier) git.reset(default_branch, _cwd=identifier) for line in git.push("origin", "stable", "-f", _cwd=identifier, _iter=True): log.info(line)
def author_email(): """Get the author email.""" if "GIT_AUTHOR_EMAil" in os.environ: return os.environ["GIT_AUTHOR_EMAIL"].strip() return git.config("--get", "user.email").strip()
def author_name(): """Get the author name.""" if "GIT_AUTHOR_NAME" in os.environ: return os.environ["GIT_AUTHOR_NAME"].strip() return git.config("--get", "user.name").strip()