Skip to content

Commit f2db9f7

Browse files
savitaashturechmouel
authored andcommitted
Add GitOps comments functionality to push request
This commit enables PAC users to include GitOps comments for push request. Supported GitOps comments are - /test - /retest - /cancel - /test prname - /retest prname - /cancel prname Signed-off-by: Savita Ashture <[email protected]>
1 parent 0edaa50 commit f2db9f7

File tree

19 files changed

+934
-94
lines changed

19 files changed

+934
-94
lines changed

docs/content/docs/guide/running.md

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ entire suite of checks once again.
114114

115115
![github apps rerun check](/images/github-apps-rerun-checks.png)
116116

117-
### Gitops command on pull or merge request
117+
### GitOps command on pull or merge request
118118

119-
If you are targeting a pull or merge request you can use `GitOps` comment
119+
If you are targeting a push, pull or merge request you can use `GitOps` comment
120120
inside your pull request, to restart all or specific Pipelines.
121121

122-
For example you want to restart all your pipeline you can add a comment starting
122+
For example, you want to restart all your pipeline you can add a comment starting
123123
with `/retest` and all PipelineRun attached to that pull or merge request will be
124124
restarted :
125125

@@ -141,6 +141,58 @@ roses are red, violets are blue. pipeline are bound to flake by design.
141141
/test <pipelinerun-name>
142142
```
143143

144+
### GitOps command on push request
145+
146+
To trigger GitOps commands in response to a push request, you can include `GitOps`
147+
comments within your commit messages. These comments can be used to restart
148+
either all pipelines or specific ones. Here's how it works:
149+
150+
For restarting all pipeline runs:
151+
152+
1. Use `/retest` or `/test` within your commit message.
153+
154+
For restarting a specific pipeline run:
155+
2. Use `/retest <pipelinerun-name>` or `/test <pipelinerun-name>` within your
156+
commit message. Replace `<pipelinerun-name>` with the specific name of the
157+
pipeline run you want to restart.
158+
159+
**Note:**
160+
161+
When executing `GitOps` commands on a commit that exists in multiple branches
162+
within a push request, the branch with the latest commit will be used.
163+
164+
This means:
165+
166+
1. If a user specifies commands like `/retest` or `/test` without any argument
167+
in a comment on a branch, the test will automatically be performed on the **main** branch.
168+
169+
Examples :
170+
1. `/retest`
171+
2. `/test`
172+
3. `/retest <pipelinerun-name>`
173+
4. `/test <pipelinerun-name>`
174+
175+
2. If the user includes a branch specification such as `/retest branch:test` or
176+
`/test branch:test`, the test will be executed on the commit where the comment is
177+
located, with the context of the **test** branch.
178+
179+
Examples :
180+
1. `/retest branch:test`
181+
2. `/test branch:test`
182+
3. `/retest <pipelinerun-name> branch:test`
183+
4. `/test <pipelinerun-name> branch:test`
184+
185+
To add `GitOps` comments to a push request, follow these steps:
186+
187+
1. Go to your repository.
188+
2. Click on the **Commits** section.
189+
3. Choose one of the individual **Commit**.
190+
4. Click on the line number where you want to add a `GitOps` comment, as shown in the image below:
191+
192+
![GitOps Commits For Comments](/images/gitops-comments-on-commit.png)
193+
194+
Please note that this feature is supported for the GitHub provider only.
195+
144196
## Cancelling the PipelineRun
145197

146198
You can cancel a running PipelineRun by commenting on the PullRequest.
@@ -159,7 +211,7 @@ It seems the infra is down, so cancelling the pipelineruns.
159211
If you have multiple `PipelineRun` and you want to target a specific
160212
`PipelineRun` you can use the `/cancel` comment with the PipelineRun name
161213

162-
Example:
214+
Example :
163215

164216
```text
165217
roses are red, violets are blue. why to run the pipeline when the infra is down.
@@ -170,3 +222,41 @@ roses are red, violets are blue. why to run the pipeline when the infra is down.
170222
On GitHub App the status of the Pipeline will be set to `cancelled`.
171223

172224
![pipelinerun canceled](/images/pr-cancel.png)
225+
226+
### Cancelling the PipelineRun on push request
227+
228+
You can cancel a running PipelineRun by commenting on the commit.
229+
Here's how you can do it.
230+
231+
Example :
232+
233+
1. Use `/cancel` to cancel all PipeineRuns.
234+
2. Use `/cancel <pipelinerun-name>` to cancel a specific PipeineRun
235+
236+
**Note:**
237+
238+
When executing `GitOps` comments on a commit that exists in multiple branches
239+
within a push request, the branch with the latest commit will be used.
240+
241+
This means:
242+
243+
1. If a user specifies commands like `/cancel`
244+
without any argument in a comment on a branch,
245+
it will automatically target the **main** branch.
246+
247+
Examples :
248+
1. `/cancel`
249+
2. `/cancel <pipelinerun-name>`
250+
251+
2. If the user issues a command like `/cancel branch:test`,
252+
it will target the commit where the comment was made but use the **test** branch.
253+
254+
Examples :
255+
1. `/cancel branch:test`
256+
2. `/cancel <pipelinerun-name> branch:test`
257+
258+
In the GitHub App, the status of the Pipeline will be set to `cancelled`.
259+
260+
![GitOps Commits For Comments For PipelineRun Canceled](/images/gitops-comments-on-commit-cancel.png)
261+
262+
Please note that this feature is supported for the GitHub provider only.

