Example #1
def create_branch_and_update_yaml(deployment_type, deployed_data, yaml_data,
                                  hyper_params, latest_commit_hash):
    """Create branch and update yaml content on fork repo."""
    # Update yaml model version for the given deployment
    new_yaml_data = update_yaml_data(yaml_data['dict'], deployment_type,
                                     MODEL_VERSION, hyper_params)
    _logger.info('Modified yaml data, new length: %d', len(new_yaml_data))

    # Connect to fabric8 analytic repo & get latest commit hash
    f8a_repo = Github(GITHUB_TOKEN).get_repo(f'{FORK_REPO_NAME}/{PROJECT_NAME}')
    _logger.info('f8a fork repo: %s', f8a_repo)

    # Create a new branch on f8a repo
    branch_name = f'bump_f8a-pypi-insights_for_{deployment_type}_to_{MODEL_VERSION}'
    branch = f8a_repo.create_git_ref(f'refs/heads/{branch_name}', latest_commit_hash)
    _logger.info('Created new branch [%s] at [%s]', branch, latest_commit_hash)

    # Update the yaml content in branch on f8a repo
    commit_message = f'Bump up f8a-pypi-insights for {deployment_type} from ' \
                     f'{deployed_data["version"]} to {MODEL_VERSION}'
    update = f8a_repo.update_file(YAML_FILE_PATH, commit_message, new_yaml_data,
                                  yaml_data['content_sha'], branch=f'refs/heads/{branch_name}')
    _logger.info('New yaml content hash %s', update['commit'].sha)

    return branch_name, commit_message
Example #2
class GitTestRepo:
    def __init__(self, token, repo_name):
        self.token = token
        self.repo_name = repo_name
        self.repo = Github(self.token).get_repo(repo_name)

    def create_and_tag(self, author, filename, data_frame, tag_name,
        self.repo.create_file(filename, "initial creation",
        fetched_file = self.repo.get_contents(filename, ref=branch_name)
        data = fetched_file.decoded_content.decode("utf-8")
        commit = push(author, self.repo, filename, "commit message", data,
        gtag = self.repo.create_git_tag(tag=str(tag_name),
                                        message="initial load",
        print('Created new file {} with tag {}'.format(filename, tag_name))
        self.repo.create_git_ref('refs/tags/{}'.format(gtag.tag), gtag.sha)
Example #3
class GitRepo():
    Extension of PyGithub with a couple of other helper methods.

    def __init__(self, repo_name, credentials=None):
        """Retrieves a Repository by its fully qualified name. If credentials are passed
        they will be used."""
        if not credentials:
            self._github = Github().get_repo(repo_name)
        elif credentials.token:
            self._github = Github(credentials.token).get_repo(repo_name)
            self._github = Github(credentials.username, credentials.password).get_repo(repo_name)

    def github(self):
        Direct access to the underlying PyGithub object.
        return self._github

    def get_file(self, filename):
        """Fetch and decode the file from the master branch.
        Note that GitHub's API only supports files up to 1MB in size."""
        return self._github.get_contents(filename).decoded_content.decode('utf-8')

    def modify_and_branch(self, base_branch, new_branch_name, commit_message, filename, file_content):
        """Create a new branch from base_branch, makes changes to a file, and
        commits it to the new branch."""

        base_sha = self._github.get_git_ref('heads/{}'.format(base_branch)).object.sha
        base_tree = self._github.get_git_tree(base_sha)
        element = InputGitTreeElement(filename, '100644', 'blob', file_content)
        tree = self._github.create_git_tree([element], base_tree)

        parent = self._github.get_git_commit(base_sha)
        commit = self._github.create_git_commit(commit_message, tree, [parent])

        self._github.create_git_ref('refs/heads/{}'.format(new_branch_name), commit.sha)