有没有办法使用 python-gitlab 按上周的名称获取特定工作?

发布于 2025-01-15 02:34:47 字数 363 浏览 3 评论 0原文

我想要做什么:

要从 gitlab 工件创建报告,我想查看具有特定名称(在我的示例中为 lighthouse)的所有最近作业(过去一周左右)并提取所有工件。

问题:

当我知道作业 ID 时,我可以使用 python-gitlab 包从特定作业中提取工件。到目前为止,我没能做到的是扫描我的项目,查找上周名为“lighthouse”的所有作业,并以这种方式获取所有必要的作业。

问题:

有没有办法使用 gitlab-python 包解决我的问题? 我也尝试过使用curl,但据我所知,我的问题也无法用curl 解决。 有什么建议吗? :)

What I want to do:

To create reports from gitlab artifacts, I want to look at all recent jobs (the past week or so) with a certain name (lighthouse in my example) and extract all artifacts.

The problem:

I got to a point where I can extract artifacts from a specific job using the python-gitlab package, when I know the jobs ID. What I didn't manage to do so far is scan my project for all jobs in the last week with the name "lighthouse" and get all the necessary jobs this way.

The question:

Is there a way to solve my problem using the gitlab-python package?
I've also tried using curl as well, but as far as I can see my problem isn't solvable with curl either.
Any tips? :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缘字诀 2025-01-22 02:34:47

这可以使用 项目管道列表 API列出管道作业 API

使用 python-gitlab 库,它可能看起来像这样:

import datetime
from typing import Sequence
import gitlab
from gitlab.v4.objects.pipelines import ProjectPipelineJob

gl = gitlab.Gitlab('https://gitlab.com', private_token='Your API token')

def scan_for_jobs(project_id, job_name, date_threshold) -> Sequence[ProjectPipelineJob]:
    """
    Given a project id, job name and a datetime threshold,
    returns a sequence of pipeline jobs matching the name
    where the pipeline ran at or after the threshold date.
    """
    jobs = []
    project = gl.projects.get(project_id)
    for pipeline in project.pipelines.list(as_list=False):
        created_at = datetime.datetime.fromisoformat(pipeline.created_at.replace('Z', '+00:00'))
        if created_at < date_threshold:
            break
        for job in pipeline.jobs.list(as_list=False):
            if job.name == job_name:
                jobs.append(job)
    return jobs

假设我想从过去 30 个创建的管道中的项目 spyoungtech/testproject 中获取所有名为 test 的作业天。

now = datetime.datetime.now().astimezone(datetime.timezone.utc)
threshold = now - datetime.timedelta(days=30)
test_jobs = scan_for_jobs('spyoungtech/testproject', 'test', threshold)
for job in test_jobs:
   print(job.id, job.created_at)
2196256156 2022-03-12T22:31:55.342Z
2184928033 2022-03-10T00:13:35.108Z
2184923701 2022-03-10T00:11:20.513Z
2184922621 2022-03-10T00:10:55.615Z

对于你的情况,你可能会这样做:

now = datetime.datetime.now().astimezone(datetime.timezone.utc)
one_week_ago = now - datetime.timedelta(days=7)
lighthouse_jobs = scan_for_jobs(project_id=1234, 
                                job_name='lighthouse', 
                                date_threshold=one_week_ago
)
for job in lighthouse_jobs:
    download_artifacts(job)  # you implement this

This is possible using the project pipelines list API and list pipeline jobs API.

With the python-gitlab library, it might look something like this:

import datetime
from typing import Sequence
import gitlab
from gitlab.v4.objects.pipelines import ProjectPipelineJob

gl = gitlab.Gitlab('https://gitlab.com', private_token='Your API token')

def scan_for_jobs(project_id, job_name, date_threshold) -> Sequence[ProjectPipelineJob]:
    """
    Given a project id, job name and a datetime threshold,
    returns a sequence of pipeline jobs matching the name
    where the pipeline ran at or after the threshold date.
    """
    jobs = []
    project = gl.projects.get(project_id)
    for pipeline in project.pipelines.list(as_list=False):
        created_at = datetime.datetime.fromisoformat(pipeline.created_at.replace('Z', '+00:00'))
        if created_at < date_threshold:
            break
        for job in pipeline.jobs.list(as_list=False):
            if job.name == job_name:
                jobs.append(job)
    return jobs

Suppose I wanted to get all jobs named test from the project spyoungtech/testproject from pipelines created in the last 30 days.

now = datetime.datetime.now().astimezone(datetime.timezone.utc)
threshold = now - datetime.timedelta(days=30)
test_jobs = scan_for_jobs('spyoungtech/testproject', 'test', threshold)
for job in test_jobs:
   print(job.id, job.created_at)
2196256156 2022-03-12T22:31:55.342Z
2184928033 2022-03-10T00:13:35.108Z
2184923701 2022-03-10T00:11:20.513Z
2184922621 2022-03-10T00:10:55.615Z

In your case, you might do:

now = datetime.datetime.now().astimezone(datetime.timezone.utc)
one_week_ago = now - datetime.timedelta(days=7)
lighthouse_jobs = scan_for_jobs(project_id=1234, 
                                job_name='lighthouse', 
                                date_threshold=one_week_ago
)
for job in lighthouse_jobs:
    download_artifacts(job)  # you implement this
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文