gitlab api批量操作 批量添加用户

import os,time
import requests,json

# def downloadFile(name, url):
#     headers = {'Proxy-Connection': 'keep-alive'}
#     r = requests.get(url, stream=True, headers=headers)
#     length = float(r.headers['content-length'])
#     f = open(name, 'wb')
#     count = 0
#     count_tmp = 0
#     time1 = time.time()
#     for chunk in r.iter_content(chunk_size=512):
#         if chunk:
#             f.write(chunk)
#             count += len(chunk)
#             if time.time() - time1 > 2:
#                 p = count / length * 100
#                 speed = (count - count_tmp) / 1024  / 2
#                 count_tmp = count
#                 print(name + ': ' + formatFloat(p) + '%' + ' Speed: ' + formatFloat(speed) + 'KB/S')
#                 time1 = time.time()
#     f.close()
#
# def formatFloat(num):
#     return '{:.2f}'.format(num)

# if __name__ == '__main__':
#     downloadFile('movie6.mp4', 'http://download.xunleizuida.com/1905/神秘慈善家.BD1280高清中字版.mp4')
#

#批量添加用户
def add_user(project_id,user_id,access_level):
    '''

    :param name:
    :param description:
    :return:
    '''
    #url = "http://gitlab.com/api/v4/projects?per_page=100&pages=8"
    datas = {"user_id":user_id,"access_level":access_level}
    url = 'http://gitlab.com/api/v4/projects/%s/members'%project_id
    headers = {"Authorization": "bearer 7df71632701daaf11fec128ca071a26229b99462d2e20ba9ff48c6e8c953540d",
               "Content-Type": "application/x-www-form-urlencoded"}
    print(url)

    result = requests.post(url, headers=headers,data=datas)
    print("项目:%s 已添加用户"%project_id)


def get_pagenum(url):
    '''
    获取所有项目id
    :param name:
    :param description:
    :return:
    '''
    #url = "http://gitlab.com/api/v4/projects?per_page=100&pages=8"
    headers = {"Authorization": "bearer 7df71632701daaf11fec128ca071a26229b99462d2e20ba9ff48c6e8c953540d",
               "Content-Type": "application/x-www-form-urlencoded"}

    headers = {"Authorization": "bearer 7df71632701daaf11fec128ca071a26229b99462d2e20ba9ff48c6e8c953540d",
               "Content-Type": "application/x-www-form-urlencoded"}
    result = requests.get(url, headers=headers)
    #result = json.loads(result.content)
    print(result.content)
    result = json.loads(result.content)
    for re in result:
        #un_protect(re.get("id"))
        #print(re)
        print(re.get("id"))
        add_user(str(re.get("id")),"209","20")
#get_pagenum(url = "http://gitlab.com/api/v4/projects?membership=false&order_by=created_at&owned=false&page=7&per_page=100&simple=false&sort=desc&starred=false&statistics=false&with_custom_attributes=false&with_issues_enabled=false&with_merge_requests_enabled=false")

for i in range(1,8):
    url = "http://gitlab.com/api/v4/projects?membership=false&order_by=created_at&owned=false&page=%d&per_page=100&simple=false&sort=desc&starred=false&statistics=false&with_custom_attributes=false&with_issues_enabled=false&with_merge_requests_enabled=false"%i
    get_pagenum(url)





user_id = "209"
access_level = "20"
#add_user(id,user_id,access_level)

猜你喜欢

转载自www.cnblogs.com/liruixin/p/10896123.html