Gitlab用户列表API使用Python和Amazon S3

huangapple go评论74阅读模式
英文:

Gitlab User list API with Python and Amazon S3

问题

我正在尝试从我们的私有GitLab服务器实例中提取完整的用户列表,并将其输入到一个S3存储桶中,以供我们需要时进行引用。最终,我会使用Lambda/cfn的某种形式来定期删除它并重新运行它,以在每周更新它。我在Python方面不是很擅长,目前这是我的代码:

import json
import boto3
import re
import os
import sys
import botocore
import urllib3
from pprint import pprint

sess = boto3.Session(profile_name="sso-profile-here")

s3_client = sess.client("s3")
bucket_name = "user-statistics"

http = urllib3.PoolManager()

baseuri = "https://git.tools.dev.mycompany.net/api/v4/"

access_token = "access-token-code"

def get_gitlab_users(access_token=access_token, baseuri=baseuri):
    headers = {
        "Content-Type": "application/json",
        "Authorization": "Bearer {}".format(access_token),
    }
    url = "{}/users/?per_page=100&active=true&without_project_bots=true&next_page=x-next-page".format(
        baseuri
    )
    req = http.request(method="GET", url=url, headers=headers)
    result = json.loads(req.data)
    s3_client.put_object(
        Bucket=bucket_name, Key="get_users_gitlab.json", Body=json.dumps(result)
    )

if __name__ == "__main__":
    get_gitlab_users(access_token=access_token, baseuri=baseuri)

我希望能够在S3存储桶中获取每个页面上的所有用户,并且还希望在S3存储桶中对其进行一些格式化,当我从存储桶中下载时,格式看起来非常难以阅读,我不确定是否可以改进它,有人能提出我可以做的建议吗?

请忽略访问令牌直接存储在代码中的事实,这仅用于测试阶段,我将确保它不会直接存储在代码中。

提前感谢任何建议。

英文:

I'm trying to pull a complete user list from our private gitlab server instance and input it into an S3 Bucket to reference whenever we need. Eventually I will have some form of Lambda/cfn deleting it and running it again every week to update it. I'm not so great with Python and this is what I have so far..

import json
import boto3
import re
import os
import sys
import botocore
import urllib3
from pprint import pprint

sess = boto3.Session(profile_name="sso-profile-here")

s3_client = sess.client("s3")
bucket_name = "user-statistics"

http = urllib3.PoolManager()


baseuri = "https://git.tools.dev.mycompany.net/api/v4/"


access_token = "access-token-code"



def get_gitlab_users(access_token=access_token, baseuri=baseuri):
    headers = {
        "Content-Type": "application/json",
        "Authorization": "Bearer {}".format(access_token),
    }
    url = "{}/users/?per_page=100&active=true&without_project_bots=true&next_page=x-next-page".format(
        baseuri
    )
    req = http.request(method="GET", url=url, headers=headers)
    result = json.loads(req.data)
    s3_client.put_object(
        Bucket=bucket_name, Key="get_users_gitlab.json", Body=json.dumps(result)
    )


if __name__ == "__main__":
    get_gitlab_users(access_token=access_token, baseuri=baseuri)

What I would like to be able to do is pull all the users on each page and also format it a bit neater in the S3 bucket, When I download it from the bucket the format is really unreadable and I'm not sure if I can improve it, can anyone suggest anything I can do?

Please also ignore the fact my access token is directly in the code here, it's for testing at this stage and I will make sure it's not stored directly in code.

Thanks in advance for any suggestions.

答案1

得分: 0

你可以尝试使用 python-gitlab 包来代替 requests。这样获取用户信息应该会更容易:

import gitlab

baseuri = "https://git.tools.dev.mycompany.net"
access_token = "access-token-code"

gl = gitlab.Gitlab(baseuri, private_token=access_token)

users = [user.asdict() for user in gl.users.list()]
users
# [{'id': 1,
#   'username': 'username1',
#   'name': 'name1',
#   'state': 'active',
#   'avatar_url': 'https://avatar.com/1',
#   'web_url': 'https://git.tools.dev.mycompany.net/username1'},
# ...]
英文:

You can try to use python-gitlab package instead of requests. It should be a lot easier to get user infos :

import gitlab

baseuri = "https://git.tools.dev.mycompany.net"
access_token = "access-token-code"

gl = gitlab.Gitlab(baseuri , private_token=access_token)

users = [user.asdict() for user in gl.users.list()]
users
# [{'id': 1,
#  'username': 'username1',
#  'name': 'name1',
#  'state': 'active',
#  'avatar_url': 'https://avatar.com/1',
#  'web_url': 'https://git.tools.dev.mycompany.net/username1'},
# ...]

huangapple
  • 本文由 发表于 2023年1月9日 19:26:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056605.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定