From ea421cba4ee3899dd64a28e79a9026fb13a65dab Mon Sep 17 00:00:00 2001 From: Mathieu Fenniak Date: Mon, 5 Jan 2026 14:47:27 +0100 Subject: [PATCH] fix: retain Forgejo Action's commit_status entries with distinct descriptions (#10696) In #10678, I fixed an incorrect codepath that was intended to prevent duplicate redundant entries in `commit_status`. However, the codepath that was repaired didn't take into account changes in the `description` field -- eg. going from `Waiting to run` to `Has started running` both have the `pending` commit status state but are distinct and should be retained. This PR fixes the fix so that changes in description do still cause new entries into the `commit_status` table. This issue was raised due to an end-to-end test failure in the `push-cancel` actions test. I've manually tested that this fixes the end-to-end test. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10696 Reviewed-by: Gusted Co-authored-by: Mathieu Fenniak Co-committed-by: Mathieu Fenniak --- services/actions/commit_status.go | 47 +++++++++++++------------- services/actions/commit_status_test.go | 24 +++++++++++-- 2 files changed, 46 insertions(+), 25 deletions(-) diff --git a/services/actions/commit_status.go b/services/actions/commit_status.go index 5a72a17022..2a8ca2810c 100644 --- a/services/actions/commit_status.go +++ b/services/actions/commit_status.go @@ -85,29 +85,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, false); 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) to match stored value which is trimmed in `git.NewCommitStatus` - if v.Context == strings.TrimSpace(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 @@ -127,6 +104,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, false); 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 index 3252969793..fc39c91bfe 100644 --- a/services/actions/commit_status_test.go +++ b/services/actions/commit_status_test.go @@ -66,6 +66,7 @@ func TestCreateCommitStatus_AvoidsDuplicates(t *testing.T) { 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) @@ -74,11 +75,30 @@ func TestCreateCommitStatus_AvoidsDuplicates(t *testing.T) { // 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) - // Should have 2 commit statuses now - assert.Equal(t, 2, unittest.GetCount(t, &git_model.CommitStatus{}, targetCommitStatusFilter)) + // 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) }