提交 b9d2f2d9 编写于 作者: 徐晓伟's avatar 徐晓伟

删除作业(删除作业产物和作业日志)

上级 aeaa157e
......@@ -140,7 +140,7 @@ go run main.go
- Linux 环境为 $xxx
```shell
go run main.go help
go run main.go --help
```
```shell
......@@ -374,20 +374,26 @@ COPYRIGHT:
COMMANDS:
list 列出项目作业
erase 删除作业(删除作业产物和作业日志)
help, h Shows a list of commands or help for one command
OPTIONS:
--base-url value 实例地址,例如:https://gitlab.xuxiaowei.com.cn/api/v4 (default: "https://gitlab.com/api/v4") [%CI_API_V4_URL%]
--token value your_access_token
--page value 页码(默认:1),中文文档 https://docs.gitlab.cn/jh/api/rest/index.html#pagination (default: 1)
--per-page value 每页列出的项目数(默认:20;最大:100),中文文档 https://docs.gitlab.cn/jh/api/rest/index.html#pagination (default: 20)
--print-json 打印 JSON (default: false)
--print-time 打印时间 (default: false)
--recursion 递归 (default: false)
--sort value 按照 asc 或者 desc 排序 (default: "desc")
--id value 项目 ID 或 URL 编码的路径
--scope value 要显示的作业范围。以下之一或数组:created、pending、running、failed、success、canceled、skipped、waiting_for_resource 或 manual。范围如果未提供,则返回所有作业。
--help, -h show help
--base-url value 实例地址,例如:https://gitlab.xuxiaowei.com.cn/api/v4 (default: "https://gitlab.com/api/v4") [%CI_API_V4_URL%]
--token value your_access_token
--page value 页码(默认:1),中文文档 https://docs.gitlab.cn/jh/api/rest/index.html#pagination (default: 1)
--per-page value 每页列出的项目数(默认:20;最大:100),中文文档 https://docs.gitlab.cn/jh/api/rest/index.html#pagination (default: 20)
--print-json 打印 JSON (default: false)
--print-time 打印时间 (default: false)
--recursion 递归 (default: false)
--sort value 按照 asc 或者 desc 排序 (default: "desc")
--id value 项目 ID 或 URL 编码的路径
--job-id-range value [ --job-id-range value ] Job ID的范围,支持范围如下:
单数:1
多个数字(使用英文逗号隔开):1,2,3,7,8,15
支持范围:5-10,
支持范围方向选择:-10(小于等于10,即:从 0 到 10),214-(大于等于214,即:从 214 到 214 + 10000,数据范围不超过 10000)
--scope value 要显示的作业范围。以下之一或数组:created、pending、running、failed、success、canceled、skipped、waiting_for_resource 或 manual。范围如果未提供,则返回所有作业。
--help, -h show help
```
- [pipeline - 流水线 API](https://docs.gitlab.cn/jh/api/pipelines.html)
......
......@@ -5,6 +5,7 @@ const (
BaseUrl = "base-url"
BaseUrlDefault = "https://gitlab.com/api/v4"
Id = "id"
JobIdRange = "job-id-range"
IIdRange = "iid-range"
Repository = "repository"
TagName = "tag-name"
......
......@@ -73,6 +73,18 @@ func Id(required bool) cli.Flag {
}
}
func JobIdRange(required bool) cli.Flag {
return &cli.StringSliceFlag{
Name: constant.JobIdRange,
Usage: "Job ID的范围,支持范围如下:\n\t" +
"单数:1\n\t" +
"多个数字(使用英文逗号隔开):1,2,3,7,8,15\n\t" +
"支持范围:5-10,\n\t" +
fmt.Sprintf("支持范围方向选择:-10(小于等于10,即:从 0 到 10),214-(大于等于214,即:从 214 到 214 + %d,数据范围不超过 %d)", RangeMaxInterval, RangeMaxInterval),
Required: required,
}
}
func IIdRange(required bool) cli.Flag {
return &cli.StringSliceFlag{
Name: constant.IIdRange,
......
package jobs
import (
"github.com/urfave/cli/v2"
"github.com/xanzy/go-gitlab"
"github.com/xuxiaowei-com-cn/gitlab-go/constant"
"github.com/xuxiaowei-com-cn/gitlab-go/flag"
"github.com/xuxiaowei-com-cn/gitlab-go/utils"
"log"
"sort"
)
// Erase 删除作业 https://docs.gitlab.cn/jh/api/jobs.html#%E5%88%A0%E9%99%A4%E4%BD%9C%E4%B8%9A
func Erase() *cli.Command {
return &cli.Command{
Name: "erase",
Usage: "删除作业(删除作业产物和作业日志)",
Flags: append(flag.CommonTokenRequired(), flag.Id(true), flag.JobIdRange(true)),
Action: func(context *cli.Context) error {
var baseUrl = context.String(constant.BaseUrl)
var token = context.String(constant.Token)
var id = context.String(constant.Id)
var jobIdRanges = context.StringSlice(constant.JobIdRange)
gitClient, err := gitlab.NewClient(token, gitlab.WithBaseURL(baseUrl))
if err != nil {
return err
}
jobIds := utils.Unique(utils.RangeInt(jobIdRanges))
sort.Ints(jobIds)
err = EraseJobsPrintln(gitClient, id, jobIds)
if err != nil {
return err
}
return nil
},
}
}
func EraseJobsPrintln(gitClient *gitlab.Client, id string, jobIds []int) error {
// 数字在上方已排序
for _, jobId := range jobIds {
job, response, err := gitClient.Jobs.EraseJob(id, jobId)
if err != nil {
log.Println(err)
continue
}
log.Printf("Delete %s Job %d Response %v:\n%v", id, jobId, response, job)
}
return nil
}
......@@ -17,9 +17,10 @@ func Jobs() *cli.Command {
Aliases: []string{"jobs", "j"},
Usage: "作业 API,中文文档:https://docs.gitlab.cn/jh/api/jobs.html",
Flags: append(flag.Common(), flag.Page(), flag.PerPage(), flag.PrintJson(), flag.PrintTime(), flag.Recursion(),
flag.Sort(), flag.Id(false), flag.Scope(ScopeValue, ScopeUsage)),
flag.Sort(), flag.Id(false), flag.JobIdRange(false), flag.Scope(ScopeValue, ScopeUsage)),
Subcommands: []*cli.Command{
List(),
Erase(),
},
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册