From 94960efadccae097bed2f13df94de7667d1a8baf Mon Sep 17 00:00:00 2001 From: Yazdan Mohammadi Date: Mon, 21 Oct 2024 14:23:37 +0200 Subject: [PATCH] Pass only commit time and change the function name --- docs/installation.md | 8 +++++--- internal/pkg/githubapi/github.go | 28 +++++++++++++--------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/docs/installation.md b/docs/installation.md index f87899d6..41645e57 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -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"` diff --git a/internal/pkg/githubapi/github.go b/internal/pkg/githubapi/github.go index b13989fc..9c9bf089 100644 --- a/internal/pkg/githubapi/github.go +++ b/internal/pkg/githubapi/github.go @@ -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, @@ -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 }