🚧 Jobs 作业 API

This commit is contained in:
徐晓伟 2023-10-05 00:41:53 +08:00
parent 96b306e4bd
commit a9e01dbfdf
2 changed files with 66 additions and 0 deletions

64
jobs/jobs.go Normal file
View File

@ -0,0 +1,64 @@
package jobs
import (
"fmt"
"github.com/urfave/cli/v2"
"github.com/xanzy/go-gitlab"
"github.com/xuxiaowei-com-cn/gitlab-go/constant"
)
// Jobs 作业 API https://docs.gitlab.cn/jh/api/jobs.html
func Jobs() *cli.Command {
return &cli.Command{
Name: "jobs",
Aliases: []string{"j"},
Usage: "作业 API中文文档https://docs.gitlab.cn/jh/api/jobs.html",
Action: func(context *cli.Context) error {
var baseUrl = context.String(constant.BaseUrl)
if baseUrl == "" {
baseUrl = constant.BaseUrlDefault
}
var token = context.String(constant.Token)
var id = context.String(constant.Id)
gitClient, err := gitlab.NewClient(token, gitlab.WithBaseURL(baseUrl))
if err != nil {
return err
}
opt := &gitlab.ListJobsOptions{}
jobs, response, err := gitClient.Jobs.ListProjectJobs(id, opt)
fmt.Printf("Response StatusCode: %d\n", response.Response.StatusCode)
if err != nil {
return err
}
for index, job := range jobs {
fmt.Printf("Index: %d,\t ID: %d,\t Name: %s,\t ProjectID: %d,\t Status: %s,\t CreatedAt: %s\n", index, job.ID, job.Name, job.Project.ID, job.Status, job.CreatedAt)
}
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: constant.BaseUrl,
EnvVars: []string{"CI_API_V4_URL"},
Usage: "实例地址例如https://gitlab.xuxiaowei.com.cn/api/v4",
},
&cli.StringFlag{
Name: constant.Token,
Usage: "your_access_token",
},
&cli.StringFlag{
Name: constant.Sort,
Value: constant.SortDefault,
//Usage: "按照 asc 或者 desc默认desc对流水线排序",
},
&cli.StringFlag{
Name: constant.Id,
Usage: "项目 ID 或 URL 编码的路径",
Required: true,
},
},
}
}

View File

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"github.com/xuxiaowei-com-cn/git-go/buildinfo" "github.com/xuxiaowei-com-cn/git-go/buildinfo"
"github.com/xuxiaowei-com-cn/gitlab-go/jobs"
"github.com/xuxiaowei-com-cn/gitlab-go/pipelines" "github.com/xuxiaowei-com-cn/gitlab-go/pipelines"
"github.com/xuxiaowei-com-cn/gitlab-go/projects" "github.com/xuxiaowei-com-cn/gitlab-go/projects"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
@ -55,6 +56,7 @@ func main() {
Commands: []*cli.Command{ Commands: []*cli.Command{
projects.Projects(), projects.Projects(),
pipelines.Pipelines(), pipelines.Pipelines(),
jobs.Jobs(),
}, },
} }