Skip to content

Commit

Permalink
Pass only commit time and change the function name
Browse files Browse the repository at this point in the history
  • Loading branch information
yzdann committed Oct 21, 2024
1 parent 7e7c02b commit 94960ef
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
8 changes: 5 additions & 3 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ Environment variables for the webhook process:

`TEMPLATES_PATH` Telefonistka uses Go templates to format GitHub PR comments, the variable override the default templates path("templates/"), useful for environments where the container workdir is overridden(like GitHub Actions) or when custom templates are desired.

`CUSTOM_COMMIT_STATUS_URL_TEMPLATE_PATH` Allows you to set a custom commit status target URL using Go templates. The template will be executed with the following dynamic parameters on runtime:
- EndTime: The time of the commit status update (now).
- StartTime: `EndTime` minus 10 minutes.
`CUSTOM_COMMIT_STATUS_URL_TEMPLATE_PATH` allows you to set a custom [commit status](https://docs.github.com/en/rest/commits/statuses?apiVersion=2022-11-28#about-commit-statuses) target URL using Go templates. The commit time will be passed as a dynamic parameter to the template. Here is an example:
```
https://custom-url.com?time={{.Time}}
```
`ARGOCD_SERVER_ADDR` Hostname and port of the ArgoCD API endpoint, like `argocd-server.argocd.svc.cluster.local:443`, default is `localhost:8080"`
Expand Down
28 changes: 13 additions & 15 deletions internal/pkg/githubapi/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ func SetCommitStatus(ghPrClientDetails GhPrClientDetails, state string) {
context := "telefonistka"
avatarURL := "https://avatars.githubusercontent.com/u/1616153?s=64"
description := "Telefonistka GitOps Bot"
targetURL := getTargetURL()
targetURL := commitStatusTargetURL(time.Now())

commitStatus := &github.RepoStatus{
TargetURL: &targetURL,
Expand Down Expand Up @@ -1217,29 +1217,27 @@ func GetFileContent(ghPrClientDetails GhPrClientDetails, branch string, filePath
return fileContentString, resp.StatusCode, nil
}

// getTargetURL generates a custom target URL based on the provided go template file
// In case of any problem, it falls back to default URL
// the following dynamic parameters are passed to the template, can be use if needed:
// - EndTime: the time of commit status update (now)
// - StartTime: Endtime - 10 minutes
func getTargetURL() string {
// commitStatusTargetURL generates a target URL based on an optional
// template file specified by the environment variable CUSTOM_COMMIT_STATUS_URL_TEMPLATE_PATH.
// If the template file is not found or an error occurs during template execution,
// it returns a default URL.
// passed parameter commitTime can be used in the template as .CommitTime
func commitStatusTargetURL(commitTime time.Time) string {
targetURL := "https://github.com/wayfair-incubator/telefonistka"

tmplFile := os.Getenv("CUSTOM_COMMIT_STATUS_URL_TEMPLATE_PATH")
tmplName := filepath.Base(tmplFile)
// dynamic values to be used in the template
now := time.Now()

// dynamic parameters to be used in the template
p := struct {
EndTime time.Time
StartTime time.Time
CommitTime time.Time
}{
EndTime: now,
StartTime: now.Add(-10 * time.Minute),
CommitTime: commitTime,
}

renderedURL, err := executeTemplate(tmplName, tmplFile, p)
if err != nil {
log.Warnf("couldn't render: %w", err)
// TODO: why we cannot use warnf here?
log.Errorf("Failed to render target URL template: %v", err)
return targetURL
}

Expand Down

0 comments on commit 94960ef

Please sign in to comment.