Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion charts/lighthouse/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ webhooks:
# webhooks.ingress.tls.secretName -- Specify webhooks ingress tls secretName
secretName: ""

# webhooks.customDeploymentTriggerCommand -- deployments can configure the ability to allow custom lighthouse triggers
# webhooks.customDeploymentTriggerCommand -- takes in a comma seperated list of lighthouse triggers. deployments can configure the ability to allow custom lighthouse triggers
# using their own unique chat prefix, for example extending the default `/test` trigger prefix let them specify
# `customDeploymentTriggerPrefix: foo` which means they can also use their own custom trigger /foo mycoolthing
customDeploymentTriggerCommand: ""
Expand Down
44 changes: 44 additions & 0 deletions docs/trigger/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Lighthouse custome triggers

You can enable use LH_CUSTOM_TRIGGER_COMMAND environment variable to add custom triggers. It takes in a comma seperated list of commands.

When these commands are triggered using ChatOps, Lighthouse will store the command arg in TRIGGER_COMMAND_ARG PipelineRunPara which you can refer from the pipeline.

eg:
`
LH_CUSTOM_TRIGGER_COMMAND=deploy,trigger
`
```
apiVersion: config.lighthouse.jenkins-x.io/v1alpha1
kind: TriggerConfig
spec:
presubmits:
- name: pr
context: "pr"
always_run: true
optional: false
source: "pullrequest.yaml"
- name: trigger
context: "trigger"
always_run: false
optional: false
rerun_command: /trigger this
trigger: (?m)^/trigger( all| this),?(\s+|$)
source: "trigger.yaml"
- name: deploy
context: "deployr"
always_run: false
optional: false
rerun_command: /deploy dev
trigger: (?m)^/deploy(?:[ \t]+([-\w]+(?:,[-\w]+)*))?(?:[ \t]+([-\w]+(?:,[-\w]+)*))?
source: "deploy.yaml"
postsubmits:
- name: release
context: "release"
source: "release.yaml"
branches:
- ^main$
```
you can use ChatOps command to trigger each pipeline
eg:
***/deploy qa***
10 changes: 9 additions & 1 deletion pkg/plugins/trigger/generic-comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"k8s.io/apimachinery/pkg/util/sets"
)

