forgejo/tests/integration/actions_run_now_done_notification_test.go
Earl Warren c0eeb75322
Some checks are pending
/ release (push) Waiting to run
testing-integration / test-unit (push) Waiting to run
testing-integration / test-sqlite (push) Waiting to run
testing / test-unit (push) Blocked by required conditions
testing / backend-checks (push) Waiting to run
testing / frontend-checks (push) Waiting to run
testing / test-e2e (push) Blocked by required conditions
testing / test-remote-cacher (redis) (push) Blocked by required conditions
testing / test-remote-cacher (valkey) (push) Blocked by required conditions
testing / test-remote-cacher (garnet) (push) Blocked by required conditions
testing / test-remote-cacher (redict) (push) Blocked by required conditions
testing / test-mysql (push) Blocked by required conditions
testing / test-pgsql (push) Blocked by required conditions
testing / test-sqlite (push) Blocked by required conditions
testing / security-check (push) Blocked by required conditions
fix: disable Forgejo Actions email notifications on recovery (#8374)
- Make the migration to add an index a noop - do not remove it as it would break v12.next & v13.next
- Keep the logic that relies on finding the last run, only always fail to find which is the same as assuming each run is one of a kind

Refs forgejo/forgejo#8373

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8374
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: Earl Warren <contact@earl-warren.org>
Co-committed-by: Earl Warren <contact@earl-warren.org>
2025-07-02 19:23:07 +02:00

145 lines
4.2 KiB
Go

// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package integration
import (
"context"
"net/url"
"strings"
"testing"
"time"
actions_model "forgejo.org/models/actions"
"forgejo.org/models/db"
unit_model "forgejo.org/models/unit"
"forgejo.org/models/unittest"
user_model "forgejo.org/models/user"
"forgejo.org/modules/gitrepo"
"forgejo.org/modules/setting"
actions_service "forgejo.org/services/actions"
notify_service "forgejo.org/services/notify"
files_service "forgejo.org/services/repository/files"
"forgejo.org/tests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type mockNotifier struct {
notify_service.NullNotifier
testIdx int
t *testing.T
runID int64
lastRunID int64
}
var _ notify_service.Notifier = &mockNotifier{}
func (m *mockNotifier) ActionRunNowDone(ctx context.Context, run *actions_model.ActionRun, priorStatus actions_model.Status, lastRun *actions_model.ActionRun) {
switch m.testIdx {
case 0:
// we accept the first id as okay and just check that the following ones make sense
m.runID = run.ID
assert.Equal(m.t, actions_model.StatusSuccess, run.Status)
assert.Equal(m.t, actions_model.StatusRunning, priorStatus)
assert.Nil(m.t, lastRun)
assert.True(m.t, run.NotifyEmail)
case 1:
assert.Equal(m.t, m.runID, run.ID)
assert.Equal(m.t, actions_model.StatusFailure, run.Status)
assert.Equal(m.t, actions_model.StatusRunning, priorStatus)
assert.True(m.t, run.NotifyEmail)
case 2:
assert.Equal(m.t, m.runID, run.ID)
assert.Equal(m.t, actions_model.StatusCancelled, run.Status)
assert.Equal(m.t, actions_model.StatusRunning, priorStatus)
assert.True(m.t, run.NotifyEmail)
default:
assert.Fail(m.t, "too many notifications")
}
m.runID++
m.testIdx++
}
// ensure all tests have been run
func (m *mockNotifier) complete() {
assert.Equal(m.t, 3, m.testIdx)
}
func TestActionNowDoneNotification(t *testing.T) {
if !setting.Database.Type.IsSQLite3() {
t.Skip()
}
onGiteaRun(t, func(t *testing.T, u *url.URL) {
notifier := mockNotifier{t: t, testIdx: 0, lastRunID: -1, runID: -1}
notify_service.RegisterNotifier(&notifier)
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
// create the repo
repo, sha, f := tests.CreateDeclarativeRepo(t, user2, "repo-workflow-dispatch",
[]unit_model.Type{unit_model.TypeActions}, nil,
[]*files_service.ChangeRepoFile{
{
Operation: "create",
TreePath: ".forgejo/workflows/dispatch.yml",
ContentReader: strings.NewReader(
"name: test\n" +
"enable-email-notifications: true\n" +
"on: [workflow_dispatch]\n" +
"jobs:\n" +
" test:\n" +
" runs-on: ubuntu-latest\n" +
" steps:\n" +
" - run: echo helloworld\n",
),
},
},
)
defer f()
gitRepo, err := gitrepo.OpenRepository(db.DefaultContext, repo)
require.NoError(t, err)
defer gitRepo.Close()
workflow, err := actions_service.GetWorkflowFromCommit(gitRepo, "main", "dispatch.yml")
require.NoError(t, err)
assert.Equal(t, "refs/heads/main", workflow.Ref)
assert.Equal(t, sha, workflow.Commit.ID.String())
inputGetter := func(key string) string {
return ""
}
runner := newMockRunner()
runner.registerAsRepoRunner(t, user2.Name, repo.Name, "mock-runner", []string{"ubuntu-latest"})
// 0: first successful run
_, _, err = workflow.Dispatch(db.DefaultContext, inputGetter, repo, user2)
require.NoError(t, err)
task := runner.fetchTask(t)
runner.succeedAtTask(t, task)
// we can't differentiate different runs without a delay
time.Sleep(time.Millisecond * 2000)
// 1: failed run
_, _, err = workflow.Dispatch(db.DefaultContext, inputGetter, repo, user2)
require.NoError(t, err)
task = runner.fetchTask(t)
runner.failAtTask(t, task)
// we can't differentiate different runs without a delay
time.Sleep(time.Millisecond * 2000)
// 2: canceled run
_, _, err = workflow.Dispatch(db.DefaultContext, inputGetter, repo, user2)
require.NoError(t, err)
task = runner.fetchTask(t)
require.NoError(t, actions_service.StopTask(db.DefaultContext, task.Id, actions_model.StatusCancelled))
notifier.complete()
})
}