|
| 1 | +package gogitlab |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "io" |
| 6 | +) |
| 7 | + |
| 8 | +const ( |
| 9 | + project_builds = "/projects/:id/builds" // List project builds |
| 10 | + project_build = "/projects/:id/builds/:build_id" // Get a single build |
| 11 | + project_commit_builds = "/projects/:id/repository/commits/:sha/builds" // List commit builds |
| 12 | + project_build_artifacts = "/projects/:id/builds/:build_id/artifacts" // Get build artifacts |
| 13 | + project_build_cancel = "/projects/:id/builds/:build_id/cancel" // Cancel a build |
| 14 | + project_build_retry = "/projects/:id/builds/:build_id/retry" // Retry a build |
| 15 | + project_build_erase = "/projects/:id/builds/:build_id/erase" // Erase a build |
| 16 | +) |
| 17 | + |
| 18 | +type ArtifactsFile struct { |
| 19 | + Filename string `json:"filename"` |
| 20 | + Size int `json:"size"` |
| 21 | +} |
| 22 | + |
| 23 | +type Build struct { |
| 24 | + Id int `json:"id"` |
| 25 | + ArtifactsFile ArtifactsFile `json:"artifacts_file"` |
| 26 | + Commit Commit `json:"commit"` |
| 27 | + CreatedAt string `json:"created_at"` |
| 28 | + DownloadURL string `json:"download_url"` |
| 29 | + FinishedAt string `json:"finished_at"` |
| 30 | + Name string `json:"name"` |
| 31 | + Ref string `json:"ref"` |
| 32 | + Stage string `json:"stage"` |
| 33 | + StartedAt string `json:"started_at"` |
| 34 | + Status string `json:"status"` |
| 35 | + Tag bool `json:"tag"` |
| 36 | + User User `json:"user"` |
| 37 | +} |
| 38 | + |
| 39 | +func (g *Gitlab) ProjectBuilds(id string) ([]*Build, error) { |
| 40 | + url, opaque := g.ResourceUrlRaw(project_builds, map[string]string{ |
| 41 | + ":id": id, |
| 42 | + }) |
| 43 | + |
| 44 | + builds := make([]*Build, 0) |
| 45 | + |
| 46 | + contents, err := g.buildAndExecRequestRaw("GET", url, opaque, nil) |
| 47 | + if err != nil { |
| 48 | + return builds, err |
| 49 | + } |
| 50 | + |
| 51 | + err = json.Unmarshal(contents, &builds) |
| 52 | + |
| 53 | + return builds, err |
| 54 | +} |
| 55 | + |
| 56 | +func (g *Gitlab) ProjectCommitBuilds(id, sha1 string) ([]*Build, error) { |
| 57 | + url, opaque := g.ResourceUrlRaw(project_commit_builds, map[string]string{ |
| 58 | + ":id": id, |
| 59 | + ":sha": sha1, |
| 60 | + }) |
| 61 | + |
| 62 | + builds := make([]*Build, 0) |
| 63 | + |
| 64 | + contents, err := g.buildAndExecRequestRaw("GET", url, opaque, nil) |
| 65 | + if err != nil { |
| 66 | + return builds, err |
| 67 | + } |
| 68 | + |
| 69 | + err = json.Unmarshal(contents, &builds) |
| 70 | + |
| 71 | + return builds, err |
| 72 | +} |
| 73 | + |
| 74 | +func (g *Gitlab) ProjectBuild(id, buildId string) (*Build, error) { |
| 75 | + url, opaque := g.ResourceUrlRaw(project_build, map[string]string{ |
| 76 | + ":id": id, |
| 77 | + ":build_id": buildId, |
| 78 | + }) |
| 79 | + |
| 80 | + build := &Build{} |
| 81 | + |
| 82 | + contents, err := g.buildAndExecRequestRaw("GET", url, opaque, nil) |
| 83 | + if err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + |
| 87 | + err = json.Unmarshal(contents, &build) |
| 88 | + |
| 89 | + return build, err |
| 90 | +} |
| 91 | + |
| 92 | +func (g *Gitlab) ProjectBuildArtifacts(id, buildId string) (io.ReadCloser, error) { |
| 93 | + url, _ := g.ResourceUrlRaw(project_build_artifacts, map[string]string{ |
| 94 | + ":id": id, |
| 95 | + ":build_id": buildId, |
| 96 | + }) |
| 97 | + |
| 98 | + resp, err := g.execRequest("GET", url, nil) |
| 99 | + |
| 100 | + if err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + |
| 104 | + return resp.Body, nil |
| 105 | +} |
| 106 | + |
| 107 | +func (g *Gitlab) ProjectCancelBuild(id, buildId string) (*Build, error) { |
| 108 | + url, opaque := g.ResourceUrlRaw(project_build_cancel, map[string]string{ |
| 109 | + ":id": id, |
| 110 | + ":build_id": buildId, |
| 111 | + }) |
| 112 | + |
| 113 | + build := &Build{} |
| 114 | + |
| 115 | + contents, err := g.buildAndExecRequestRaw("POST", url, opaque, nil) |
| 116 | + if err != nil { |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + |
| 120 | + err = json.Unmarshal(contents, &build) |
| 121 | + |
| 122 | + return build, err |
| 123 | +} |
| 124 | + |
| 125 | +func (g *Gitlab) ProjectRetryBuild(id, buildId string) (*Build, error) { |
| 126 | + url, opaque := g.ResourceUrlRaw(project_build_retry, map[string]string{ |
| 127 | + ":id": id, |
| 128 | + ":build_id": buildId, |
| 129 | + }) |
| 130 | + |
| 131 | + build := &Build{} |
| 132 | + |
| 133 | + contents, err := g.buildAndExecRequestRaw("POST", url, opaque, nil) |
| 134 | + if err != nil { |
| 135 | + return nil, err |
| 136 | + } |
| 137 | + |
| 138 | + err = json.Unmarshal(contents, &build) |
| 139 | + |
| 140 | + return build, err |
| 141 | +} |
| 142 | + |
| 143 | +func (g *Gitlab) ProjectEraseBuild(id, buildId string) (*Build, error) { |
| 144 | + url, opaque := g.ResourceUrlRaw(project_build_erase, map[string]string{ |
| 145 | + ":id": id, |
| 146 | + ":build_id": buildId, |
| 147 | + }) |
| 148 | + |
| 149 | + build := &Build{} |
| 150 | + |
| 151 | + contents, err := g.buildAndExecRequestRaw("POST", url, opaque, nil) |
| 152 | + if err != nil { |
| 153 | + return nil, err |
| 154 | + } |
| 155 | + |
| 156 | + err = json.Unmarshal(contents, &build) |
| 157 | + |
| 158 | + return build, err |
| 159 | +} |
0 commit comments