docs/content/docs/install/github_apps.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Alternatively, you could set up manually by following the steps [here](#setup-ma
4848
* Check run
4949
* Check suite
5050
* Issue comment
51+
* Commit comment
5152
* Pull request
5253
* Push
5354

100 KB
Loading
99.7 KB
Loading

pkg/cmd/tknpac/bootstrap/github.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func generateManifest(opts *bootstrapOpts) ([]byte, error) {
2121
"check_run",
2222
"check_suite",
2323
"issue_comment",
24+
"commit_comment",
2425
"pull_request",
2526
"push",
2627
},

pkg/pipelineascode/cancel_pipelinerun_test.go

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ var (
3232
URL: "https://github.com/fooorg/foo",
3333
},
3434
}
35+
fooRepoLabelsForPush = map[string]string{
36+
keys.URLRepository: formatting.CleanValueKubernetes("foo"),
37+
keys.SHA: formatting.CleanValueKubernetes("foosha"),
38+
}
3539
fooRepoLabels = map[string]string{
3640
keys.URLRepository: formatting.CleanValueKubernetes("foo"),
3741
keys.SHA: formatting.CleanValueKubernetes("foosha"),
@@ -66,12 +70,6 @@ func TestCancelPipelinerun(t *testing.T) {
6670
pipelineRuns []*pipelinev1.PipelineRun
6771
cancelledPipelineRuns map[string]bool
6872
}{
69-
{
70-
name: "not a pull request event",
71-
event: &info.Event{
72-
TriggerTarget: "push",
73-
},
74-
},
7573
{
7674
name: "cancel running",
7775
event: &info.Event{
@@ -203,6 +201,76 @@ func TestCancelPipelinerun(t *testing.T) {
203201
repo: fooRepo,
204202
cancelledPipelineRuns: map[string]bool{},
205203
},
204+
{
205+
name: "cancel running for push event",
206+
event: &info.Event{
207+
Repository: "foo",
208+
SHA: "foosha",
209+
TriggerTarget: "push",
210+
State: info.State{
211+
CancelPipelineRuns: true,
212+
},
213+
},
214+
pipelineRuns: []*pipelinev1.PipelineRun{
215+
{
216+
ObjectMeta: metav1.ObjectMeta{
217+
Name: "pr-foo",
218+
Namespace: "foo",
219+
Labels: fooRepoLabelsForPush,
220+
},
221+
Spec: pipelinev1.PipelineRunSpec{},
222+
},
223+
},
224+
repo: fooRepo,
225+
cancelledPipelineRuns: map[string]bool{
226+
"pr-foo": true,
227+
},
228+
},
229+
{
230+
name: "cancel a specific run for push event",
231+
event: &info.Event{
232+
Repository: "foo",
233+
SHA: "foosha",
234+
TriggerTarget: "push",
235+
State: info.State{
236+
CancelPipelineRuns: true,
237+
TargetCancelPipelineRun: "pr-foo-abc",
238+
},
239+
},
240+
pipelineRuns: []*pipelinev1.PipelineRun{
241+
{
242+
ObjectMeta: metav1.ObjectMeta{
243+
Name: "pr-foo",
244+
Namespace: "foo",
245+
Labels: fooRepoLabelsForPush,
246+
Annotations: fooRepoAnnotations,
247+
},
248+
Spec: pipelinev1.PipelineRunSpec{},
249+
},
250+
{
251+
ObjectMeta: metav1.ObjectMeta{
252+
Name: "pr-foo-abc-123",
253+
Namespace: "foo",
254+
Labels: fooRepoLabelsPrFooAbc,
255+
Annotations: fooRepoAnnotationsPrFooAbc,
256+
},
257+
Spec: pipelinev1.PipelineRunSpec{},
258+
},
259+
{
260+
ObjectMeta: metav1.ObjectMeta{
261+
Name: "pr-foo-pqr",
262+
Namespace: "foo",
263+
Labels: fooRepoLabelsForPush,
264+
Annotations: fooRepoAnnotations,
265+
},
266+
Spec: pipelinev1.PipelineRunSpec{},
267+
},
268+
},
269+
repo: fooRepo,
270+
cancelledPipelineRuns: map[string]bool{
271+
"pr-foo-abc-123": true,
272+
},
273+
},
206274
}
207275

208276
for _, tt := range tests {

pkg/pipelineascode/cancel_pipelineruns.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,19 @@ var cancelMergePatch = map[string]interface{}{
2424
}
2525

2626
func (p *PacRun) cancelPipelineRuns(ctx context.Context, repo *v1alpha1.Repository) error {
27-
if p.event.TriggerTarget != "pull_request" {
28-
msg := fmt.Sprintf("not a pullRequest event, event: %v", p.event.TriggerTarget)
29-
p.eventEmitter.EmitMessage(repo, zap.WarnLevel, "RepositoryEvent", msg)
30-
return nil
27+
labelSelector := getLabelSelector(map[string]string{
28+
keys.URLRepository: formatting.CleanValueKubernetes(p.event.Repository),
29+
keys.SHA: formatting.CleanValueKubernetes(p.event.SHA),
30+
})
31+
32+
if p.event.TriggerTarget == "pull_request" {
33+
labelSelector = getLabelSelector(map[string]string{
34+
keys.PullRequest: strconv.Itoa(p.event.PullRequestNumber),
35+
})
3136
}
3237

3338
prs, err := p.run.Clients.Tekton.TektonV1().PipelineRuns(repo.Namespace).List(ctx, metav1.ListOptions{
34-
LabelSelector: getLabelSelector(map[string]string{
35-
keys.URLRepository: formatting.CleanValueKubernetes(p.event.Repository),
36-
keys.SHA: formatting.CleanValueKubernetes(p.event.SHA),
37-
keys.PullRequest: strconv.Itoa(p.event.PullRequestNumber),
38-
}),
39+
LabelSelector: labelSelector,
3940
})
4041
if err != nil {
4142
return fmt.Errorf("failed to list pipelineRuns : %w", err)

pkg/provider/github/detect.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ func detectTriggerTypeFromPayload(ghEventType string, eventInt any) (info.Trigge
8888
return info.TriggerTypeCheckRunRerequested, ""
8989
}
9090
return "", fmt.Sprintf("check_run: unsupported action \"%s\"", event.GetAction())
91+
case *github.CommitCommentEvent:
92+
if event.GetAction() == "created" {
93+
if provider.IsTestRetestComment(event.GetComment().GetBody()) {
94+
return info.TriggerTypeRetest, ""
95+
}
96+
if provider.IsOkToTestComment(event.GetComment().GetBody()) {
97+
return info.TriggerTypeOkToTest, ""
98+
}
99+
if provider.IsCancelComment(event.GetComment().GetBody()) {
100+
return info.TriggerTypeCancel, ""
101+
}
102+
}
103+
return "", fmt.Sprintf("commit_comment: unsupported action \"%s\"", event.GetAction())
91104
}
92105
return "", fmt.Sprintf("github: event \"%v\" is not supported", ghEventType)
93106
}

pkg/provider/github/detect_test.go

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,17 @@ func TestProvider_Detect(t *testing.T) {
6363
event: github.CommitCommentEvent{
6464
Action: github.String("something"),
6565
},
66+
eventType: "release",
67+
wantReason: "event \"release\" is not supported",
68+
isGH: true,
69+
processReq: false,
70+
},
71+
{
72+
name: "invalid commit_comment Event",
73+
event: github.CommitCommentEvent{
74+
Action: github.String("something"),
75+
},
6676
eventType: "commit_comment",
67-
wantReason: "event \"commit_comment\" is not supported",
6877
isGH: true,
6978
processReq: false,
7079
},
@@ -228,7 +237,7 @@ func TestProvider_Detect(t *testing.T) {
228237
processReq: true,
229238
},
230239
{
231-
name: "issue comment Event with retest",
240+
name: "issue comment Event with cancel comment ",
232241
event: github.IssueCommentEvent{
233242
Action: github.String("created"),
234243
Issue: &github.Issue{
@@ -246,6 +255,45 @@ func TestProvider_Detect(t *testing.T) {
246255
isGH: true,
247256
processReq: true,
248257
},
258+
{
259+
name: "commit comment event with cancel comment",
260+
event: github.CommitCommentEvent{
261+
Action: github.String("created"),
262+
Installation: &github.Installation{
263+
ID: github.Int64(123),
264+
},
265+
Comment: &github.RepositoryComment{Body: github.String("/cancel")},
266+
},
267+
eventType: "commit_comment",
268+
isGH: true,
269+
processReq: true,
270+
},
271+
{
272+
name: "commit comment Event with retest",
273+
event: github.CommitCommentEvent{
274+
Action: github.String("created"),
275+
Installation: &github.Installation{
276+
ID: github.Int64(123),
277+
},
278+
Comment: &github.RepositoryComment{Body: github.String("/retest")},
279+
},
280+
eventType: "commit_comment",
281+
isGH: true,
282+
processReq: true,
283+
},
284+
{
285+
name: "commit comment Event with test",
286+
event: github.CommitCommentEvent{
287+
Action: github.String("created"),
288+
Installation: &github.Installation{
289+
ID: github.Int64(123),
290+
},
291+
Comment: &github.RepositoryComment{Body: github.String("/test")},
292+
},
293+
eventType: "commit_comment",
294+
isGH: true,
295+
processReq: true,
296+
},
249297
}
250298

251299
for _, tt := range tests {

pkg/provider/github/github.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,3 +544,20 @@ func uniqueRepositoryID(repoIDs []int64, id int64) []int64 {
544544
}
545545
return r
546546
}
547+
548+
// isBranchContainsCommit checks whether provided branch has sha or not.
549+
func (v *Provider) isBranchContainsCommit(ctx context.Context, runevent *info.Event, branchName string) error {
550+
if v.Client == nil {
551+
return fmt.Errorf("no github client has been initialized, " +
552+
"exiting... (hint: did you forget setting a secret on your repo?)")
553+
}
554+
555+
branchInfo, _, err := v.Client.Repositories.GetBranch(ctx, runevent.Organization, runevent.Repository, branchName, true)
556+
if err != nil {
557+
return err
558+
}
559+
if branchInfo.Commit.GetSHA() == runevent.SHA {
560+
return nil
561+
}
562+
return fmt.Errorf("provided branch %s does not contains sha %s", branchName, runevent.SHA)
563+
}

0 commit comments

Comments
 (0)