Pygithub: [Exemplo] Exemplo completo de commit de vários arquivos

Criado em 30 jul. 2020  ·  5Comentários  ·  Fonte: PyGithub/PyGithub

Estou migrando do gitlab para o github.
Usamos https://github.com/python-gitlab/python-gitlab como a biblioteca
Para fazer um commit para vários arquivos, usamos exatamente este exemplo: https://python-gitlab.readthedocs.io/en/stable/gl_objects/commits.html

Acho que create_git_commit faria a mesma coisa no github, mas falta de documentos.
Alguém poderia ajudar a fornecer um exemplo de criação dessas equipes da Árvore etc.?

Muito obrigado.

stale

Comentários muito úteis

Aqui está um exemplo completo abstraído do meu uso real:
Recebo muitas dicas deste https://github.com/PyGithub/PyGithub/issues/863#issuecomment -517927446

Para se comprometer com o branch master

import github
g = github.Github(base_url="https://git.mycompany.com/api/v3",login_or_token="mytoken")
repo = g.get_repo('myorg/myrepo')
# you can read file content into blob, here just use str for example
blob1 = repo.create_git_blob("file-content", "utf-8")
element1 = github.InputGitTreeElement(path="path-in-repo.ext", mode='100644', type='blob', sha=blob1.sha)
blob2 = repo.create_git_blob("file-content2", "utf-8")
element2 = github.InputGitTreeElement(path="folder/path-in-repo.ext", mode='100644', type='blob', sha=blob2.sha)
head_sha = repo.get_branch('master').commit.sha
base_tree = repo.get_git_tree(sha=head_sha)
tree = repo.create_git_tree([element1, element2], base_tree)
parent = repo.get_git_commit(sha=head_sha)
commit = repo.create_git_commit("commit_message", tree, [parent])
master_refs = self.github_repo.get_git_ref('heads/master')
master_ref.edit(sha=commit.sha)

Todos 5 comentários

bem, finalmente acabo com o processo completo... espero compartilhar mais tarde...

Aqui está um exemplo completo abstraído do meu uso real:
Recebo muitas dicas deste https://github.com/PyGithub/PyGithub/issues/863#issuecomment -517927446

Para se comprometer com o branch master

import github
g = github.Github(base_url="https://git.mycompany.com/api/v3",login_or_token="mytoken")
repo = g.get_repo('myorg/myrepo')
# you can read file content into blob, here just use str for example
blob1 = repo.create_git_blob("file-content", "utf-8")
element1 = github.InputGitTreeElement(path="path-in-repo.ext", mode='100644', type='blob', sha=blob1.sha)
blob2 = repo.create_git_blob("file-content2", "utf-8")
element2 = github.InputGitTreeElement(path="folder/path-in-repo.ext", mode='100644', type='blob', sha=blob2.sha)
head_sha = repo.get_branch('master').commit.sha
base_tree = repo.get_git_tree(sha=head_sha)
tree = repo.create_git_tree([element1, element2], base_tree)
parent = repo.get_git_commit(sha=head_sha)
commit = repo.create_git_commit("commit_message", tree, [parent])
master_refs = self.github_repo.get_git_ref('heads/master')
master_ref.edit(sha=commit.sha)

Este problema foi marcado automaticamente como obsoleto porque não teve atividade recente. Será fechado se não ocorrer mais nenhuma atividade. Obrigado por suas contribuições.

Eu queria fazer algo semelhante (ou seja, enviar vários arquivos ao mesmo tempo), mas desta vez em uma ramificação. Abaixo está o código de trabalho (deixe-me saber se ele pode ser otimizado)

import github
g = github.Github("mytoken")
repo = g.get_repo('myorg/myrepo')

blob1 = repo.create_git_blob("file-content", "utf-8")
element1 = github.InputGitTreeElement(path="path-in-repo.ext", mode='100644', type='blob', sha=blob1.sha)
blob2 = repo.create_git_blob("file-content2", "utf-8")
element2 = github.InputGitTreeElement(path="folder/path-in-repo.ext", mode='100644', type='blob', sha=blob2.sha)

head_sha = repo.get_branch('master').commit.sha

# everything above is identical to <strong i="6">@xpdable</strong> 
# but here I create the branch
branch = repo.create_git_ref(ref=f"refs/heads/branch-name", sha=head_sha)
branch_sha = repo.get_branch("branch-name").commit.sha

# similar to <strong i="7">@xpdable</strong> but replacing head_sha with branch_sha
base_tree = repo.get_git_tree(sha=branch_sha)
tree = repo.create_git_tree([configuration_element, stats_element], base_tree)
parent = repo.get_git_commit(sha=branch_sha)
commit = repo.create_git_commit("commit_message", tree, [parent])
branch_refs = repo.get_git_ref("heads/branch-name")
branch_refs.edit(sha=commit.sha)

# optional : open a PR with the new branch
repo.create_pull(title="PR title", body="PR body", base="master", head="branch-name")

Algum conselho sobre como excluir arquivos como parte de vários commits de arquivos?

Alguns posts falam sobre como criar novas árvores com referências a caminhos com sha's nulos, ou modificar árvores existentes incl. idéias sobre loop e encontrar todas as referências aos blobs excluídos?

Esta página foi útil?
0 / 5 - 0 avaliações