func handleGenericComment(c Client, trigger *plugins.Trigger, gc scmprovider.GenericCommentEvent) error {
func handleGenericComment(c Client, trigger *plugins.Trigger, gc scmprovider.GenericCommentEvent, arg string) error {
org := gc.Repo.Namespace
repo := gc.Repo.Name
number := gc.Number
Expand Down Expand Up @@ -94,6 +94,14 @@ func handleGenericComment(c Client, trigger *plugins.Trigger, gc scmprovider.Gen
if err != nil {
return err
}
if arg != "" {
for i := range toTest {
toTest[i].Base.PipelineRunParams = append(toTest[i].Base.PipelineRunParams , job.PipelineRunParam{
Name: "TRIGGER_COMMAND_ARG",
ValueTemplate: arg,
})
}
}
Comment on lines +97 to +104
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The feature should be documented and unit test added

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

836cdca adding in unit tests. please let me know if I have missed anything. I updated the code so that handleGenericComment take in the command arg.
documentation for this feature is in

When these commands are triggered using ChatOps, Lighthouse will store the command arg in TRIGGER_COMMAND_ARG PipelineRunPara which you can refer from the pipeline.

return RunAndSkipJobs(c, pr, toTest, toSkip, gc.GUID, trigger.ElideSkippedContexts)
}

Expand Down
44 changes: 42 additions & 2 deletions pkg/plugins/trigger/generic-comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type testcase struct {
IssueLabels []string
IgnoreOkToTest bool
ElideSkippedContexts bool
TriggerCommandArg string
}

func TestHandleGenericComment(t *testing.T) {
Expand Down Expand Up @@ -829,6 +830,26 @@ func TestHandleGenericComment(t *testing.T) {
ShouldBuild: false,
IssueLabels: issueLabels(labels.LGTM, labels.Approved),
},
{
name: "Trigger Command should pass args to the job as a PipelineRunParam TRIGGER_COMMAND_ARG",
Author: "trusted-member",
Body: "/test jub",
State: "open",
IsPR: true,
Presubmits: map[string][]job.Presubmit{
"org/repo": {
{
Base: job.Base{
Name: "jub",
},
Trigger: `(?m)^/test (?:.*? )?jub(?: .*?)?$`,
RerunCommand: `/test jub`,
},
},
},
ShouldBuild: true,
TriggerCommandArg: "jub",
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
Expand Down Expand Up @@ -938,13 +959,13 @@ func TestHandleGenericComment(t *testing.T) {
// For instance on Issue/PR creation and modification.
// Let's call it twice to ensure idempotency.
if err := plugin.InvokeCommandHandler(&event, func(_ plugins.CommandEventHandler, e *scmprovider.GenericCommentEvent, _ plugins.CommandMatch) error {
return handleGenericComment(c, trigger, *e)
return handleGenericComment(c, trigger, *e, tc.TriggerCommandArg)
}); err != nil {
t.Fatalf("%s: didn't expect error: %s", tc.name, err)
}
validate(tc.name, fakeLauncher, g, tc, t)
if err := plugin.InvokeCommandHandler(&event, func(_ plugins.CommandEventHandler, e *scmprovider.GenericCommentEvent, _ plugins.CommandMatch) error {
return handleGenericComment(c, trigger, *e)
return handleGenericComment(c, trigger, *e, tc.TriggerCommandArg)
}); err != nil {
t.Fatalf("%s: didn't expect error: %s", tc.name, err)
}
Expand All @@ -958,6 +979,7 @@ func validate(name string, fakeLauncher *fake.Launcher, g *fake2.SCMClient, tc t
for _, job := range fakeLauncher.Pipelines {
startedContexts.Insert(job.Spec.Context)
}

if len(startedContexts) > 0 && !tc.ShouldBuild {
t.Errorf("Built but should not have: %+v", tc)
} else if len(startedContexts) == 0 && tc.ShouldBuild {
Expand Down Expand Up @@ -986,6 +1008,24 @@ func validate(name string, fakeLauncher *fake.Launcher, g *fake2.SCMClient, tc t
if !reflect.DeepEqual(labelsRemoved, tc.RemovedLabels) {
t.Errorf("%s: expected %q to be removed, got %q", name, tc.RemovedLabels, labelsRemoved)
}
if tc.TriggerCommandArg != "" {
for _, job := range fakeLauncher.Pipelines {
found := false
for _, param := range job.Spec.PipelineRunParams {
if param.Name == "TRIGGER_COMMAND_ARG" {
found = true

if param.ValueTemplate != tc.TriggerCommandArg {
t.Errorf("PipelineRunParam had incorrect ValueTemplate: got %s, want %s", tc.TriggerCommandArg, param.ValueTemplate)
}
break
}
}
if !found {
t.Errorf("PipelineRunParam 'TRIGGER_COMMAND_ARG' was not added")
}
}
}
}

func TestRetestFilter(t *testing.T) {
Expand Down
37 changes: 18 additions & 19 deletions pkg/plugins/trigger/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,22 @@ var plugin = plugins.Plugin{
}

func init() {
customTriggerCommand := os.Getenv(customerTriggerCommandEnvVar)
if customTriggerCommand != "" {
customCommand := plugins.Command{
Name: customTriggerCommand,
Arg: &plugins.CommandArg{
Pattern: `[-\w]+(?:,[-\w]+)*`,
},
Description: fmt.Sprintf("Manually trigger /%s chatops commands.", customTriggerCommand),
Featured: true,
Action: plugins.
Invoke(handleGenericCommentEvent).
When(plugins.Action(scm.ActionCreate), plugins.IsPR(), plugins.IssueState("open")),
}
plugin.Commands = append(plugin.Commands, customCommand)
}

plugins.RegisterPlugin(pluginName, plugin)
customTriggerCommand := os.Getenv(customerTriggerCommandEnvVar)
for _, trigger := range strings.Split(customTriggerCommand, ","){
Copy link
Contributor

@dippynark dippynark Jan 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you not already do this using a regex pattern for the customDeploymentTriggerCommand like: build|plan|apply?

We are doing this currently to trigger builds with /build commands and Terraform plans/applies with /plan and /apply commands. Perhaps the docs just need to be updated to make this clearer?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree. This is interpreted as a regexp, so I think you should revert this change and update the documentation and tests accordingly.

customCommand := plugins.Command{
Name: trigger,
Arg: &plugins.CommandArg{
Pattern: `[-\w]+(?:,[-\w]+)*`,
},
Description: fmt.Sprintf("Manually trigger /%s chatops commands.", trigger),
Featured: true,
Action: plugins.
Invoke(handleGenericCommentEvent).
When(plugins.Action(scm.ActionCreate), plugins.IsPR(), plugins.IssueState("open")),
}
plugin.Commands = append(plugin.Commands, customCommand)
}
plugins.RegisterPlugin(pluginName, plugin)
}

func configHelp(config *plugins.Configuration, enabledRepos []string) (map[string]string, error) {
Expand Down Expand Up @@ -170,8 +169,8 @@ func handlePullRequest(pc plugins.Agent, pr scm.PullRequestHook) error {
return handlePR(getClient(pc), pc.PluginConfig.TriggerFor(org, repo), pr)
}

func handleGenericCommentEvent(_ plugins.CommandMatch, pc plugins.Agent, gc scmprovider.GenericCommentEvent) error {
return handleGenericComment(getClient(pc), pc.PluginConfig.TriggerFor(gc.Repo.Namespace, gc.Repo.Name), gc)
func handleGenericCommentEvent(cm plugins.CommandMatch, pc plugins.Agent, gc scmprovider.GenericCommentEvent) error {
return handleGenericComment(getClient(pc), pc.PluginConfig.TriggerFor(gc.Repo.Namespace, gc.Repo.Name), gc, cm.Arg)
}

func handlePush(pc plugins.Agent, pe scm.PushHook) error {
Expand Down