diff --git a/github-comment.yaml b/github-comment.yaml index 997690b8..5b6d4892 100644 --- a/github-comment.yaml +++ b/github-comment.yaml @@ -1,11 +1,11 @@ --- post: default: | - CIRCLE_PULL_REQUEST: {{Env "CIRCLE_PULL_REQUEST"}} + foo hello: # update: 'Comment.HasMeta && Comment.Meta.TemplateKey == "hello"' template: | - $Hello: {{Env "HELLO"}} {{ "hello!" | upper | repeat 5 }} + $Hello: foo {{ "hello!" | upper | repeat 5 }} exec: hello: @@ -18,7 +18,6 @@ exec: combined output: {{.CombinedOutput}} - when: true template: | - $Hello: {{Env "HELLO"}} exit code: {{.ExitCode}} stdout: {{.Stdout}} stderr: {{.Stderr}} diff --git a/pkg/api/exec.go b/pkg/api/exec.go index a954265f..1692fa72 100644 --- a/pkg/api/exec.go +++ b/pkg/api/exec.go @@ -118,7 +118,6 @@ func (ctrl *ExecController) Exec(ctx context.Context, opts *option.ExecOptions) TemplateKey: opts.TemplateKey, Template: opts.Template, Vars: cfg.Vars, - Env: ctrl.Getenv, }, templates); err != nil { if !opts.Silent { fmt.Fprintf(ctrl.Stderr, "github-comment error: %+v\n", err) @@ -137,7 +136,6 @@ type ExecCommentParams struct { Command string JoinCommand string ExitCode int - Env func(string) string // PRNumber is the pull request number where the comment is posted PRNumber int // Org is the GitHub Organization or User name diff --git a/pkg/api/hide.go b/pkg/api/hide.go index 6d4f44c5..c67ba3ca 100644 --- a/pkg/api/hide.go +++ b/pkg/api/hide.go @@ -36,7 +36,7 @@ func (ctrl *HideController) Hide(ctx context.Context, opts *option.HideOptions) return err } nodeIDs, err := listHiddenComments( - ctx, ctrl.GitHub, ctrl.Expr, ctrl.Getenv, param, nil) + ctx, ctrl.GitHub, ctrl.Expr, param, nil) if err != nil { return err } @@ -139,7 +139,6 @@ type ParamListHiddenComments struct { func listHiddenComments( //nolint:funlen ctx context.Context, gh GitHub, exp Expr, - getEnv func(string) string, param *ParamListHiddenComments, paramExpr map[string]interface{}, ) ([]string, error) { @@ -203,7 +202,6 @@ func listHiddenComments( //nolint:funlen }, "HideKey": param.HideKey, "Vars": param.Vars, - "Env": getEnv, } for k, v := range paramExpr { paramMap[k] = v diff --git a/pkg/api/post.go b/pkg/api/post.go index 9cf39c78..324a1c9e 100644 --- a/pkg/api/post.go +++ b/pkg/api/post.go @@ -100,7 +100,6 @@ func (ctrl *PostController) setUpdatedCommentID(ctx context.Context, cmt *github "SHA1": cmt.SHA1, }, "Vars": cmt.Vars, - "Env": ctrl.Getenv, } logrus.WithFields(logrus.Fields{ diff --git a/pkg/api/post_internal_test.go b/pkg/api/post_internal_test.go index ac859f3b..60104ab8 100644 --- a/pkg/api/post_internal_test.go +++ b/pkg/api/post_internal_test.go @@ -143,7 +143,7 @@ func TestPostController_getCommentParams(t *testing.T) { //nolint:funlen Repo: "github-comment", Token: "xxx", PRNumber: 1, - Template: `{{Env "FOO"}} {{.Org}} {{.Repo}} {{.PRNumber}}`, + Template: `{{.Org}} {{.Repo}} {{.PRNumber}}`, }, }, exp: &github.Comment{ diff --git a/pkg/cmd/exec.go b/pkg/cmd/exec.go index cbc514ee..7afcd5ec 100644 --- a/pkg/cmd/exec.go +++ b/pkg/cmd/exec.go @@ -79,7 +79,7 @@ func (runner *Runner) execAction(c *cli.Context) error { opts.SkipNoToken = opts.SkipNoToken || cfg.SkipNoToken opts.Silent = opts.Silent || cfg.Silent - var pt api.Platform = platform.Get(getPlatformParam(cfg.Complement)) + var pt api.Platform = platform.Get() gh, err := getGitHub(c.Context, &opts.Options, cfg) if err != nil { diff --git a/pkg/cmd/hide.go b/pkg/cmd/hide.go index ec0e2a3c..b7b9a860 100644 --- a/pkg/cmd/hide.go +++ b/pkg/cmd/hide.go @@ -63,7 +63,7 @@ func (runner *Runner) hideAction(c *cli.Context) error { } opts.SkipNoToken = opts.SkipNoToken || cfg.SkipNoToken - var pt api.Platform = platform.Get(getPlatformParam(cfg.Complement)) + var pt api.Platform = platform.Get() gh, err := getGitHub(c.Context, &opts.Options, cfg) if err != nil { diff --git a/pkg/cmd/post.go b/pkg/cmd/post.go index fce15d8e..c8e3bd74 100644 --- a/pkg/cmd/post.go +++ b/pkg/cmd/post.go @@ -147,7 +147,7 @@ func (runner *Runner) postAction(c *cli.Context) error { } opts.SkipNoToken = opts.SkipNoToken || cfg.SkipNoToken - var pt api.Platform = platform.Get(getPlatformParam(cfg.Complement)) + var pt api.Platform = platform.Get() gh, err := getGitHub(c.Context, &opts.Options, cfg) if err != nil { diff --git a/pkg/cmd/util.go b/pkg/cmd/util.go deleted file mode 100644 index 3ca0cf8a..00000000 --- a/pkg/cmd/util.go +++ /dev/null @@ -1,19 +0,0 @@ -package cmd - -import ( - "github.com/suzuki-shunsuke/github-comment/pkg/config" - "github.com/suzuki-shunsuke/github-comment/pkg/platform" -) - -func getPlatformParam(comp *config.Complement) *platform.Param { - if comp == nil { - return &platform.Param{} - } - return &platform.Param{ - PRNumber: comp.PR, - RepoName: comp.Repo, - RepoOwner: comp.Org, - SHA: comp.SHA1, - Vars: comp.Vars, - } -} diff --git a/pkg/config/complement.go b/pkg/config/complement.go deleted file mode 100644 index cfc63a74..00000000 --- a/pkg/config/complement.go +++ /dev/null @@ -1,105 +0,0 @@ -package config - -import ( - "errors" - - "github.com/suzuki-shunsuke/github-comment/pkg/domain" -) - -type Complement struct { - PR []domain.ComplementEntry - Org []domain.ComplementEntry - Repo []domain.ComplementEntry - SHA1 []domain.ComplementEntry - Vars map[string][]domain.ComplementEntry -} - -type rawComplement struct { - PR []map[string]interface{} - Org []map[string]interface{} - Repo []map[string]interface{} - SHA1 []map[string]interface{} - Vars map[string][]map[string]interface{} -} - -func convComplementEntries(maps []map[string]interface{}) ([]domain.ComplementEntry, error) { - entries := make([]domain.ComplementEntry, len(maps)) - for i, m := range maps { - entry, err := convComplementEntry(m) - if err != nil { - return nil, err - } - entries[i] = entry - } - return entries, nil -} - -func convComplementEntry(m map[string]interface{}) (domain.ComplementEntry, error) { - t, ok := m["type"] - if !ok { - return nil, errors.New(`"type" is required`) - } - typ, ok := t.(string) - if !ok { - return nil, errors.New(`"type" must be string`) - } - switch typ { - case "envsubst": - entry := ComplementEnvsubstEntry{} - if err := newComplementEnvsubstEntry(m, &entry); err != nil { - return nil, err - } - return &entry, nil - case "template": - entry := ComplementTemplateEntry{} - if err := newComplementTemplateEntry(m, &entry); err != nil { - return nil, err - } - return &entry, nil - default: - return nil, errors.New(`unsupported type: ` + typ) - } -} - -func (cpl *Complement) UnmarshalYAML(unmarshal func(interface{}) error) error { - var val rawComplement - if err := unmarshal(&val); err != nil { - return err - } - - pr, err := convComplementEntries(val.PR) - if err != nil { - return err - } - cpl.PR = pr - - org, err := convComplementEntries(val.Org) - if err != nil { - return err - } - cpl.Org = org - - repo, err := convComplementEntries(val.Repo) - if err != nil { - return err - } - cpl.Repo = repo - - sha, err := convComplementEntries(val.SHA1) - if err != nil { - return err - } - cpl.SHA1 = sha - - vars := make(map[string][]domain.ComplementEntry, len(val.Vars)) - for k, v := range val.Vars { - a, err := convComplementEntries(v) - if err != nil { - return err - } - vars[k] = a - } - cpl.Vars = vars - - return nil -} diff --git a/pkg/config/complement_envsubst.go b/pkg/config/complement_envsubst.go deleted file mode 100644 index 6e3ccb24..00000000 --- a/pkg/config/complement_envsubst.go +++ /dev/null @@ -1,32 +0,0 @@ -package config - -import ( - "errors" - - "github.com/drone/envsubst" -) - -type ComplementEnvsubstEntry struct { - Value string -} - -func (entry *ComplementEnvsubstEntry) Type() string { - return "envsubst" -} - -func (entry *ComplementEnvsubstEntry) Entry() (string, error) { - return envsubst.EvalEnv(entry.Value) //nolint:wrapcheck -} - -func newComplementEnvsubstEntry(m map[string]interface{}, entry *ComplementEnvsubstEntry) error { - v, ok := m["value"] - if !ok { - return errors.New(`"value" is required`) - } - val, ok := v.(string) - if !ok { - return errors.New(`"value" must be string`) - } - entry.Value = val - return nil -} diff --git a/pkg/config/complement_template.go b/pkg/config/complement_template.go deleted file mode 100644 index 86236614..00000000 --- a/pkg/config/complement_template.go +++ /dev/null @@ -1,43 +0,0 @@ -package config - -import ( - "bytes" - "errors" - "fmt" - "text/template" - - "github.com/Masterminds/sprig/v3" -) - -type ComplementTemplateEntry struct { - Value string -} - -func (entry *ComplementTemplateEntry) Type() string { - return "template" -} - -func (entry *ComplementTemplateEntry) Entry() (string, error) { - tmpl, err := template.New("_").Funcs(sprig.TxtFuncMap()).Parse(entry.Value) - if err != nil { - return "", fmt.Errorf("parse a template %s: %w", entry.Value, err) - } - buf := &bytes.Buffer{} - if err := tmpl.Execute(buf, nil); err != nil { - return "", fmt.Errorf("render a template with params %s: %w", entry.Value, err) - } - return buf.String(), nil -} - -func newComplementTemplateEntry(m map[string]interface{}, entry *ComplementTemplateEntry) error { - v, ok := m["value"] - if !ok { - return errors.New(`"value" is required`) - } - val, ok := v.(string) - if !ok { - return errors.New(`"value" must be string`) - } - entry.Value = val - return nil -} diff --git a/pkg/config/config.go b/pkg/config/config.go index e0e00b0b..94070b0a 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -19,7 +19,6 @@ type Config struct { Hide map[string]string SkipNoToken bool `yaml:"skip_no_token"` Silent bool - Complement *Complement } type Base struct { diff --git a/pkg/platform/generic.go b/pkg/platform/generic.go deleted file mode 100644 index 73f372fc..00000000 --- a/pkg/platform/generic.go +++ /dev/null @@ -1,82 +0,0 @@ -package platform - -import ( - "fmt" - "strconv" - - "github.com/suzuki-shunsuke/github-comment/pkg/domain" -) - -type Param struct { - RepoOwner []domain.ComplementEntry - RepoName []domain.ComplementEntry - SHA []domain.ComplementEntry - PRNumber []domain.ComplementEntry - Vars map[string][]domain.ComplementEntry -} - -type generic struct { - param *Param -} - -func (gen *generic) render(entries []domain.ComplementEntry) (string, error) { - var e error - for _, entry := range entries { - a, err := entry.Entry() - if err != nil { - e = err - continue - } - if a != "" { - return a, nil - } - } - return "", e -} - -func (gen *generic) returnString(entries []domain.ComplementEntry) string { - s, err := gen.render(entries) - if err != nil { - return "" - } - return s -} - -func (gen *generic) RepoOwner() string { - return gen.returnString(gen.param.RepoOwner) -} - -func (gen *generic) RepoName() string { - return gen.returnString(gen.param.RepoName) -} - -func (gen *generic) SHA() string { - return gen.returnString(gen.param.SHA) -} - -func (gen *generic) IsPR() bool { - return gen.returnString(gen.param.PRNumber) != "" -} - -func (gen *generic) PRNumber() (int, error) { - s, err := gen.render(gen.param.PRNumber) - if err != nil { - return 0, err - } - if s == "" { - return 0, nil - } - b, err := strconv.Atoi(s) - if err == nil { - return b, nil - } - return 0, fmt.Errorf("parse pull request number as int: %w", err) -} - -func (gen *generic) Vars() map[string]string { - vars := make(map[string]string, len(gen.param.Vars)) - for k, v := range gen.param.Vars { - vars[k] = gen.returnString(v) - } - return vars -} diff --git a/pkg/platform/platform.go b/pkg/platform/platform.go index 7a654571..ce780881 100644 --- a/pkg/platform/platform.go +++ b/pkg/platform/platform.go @@ -11,7 +11,6 @@ import ( type Platform struct { platform cienv.Platform - generic *generic } func (pt *Platform) getRepoOrg() (string, error) { //nolint:unparam @@ -20,7 +19,7 @@ func (pt *Platform) getRepoOrg() (string, error) { //nolint:unparam return org, nil } } - return pt.generic.RepoOwner(), nil + return "", nil } func (pt *Platform) getRepoName() (string, error) { //nolint:unparam @@ -29,7 +28,7 @@ func (pt *Platform) getRepoName() (string, error) { //nolint:unparam return repo, nil } } - return pt.generic.RepoName(), nil + return "", nil } func (pt *Platform) getSHA1() (string, error) { //nolint:unparam @@ -38,7 +37,7 @@ func (pt *Platform) getSHA1() (string, error) { //nolint:unparam return sha1, nil } } - return pt.generic.SHA(), nil + return "", nil } func (pt *Platform) getPRNumber() (int, error) { @@ -61,7 +60,7 @@ func (pt *Platform) getPRNumber() (int, error) { return a, nil } } - return pt.generic.PRNumber() + return 0, nil } func (pt *Platform) complement(opts *option.Options) error { @@ -95,12 +94,6 @@ func (pt *Platform) complement(opts *option.Options) error { } opts.PRNumber = pr - vars := pt.generic.Vars() - for k, v := range opts.Vars { - vars[k] = v - } - opts.Vars = vars - return nil } @@ -123,14 +116,11 @@ func (pt *Platform) ComplementExec(opts *option.ExecOptions) error { return pt.complement(&opts.Options) } -func Get(param *Param) *Platform { +func Get() *Platform { cienv.Add(func(param *cienv.Param) cienv.Platform { return NewGoogleCloudBuild(param) }) return &Platform{ platform: cienv.Get(nil), - generic: &generic{ - param: param, - }, } } diff --git a/pkg/template/template.go b/pkg/template/template.go index 32e3f85c..a55872ba 100644 --- a/pkg/template/template.go +++ b/pkg/template/template.go @@ -23,12 +23,28 @@ func GetTemplates(param *ParamGetTemplates) map[string]string { cloudBuildRegion = "global" } buildLinks := map[string]string{ - "circleci": `[workflow](https://circleci.com/workflow-run/{{env "CIRCLE_WORKFLOW_ID" }}) [job]({{env "CIRCLE_BUILD_URL"}}) (job: {{env "CIRCLE_JOB"}})`, - "codebuild": `[Build link]({{env "CODEBUILD_BUILD_URL"}})`, - "drone": `[build]({{env "DRONE_BUILD_LINK"}}) [step]({{env "DRONE_BUILD_LINK"}}/{{env "DRONE_STAGE_NUMBER"}}/{{env "DRONE_STEP_NUMBER"}})`, - "github-actions": `[Build link]({{env "GITHUB_SERVER_URL"}}/{{env "GITHUB_REPOSITORY"}}/actions/runs/{{env "GITHUB_RUN_ID"}})`, - "google-cloud-build": fmt.Sprintf( - "[Build link](https://console.cloud.google.com/cloud-build/builds;region=%s/%s?project=%s)", + "circleci": fmt.Sprintf( + `[workflow](https://circleci.com/workflow-run/%s) [job](%s) (job: %s)`, + os.Getenv("CIRCLE_WORKFLOW_ID"), + os.Getenv("CIRCLE_BUILD_URL"), + os.Getenv("CIRCLE_JOB"), + ), + "codebuild": fmt.Sprintf(`[Build link](%s)`, os.Getenv("CODEBUILD_BUILD_URL")), + "drone": fmt.Sprintf( + `[build](%s) [step](%s/%s/%s)`, + os.Getenv("DRONE_BUILD_LINK"), + os.Getenv("DRONE_BUILD_LINK"), + os.Getenv("DRONE_STAGE_NUMBER"), + os.Getenv("DRONE_STEP_NUMBER"), + ), + "github-actions": fmt.Sprintf( + `[Build link](%s/%s/actions/runs/%s)`, + os.Getenv("GITHUB_SERVER_URL"), + os.Getenv("GITHUB_REPOSITORY"), + os.Getenv("GITHUB_RUN_ID"), + ), + "cloud-build": fmt.Sprintf( + "https://console.cloud.google.com/cloud-build/builds;region=%s/%s?project=%s", cloudBuildRegion, os.Getenv("BUILD_ID"), os.Getenv("PROJECT_ID"), @@ -81,10 +97,16 @@ func avoidHTMLEscape(text string) template.HTML { func (renderer *Renderer) Render(tpl string, templates map[string]string, params interface{}) (string, error) { tpl = addTemplates(tpl, templates) + + // delete some functions for security reason + funcs := sprig.FuncMap() + delete(funcs, "env") + delete(funcs, "expandenv") + delete(funcs, "getHostByName") tmpl, err := template.New("comment").Funcs(template.FuncMap{ "Env": renderer.Getenv, "AvoidHTMLEscape": avoidHTMLEscape, - }).Funcs(sprig.FuncMap()).Parse(tpl) + }).Funcs(funcs).Parse(tpl) if err != nil { return "", fmt.Errorf("parse a template: %w", err) }