一、token生成
相关文档
https://python-gitlab.readthedocs.io/en/master/api-usage.html
https://blog.csdn.net/xie_0723/article/details/75215869
代码演示
import gitlab # pip install python-gitlab class Gitlabapi: def __init__(self,url,token): self.url = url self.token = token self.conn = gitlab.Gitlab(self.url,self.token) def get_projects(self): # 获取所有的项目 projects = self.conn.projects.list(all=True,as_list=False) projectslist = [] for pro in projects: projectslist.append(pro.attributes) return projectslist def get_projects_visibility(self,visibility="public"): # 根据属性获取项目 visibilitydict = {"public":"public","internal":"internal","private":"private"} if visibility in visibilitydict: attribute = visibilitydict[visibility] else: attribute = visibilitydict["public"] projects = self.conn.projects.list(all=True,visibility=attribute) projectslist = [] for pro in projects: projectslist.append(pro.attributes) return projectslist def get_projects_id(self,id): # 根据id获取项目 res = self.conn.projects.get(id) return res.attributes def get_projects_search(self,name): # 模糊搜索项目 projects = self.conn.projects.list(search=name) projectslist = [] for pro in projects: projectslist.append(pro.attributes) return projectslist def create_project(self,name): # 创建项目 res = self.conn.projects.create({"name":name}) return res.attributes def get_project_brances(self,id): # 获取项目所有分支 project = self.conn.projects.get(id) brancheslist = [] for branches in project.branches.list(): brancheslist.append(branches.attributes) return brancheslist def get_project_brance_attribute(self,id,branch): # 获取指定项目指定分支 project = self.conn.projects.get(id) res = project.branches.get(branch) return res.attributes def create_get_project_brance(self,id,branch): # 创建分支 project = self.conn.projects.get(id) res = project.branches.create({"branch":branch,"ref":"master"}) return res.attributes def delete_project_brance(self,id,branch): # 删除分支 project = self.conn.projects.get(id) project.branches.delete(branch) def protect_project_brance(self,id,branch,is_protect=None): # 分支保护 project = self.conn.projects.get(id) branch = project.branches.get(branch) if is_protect == "protect": branch.unprotect() else: branch.protect() def get_project_tags(self,id): # 获取所有的tag project = self.conn.projects.get(id) tags = project.tags.list() taglist = [] for tag in tags: taglist.append(tag.attributes) return taglist def get_project_tag_name(self,id,name): # 获取指定的tag project = self.conn.projects.get(id) tags = project.tags.get(name) return tags.attributes def create_project_tag(self,id,name,branch="master"): # 创建tag project = self.conn.projects.get(id) tags = project.tags.create({"tag_name":name,"ref":branch}) return tags.attributes def delete_project_tag(self,id,name): # 删除tag project = self.conn.projects.get(id) project.tags.delete(name) def get_project_commits(self,id): # 获取所有的commit project = self.conn.projects.get(id) commits = project.commits.list() commitslist = [] for com in commits: commitslist.append(com.attributes) return commitslist def get_project_commit_info(self,id,short_id): # 获取指定的commit project = self.conn.projects.get(id) commit = project.commits.get(short_id) return commit.attributes def get_project_merge(self,id): # 获取所有的合并请求 project = self.conn.projects.get(id) mergerquests = project.mergerequests.list() mergerquestslist = [] for mergerquest in mergerquests: mergerquestslist.append(mergerquest.attributes) return mergerquestslist def get_project_merge_id(self,id,mr_id): # 获取请求的详细信息 project = self.conn.projects.get(id) mrinfo = project.mergerequests.get(mr_id) return mrinfo.attributes def create_project_merge(self,id,source_branch,target_branch,title): # 创建合并请求 project = self.conn.projects.get(id) res = project.mergerequests.create({"source_branch":source_branch,"target_branch":target_branch,"title":title}) return res def update_project_merge_info(self,id,mr_id,data): # data = {"description":"new描述","state_event":"close"} project = self.conn.projects.get(id) mr = project.mergerequests.get(mr_id) if "description" in data: mr.description = data["description"] if "state_event" in data: state_event = ["close", "reopen"] if data["state_event"] in state_event: mr.state_event = data["state_event"] res = mr.save() return res def delete_project_merge(self,id,mr_id): project = self.conn.projects.get(id) res = project.mergerequests.delete(mr_id) return res def access_project_merge(self,id,mr_id): project = self.conn.projects.get(id) mr = project.mergerequests.get(mr_id) res = mr.merge() return res def search_project_merge(self,id,state,sort): ''' :param id: :param state: state of the mr,It can be one of all,merged,opened or closed :param sort: sort order (asc or desc) :param order_by: sort by created_at or updated_at :return: ''' stateinfo = ["merged","opened","closed"] sortinfo = ["asc","desc"] if state not in stateinfo: state = "merged" if sort not in sortinfo: sort = "asc" project = self.conn.projects.get(id) mergerquests = project.mergerequests.list(state=state,sort=sort) mergerquestslist = [] for mergerquest in mergerquests: mergerquestslist.append(mergerquest.attributes) return mergerquestslist def create_project_commit(self,id): project = self.conn.projects.get(id) data = { 'branch_name':'master', 'commit_message':'mytest commit', 'actions':[{ 'action':'create', 'file_path':'myreadme', 'contend':'commit_test' }] } commit = project.commits.create(data) return commit def diff_project_branches(self,id,source_branch,target_branch): # 比较2个分支 project = self.conn.projects.get(id) result = project.repository_compare(source_branch,target_branch) # commits = result["commits"] # commits = result["diffs"] return result if __name__ == '__main__': url = "http://192.168.184.103" token = "tecjmW8kaV26qhbOL9A3pMrScD7D6HdHRU2vPufs" gl = Gitlabapi(url,token) res = gl.get_project_merge(3) print(res)