Pygithub: [Ejemplo] Ejemplo completo de archivo múltiple de confirmación

Creado en 30 jul. 2020  ·  5Comentarios  ·  Fuente: PyGithub/PyGithub

Me estoy mudando de gitlab a github.
Usamos https://github.com/python-gitlab/python-gitlab como biblioteca
Para realizar una confirmación para varios archivos, usamos exactamente este ejemplo: https://python-gitlab.readthedocs.io/en/stable/gl_objects/commits.html

Encuentro que create_git_commit haría lo mismo en github pero sin documentos.
¿Alguien podría ayudar a proporcionar un ejemplo de cómo crear esos árboles, etc., personal?

Muchas gracias.

stale

Comentario más útil

Aquí hay un ejemplo completo abstraído de mi uso real:
Recibo muchas sugerencias de esto https://github.com/PyGithub/PyGithub/issues/863#issuecomment -517927446

Para comprometerse con la rama principal

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 comentarios

bueno, finalmente terminé con el proceso completo... espero compartirlo más tarde...

Aquí hay un ejemplo completo abstraído de mi uso real:
Recibo muchas sugerencias de esto https://github.com/PyGithub/PyGithub/issues/863#issuecomment -517927446

Para comprometerse con la rama principal

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 se ha marcado automáticamente como obsoleto porque no ha tenido actividad reciente. Se cerrará si no se produce más actividad. Gracias por sus aportaciones.

Quería hacer algo similar (es decir, confirmar varios archivos a la vez), pero esta vez en una rama. A continuación se muestra el código de trabajo (avíseme si se puede optimizar)

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")

¿Algún consejo sobre cómo eliminar archivos como parte de tales confirmaciones de archivos múltiples?

Algunas publicaciones hablan sobre la creación de nuevos árboles con referencias a rutas con sha nulos, o la modificación de árboles existentes, incl. ideas sobre bucles y encontrar todas las referencias a los blobs eliminados?

¿Fue útil esta página
0 / 5 - 0 calificaciones