Skip to content

Commit

Permalink
more fiexes
Browse files Browse the repository at this point in the history
  • Loading branch information
Claire.Nicholas authored and Claire.Nicholas committed Nov 27, 2023
1 parent 135843e commit d700f0c
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions cmd/vela-slack/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,25 @@ func (p *Plugin) Validate() error {
return nil
}

// getRemoteAttachment function to open and parse slack attachment json file into
// slack webhook message payload.
func replaceString(bytes []byte, p *Plugin) []byte {
// Converts bytes into string and replaces {{ .BuildCreated }}
// with a timestamp before returning it back into bytes again.
bStr := string(bytes)
// x := strconv.Itoa(p.Env.BuildCreated)
bStr = strings.ReplaceAll(bStr, "{{ .BuildCreated }}", strconv.Itoa(p.Env.BuildCreated))
bStr = strings.ReplaceAll(bStr, "{{ .BuildEnqueued }}", strconv.Itoa(p.Env.BuildEnqueued))
bStr = strings.ReplaceAll(bStr, "{{ .BuildFinished }}", strconv.Itoa(p.Env.BuildFinished))
bStr = strings.ReplaceAll(bStr, "{{ .BuildNumber }}", strconv.Itoa(p.Env.BuildNumber))
bStr = strings.ReplaceAll(bStr, "{{ .BuildParent }}", strconv.Itoa(p.Env.BuildParent))
bStr = strings.ReplaceAll(bStr, "{{ .BuildStarted }}", strconv.Itoa(p.Env.BuildStarted))
bStr = strings.ReplaceAll(bStr, "{{ .RepositoryTimeout }}", strconv.Itoa(p.Env.RepositoryTimeout))
bytes = []byte(bStr)

return bytes
}

// getAttachmentFromFile function to open and parse json file into
// slack webhook message payload.
func getAttachmentFromFile(p *Plugin) ([]slack.Attachment, error) {
Expand All @@ -221,18 +240,7 @@ func getAttachmentFromFile(p *Plugin) ([]slack.Attachment, error) {
return nil, fmt.Errorf("unable to read json file: %w", err)
}

// Converts bytes into string and replaces {{ .BuildCreated }}
// with a timestamp before returning it back into bytes again.
bStr := string(bytes)
// x := strconv.Itoa(p.Env.BuildCreated)
bStr = strings.Replace(bStr, "{{ .BuildCreated }}", strconv.Itoa(p.Env.BuildCreated), -1)
bStr = strings.Replace(bStr, "{{ .BuildEnqueued }}", strconv.Itoa(p.Env.BuildEnqueued), -1)
bStr = strings.Replace(bStr, "{{ .BuildFinished }}", strconv.Itoa(p.Env.BuildFinished), -1)
bStr = strings.Replace(bStr, "{{ .BuildNumber }}", strconv.Itoa(p.Env.BuildNumber), -1)
bStr = strings.Replace(bStr, "{{ .BuildParent }}", strconv.Itoa(p.Env.BuildParent), -1)
bStr = strings.Replace(bStr, "{{ .BuildStarted }}", strconv.Itoa(p.Env.BuildStarted), -1)
bStr = strings.Replace(bStr, "{{ .RepositoryTimeout }}", strconv.Itoa(p.Env.RepositoryTimeout), -1)
bytes = []byte(bStr)
bytes = replaceString(bytes, p)

// create a variable to hold our message
var msg slack.WebhookMessage
Expand Down Expand Up @@ -278,18 +286,7 @@ func getRemoteAttachment(p *Plugin) ([]slack.Attachment, error) {
return nil, err
}

// Converts bytes into string and replaces {{ .BuildCreated }}
// with a timestamp before returning it back into bytes again.
bStr := string(bytes)
// x := strconv.Itoa(p.Env.BuildCreated)
bStr = strings.Replace(bStr, "{{ .BuildCreated }}", strconv.Itoa(p.Env.BuildCreated), -1)
bStr = strings.Replace(bStr, "{{ .BuildEnqueued }}", strconv.Itoa(p.Env.BuildEnqueued), -1)
bStr = strings.Replace(bStr, "{{ .BuildFinished }}", strconv.Itoa(p.Env.BuildFinished), -1)
bStr = strings.Replace(bStr, "{{ .BuildNumber }}", strconv.Itoa(p.Env.BuildNumber), -1)
bStr = strings.Replace(bStr, "{{ .BuildParent }}", strconv.Itoa(p.Env.BuildParent), -1)
bStr = strings.Replace(bStr, "{{ .BuildStarted }}", strconv.Itoa(p.Env.BuildStarted), -1)
bStr = strings.Replace(bStr, "{{ .RepositoryTimeout }}", strconv.Itoa(p.Env.RepositoryTimeout), -1)
bytes = []byte(bStr)
bytes = replaceString(bytes, p)

// create a variable to hold our message
var msg slack.WebhookMessage
Expand Down

0 comments on commit d700f0c

Please sign in to comment.