Skip to content

Commit 283a001

Browse files
mfenniakMathieu Fenniak
authored andcommitted
fix: cancel runs pending approval when a PR is closed (#11134)
Fixes #11125. When a PR is closed, cancel any action runs associated with the pull request that are not approved so that they do not remain in the Actions list as a blocked action. ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [x] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change. - [ ] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change. *The decision if the pull request will be shown in the release notes is up to the mergers / release team.* The content of the `release-notes/<pull request number>.md` file will serve as the basis for the release notes. If the file does not exist, the title of the pull request will be used instead. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11134 Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org> Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net> Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
1 parent 0b9b1ec commit 283a001

4 files changed

Lines changed: 77 additions & 0 deletions

File tree

services/actions/notifier.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,15 @@ func (n *actionsNotifier) IssueChangeStatus(ctx context.Context, doer *user_mode
138138
WithPayload(apiPullRequest).
139139
WithPullRequest(issue.PullRequest).
140140
Notify(ctx)
141+
142+
// PR may have left unapproved runs if the author wasn't trusted, and now that it's been closed those should be
143+
// cancelled. Note that this occurs after new events are fired -- even a 'closed' event could generate a pull
144+
// request run that needs to be cancelled.
145+
if isClosed {
146+
if err := cleanupPullRequestUnapprovedRuns(ctx, issue.RepoID, issue.PullRequest.ID); err != nil {
147+
log.Error("cleanupPullRequestUnapprovedRuns: %v", err)
148+
}
149+
}
141150
return
142151
}
143152
apiIssue := &api.IssuePayload{

services/actions/trust.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package actions
55

66
import (
77
"context"
8+
"errors"
89
"fmt"
910

1011
actions_model "forgejo.org/models/actions"
@@ -324,3 +325,21 @@ func pullRequestApprove(ctx context.Context, doerID, repoID, pullRequestID int64
324325
}
325326
return nil
326327
}
328+
329+
func cleanupPullRequestUnapprovedRuns(ctx context.Context, repoID, pullRequestID int64) error {
330+
runs, err := actions_model.GetRunsThatNeedApprovalByRepoIDAndPullRequestID(ctx, repoID, pullRequestID)
331+
if err != nil {
332+
return err
333+
}
334+
335+
errorSlice := []error{}
336+
for _, run := range runs {
337+
if err := CancelRun(ctx, run); err != nil {
338+
errorSlice = append(errorSlice, err)
339+
}
340+
}
341+
if len(errorSlice) > 0 {
342+
return errors.Join(errorSlice...)
343+
}
344+
return nil
345+
}

services/actions/trust_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,20 @@ jobs:
227227
assert.False(t, run.NeedApproval)
228228
}
229229
})
230+
231+
t.Run("cleanupPullRequestUnapprovedRuns", func(t *testing.T) {
232+
pullRequestID := int64(534)
233+
runNotApproved := createPullRequestRun(t, pullRequestID, repoID)
234+
235+
previousCancelledCount := unittest.GetCount(t, &actions_model.ActionRun{Status: actions_model.StatusCancelled})
236+
237+
require.NoError(t, cleanupPullRequestUnapprovedRuns(t.Context(), repoID, pullRequestID))
238+
239+
currentCancelledCount := unittest.GetCount(t, &actions_model.ActionRun{Status: actions_model.StatusCancelled})
240+
assert.Equal(t, previousCancelledCount+1, currentCancelledCount)
241+
run := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{ID: runNotApproved.ID})
242+
assert.Equal(t, actions_model.StatusCancelled.String(), run.Status.String())
243+
})
230244
}
231245

232246
func TestActionsTrust_GetPullRequestUserIsTrustedWithActions(t *testing.T) {

tests/integration/actions_trust_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
actions_model "forgejo.org/models/actions"
15+
auth_model "forgejo.org/models/auth"
1516
issues_model "forgejo.org/models/issues"
1617
repo_model "forgejo.org/models/repo"
1718
unit_model "forgejo.org/models/unit"
@@ -436,3 +437,37 @@ func TestActionsPullRequestTrustPanelWIPConflicts(t *testing.T) {
436437
})
437438
})
438439
}
440+
441+
func TestActionsPullRequestTrustCancelOnClose(t *testing.T) {
442+
onApplicationRun(t, func(t *testing.T, u *url.URL) {
443+
ownerUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
444+
445+
regularUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5})
446+
regularSession := loginUser(t, regularUser.Name)
447+
token := getTokenForLoggedInUser(t, regularSession, auth_model.AccessTokenScopeWriteIssue)
448+
449+
baseRepo, f := actionsTrustTestCreateBaseRepo(t, ownerUser)
450+
defer f()
451+
452+
_, pullRequest, addFileToForkedResp := actionsTrustTestCreatePullRequestFromForkedRepo(t, ownerUser, baseRepo, regularUser)
453+
prAPILink := pullRequest.Issue.APIURL(t.Context())
454+
455+
{
456+
actionRun := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{RepoID: baseRepo.ID, CommitSHA: addFileToForkedResp.Commit.SHA})
457+
assert.True(t, actionRun.NeedApproval)
458+
actionRunJob := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunJob{RunID: actionRun.ID, RepoID: baseRepo.ID})
459+
assert.Equal(t, actions_model.StatusBlocked, actionRunJob.Status)
460+
}
461+
462+
req := NewRequestWithJSON(t, "PATCH", prAPILink, &structs.PullRequest{State: "closed"}).AddTokenAuth(token)
463+
MakeRequest(t, req, http.StatusCreated)
464+
465+
{
466+
actionRun := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{RepoID: baseRepo.ID, CommitSHA: addFileToForkedResp.Commit.SHA})
467+
assert.False(t, actionRun.NeedApproval)
468+
assert.Equal(t, actions_model.StatusCancelled, actionRun.Status)
469+
actionRunJob := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunJob{RunID: actionRun.ID, RepoID: baseRepo.ID})
470+
assert.Equal(t, actions_model.StatusCancelled, actionRunJob.Status)
471+
}
472+
})
473+
}

0 commit comments

Comments
 (0)