Skip to content

Commit 7b4dba3

Browse files
earl-warrenEarl Warren
authored andcommitted
[ACTIONS] skip superflous pull request synchronized event (go-gitea#2314)
Skip a HookEventPullRequestSync event if it has the same CommitSHA as an existing HookEventPullRequest event in the ActionRun table. A HookEventPullRequestSync event must only create an ActionRun if the CommitSHA is different from what it was when the PR was open. This guards against a race that can happen when the following is done in parallel: * A commit C is pushed to a repo on branch B * A pull request with head on branch B it is then possible that the pull request is created first, successfully. The commit that was just pushed is not known yet but the PR only references the repository and the B branch so it is fine. A HookEventPullRequest event is sent to the notification queue but not processed immediately. The commit C is pushed and processed successfully. Since the PR already exists and has a head that matches the branch, the head of the PR is updated with the commit C and a HookEventPullRequestSync event is sent to the notification queue. The HookEventPullRequest event is processed and since the head of the PR was updated to be commit C, an ActionRun with CommitSHA C is created. The HookEventPullRequestSync event is then processed and also has a CommitSHA equal to C. Refs: https://codeberg.org/forgejo/forgejo/issues/2009 Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/2314 Co-authored-by: Earl Warren <[email protected]> Co-committed-by: Earl Warren <[email protected]>
1 parent 40b9f39 commit 7b4dba3

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

services/actions/main_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2024 The Forgejo Authors
2+
// SPDX-License-Identifier: MIT
3+
4+
package actions
5+
6+
import (
7+
"testing"
8+
9+
"code.gitea.io/gitea/models/unittest"
10+
11+
_ "code.gitea.io/gitea/models/actions"
12+
_ "code.gitea.io/gitea/models/activities"
13+
)
14+
15+
func TestMain(m *testing.M) {
16+
unittest.MainTest(m)
17+
}

services/actions/notifier_helper.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ func notify(ctx context.Context, input *notifyInput) error {
152152
return nil
153153
}
154154

155+
if SkipPullRequestEvent(ctx, input.Event, input.Repo.ID, commit.ID.String()) {
156+
log.Trace("repo %s with commit %s skip event %v", input.Repo.RepoPath(), commit.ID, input.Event)
157+
return nil
158+
}
159+
155160
var detectedWorkflows []*actions_module.DetectedWorkflow
156161
actionsConfig := input.Repo.MustGetUnit(ctx, unit_model.TypeActions).ActionsConfig()
157162
workflows, schedules, err := actions_module.DetectWorkflows(gitRepo, commit, input.Event, input.Payload)
@@ -203,6 +208,24 @@ func notify(ctx context.Context, input *notifyInput) error {
203208
return handleWorkflows(ctx, detectedWorkflows, commit, input, ref)
204209
}
205210

211+
func SkipPullRequestEvent(ctx context.Context, event webhook_module.HookEventType, repoID int64, commitSHA string) bool {
212+
if event != webhook_module.HookEventPullRequestSync {
213+
return false
214+
}
215+
216+
run := actions_model.ActionRun{
217+
Event: webhook_module.HookEventPullRequest,
218+
RepoID: repoID,
219+
CommitSHA: commitSHA,
220+
}
221+
exist, err := db.GetEngine(ctx).Exist(&run)
222+
if err != nil {
223+
log.Error("Exist ActionRun %v: %v", run, err)
224+
return false
225+
}
226+
return exist
227+
}
228+
206229
func skipWorkflowsForCommit(input *notifyInput, commit *git.Commit) bool {
207230
// skip workflow runs with a configured skip-ci string in commit message if the event is push or pull_request(_sync)
208231
// https://docs.github.com/en/actions/managing-workflow-runs/skipping-workflow-runs
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2024 The Forgejo Authors
2+
// SPDX-License-Identifier: MIT
3+
4+
package actions
5+
6+
import (
7+
"testing"
8+
9+
actions_model "code.gitea.io/gitea/models/actions"
10+
"code.gitea.io/gitea/models/db"
11+
"code.gitea.io/gitea/models/unittest"
12+
webhook_module "code.gitea.io/gitea/modules/webhook"
13+
14+
"github.com/stretchr/testify/assert"
15+
)
16+
17+
func Test_SkipPullRequestEvent(t *testing.T) {
18+
assert.NoError(t, unittest.PrepareTestDatabase())
19+
20+
repoID := int64(1)
21+
commitSHA := "1234"
22+
23+
// event is not webhook_module.HookEventPullRequestSync, never skip
24+
assert.False(t, SkipPullRequestEvent(db.DefaultContext, webhook_module.HookEventPullRequest, repoID, commitSHA))
25+
26+
// event is webhook_module.HookEventPullRequestSync but there is nothing in the ActionRun table, do not skip
27+
assert.False(t, SkipPullRequestEvent(db.DefaultContext, webhook_module.HookEventPullRequestSync, repoID, commitSHA))
28+
29+
// there is a webhook_module.HookEventPullRequest event but the SHA is different, do not skip
30+
index := int64(1)
31+
run := &actions_model.ActionRun{
32+
Index: index,
33+
Event: webhook_module.HookEventPullRequest,
34+
RepoID: repoID,
35+
CommitSHA: "othersha",
36+
}
37+
unittest.AssertSuccessfulInsert(t, run)
38+
assert.False(t, SkipPullRequestEvent(db.DefaultContext, webhook_module.HookEventPullRequestSync, repoID, commitSHA))
39+
40+
// there already is a webhook_module.HookEventPullRequest with the same SHA, skip
41+
index++
42+
run = &actions_model.ActionRun{
43+
Index: index,
44+
Event: webhook_module.HookEventPullRequest,
45+
RepoID: repoID,
46+
CommitSHA: commitSHA,
47+
}
48+
unittest.AssertSuccessfulInsert(t, run)
49+
assert.True(t, SkipPullRequestEvent(db.DefaultContext, webhook_module.HookEventPullRequestSync, repoID, commitSHA))
50+
}

tests/integration/actions_trigger_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
actions_module "code.gitea.io/gitea/modules/actions"
1919
"code.gitea.io/gitea/modules/git"
2020
"code.gitea.io/gitea/modules/setting"
21+
webhook_module "code.gitea.io/gitea/modules/webhook"
22+
actions_service "code.gitea.io/gitea/services/actions"
2123
pull_service "code.gitea.io/gitea/services/pull"
2224
repo_service "code.gitea.io/gitea/services/repository"
2325
files_service "code.gitea.io/gitea/services/repository/files"
@@ -120,6 +122,8 @@ func TestPullRequestTargetEvent(t *testing.T) {
120122
}
121123
err = pull_service.NewPullRequest(git.DefaultContext, baseRepo, pullIssue, nil, nil, pullRequest, nil)
122124
assert.NoError(t, err)
125+
// if a PR "synchronized" event races the "opened" event by having the same SHA, it must be skipped. See https://codeberg.org/forgejo/forgejo/issues/2009.
126+
assert.True(t, actions_service.SkipPullRequestEvent(git.DefaultContext, webhook_module.HookEventPullRequestSync, baseRepo.ID, addFileToForkedResp.Commit.SHA))
123127

124128
// load and compare ActionRun
125129
assert.Equal(t, 1, unittest.GetCount(t, &actions_model.ActionRun{RepoID: baseRepo.ID}))

0 commit comments

Comments
 (0)