這篇文章給大家分享的是有關(guān)Python中Gitlab Api怎么用的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都做網(wǎng)站、成都網(wǎng)站建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的林州網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!安裝
pip install python-gitlab
環(huán)境: py3
DEMO
# -*- coding: utf-8 -*- __Author__ = "xiewm" __Date__ = '2017/12/26 13:46' """ gitlab 經(jīng)常使用到的api DOC_URL: http://python-gitlab.readthedocs.io/en/stable/ LOCAL_PATH: C:\Python36\Lib\site-packages\gitlab """ import gitlab url = 'http://xxxxxxx' token = 'xxxxxxxxxxxxxx' # 登錄 gl = gitlab.Gitlab(url, token) # ---------------------------------------------------------------- # # 獲取第一頁(yè)project projects = gl.projects.list() # 獲取所有的project projects = gl.projects.list(all=True) # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 獲取所有project的name,id for p in gl.projects.list(all=True, as_list=False): print(p.name, p.id) # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 獲取第一頁(yè)project的name,id for p in gl.projects.list(page=1): print(p.name, p.id) # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 通過(guò)指定id 獲取 project 對(duì)象 project = gl.projects.get(501) # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 查找項(xiàng)目 projects = gl.projects.list(search='keyword') # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 創(chuàng)建一個(gè)項(xiàng)目 project = gl.projects.create({'name':'project1'}) # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 獲取公開(kāi)的項(xiàng)目 projects = gl.projects.list(visibility='public') # public, internal or private # ---------------------------------------------------------------- # # 獲取 project 對(duì)象是以下操作的基礎(chǔ) # ---------------------------------------------------------------- # # 通過(guò)指定project對(duì)象獲取該項(xiàng)目的所有分支 branches = project.branches.list() print(branches) # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 獲取指定分支的屬性 branch = project.branches.get('master') print(branch) # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 創(chuàng)建分支 branch = project.branches.create({'branch_name': 'feature1', 'ref': 'master'}) # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 刪除分支 project.branches.delete('feature1') # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 分支保護(hù)/取消保護(hù) branch.protect() branch.unprotect() # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 獲取指定項(xiàng)目的所有tags tags = project.tags.list() # 獲取某個(gè)指定tag 的信息 tags = project.tags.list('1.0') # 創(chuàng)建一個(gè)tag tag = project.tags.create({'tag_name':'1.0', 'ref':'master'}) # 設(shè)置tags 說(shuō)明: tag.set_release_description('awesome v1.0 release') # 刪除tags project.tags.delete('1.0') # or tag.delete() # ---------------------------------------------------------------- # # 獲取所有commit info commits = project.commits.list() for c in commits: print(c.author_name, c.message, c.title) # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 獲取指定commit的info commit = project.commits.get('e3d5a71b') # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 獲取指定項(xiàng)目的所有merge request mrs = project.mergerequests.list() print(mrs) # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 獲取 指定mr info mr = project.mergerequests.get(mr_id) # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 創(chuàng)建一個(gè)merge request mr = project.mergerequests.create({'source_branch':'cool_feature', 'target_branch':'master', 'title':'merge cool feature', }) # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 更新一個(gè)merge request 的描述 mr.description = 'New description' mr.save() # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 開(kāi)關(guān)一個(gè)merge request (close or reopen): mr.state_event = 'close' # or 'reopen' mr.save() # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # Delete a MR: project.mergerequests.delete(mr_id) # or mr.delete() # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # Accept a MR: mr.merge() # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 指定條件過(guò)濾 所有的merge request # state: state of the MR. It can be one of all, merged, opened or closed # order_by: sort by created_at or updated_at # sort: sort order (asc or desc) mrs = project.mergerequests.list(state='merged', sort='asc') # all, merged, opened or closed # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # 創(chuàng)建一個(gè)commit data = { 'branch_name': 'master', # v3 'commit_message': 'blah blah blah', 'actions': [ { 'action': 'create', 'file_path': 'blah', 'content': 'blah' } ] } commit = project.commits.create(data) # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # Compare two branches, tags or commits: result = project.repository_compare('develop', 'feature-20180104') print(result) # get the commits for commit in result['commits']: print(commit) # # get the diffs for file_diff in result['diffs']: print(file_diff) # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # # get the commits for commit in result['commits']: print(commit) # # get the diffs for file_diff in result['diffs']: print(file_diff) # ---------------------------------------------------------------- #
感謝各位的閱讀!關(guān)于“Python中Gitlab Api怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。