Pygithub: [์˜ˆ์‹œ] ๋‹ค์ค‘ ํŒŒ์ผ ์ปค๋ฐ‹์˜ ์ „์ฒด ์˜ˆ

์— ๋งŒ๋“  2020๋…„ 07์›” 30์ผ  ยท  5์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: PyGithub/PyGithub

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)

๋ชจ๋“  5 ๋Œ“๊ธ€

๊ธ€์Ž„, ๋งˆ์นจ๋‚ด ๋‚˜๋Š” ์ „์ฒด ๊ณผ์ •์„ ๋๋‚ด๊ณ  ... ์ž˜ํ•˜๋ฉด ๋‚˜์ค‘์— ๊ณต์œ  ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค ...

๋‹ค์Œ์€ ์‹ค์ œ ์‚ฌ์šฉ์—์„œ ์ถ”์ƒํ™”๋œ ์ „์ฒด ์˜ˆ์ž…๋‹ˆ๋‹ค.
์ด 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์— ๋Œ€ํ•œ ๋ชจ๋“  ์ฐธ์กฐ๋ฅผ ๋ฐ˜๋ณตํ•˜๊ณ  ์ฐพ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์•„์ด๋””์–ด๊ฐ€ ์žˆ์Šต๋‹ˆ๊นŒ?

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