gitlab์์ github์ผ๋ก ์ฎ๊ฒจ๊ฐ๊ณ ์์ต๋๋ค.
https://github.com/python-gitlab/python-gitlab ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ์ฌ์ฉํฉ๋๋ค.
์ฌ๋ฌ ํ์ผ์ ๋ํ ์ปค๋ฐ์ ๋ง๋ค๊ธฐ ์ํด ์ ํํ ๋ค์ ์์ ๋ฅผ ์ฌ์ฉํฉ๋๋ค. https://python-gitlab.readthedocs.io/en/stable/gl_objects/commits.html
create_git_commit์ด github์์ ๋์ผํ ์์
์ ์ํํ์ง๋ง ๋ฌธ์๊ฐ ๋ถ์กฑํ๋ค๋ ๊ฒ์ ์์์ต๋๋ค.
๋๊ตฐ๊ฐ ๊ทธ ๋๋ฌด ๋ฑ์ ๋ง๋๋ ์๋ฅผ ์ ๊ณตํ๋ ๋ฐ ๋์์ด ๋ ๊น์? ์ง์?
๋งค์ฐ ๊ฐ์ฌํฉ๋๋ค.
๊ธ์, ๋ง์นจ๋ด ๋๋ ์ ์ฒด ๊ณผ์ ์ ๋๋ด๊ณ ... ์ํ๋ฉด ๋์ค์ ๊ณต์ ํ ๊ฒ์ ๋๋ค ...
๋ค์์ ์ค์ ์ฌ์ฉ์์ ์ถ์ํ๋ ์ ์ฒด ์์
๋๋ค.
์ด https://github.com/PyGithub/PyGithub/issues/863#issuecomment -517927446์์ ๋ง์ ํํธ๋ฅผ ์ป์ต๋๋ค.
๋ง์คํฐ ๋ธ๋์น์ ์ปค๋ฐํ๋ ค๋ฉด
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)
์ด ๋ฌธ์ ๋ ์ต๊ทผ ํ๋์ด ์์๊ธฐ ๋๋ฌธ์ ์๋์ผ๋ก ์ค๋๋ ๊ฒ์ผ๋ก ํ์๋์์ต๋๋ค. ๋ ์ด์ ํ๋์ด ์์ผ๋ฉด ํ์๋ฉ๋๋ค. ๊ทํ์ ๊ธฐ์ฌ์ ๊ฐ์ฌ๋๋ฆฝ๋๋ค.
๋น์ทํ ์ผ์ ํ๊ณ ์ถ์์ง๋ง(์ฆ, ํ ๋ฒ์ ์ฌ๋ฌ ํ์ผ์ ์ปค๋ฐ) ์ด๋ฒ์๋ ์ง์ ์์. ์๋๋ ์์ ์ฝ๋์ ๋๋ค(์ต์ ํํ ์ ์๋์ง ์๋ ค์ฃผ์ธ์)
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")
์ด๋ฌํ ๋ค์ค ํ์ผ ์ปค๋ฐ์ ์ผ๋ถ๋ก ํ์ผ์ ์ญ์ ํ๋ ๋ฐฉ๋ฒ์ ๋ํ ์กฐ์ธ์ด ์์ต๋๊น?
์ผ๋ถ ๊ฒ์๋ฌผ์์๋ null sha๊ฐ ์๋ ๊ฒฝ๋ก์ ๋ํ ์ฐธ์กฐ๋ฅผ ์ฌ์ฉํ์ฌ ์ ๋๋ฌด๋ฅผ ์์ฑํ๊ฑฐ๋ ํฌํจ๋ ๊ธฐ์กด ๋๋ฌด๋ฅผ ์์ ํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์ค๋ช ํฉ๋๋ค. ์ญ์ ๋ blob์ ๋ํ ๋ชจ๋ ์ฐธ์กฐ๋ฅผ ๋ฐ๋ณตํ๊ณ ์ฐพ๋ ๋ฐฉ๋ฒ์ ๋ํ ์์ด๋์ด๊ฐ ์์ต๋๊น?
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๋ค์์ ์ค์ ์ฌ์ฉ์์ ์ถ์ํ๋ ์ ์ฒด ์์ ๋๋ค.
์ด https://github.com/PyGithub/PyGithub/issues/863#issuecomment -517927446์์ ๋ง์ ํํธ๋ฅผ ์ป์ต๋๋ค.
๋ง์คํฐ ๋ธ๋์น์ ์ปค๋ฐํ๋ ค๋ฉด