Skip to content

Commit 707bc84

Browse files
author
Vyacheslav Pryimak
authored
Merge pull request #8 from EGT-Ukraine/feature/pipeline-url-output
trigger's job URL added to log output & urlPrefix param fix
2 parents 93514a7 + 279b0be commit 707bc84

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func main() {
7777
ctx.GlobalBool("skipVerifyTLS"),
7878
schema,
7979
ctx.GlobalString("host"),
80+
ctx.GlobalString("urlPrefix"),
8081
ctx.GlobalString("privateToken"),
8182
ctx.GlobalString("token"),
8283
ctx.GlobalString("ref"),
@@ -89,6 +90,8 @@ func main() {
8990
log.Fatal(err)
9091
}
9192

93+
log.Printf("trigger's job URL: %s", triggerRunResp.WebURL)
94+
9295
go func() {
9396
time.Sleep(lifecycleTimeout)
9497
log.Fatalf("lifecycle time(%v) has been expired", lifecycleTimeout)

trigger/trigger.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@ var (
3434
)
3535

3636
type Trigger struct {
37-
client *http.Client
38-
schema Schema
39-
host, privateToken, token, ref string
40-
projectID int
41-
variables []string
37+
client *http.Client
38+
schema Schema
39+
host, urlPrefix, privateToken, token, ref string
40+
projectID int
41+
variables []string
4242
}
4343

44-
func New(tlsInsecureSkipVerify bool, schema Schema, host, privateToken, token, ref string, projectID int, variables []string) *Trigger {
44+
func New(tlsInsecureSkipVerify bool, schema Schema,
45+
host, urlPrefix, privateToken, token, ref string, projectID int, variables []string) *Trigger {
46+
4547
httpTransport := &http.Transport{
4648
DialContext: (&net.Dialer{
4749
Timeout: httpClientTimeout,
@@ -57,7 +59,8 @@ func New(tlsInsecureSkipVerify bool, schema Schema, host, privateToken, token, r
5759
Timeout: httpClientTimeout,
5860
}
5961

60-
return &Trigger{client: client, schema: schema, host: host, privateToken: privateToken, token: token, ref: ref, projectID: projectID, variables: variables}
62+
return &Trigger{client: client, schema: schema, host: host, urlPrefix: urlPrefix,
63+
privateToken: privateToken, token: token, ref: ref, projectID: projectID, variables: variables}
6164
}
6265

6366
func (p Trigger) RunPipeline() (*models.CreatePipelineResponse, error) {
@@ -196,7 +199,7 @@ func (p Trigger) urlVariables() url.Values {
196199
}
197200

198201
func (p Trigger) createPipelineTpl() (*template.Template, error) {
199-
urlTpl := "{{.Schema}}://{{.Host}}/api/v4/projects/{{.ProjectID}}/trigger/pipeline"
202+
urlTpl := "{{.Schema}}://{{.Host}}/{{.URLPrefix}}api/v4/projects/{{.ProjectID}}/trigger/pipeline"
200203
tpl, err := template.New("tpl").Parse(urlTpl)
201204
if err != nil {
202205
return nil, errors.New("failed to parse pipeline url template")
@@ -206,7 +209,7 @@ func (p Trigger) createPipelineTpl() (*template.Template, error) {
206209
}
207210

208211
func (p Trigger) pollPipelineTpl() (*template.Template, error) {
209-
urlTpl := "{{.Schema}}://{{.Host}}/api/v4/projects/{{.ProjectID}}/pipelines"
212+
urlTpl := "{{.Schema}}://{{.Host}}/{{.URLPrefix}}api/v4/projects/{{.ProjectID}}/pipelines"
210213
tpl, err := template.New("tpl").Parse(urlTpl)
211214
if err != nil {
212215
return nil, errors.New("failed to parse pipeline url template")
@@ -220,12 +223,17 @@ func (p *Trigger) urlByTemplate(tpl *template.Template) (string, error) {
220223
p.host = DefaultHost
221224
}
222225

226+
if p.urlPrefix != "" && !strings.HasSuffix(p.urlPrefix, "/") {
227+
p.urlPrefix = p.urlPrefix + "/"
228+
}
229+
223230
var buf bytes.Buffer
224231
if err := tpl.ExecuteTemplate(&buf, "tpl", &struct {
225232
Schema string
226233
Host string
227234
ProjectID int
228-
}{Schema: p.schemaName(p.schema), Host: p.host, ProjectID: p.projectID}); err != nil {
235+
URLPrefix string
236+
}{Schema: p.schemaName(p.schema), Host: p.host, URLPrefix: p.urlPrefix, ProjectID: p.projectID}); err != nil {
229237
return "", errors.Wrap(err, "failed to execute pipeline url template")
230238
}
231239

0 commit comments

Comments
 (0)