🚧 Jobs 作业 API
This commit is contained in:
parent
96b306e4bd
commit
a9e01dbfdf
|
@ -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,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
2
main.go
2
main.go
|
@ -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(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue