gitlab-go/mix/delete_artifacts.go

176 lines
4.5 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package mix
import (
"fmt"
"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/pipelines"
"github.com/xuxiaowei-com-cn/gitlab-go/utils"
"log"
"sort"
)
// DeleteArtifacts 删除产物
func DeleteArtifacts() *cli.Command {
return &cli.Command{
Name: "artifact",
Aliases: []string{"artifacts"},
Usage: "根据项目路径/ID、流水线IID范围删除产物混合命令多接口命令立即删除",
Flags: append(flag.CommonTokenRequired(), flag.Sort(), flag.Page(), flag.PerPage(), flag.Id(true), flag.IIdRange(true)),
Action: func(context *cli.Context) error {
var baseUrl = context.String(constant.BaseUrl)
var token = context.String(constant.Token)
var sortStr = context.String(constant.Sort)
var id = context.String(constant.Id)
var page = context.Int(constant.Page)
var perPage = context.Int(constant.PerPage)
var iidRanges = context.StringSlice(constant.IIdRange)
fmt.Println(iidRanges)
gitClient, err := gitlab.NewClient(token, gitlab.WithBaseURL(baseUrl))
if err != nil {
return err
}
iids := utils.Unique(utils.RangeInt(iidRanges))
sort.Ints(iids)
fmt.Printf("%d\n", iids)
iidsLen := len(iids)
if iidsLen == 0 {
// 输入数字为空,结束程序
return nil
}
err = DeleteArtifactsRecursion(gitClient, id, page, perPage, sortStr, iids)
if err != nil {
return err
}
return nil
},
}
}
func DeleteArtifactsRecursion(gitClient *gitlab.Client, id interface{}, page int, perPage int, sort string, iids []int) error {
pipelineInfos, response, err := pipelines.ListProjectPipelines(gitClient, id, page, perPage, sort)
if err != nil {
return err
}
log.Printf("Page %d, PerPage: %d, Response StatusCode: %d\n", page, perPage, response.Response.StatusCode)
for _, pipelineInfo := range pipelineInfos {
fmt.Printf("%d\n", pipelineInfo.IID)
// 数字在上方已排序
// 最小值
iidsMin := iids[0]
// 最大值
iidsMax := iids[len(iids)-1]
if iidsMin == pipelineInfo.IID {
// 等于最小值,删除最小值
iids = iids[1:]
err = ExecuteDeleteArtifacts(gitClient, id, pipelineInfo.ID, 1, 100)
if err != nil {
return err
}
} else if pipelineInfo.IID == iidsMax {
// 等于最大值
iids = iids[:len(iids)-1]
err = ExecuteDeleteArtifacts(gitClient, id, pipelineInfo.ID, 1, 100)
if err != nil {
return err
}
} else if iidsMin < pipelineInfo.IID {
// 大于最小值
err = artifactsForExecute(&iids, pipelineInfo.IID, gitClient, id, pipelineInfo.ID)
if err != nil {
return err
}
} else if pipelineInfo.IID < iidsMax {
// 小于最大值
err = artifactsForExecute(&iids, pipelineInfo.IID, gitClient, id, pipelineInfo.ID)
if err != nil {
return err
}
} else {
// 不在合法范围内
return nil
}
iidsLen := len(iids)
if iidsLen == 0 {
// 输入数字已处理完成,跳出循环
return nil
}
}
if len(pipelineInfos) > 0 {
err := DeleteArtifactsRecursion(gitClient, id, page+1, perPage, sort, iids)
if err != nil {
return err
}
}
return nil
}
func artifactsForExecute(iids *[]int, pipelineInfoIId int, gitClient *gitlab.Client, id interface{}, pipelineInfoId int) error {
for i := 0; i < len(*iids); i++ {
if (*iids)[i] == pipelineInfoIId {
fmt.Printf("数组中包含%d\n", pipelineInfoIId)
*iids = append((*iids)[:i], (*iids)[i+1:]...)
err := ExecuteDeleteArtifacts(gitClient, id, pipelineInfoId, 1, 100)
if err != nil {
return err
}
break
}
}
return nil
}
func ExecuteDeleteArtifacts(gitClient *gitlab.Client, id interface{}, pipelineInfoId int, page int, perPage int) error {
fmt.Printf("执行删除 %d \n", pipelineInfoId)
opt := &gitlab.ListJobsOptions{
ListOptions: gitlab.ListOptions{
Page: page,
PerPage: perPage,
},
}
jobs, response, err := gitClient.Jobs.ListPipelineJobs(id, pipelineInfoId, opt)
if err != nil {
return err
}
log.Printf("List Project %s Pipeline %d Jobs Response StatusCode: %d\n", id, pipelineInfoId, response.Response.StatusCode)
for _, job := range jobs {
response, err = gitClient.Jobs.DeleteArtifacts(id, job.ID)
if err != nil {
return err
}
log.Printf("Delete Project %s Job %d Response StatusCode: %d\n", id, job.ID, response.Response.StatusCode)
}
if len(jobs) == perPage {
err = ExecuteDeleteArtifacts(gitClient, id, pipelineInfoId, page+1, perPage)
if err != nil {
return err
}
}
return nil
}