mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-12 22:10:25 +00:00
feat: cancel all pull requests runs when blocking a user
This commit is contained in:
parent
3fa7ceeb76
commit
917d0e9fa0
2 changed files with 43 additions and 0 deletions
|
|
@ -9,6 +9,7 @@ import (
|
|||
"forgejo.org/models/db"
|
||||
repo_model "forgejo.org/models/repo"
|
||||
user_model "forgejo.org/models/user"
|
||||
actions_service "forgejo.org/services/actions"
|
||||
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
|
@ -91,5 +92,12 @@ func BlockUser(ctx context.Context, userID, blockID int64) error {
|
|||
return err
|
||||
}
|
||||
|
||||
err = db.Iterate(ctx, builder.Eq{"owner_id": userID}, func(ctx context.Context, repo *repo_model.Repository) error {
|
||||
return actions_service.RevokeTrust(ctx, repo.ID, blockID)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return committer.Commit()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,11 +7,13 @@ import (
|
|||
"testing"
|
||||
|
||||
model "forgejo.org/models"
|
||||
actions_model "forgejo.org/models/actions"
|
||||
"forgejo.org/models/db"
|
||||
issues_model "forgejo.org/models/issues"
|
||||
repo_model "forgejo.org/models/repo"
|
||||
"forgejo.org/models/unittest"
|
||||
user_model "forgejo.org/models/user"
|
||||
actions_module "forgejo.org/modules/actions"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
|
@ -110,4 +112,37 @@ func TestBlockUser(t *testing.T) {
|
|||
_, err = issues_model.ChangeIssueStatus(db.DefaultContext, issue, blockedUser, false)
|
||||
require.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("Pull requests actions are cancelled", func(t *testing.T) {
|
||||
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2, OwnerID: doer.ID})
|
||||
blockedUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4})
|
||||
defer user_model.UnblockUser(db.DefaultContext, doer.ID, blockedUser.ID)
|
||||
|
||||
pullRequestPosterID := blockedUser.ID
|
||||
singleWorkflows, err := actions_module.JobParser([]byte(`
|
||||
jobs:
|
||||
job:
|
||||
runs-on: docker
|
||||
steps:
|
||||
- run: echo OK
|
||||
`))
|
||||
require.NoError(t, err)
|
||||
require.Len(t, singleWorkflows, 1)
|
||||
runWaiting := &actions_model.ActionRun{
|
||||
TriggerUserID: 2,
|
||||
RepoID: repo.ID,
|
||||
Status: actions_model.StatusWaiting,
|
||||
PullRequestPosterID: pullRequestPosterID,
|
||||
}
|
||||
require.NoError(t, actions_model.InsertRun(t.Context(), runWaiting, singleWorkflows))
|
||||
|
||||
run := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{ID: runWaiting.ID})
|
||||
require.Equal(t, actions_model.StatusWaiting.String(), run.Status.String())
|
||||
|
||||
require.NoError(t, BlockUser(db.DefaultContext, doer.ID, blockedUser.ID))
|
||||
|
||||
run = unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{ID: runWaiting.ID})
|
||||
require.Equal(t, actions_model.StatusCancelled.String(), run.Status.String())
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue