-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
71 lines (63 loc) · 1.62 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
)
type Job struct {
ID int `json:"id"`
}
func main () {
var project_id int
var per_page int
var pages int
var token string
var server string
flag.IntVar(&project_id, "project_id", 0, "Project ID")
flag.IntVar(&per_page, "per_page", 100, "Number of jobs per page")
flag.IntVar(&pages, "pages", 1, "Number of pages")
flag.StringVar(&token, "token", "", "Private token")
flag.StringVar(&server, "server", "", "Gitlab server")
flag.Parse()
fmt.Printf("Fetching %d pages from %v\n", pages, server)
for i := 1; i <= pages; i++ {
url := fmt.Sprintf("%v/api/v4/projects/%v/jobs?per_page=%d&page=%d&artifact_expired=false", server, project_id, per_page, i)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println(err)
return
}
println("page number:", i)
req.Header.Set("PRIVATE-TOKEN", token)
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
var jobs []Job
err = json.NewDecoder(resp.Body).Decode(&jobs)
if err != nil {
fmt.Println(err)
return
}
for _, job := range jobs {
fmt.Println("job to process:", job.ID)
url := fmt.Sprintf("%v/api/v4/projects/%v/jobs/%d/artifacts", server, project_id, job.ID)
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
fmt.Println(err)
return
}
req.Header.Set("PRIVATE-TOKEN", token)
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
fmt.Println("job artifacts deleted:", job.ID)
}
}
}