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 <gusted@noreply.codeberg.org>
Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
This commit is contained in:
Mathieu Fenniak 2026-01-05 14:47:27 +01:00 committed by Gusted
parent acdfb122cb
commit ea421cba4e
2 changed files with 46 additions and 25 deletions

View file

@ -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)

View file

@ -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)
}