python-gitlab 進行分支管理惜索,項目文件添加

使用python-gitlab 進行分支管理,給所有項目增加相同文件剃浇,

import common_data
import gitlab

gitlab_host = 'https://xxxxx.com'
gitlab_token = "xxxxxxxxxxx"
gl = gitlab.Gitlab(gitlab_host, private_token=gitlab_token)


# group = gl.groups.get(group_id)
# projects = group.projects.list(all=True)
# print(len(projects))


def get_branch_info(project_id):
    # # 通過指定id 獲取 project 對象
    project_info = gl.projects.get(project_id)
    # branch_info = gl.projects.get(project.id)
    # branches = project_info.branches.list(get_all=True)
    # branch_v4_6_0 = project_info.branches.get('v4.5.0')
    # print(branch_v4_6_0)
    # branch = project_info.branches.create({'branch_name': 'xxxxx', 'ref': 'v4.5.0'})
    tags = project_info.tags.list()
    print("before create ", tags)
    last_tag_name = tags[0].attributes["name"]
    if last_tag_name == 'testv4.5.0':
        print("the tag name %s is already exists" % last_tag_name)
    else:
        tag = project_info.tags.create({'tag_name': 'testv4.5.0', 'ref': 'v4.5.0'})
        print("the tag %s create " % tag)


# 0.新建分支
def create_branch(project_name, branch_name):
    project = gl.projects.get(common_data.project_name_dir[project_name])
    branches = project.branches.list(get_all=True)
    branches_name = []
    i = 0
    while i < len(branches):
        branches_name.append(branches[i].attributes["name"])
        i += 1
    print(branches_name)
    if branch_name not in branches_name:
        print(branch_name)
        print("not exits project ", branch_name)
        branch = project.branches.create({'branch': branch_name, 'ref': 'v4.6.0'})
        print("finish create branch ", branch)
    else:
        print("the branch %s already exits" % branch_name, project_name)


# 1.給group組的所有項目打tag巾兆,
def create_tags(project_name, tag_name):
    # # 通過指定name 獲取 project 對象
    project = gl.projects.get(common_data.project_name_dir[project_name])

    tags = project.tags.list()
    if len(tags) != 0:
        last_tag_name = tags[0].attributes["name"]
        if last_tag_name == tag_name:
            print("the tag name %s is already exists" % last_tag_name)
        else:
            tag = project.tags.create({'tag_name': tag_name, 'ref': tag_name})
            print("the tag %s created " % tag)
    else:
        tag = project.tags.create({'tag_name': tag_name, 'ref': tag_name})
        print("the tag %s created " % tag.attributes["name"])


# 2.分支保護設(shè)置
def branch_protect(project_name, branch):
    project = gl.projects.get(common_data.project_name_dir[project_name])
    # print(project.attributes["name"])
    try:
        p_branches = project.protectedbranches.get(branch)
    except Exception as err:
        print("the branch is not protected", err)
        p_branch = project.protectedbranches.create({
            'name': branch,
            'merge_access_level': gitlab.const.AccessLevel.DEVELOPER,
            'push_access_level': gitlab.const.AccessLevel.MAINTAINER
        })
        print(branch, "protected done")
    else:
        print(project_name, " already protected", p_branches.attributes["name"])


# 3.增加新文件
def add_new_file(project_name):

    project = gl.projects.get(common_data.project_name_dir[project_name])
    # f = project.files.get(file_path='.gitlab-ci.yml', ref='CICD_test')
    # print(f)
    try:
        f = project.files.get(file_path='.gitlab-ci.yml', ref='v4.6.0')
    except Exception as err:
        print("the file in %s is not exits" % project_name, err)
        file_content = open('gitlab-ci.yml', 'r', encoding='UTF-8').read()
        nf = project.files.create(
            {'file_path': '.gitlab-ci.yml', 'branch': 'v4.6.0',
             'content': file_content, 'author_email': 'xxxxx@xxx.com',
             'author_name': 'xxxx',
             'commit_message': 'Add CICD config file'})
        print("add gitlab-ci.yml done", nf)
    else:
        print("the file is exits", f)
#
# get_branch_info(25506)

#common_data 所有項目的和對應(yīng)projectID

# for project_obj in common_data.project_name_dir:
#     branch_protect(project_obj, "v4.6.0")
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市虎囚,隨后出現(xiàn)的幾起案子角塑,更是在濱河造成了極大的恐慌,老刑警劉巖淘讥,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件圃伶,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機窒朋,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進店門搀罢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人侥猩,你說我怎么就攤上這事魄揉。” “怎么了?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵肴掷,是天一觀的道長敲霍。 經(jīng)常有香客問我兵怯,道長,這世上最難降的妖魔是什么腔剂? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任媒区,我火速辦了婚禮,結(jié)果婚禮上掸犬,老公的妹妹穿的比我還像新娘袜漩。我一直安慰自己,他們只是感情好湾碎,可當(dāng)我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布宙攻。 她就那樣靜靜地躺著,像睡著了一般介褥。 火紅的嫁衣襯著肌膚如雪座掘。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天柔滔,我揣著相機與錄音溢陪,去河邊找鬼。 笑死睛廊,一個胖子當(dāng)著我的面吹牛形真,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播超全,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼咆霜,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了卵迂?” 一聲冷哼從身側(cè)響起裕便,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎见咒,沒想到半個月后偿衰,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年下翎,在試婚紗的時候發(fā)現(xiàn)自己被綠了缤言。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡视事,死狀恐怖胆萧,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情俐东,我是刑警寧澤跌穗,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站虏辫,受9級特大地震影響蚌吸,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜砌庄,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一羹唠、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧娄昆,春花似錦佩微、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至杆怕,卻和暖如春族购,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背陵珍。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留违施,地道東北人互纯。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像磕蒲,于是被迫代替她去往敵國和親留潦。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,044評論 2 355

推薦閱讀更多精彩內(nèi)容