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

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

Gitlab User list API with Python and Amazon S3

问题

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

  1. import json
  2. import boto3
  3. import re
  4. import os
  5. import sys
  6. import botocore
  7. import urllib3
  8. from pprint import pprint
  9. sess = boto3.Session(profile_name="sso-profile-here")
  10. s3_client = sess.client("s3")
  11. bucket_name = "user-statistics"
  12. http = urllib3.PoolManager()
  13. baseuri = "https://git.tools.dev.mycompany.net/api/v4/"
  14. access_token = "access-token-code"
  15. def get_gitlab_users(access_token=access_token, baseuri=baseuri):
  16. headers = {
  17. "Content-Type": "application/json",
  18. "Authorization": "Bearer {}".format(access_token),
  19. }
  20. url = "{}/users/?per_page=100&active=true&without_project_bots=true&next_page=x-next-page".format(
  21. baseuri
  22. )
  23. req = http.request(method="GET", url=url, headers=headers)
  24. result = json.loads(req.data)
  25. s3_client.put_object(
  26. Bucket=bucket_name, Key="get_users_gitlab.json", Body=json.dumps(result)
  27. )
  28. if __name__ == "__main__":
  29. 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..

  1. import json
  2. import boto3
  3. import re
  4. import os
  5. import sys
  6. import botocore
  7. import urllib3
  8. from pprint import pprint
  9. sess = boto3.Session(profile_name="sso-profile-here")
  10. s3_client = sess.client("s3")
  11. bucket_name = "user-statistics"
  12. http = urllib3.PoolManager()
  13. baseuri = "https://git.tools.dev.mycompany.net/api/v4/"
  14. access_token = "access-token-code"
  15. def get_gitlab_users(access_token=access_token, baseuri=baseuri):
  16. headers = {
  17. "Content-Type": "application/json",
  18. "Authorization": "Bearer {}".format(access_token),
  19. }
  20. url = "{}/users/?per_page=100&active=true&without_project_bots=true&next_page=x-next-page".format(
  21. baseuri
  22. )
  23. req = http.request(method="GET", url=url, headers=headers)
  24. result = json.loads(req.data)
  25. s3_client.put_object(
  26. Bucket=bucket_name, Key="get_users_gitlab.json", Body=json.dumps(result)
  27. )
  28. if __name__ == "__main__":
  29. 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。这样获取用户信息应该会更容易:

  1. import gitlab
  2. baseuri = "https://git.tools.dev.mycompany.net"
  3. access_token = "access-token-code"
  4. gl = gitlab.Gitlab(baseuri, private_token=access_token)
  5. users = [user.asdict() for user in gl.users.list()]
  6. users
  7. # [{'id': 1,
  8. # 'username': 'username1',
  9. # 'name': 'name1',
  10. # 'state': 'active',
  11. # 'avatar_url': 'https://avatar.com/1',
  12. # 'web_url': 'https://git.tools.dev.mycompany.net/username1'},
  13. # ...]
英文:

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

  1. import gitlab
  2. baseuri = "https://git.tools.dev.mycompany.net"
  3. access_token = "access-token-code"
  4. gl = gitlab.Gitlab(baseuri , private_token=access_token)
  5. users = [user.asdict() for user in gl.users.list()]
  6. users
  7. # [{'id': 1,
  8. # 'username': 'username1',
  9. # 'name': 'name1',
  10. # 'state': 'active',
  11. # 'avatar_url': 'https://avatar.com/1',
  12. # 'web_url': 'https://git.tools.dev.mycompany.net/username1'},
  13. # ...]

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:

确定