Skip to content

Commit

Permalink
Merge pull request #8 from EGT-Ukraine/feature/pipeline-url-output
Browse files Browse the repository at this point in the history
trigger's job URL added to log output & urlPrefix param fix
  • Loading branch information
Vyacheslav Pryimak authored Feb 13, 2019
2 parents 93514a7 + 279b0be commit 707bc84
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func main() {
ctx.GlobalBool("skipVerifyTLS"),
schema,
ctx.GlobalString("host"),
ctx.GlobalString("urlPrefix"),
ctx.GlobalString("privateToken"),
ctx.GlobalString("token"),
ctx.GlobalString("ref"),
Expand All @@ -89,6 +90,8 @@ func main() {
log.Fatal(err)
}

log.Printf("trigger's job URL: %s", triggerRunResp.WebURL)

go func() {
time.Sleep(lifecycleTimeout)
log.Fatalf("lifecycle time(%v) has been expired", lifecycleTimeout)
Expand Down
28 changes: 18 additions & 10 deletions trigger/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ var (
)

type Trigger struct {
client *http.Client
schema Schema
host, privateToken, token, ref string
projectID int
variables []string
client *http.Client
schema Schema
host, urlPrefix, privateToken, token, ref string
projectID int
variables []string
}

func New(tlsInsecureSkipVerify bool, schema Schema, host, privateToken, token, ref string, projectID int, variables []string) *Trigger {
func New(tlsInsecureSkipVerify bool, schema Schema,
host, urlPrefix, privateToken, token, ref string, projectID int, variables []string) *Trigger {

httpTransport := &http.Transport{
DialContext: (&net.Dialer{
Timeout: httpClientTimeout,
Expand All @@ -57,7 +59,8 @@ func New(tlsInsecureSkipVerify bool, schema Schema, host, privateToken, token, r
Timeout: httpClientTimeout,
}

return &Trigger{client: client, schema: schema, host: host, privateToken: privateToken, token: token, ref: ref, projectID: projectID, variables: variables}
return &Trigger{client: client, schema: schema, host: host, urlPrefix: urlPrefix,
privateToken: privateToken, token: token, ref: ref, projectID: projectID, variables: variables}
}

func (p Trigger) RunPipeline() (*models.CreatePipelineResponse, error) {
Expand Down Expand Up @@ -196,7 +199,7 @@ func (p Trigger) urlVariables() url.Values {
}

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

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

if p.urlPrefix != "" && !strings.HasSuffix(p.urlPrefix, "/") {
p.urlPrefix = p.urlPrefix + "/"
}

var buf bytes.Buffer
if err := tpl.ExecuteTemplate(&buf, "tpl", &struct {
Schema string
Host string
ProjectID int
}{Schema: p.schemaName(p.schema), Host: p.host, ProjectID: p.projectID}); err != nil {
URLPrefix string
}{Schema: p.schemaName(p.schema), Host: p.host, URLPrefix: p.urlPrefix, ProjectID: p.projectID}); err != nil {
return "", errors.Wrap(err, "failed to execute pipeline url template")
}

Expand Down

0 comments on commit 707bc84

Please sign in to comment.