def add_pacakge_to_checkouts(self, versions): """Add package to checkouts.cfg on buildout.coredev plone version""" last_commit = self.get_pull_request_last_commit() user = InputGitAuthor( last_commit.commit.author.name, last_commit.commit.author.email, datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ'), ) org = self.github.get_organization('plone') repo = org.get_repo('buildout.coredev') for version in versions: attempts = 0 while attempts < 5: try: self.make_commit(repo, version, user) except GithubException: attempts += 1 if attempts == 5: self.log( f'Could not update checkouts.cfg of {version} ' f'with {self.repo_name}', level='warn', ) else: self.log( f'add to checkouts.cfg of buildout.coredev {version}') break
def commitChanges(data, repo, updatedModules): author = InputGitAuthor(packageUser, packageEmail) # If branch already exists checkout and commit else create new branch from master branch and commit try: source = repo.get_branch(branch="automated-stdlib-version-update") except GithubException: try: source = repo.get_branch("main") except GithubException: source = repo.get_branch("master") repo.create_git_ref(ref=f"refs/heads/automated-stdlib-version-update", sha=source.commit.sha) contents = repo.get_contents("gradle.properties", ref="automated-stdlib-version-update") if len(updatedModules) > 0: commitMessage = "Bump the version of stdlib module(s) - " for updatedModule in updatedModules: commitMessage += updatedModule commitMessage += " " else: commitMessage = "Update gradle.properties" repo.update_file(contents.path, commitMessage, data, contents.sha, branch="automated-stdlib-version-update", author=author)
def commit_changes(data, current_version, repo, module, latest_version): author = InputGitAuthor(packageUser, packageEmail) # If branch already exists checkout and commit else create new branch from master branch and commit try: source = repo.get_branch(MAIN_BRANCH) except GithubException: source = repo.get_branch(MASTER_BRANCH) try: repo.get_branch(branch=DEPENDABOT_BRANCH_NAME) try: repo.merge(DEPENDABOT_BRANCH_NAME, source.commit.sha, "Sync default branch") except Exception as e: print(e) except: repo.create_git_ref(ref=f"refs/heads/" + DEPENDABOT_BRANCH_NAME, sha=source.commit.sha) contents = repo.get_contents(PROPERTIES_FILE, ref=DEPENDABOT_BRANCH_NAME) repo.update_file(contents.path, "[Automated] Bump " + module + " from " + current_version + " to " + latest_version, data, contents.sha, branch=DEPENDABOT_BRANCH_NAME, author=author)
def add_task_variables_to_default_variables_if_needed(self): default_vars_to_add = sorted(self.added_variables) lines = [ "---", "# defaults file for {role_name}\n".format( role_name=self.role_name), ] lines += [ "{var_name}: true".format(var_name=var_name) for var_name in default_vars_to_add ] lines.append("") default_vars_local_content = "\n".join(lines) default_vars_remote_content = self.remote_repo.get_file_contents( "/defaults/main.yml") if default_vars_local_content != default_vars_remote_content.decoded_content: self.remote_repo.update_file("/defaults/main.yml", "Updates defaults/main.yml", default_vars_local_content, default_vars_remote_content.sha, author=InputGitAuthor( GIT_COMMIT_AUTHOR_NAME, GIT_COMMIT_AUTHOR_EMAIL)) print("Updating defaults/main.yml in %s" % self.remote_repo.name)
def commit_file(repo, file_path, updated_file_content, branch_name, commit_message): try: temp_branch = branch_name + "_temp" author = InputGitAuthor(ballerina_bot_username, ballerina_bot_email) pulls = repo.get_pulls(state='open') for pull in pulls: if (pull.head.ref == temp_branch): print("[info] " + branch_name + " has a pull request from " + temp_branch) pull.edit(state='closed') branches = repo.get_branches() for branch in branches: if branch.name == temp_branch: ref = repo.get_git_ref('heads/' + temp_branch) ref.delete() break repo.create_git_ref(ref='refs/heads/' + temp_branch, sha=branch_name.commit.sha) # commit the changes to temporary branch repo.update_file(file_path, commit_message, updated_file_content, repo.get_contents(file_path).sha, branch=temp_branch, author=author) return True except GithubException as e: raise e
def create_release(self, sha, tag, release_message, prerelease): logger.debug("GitHub.create_release(tag=%s)", tag) real_tag = tag.replace(":", "/") try: release = self.repo.get_release(real_tag) except GithubException: release = None if release: release.delete_release() logger.warning("Release %s already existed, deleted!", real_tag) try: ref = self.repo.get_git_ref(ref="tags/" + real_tag) except GithubException: ref = None if ref: ref.delete() logger.warning("Tag %s already existed, deleted!", real_tag) release = self.repo.create_git_tag_and_release( tag=real_tag, tag_message=tag, release_name=tag, release_message=release_message, object=sha, type="commit", draft=False, prerelease=prerelease, tagger=InputGitAuthor( self.github.get_user().name, self.github.get_user().email, datetime.now(timezone.utc).replace(microsecond=0).isoformat(), ), ) logger.info("Release %s created.", real_tag)
def commit_image_file(repository_name, file_path, updated_file_content, commit_branch, commit_message): try: author = InputGitAuthor(ballerina_bot_username, ballerina_bot_email) repo = github.get_repo(constants.BALLERINA_ORG_NAME + '/' + repository_name) remote_file = repo.get_contents(file_path) remote_file_contents = remote_file.decoded_content try: remote_file_in_pr_branch = repo.get_contents( file_path, commit_branch) remote_file_in_pr_branch = remote_file_in_pr_branch.decoded_content except GithubException: remote_file_in_pr_branch = "" if updated_file_content == remote_file_contents: return elif updated_file_content == remote_file_in_pr_branch: return else: repo.update_file(file_path, commit_message, updated_file_content, remote_file.sha, branch=commit_branch, author=author) return except GithubException as e: raise e
def _update_readme_content_if_needed(self, repo_status): local_readme_content = self._generate_readme_content() remote_readme_file = self.remote_repo.get_file_contents("/README.md") if repo_status == "update": local_readme_content = remote_readme_file.decoded_content.decode( "utf-8") local_readme_content = re.sub( r'Ansible version (\d*\.\d+|\d+)', "Ansible version %s" % ssg.ansible.min_ansible_version, local_readme_content) local_readme_content = re.sub( r'%s.[a-zA-Z0-9\-_]+' % ORGANIZATION_NAME, "%s.%s" % (ORGANIZATION_NAME, self.role_name), local_readme_content) if local_readme_content != remote_readme_file.decoded_content.decode( "utf-8"): print("Updating README.md in %s" % self.remote_repo.name) self.remote_repo.update_file("/README.md", "Updates README.md", local_readme_content, remote_readme_file.sha, author=InputGitAuthor( GIT_COMMIT_AUTHOR_NAME, GIT_COMMIT_AUTHOR_EMAIL))
def push(path, message, content, branch, new_file=False, append_content=False): github_credentials = cfg["github"] api_key = github_credentials['api_key'] author = InputGitAuthor("RemisHaroon", "*****@*****.**") g = Github(api_key) repo = g.search_repositories("tech_roastery")[0] if not new_file and append_content: file = repo.get_contents(path, ref="master") # Get file from branch data = file.decoded_content.decode("utf-8") # Get raw string data data += "\n " + content # Modify/Create file else: data = content if branch != "master": source = repo.get_branch("master") repo.create_git_ref( ref=f"refs/heads/{branch}", sha=source.commit.sha) # Create new branch from master if not new_file: # If file already exists, update it contents = repo.get_contents( path, ref=branch) # Retrieve old file to get its SHA and path repo.update_file(contents.path, message, data, contents.sha, branch=branch, author=author) # Add, commit and push branch else: # If file doesn't exist, create it repo.create_file(path, message, data, branch=branch, author=author) # Add, commit and push branch
def create_submission_tag(self, course, team, tag_name, tag_message, commit_sha): github_repo = self.organization.get_repo( self.__get_team_ghrepo_name(course, team)) commit = self.get_commit(course, team, commit_sha) this_user = self.gh.get_user() if this_user.name is None: user_name = "Team %s" % team.id else: user_name = this_user.name if this_user.email is None: user_email = "*****@*****.**" else: user_email = this_user.email tz = pytz.timezone("America/Chicago") dt = tz.localize(datetime.now().replace(microsecond=0)) iu = InputGitAuthor(user_name, user_email, dt.isoformat()) tag = github_repo.create_git_tag(tag_name, tag_message, commit.sha, "commit", iu) github_repo.create_git_ref("refs/tags/" + tag.tag, tag.sha)
def add_task_variables_to_default_variables_if_needed(self): default_vars_to_add = sorted(self.added_variables) default_vars_local_content = yaml.dump(self.default_vars_data, width=120, indent=4, default_flow_style=False) header = [ "---", "# defaults file for {role_name}\n".format( role_name=self.role_name), ] lines = [ "{var_name}: true".format(var_name=var_name) for var_name in default_vars_to_add ] lines.append("") default_vars_local_content = ( "%s%s%s" % ("\n".join(header), default_vars_local_content, "\n".join(lines))) default_vars_remote_content = self.remote_repo.get_contents( "defaults/main.yml") if default_vars_local_content != default_vars_remote_content.decoded_content: self.remote_repo.update_file("defaults/main.yml", "Updates defaults/main.yml", default_vars_local_content, default_vars_remote_content.sha, author=InputGitAuthor( GIT_COMMIT_AUTHOR_NAME, GIT_COMMIT_AUTHOR_EMAIL)) print("Updating defaults/main.yml in %s" % self.remote_repo.name)
def commit_changes(data, repo, updated_modules): author = InputGitAuthor(packageUser, packageEmail) # If branch already exists checkout and commit else create new branch from master branch and commit try: source = repo.get_branch(MAIN_BRANCH) except GithubException: source = repo.get_branch(MASTER_BRANCH) try: repo.get_branch(branch=VERSION_UPDATE_BRANCH_NAME) repo.merge(VERSION_UPDATE_BRANCH_NAME, source.commit.sha, "Sync default branch") except: repo.create_git_ref(ref=f"refs/heads/" + VERSION_UPDATE_BRANCH_NAME, sha=source.commit.sha) contents = repo.get_contents(PROPERTIES_FILE, ref=VERSION_UPDATE_BRANCH_NAME) if len(updated_modules) > 0: commit_message = "Bump the version of stdlib module(s) - " for updated_module in updated_modules: commit_message += updated_module commit_message += " " else: commit_message = "Update gradle.properties" repo.update_file(contents.path, commit_message, data, contents.sha, branch=VERSION_UPDATE_BRANCH_NAME, author=author)
def commit_changes(repo, updated_file, lang_version): author = InputGitAuthor(packageUser, packageEmail) try: base = repo.get_branch(MASTER_BRANCH) except: base = repo.get_branch(MAIN_BRANCH) try: ref = f"refs/heads/" + LANG_VERSION_UPDATE_BRANCH repo.create_git_ref(ref=ref, sha=base.commit.sha) except : try: repo.get_branch(LANG_VERSION_UPDATE_BRANCH) repo.merge(LANG_VERSION_UPDATE_BRANCH, base.commit.sha, "Sync default branch") except GithubException as e: print("Error occurred: ", e) current_file = repo.get_contents(PROPERTIES_FILE, ref=LANG_VERSION_UPDATE_BRANCH) repo.update_file( current_file.path, COMMIT_MESSAGE_PREFIX + lang_version, updated_file, current_file.sha, branch=LANG_VERSION_UPDATE_BRANCH, author=author )
def create_release(self, version: str, sha: str) -> None: """Create a GitHub release.""" target = sha if self._commit_sha_of_release_branch() == sha: # If we use <branch> as the target, GitHub will show # "<n> commits to <branch> since this release" on the release page. target = self._release_branch logger.debug(f"Release {version} target: {target}") log = self._changelog.get(version, sha) if self._ssh or self._gpg: logger.debug("Creating tag via Git CLI") self._git.create_tag(version, sha, log) else: logger.debug("Creating tag via GitHub API") tag = self._repo.create_git_tag( version, log, sha, "commit", tagger=InputGitAuthor(self._user, self._email), ) self._repo.create_git_ref(f"refs/tags/{version}", tag.sha) logger.info(f"Creating release {version} at {sha}") self._repo.create_git_release(version, version, log, target_commitish=target)
def commit_file(repository_name, file_path, updated_file_content, commit_branch, commit_message): try: author = InputGitAuthor(ballerina_bot_username, ballerina_bot_email) repo = github.get_repo(constants.BALLERINA_ORG_NAME + '/' + repository_name) remote_file = repo.get_contents(file_path) remote_file_contents = remote_file.decoded_content.decode( constants.ENCODING) try: remote_file_in_pr_branch = repo.get_contents( file_path, commit_branch) remote_file_in_pr_branch = remote_file_in_pr_branch.decoded_content.decode( constants.ENCODING) except GithubException: remote_file_in_pr_branch = "" if updated_file_content == remote_file_contents: return False, "" elif updated_file_content == remote_file_in_pr_branch: return True, "" else: base = repo.get_branch(repo.default_branch) branch = commit_branch try: ref = f"refs/heads/" + branch repo.create_git_ref(ref=ref, sha=base.commit.sha) except GithubException: print("[Info] Unmerged '" + commit_branch + "' branch existed in '" + repository_name + "'") branch = commit_branch + '_tmp' ref = f"refs/heads/" + branch try: repo.create_git_ref(ref=ref, sha=base.commit.sha) except GithubException as e: print("[Info] Deleting '" + commit_branch + "' tmp branch existed in '" + repository_name + "'") if e.status == 422: # already exist repo.get_git_ref("heads/" + branch).delete() repo.create_git_ref(ref=ref, sha=base.commit.sha) update = repo.update_file(file_path, commit_message, updated_file_content, remote_file.sha, branch=branch, author=author) if not branch == commit_branch: update_branch = repo.get_git_ref("heads/" + commit_branch) update_branch.edit(update["commit"].sha, force=True) repo.get_git_ref("heads/" + branch).delete() return True, update["commit"].sha except GithubException as e: raise e
def getUseOrDefault(attribute, default_author=None): #Get access token for user 2 user_info = getTokenAttribute(attribute=attribute) #Test if an access token exist, if it does, try to log in user, use user 1 if user_info: return InputGitAuthor(user_info[0], user_info[1]) else: return default_author
def pushChart(self): repo = self.g.get_repo(f"{self.username}/{self.username}") committer = InputGitAuthor('readme-bot', '*****@*****.**') with open('bar_graph.png', 'rb') as input_file: data = input_file.read() try: contents = repo.get_contents("charts/bar_graph.png") repo.update_file(contents.path, "Charts Updated", data, contents.sha, committer=committer) except Exception as e: repo.create_file("charts/bar_graph.png", "Charts Added", data, committer=committer)
def _update_content_if_needed(self, filepath): remote_content, sha = self._remote_content(filepath) if self._local_content(filepath) != remote_content: self.remote_repo.update_file(filepath, "Updated " + filepath, self._local_content(filepath), sha, author=InputGitAuthor( GIT_COMMIT_AUTHOR_NAME, GIT_COMMIT_AUTHOR_EMAIL)) print("Updating %s in %s" % (filepath, self.remote_repo.name))
def _update_tasks_content_if_needed(self): tasks_remote_content = self.remote_repo.get_contents("tasks/main.yml") if self.tasks_local_content != tasks_remote_content.decoded_content: self.remote_repo.update_file("tasks/main.yml", "Updates tasks/main.yml", self.tasks_local_content, tasks_remote_content.sha, author=InputGitAuthor( GIT_COMMIT_AUTHOR_NAME, GIT_COMMIT_AUTHOR_EMAIL)) print("Updating tasks/main.yml in %s" % self.remote_repo.name)
def get_committer_data(self, committer): email = None if committer.email is not None: email = committer.email else: for item in committer.get_emails(): if item["primary"]: email = item["email"] if email is None: msg = "Unable to get {login}'s email adress. " \ "You may have to add the scope user:email".format(login=committer.login) raise NoPermissionError(msg) return InputGitAuthor(name=committer.login, email=email)
def _update_readme_content_if_needed(self): local_readme_content = self._generate_readme_content() remote_readme_file = self.remote_repo.get_file_contents("/README.md") if local_readme_content != remote_readme_file.decoded_content.decode("utf-8"): print("Updating README.md in %s" % self.remote_repo.name) self.remote_repo.update_file( "/README.md", "Updates README.md", local_readme_content, remote_readme_file.sha, author=InputGitAuthor( GIT_COMMIT_AUTHOR_NAME, GIT_COMMIT_AUTHOR_EMAIL) )
def _update_vars_content_if_needed(self): vars_remote_content = self.remote_repo.get_file_contents("/vars/main.yml") vars_local_content = yaml.dump(self.vars_data, width=120, indent=4, default_flow_style=False) if vars_local_content != vars_remote_content.decoded_content: self.remote_repo.update_file( "/vars/main.yml", "Updates vars/main.yml", vars_local_content, vars_remote_content.sha, author=InputGitAuthor( GIT_COMMIT_AUTHOR_NAME, GIT_COMMIT_AUTHOR_EMAIL) ) print("Updating vars/main.yml in %s" % self.remote_repo.name)
def update_dependencies(repo): dependencies = sp.check_output(["python3", "tools/extract_dependencies.py", "--markdown"]) dependencies = str(dependencies, "utf-8") diff = sp.check_output(["git", "diff", "--name-only", DEPENDENCIESPATH]) diff = str(diff, "utf-8") if diff != "": sha = repo.get_contents(DEPENDENCIESPATH, ref=BRANCH).sha repo.update_file( path="{}".format(DEPENDENCIESPATH), message="[Docs] Update component dependencies\nAutomatically committed through CI.\n\n[ci skip]", content=dependencies, sha=sha, committer=InputGitAuthor("ace3mod", "*****@*****.**"), branch=BRANCH ) print("Dependencies successfully updated.") else: print("Dependencies skipped - no change.")
def create_release(self, sha, tag, release_message, prerelease): logger.debug("GitHub.create_release(tag=%s)", tag) self.repo.create_git_tag_and_release( tag=tag.replace(":", "/"), tag_message=tag, release_name=tag, release_message=release_message, object=sha, type="commit", draft=False, prerelease=prerelease, tagger=InputGitAuthor( self.github.get_user().name, self.github.get_user().email, datetime.now(timezone.utc).replace(microsecond=0).isoformat(), ), )
def push(path, message, content, branch, update=False): author = InputGitAuthor(config.GitHubAuthor, config.GitHubEmail) #source = repo.get_Branch(Branch) #repo.create_git_ref(ref=f"refs/heads/{Branch}", sha=source.commit.sha) # Create new Branch from master if update: # If file already exists, update it #pass contents = repo.get_contents( path, ref=branch) # Retrieve old file to get its SHA and path repo.update_file(contents.path, message, content, contents.sha, branch=branch, author=author) # Add, commit and push Branch else: # If file doesn't exist, create it #pass repo.create_file(path, message, content, branch=branch, author=author) # Add, commit and push Branch
def _update_meta_content_if_needed(self, repo_status): remote_meta_file = self.remote_repo.get_file_contents("/meta/main.yml") with open(META_TEMPLATE_PATH, 'r') as f: meta_template = f.read() if repo_status == "new": local_meta_content = meta_template.replace("@ROLE_NAME@", self.role_name) local_meta_content = local_meta_content.replace( "@DESCRIPTION@", self.title) local_meta_content = local_meta_content.replace( "@MIN_ANSIBLE_VERSION@", ssg.ansible.min_ansible_version) else: author = re.search(r'author:.*', meta_template).group(0) description = re.search(r'description:.*', meta_template).group(0) issue_tracker_url = re.search(r'issue_tracker_url:.*', meta_template).group(0) local_meta_content = remote_meta_file.decoded_content local_meta_content = re.sub(r'role_name:.*', "role_name: %s" % self.role_name, local_meta_content) local_meta_content = re.sub(r'author:.*', author, local_meta_content) local_meta_content = re.sub( r'min_ansible_version: (\d*\.\d+|\d+)', "min_ansible_version: %s" % ssg.ansible.min_ansible_version, local_meta_content) local_meta_content = re.sub(r'description:.*', "description: %s" % self.title, local_meta_content) local_meta_content = re.sub(r'issue_tracker_url:.*', issue_tracker_url, local_meta_content) if local_meta_content != remote_meta_file.decoded_content: print("Updating meta/main.yml in %s" % self.remote_repo.name) self.remote_repo.update_file("/meta/main.yml", "Updates meta/main.yml", local_meta_content, remote_meta_file.sha, author=InputGitAuthor( GIT_COMMIT_AUTHOR_NAME, GIT_COMMIT_AUTHOR_EMAIL))
def _update_meta_content_if_needed(self): with open(META_TEMPLATE_PATH, 'r') as f: meta_template = f.read() local_meta_content = meta_template.replace("@DESCRIPTION@", self.title) local_meta_content = local_meta_content.replace( "@MIN_ANSIBLE_VERSION@", ssg.ansible.min_ansible_version) remote_meta_file = self.remote_repo.get_file_contents("/meta/main.yml") if local_meta_content != remote_meta_file.decoded_content: print("Updating meta/main.yml in %s" % self.remote_repo.name) self.remote_repo.update_file( "/meta/main.yml", "Updates meta/main.yml", local_meta_content, remote_meta_file.sha, author=InputGitAuthor( GIT_COMMIT_AUTHOR_NAME, GIT_COMMIT_AUTHOR_EMAIL) )
def release(build): token = os.environ.get('GITHUB_TOKEN', False) or open('.token').readline().replace('\n', '') g = Github(token) repo = g.get_user(OWNER).get_repo(REPO) author = InputGitAuthor(RELEASE_AUTHOR, RELEASE_AUTHOR_EMAIL) commit = repo.get_branch('master').commit.sha message = 'Automatic release containing a package built for AWS Lambda environment' release = repo.create_git_tag_and_release( tag=build.git_tag(), tag_message=message, release_name=f'Prebuilt package of {build.package_name} {build.package_version}', release_message=message, object=commit, type='commit', tagger=author ) release_path = build.create_compressed_tarball() release.upload_asset(release_path, release_path.split('/')[-1], 'application/gzip')
def commit_to_coredev(request, payload, plone_version, changeset, changeset_long, timestamp): logger.info('Commit: LETS COMMIT ON COREDEV') gh = request.registry.settings['github'] org = gh.get_organization('plone') repo = org.get_repo('buildout.coredev') head_ref = repo.get_git_ref(f'heads/{plone_version}') latest_commit = repo.get_git_commit(head_ref.object.sha) base_tree = latest_commit.tree element = InputGitTreeElement(path='last_commit.txt', mode='100644', type='blob', content=changeset_long) new_tree = repo.create_git_tree([element], base_tree) new_user = InputGitAuthor(payload['pusher']['name'], payload['pusher']['email'], timestamp) new_commit = repo.create_git_commit(f'[fc] {changeset}', new_tree, [latest_commit], new_user, new_user) head_ref.edit(sha=new_commit.sha, force=False)
def update_changelog(repo, file_path, date_string, lines, message, tries=5, branch="master"): completed = 0 while not completed and tries > 0: contents = repo.get_contents(file_path, ref=branch) changelog_data = contents.decoded_content.decode('utf8').split('\n') if not changelog_data[0]: # removing empty first line changelog_data = changelog_data[1:] if changelog_data[0] == date_string: changelog_data = changelog_data[1:] changelog_data = [''] + [date_string] + lines + changelog_data changelog_text = '\n'.join(changelog_data) print("Adding changelog:") print('\n'.join([date_string] + lines)) # Thanks Crossedfall for this bit. git_email = os.getenv("GIT_EMAIL") git_name = os.getenv("GIT_NAME") try: repo.update_file(contents.path, message, changelog_text, contents.sha, branch=branch, committer=InputGitAuthor(git_name, git_email)) except: completed = 0 traceback.print_exc() time.sleep( random.random() * 2) # just in case multiple instances are fighting or something else: completed = 1 tries -= 1 return completed