代码示例

要使用 Python 和 python-gitlab 库向 GitLab 仓库提交文件,你需要按照以下步骤操作:

  1. 安装 python-gitlab
  • 你可以使用 pip 安装 python-gitlab
pip install python-gitlab
  • 1.
  1. 获取 GitLab 个人访问令牌 (PAT)
  • 登录 GitLab 并获取个人访问令牌 (PAT),确保它具有足够的权限来创建和提交文件。
  1. 编写 Python 脚本
  • 创建一个 Python 脚本,使用 python-gitlab 库与 GitLab 交互。

以下是一个示例脚本,它将向 GitLab 仓库提交一个名为 time.txt 的文件,内容为当前系统时间:

import gitlab
import datetime

# GitLab URL
GITLAB_URL = "https://your-gitlab-instance.com"
# GitLab 个人访问令牌
GITLAB_TOKEN = "your-personal-access-token"
# GitLab 项目路径
PROJECT_PATH = "user/ntp-time-sync"
# 文件名
FILE_NAME = "time.txt"
# 分支名称
BRANCH_NAME = "main"

# 创建 GitLab API 客户端
gl = gitlab.Gitlab(GITLAB_URL, private_token=GITLAB_TOKEN)
gl.auth()

# 获取项目
project = gl.projects.get(PROJECT_PATH)

# 获取当前系统时间
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

# 创建或更新文件
try:
    # 尝试获取现有文件
    file = project.files.get(file_path=FILE_NAME, ref=BRANCH_NAME)
except gitlab.exceptions.GitlabGetError as e:
    # 如果文件不存在,则创建新文件
    file = project.files.create(
        {
            "file_path": FILE_NAME,
            "branch": BRANCH_NAME,
            "content": current_time,
            "commit_message": "Add time.txt",
        }
    )
else:
    # 如果文件存在,则更新文件内容
    file.content = current_time
    file.save(branch=BRANCH_NAME, commit_message="Update time.txt")

print(f"File '{FILE_NAME}' has been created/updated.")
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
注意
  • 请确保替换 GITLAB_URLGITLAB_TOKEN 为实际的 GitLab 实例 URL 和个人访问令牌。
  • 本示例假设仓库中存在 main 分支。如果使用其他分支,请相应地调整 BRANCH_NAME 参数。
运行脚本

保存上述脚本到一个 .py 文件,例如 update_time.py,然后运行该脚本:

python update_time.py
  • 1.
总结

这个脚本将会向 GitLab 中的 ntp-time-sync 仓库提交一个 time.txt 文件,文件内容为当前系统时间。

代码完善

解析响应以提取 time.txt 的更新时间,并将更新时间重新写入 time.txt 文件,并重新commit:

  1. 提交文件后获取提交信息
  • 在提交文件后,使用 GitLab API 获取最新的提交信息。
  • 提取提交信息中的 committed_date 字段作为更新时间。
  1. 更新文件内容
  • 将提取到的更新时间写入 time.txt 文件。

以下是完整的 Python 脚本示例:

import gitlab
import datetime

# GitLab URL
GITLAB_URL = "https://your-gitlab-instance.com"
# GitLab 个人访问令牌
GITLAB_TOKEN = "your-personal-access-token"
# GitLab 项目路径
PROJECT_PATH = "user/ntp-time-sync"
# 文件名
FILE_NAME = "time.txt"
# 分支名称
BRANCH_NAME = "main"

# 创建 GitLab API 客户端
gl = gitlab.Gitlab(GITLAB_URL, private_token=GITLAB_TOKEN)
gl.auth()

# 获取项目
project = gl.projects.get(PROJECT_PATH)

# 获取当前系统时间
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

# 创建或更新文件
try:
    # 尝试获取现有文件
    file = project.files.get(file_path=FILE_NAME, ref=BRANCH_NAME)
except gitlab.exceptions.GitlabGetError as e:
    # 如果文件不存在,则创建新文件
    file = project.files.create(
        {
            "file_path": FILE_NAME,
            "branch": BRANCH_NAME,
            "content": current_time,
            "commit_message": "Add time.txt",
        }
    )
else:
    # 如果文件存在,则更新文件内容
    file.content = current_time
    file.save(branch=BRANCH_NAME, commit_message="Update time.txt")

# 获取最新的提交信息
latest_commit = project.commits.get(ref_name=BRANCH_NAME)

# 提取提交信息中的更新时间
commit_timestamp = latest_commit.committed_date

# 将更新时间写入文件
file.content = commit_timestamp.strftime("%Y-%m-%d %H:%M:%S")
file.save(branch=BRANCH_NAME, commit_message="Update time.txt with commit timestamp")

print(f"File '{FILE_NAME}' has been created/updated with the commit timestamp.")
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.