Skip to content

Commit

Permalink
Support overriding the default template dir (#98)
Browse files Browse the repository at this point in the history
* Support overriding the default template dir.
Useful for environments where the container workdir is overridden(like
GitHub Actions) or when custom templates are desired.

* Fix some issues with event.json handeling
  • Loading branch information
Oded-B authored Aug 25, 2023
1 parent 8e5f05c commit 92c4eb8
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 4 deletions.
5 changes: 3 additions & 2 deletions cmd/telefonistka/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ func event(eventType string, eventFilePath string) {
// To use the same code path as for Webhook I'm creating an http request with the payload from the file.
// This might not be very smart.

h := http.Request{}
h, _ := http.NewRequest("POST", "", nil) //nolint:noctx
h.Body = io.NopCloser(bytes.NewReader(payload))
h.Header.Set("Content-Type", "application/json")
h.Header.Set("X-GitHub-Event", eventType)

mainGhClientCache, _ := lru.New[string, githubapi.GhClientPair](128)
prApproverGhClientCache, _ := lru.New[string, githubapi.GhClientPair](128)
githubapi.HandleEvent(&h, ctx, mainGhClientCache, prApproverGhClientCache, nil)
githubapi.HandleEvent(h, ctx, mainGhClientCache, prApproverGhClientCache, nil)
}

func getEnv(key, fallback string) string {
Expand Down
2 changes: 2 additions & 0 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ Environment variables for the webhook process:

`GITHUB_APP_ID` Application ID for Github applications style of deployments, available in the Github Application setting page.

`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.

Behavior of the bot is configured by YAML files **in the target repo**:

## Repo Configuration
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/githubapi/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func handleCommentPrEvent(ghPrClientDetails GhPrClientDetails, ce *github.IssueC

func commentPlanInPR(ghPrClientDetails GhPrClientDetails, promotions map[string]PromotionInstance) {
var templateOutput bytes.Buffer
dryRunMsgTemplate, err := template.New("dryRunMsg").ParseFiles("templates/dry-run-pr-comment.gotmpl")
dryRunMsgTemplate, err := template.New("dryRunMsg").ParseFiles(getEnv("TEMPLATES_PATH", "templates/") + "dry-run-pr-comment.gotmpl")
if err != nil {
ghPrClientDetails.PrLogger.Errorf("Failed to parse template: err=%v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/githubapi/promotion.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func DetectDrift(ghPrClientDetails GhPrClientDetails) error {
}
if len(diffOutputMap) != 0 {
var templateOutput bytes.Buffer
driftMsgTemplate, err := template.New("driftMsg").ParseFiles("templates/drift-pr-comment.gotmpl")
driftMsgTemplate, err := template.New("driftMsg").ParseFiles(getEnv("TEMPLATES_PATH", "templates/") + "drift-pr-comment.gotmpl")
if err != nil {
ghPrClientDetails.PrLogger.Errorf("Failed to parse template: err=%v", err)
return err
Expand Down

0 comments on commit 92c4eb8

Please sign in to comment.