zoukankan      html  css  js  c++  java
  • GitLab获取人员参与项目-贡献项目列表

    前言

    最近在做的统计报表项目包含人员代码提交量。
    要获取人员代码提交量首先要知道人员参与的项目。GitLab个人页面中有Contributed projects页面,如下图:

    遗憾的是GitLab api里没有相应的API接口。
    目前的做法只能通过页面爬取。做法是使用接口登录,然后访问该页面接口,然后解析数据。

    获取token

    GitLab登录页面是有csrf_token保护的,因此要先从signin页面抓取authenticity_token。页面中有多个authenticity_token,不过都是一样的。

    import requests
    import lxml
    
    signin_url = 'http://gitlab服务地址/users/sign_in'
    session = requests.session() 
    res = session.get(signin_url)
    html = etree.HTML(res.text)
    token, = html.xpath('//form[@id="new_ldap_user"]//input[@name="authenticity_token"]//@value') or ['']
    

    使用session是为了保持登录状态,这里使用lxml解析并获取token,首先要使用pip install lxml安装。也可以使用正则或者bs4进行解析。

    登录

    然后使用authenticity_token及用户名密码进行登录。

    # ldap登录
    login_url = 'http://gitlab服务地址/users/auth/ldapmain/callback'
    data = {'utf8': '✓', 'authenticity_token': token, 'username': '登录用户名', 'password': '登录密码'}
    session.post(login_url, data=data)
    

    我们这里是使用ldap登录的,使用普通方式登录的也一样,换下login_url即可。

    获取用户参与项目

    url = f'http://gitlab服务地址/users/{username}/contributed.json'
    res = session.get(url)
    html = etree.HTML(res.json()['html'])
    _projects = html.xpath('//span[@class="project-full-name"]')
    projects = [project.xpath('string(.)').replace('
    ', '').replace(' ', '') for project in _projects]
    

    这里面不是直接请求的用户贡献项目页面,而是加了.json的AJAX请求地址。使用XPath的strings()函数获取当前节点下的所有文本,并替换掉换行、空格。
    projects便是解析出的用户参与项目列表。

    完整代码

    import requests
    import lxml
    
    GITLAB_BASE_URL = 'gitlab服务地址'
    
    def gitlab_login(username, password):
        """登录gitlab返回登录后的session"""
        # 从页面获取csrf_token
        signin_url = 'GITLAB_BASE_URL/users/sign_in'
        session = requests.session()
        res = session.get(signin_url)
        html = etree.HTML(res.text)
        token, = html.xpath('//form[@id="new_ldap_user"]//input[@name="authenticity_token"]//@value') or ['']
        # ldap登录
        login_url = 'GITLAB_BASE_URL/users/auth/ldapmain/callback'
        data = {'utf8': '✓', 'authenticity_token': token, 'username': username, 'password': password}
        session.post(login_url, data=data)
        return session
    
    def get_user_contributed(username):
        """从Gitlab获取人员贡献项目"""
        session = gitlab_login('登录用户名', '登录密码')
        url = f'GITLAB_BASE_URL/users/{username}/contributed.json'
        res = session.get(url)
        html = etree.HTML(res.json()['html'])
        _projects = html.xpath('//span[@class="project-full-name"]')
        projects = [project.xpath('string(.)').replace('
    ', '').replace(' ', '') for project in _projects]
        print(projects)
        return projects
    
    if __name__ == '__main__':
        get_user_contributed('GitLab用户名')
    
    
  • 相关阅读:
    asp.net发布网站的时候三个选项
    Web Service 异常处理
    Asp.net 缓存技术总结
    自定义控件:广告内容后期加载。以及NamingContainer层次的应用
    图片显示时加水印(不改变原图片)
    .NET 2.0中的企业库异常处理块简述
    .NET反射、委托技术与设计模式
    三角函数Table.AddColumn(Power Query 之 M 语言)
    统计信息Table.AddColumn(Power Query 之 M 语言)
    提取Table.AddColumn(Power Query 之 M 语言)
  • 原文地址:https://www.cnblogs.com/superhin/p/11731404.html
Copyright © 2011-2022 走看看