From babc109b26e0f9606e5a0e5c37fa0090929da23d Mon Sep 17 00:00:00 2001 From: Mathieu Fenniak Date: Mon, 5 Jan 2026 15:03:46 +0100 Subject: [PATCH] [v11.0/forgejo] fix: don't duplicate commit status records on workflows with empty name (#10681) **Backport:** #10678 (cherry picked from commit 8f63ee9a948814e1b97f5e543b58ee219b127731) Fixes #10671. Cleanup for the inflated number of records in this table will come in a near future change. ## 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. - [ ] 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 - [ ] I do not want this change to show in the release notes. - [x] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/.md` to be be used for the release notes instead of the title. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10681 Reviewed-by: Gusted Co-authored-by: Mathieu Fenniak Co-committed-by: Mathieu Fenniak --- .../TestCreateCommitStatus/action_run.yml | 25 ++++++ .../TestCreateCommitStatus/action_run_job.yml | 26 ++++++ services/actions/commit_status.go | 47 +++++------ services/actions/commit_status_test.go | 79 +++++++++++++++++++ 4 files changed, 155 insertions(+), 22 deletions(-) create mode 100644 services/actions/TestCreateCommitStatus/action_run.yml create mode 100644 services/actions/TestCreateCommitStatus/action_run_job.yml create mode 100644 services/actions/commit_status_test.go diff --git a/services/actions/TestCreateCommitStatus/action_run.yml b/services/actions/TestCreateCommitStatus/action_run.yml new file mode 100644 index 0000000000..cbeef34d70 --- /dev/null +++ b/services/actions/TestCreateCommitStatus/action_run.yml @@ -0,0 +1,25 @@ +- + id: 900 + title: "update actions" + repo_id: 4 + owner_id: 1 + workflow_id: "artifact.yaml" + index: 200 + trigger_user_id: 1 + ref: "refs/heads/master" + commit_sha: "c7cd3cd144e6d23c9d6f3d07e52b2c1a956e0338" + event: "push" + is_fork_pull_request: false + status: 1 + started: 1683636528 + stopped: 1683636626 + created: 1683636108 + updated: 1683636626 + need_approval: false + approved_by: 0 + event_payload: | + { + "head_commit": { + "id": "c7cd3cd144e6d23c9d6f3d07e52b2c1a956e0338" + } + } diff --git a/services/actions/TestCreateCommitStatus/action_run_job.yml b/services/actions/TestCreateCommitStatus/action_run_job.yml new file mode 100644 index 0000000000..1c3b8e4b7d --- /dev/null +++ b/services/actions/TestCreateCommitStatus/action_run_job.yml @@ -0,0 +1,26 @@ +- + id: 400 + run_id: 900 + repo_id: 4 + owner_id: 1 + commit_sha: c7cd3cd144e6d23c9d6f3d07e52b2c1a956e0338 + is_fork_pull_request: false + name: job_2 + attempt: 1 + job_id: job_2 + task_id: 47 + status: 7 # blocked + started: 1683636528 + stopped: 1683636626 + workflow_payload: | + "on": + push: + jobs: + produce-artifacts: + name: produce-artifacts + runs-on: docker + steps: + - run: echo "OK!" + strategy: + matrix: + color: red diff --git a/services/actions/commit_status.go b/services/actions/commit_status.go index 1fffa6852f..deb4602ed8 100644 --- a/services/actions/commit_status.go +++ b/services/actions/commit_status.go @@ -7,6 +7,7 @@ import ( "context" "fmt" "path" + "strings" actions_model "forgejo.org/models/actions" "forgejo.org/models/db" @@ -76,28 +77,6 @@ func createCommitStatus(ctx context.Context, job *actions_model.ActionRunJob) er return nil } - repo := run.Repo - // TODO: store workflow name as a field in ActionRun to avoid parsing - runName := path.Base(run.WorkflowID) - if wfs, err := jobparser.Parse(job.WorkflowPayload); err == nil && len(wfs) > 0 { - runName = wfs[0].Name - } - ctxname := fmt.Sprintf("%s / %s (%s)", runName, job.Name, event) - state := toCommitStatus(job.Status) - if statuses, _, err := git_model.GetLatestCommitStatus(ctx, repo.ID, sha, db.ListOptionsAll); err == nil { - for _, v := range statuses { - if v.Context == ctxname { - if v.State == state { - // no need to update - return nil - } - break - } - } - } else { - return fmt.Errorf("GetLatestCommitStatus: %w", err) - } - description := "" switch job.Status { // TODO: if we want support description in different languages, we need to support i18n placeholders in it @@ -117,6 +96,30 @@ func createCommitStatus(ctx context.Context, job *actions_model.ActionRunJob) er description = "Blocked by required conditions" } + repo := run.Repo + // TODO: store workflow name as a field in ActionRun to avoid parsing + runName := path.Base(run.WorkflowID) + if wfs, err := jobparser.Parse(job.WorkflowPayload); err == nil && len(wfs) > 0 { + runName = wfs[0].Name + } + ctxname := fmt.Sprintf("%s / %s (%s)", runName, job.Name, event) + state := toCommitStatus(job.Status) + if statuses, _, err := git_model.GetLatestCommitStatus(ctx, repo.ID, sha, db.ListOptionsAll); err == nil { + for _, v := range statuses { + // TrimSpace(ctxname) & TrimSpace(description) to match stored value which is trimmed in + // `git.NewCommitStatus` + if v.Context == strings.TrimSpace(ctxname) { + if v.State == state && v.Description == strings.TrimSpace(description) { + // no need to update + return nil + } + break + } + } + } else { + return fmt.Errorf("GetLatestCommitStatus: %w", err) + } + index, err := getIndexOfJob(ctx, job) if err != nil { return fmt.Errorf("getIndexOfJob: %w", err) diff --git a/services/actions/commit_status_test.go b/services/actions/commit_status_test.go new file mode 100644 index 0000000000..1445426efb --- /dev/null +++ b/services/actions/commit_status_test.go @@ -0,0 +1,79 @@ +// Copyright 2025 The Forgejo Authors. All rights reserved. +// SPDX-License-Identifier: GPL-3.0-or-later + +package actions + +import ( + "testing" + + actions_model "forgejo.org/models/actions" + git_model "forgejo.org/models/git" + "forgejo.org/models/unittest" + "forgejo.org/modules/cache" + "forgejo.org/modules/structs" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "xorm.io/builder" +) + +func TestCreateCommitStatus_AvoidsDuplicates(t *testing.T) { + defer unittest.OverrideFixtures("services/actions/TestCreateCommitStatus")() + require.NoError(t, unittest.PrepareTestDatabase()) + cache.Init() + + job := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunJob{ID: 400}) + targetCommitStatusFilter := builder.Eq{"repo_id": 4, "sha": job.CommitSHA} + + // Begin with 0 commit statuses + assert.Equal(t, 0, unittest.GetCount(t, &git_model.CommitStatus{}, targetCommitStatusFilter)) + + err := createCommitStatus(t.Context(), job) + require.NoError(t, err) + + // Should have 1 commit status now with one createCommitStatus call + assert.Equal(t, 1, unittest.GetCount(t, &git_model.CommitStatus{}, targetCommitStatusFilter)) + status := unittest.AssertExistsAndLoadBean(t, &git_model.CommitStatus{}, targetCommitStatusFilter) + assert.EqualValues(t, 4, status.RepoID) + assert.Equal(t, structs.CommitStatusState("pending"), status.State) + assert.Equal(t, "c7cd3cd144e6d23c9d6f3d07e52b2c1a956e0338", status.SHA) + assert.Equal(t, "/user5/repo4/actions/runs/200/jobs/0", status.TargetURL) + assert.Equal(t, "Blocked by required conditions", status.Description) + assert.Equal(t, "39c46bc9f0f68e0dcfbb59118e12778fa095b066", status.ContextHash) + assert.Equal(t, "/ job_2 (push)", status.Context) // This test is intended to cover the runName = "" case, which trims whitespace in this context string -- don't change it in the future + assert.EqualValues(t, 1, status.Index) + + // No status change, but createCommitStatus invoked again + err = createCommitStatus(t.Context(), job) + require.NoError(t, err) + + // Should have just the same 1 commit status since the context & state was unchanged. + assert.Equal(t, 1, unittest.GetCount(t, &git_model.CommitStatus{}, targetCommitStatusFilter)) + + // Change status, but still pending -- should add new commit status just for the Description change + job.Status = actions_model.StatusWaiting // Blocked -> Waiting + err = createCommitStatus(t.Context(), job) + require.NoError(t, err) + + // New commit status added + assert.Equal(t, 2, unittest.GetCount(t, &git_model.CommitStatus{}, targetCommitStatusFilter)) + status = unittest.AssertExistsAndLoadBean(t, &git_model.CommitStatus{Index: 2}, targetCommitStatusFilter) + assert.Equal(t, structs.CommitStatusState("pending"), status.State) + assert.Equal(t, "Waiting to run", status.Description) + + // Invoke createCommitStatus again, check no new record created again + err = createCommitStatus(t.Context(), job) + require.NoError(t, err) + assert.Equal(t, 2, unittest.GetCount(t, &git_model.CommitStatus{}, targetCommitStatusFilter)) + + // Update job status & create new commit status + job.Status = actions_model.StatusSuccess + err = createCommitStatus(t.Context(), job) + require.NoError(t, err) + + // New commit status added w/ updated state & description + assert.Equal(t, 3, unittest.GetCount(t, &git_model.CommitStatus{}, targetCommitStatusFilter)) + status = unittest.AssertExistsAndLoadBean(t, &git_model.CommitStatus{Index: 3}, targetCommitStatusFilter) + assert.Equal(t, structs.CommitStatusState("success"), status.State) + assert.Equal(t, "Successful in 1m38s", status.Description) +}