mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-24 07:13:33 +01:00
Backport #29752 --------- Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> (cherry picked from commit aae96cc62b6e6642fa1ef3dcacb716d32f5652b9)
This commit is contained in:
parent
120a173e24
commit
2051763969
2 changed files with 135 additions and 3 deletions
|
@ -348,6 +348,12 @@ func (*actionsNotifier) MergePullRequest(ctx context.Context, doer *user_model.U
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *actionsNotifier) PushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
|
func (n *actionsNotifier) PushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
|
||||||
|
commitID, _ := git.NewIDFromString(opts.NewCommitID)
|
||||||
|
if commitID.IsZero() {
|
||||||
|
log.Trace("new commitID is empty")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
ctx = withMethod(ctx, "PushCommits")
|
ctx = withMethod(ctx, "PushCommits")
|
||||||
|
|
||||||
apiPusher := convert.ToUser(ctx, pusher, nil)
|
apiPusher := convert.ToUser(ctx, pusher, nil)
|
||||||
|
@ -380,9 +386,9 @@ func (n *actionsNotifier) CreateRef(ctx context.Context, pusher *user_model.User
|
||||||
apiRepo := convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeNone})
|
apiRepo := convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeNone})
|
||||||
|
|
||||||
newNotifyInput(repo, pusher, webhook_module.HookEventCreate).
|
newNotifyInput(repo, pusher, webhook_module.HookEventCreate).
|
||||||
WithRef(refFullName.ShortName()). // FIXME: should we use a full ref name
|
WithRef(refFullName.String()).
|
||||||
WithPayload(&api.CreatePayload{
|
WithPayload(&api.CreatePayload{
|
||||||
Ref: refFullName.ShortName(),
|
Ref: refFullName.String(),
|
||||||
Sha: refID,
|
Sha: refID,
|
||||||
RefType: refFullName.RefType(),
|
RefType: refFullName.RefType(),
|
||||||
Repo: apiRepo,
|
Repo: apiRepo,
|
||||||
|
@ -400,7 +406,7 @@ func (n *actionsNotifier) DeleteRef(ctx context.Context, pusher *user_model.User
|
||||||
newNotifyInput(repo, pusher, webhook_module.HookEventDelete).
|
newNotifyInput(repo, pusher, webhook_module.HookEventDelete).
|
||||||
WithRef(refFullName.ShortName()). // FIXME: should we use a full ref name
|
WithRef(refFullName.ShortName()). // FIXME: should we use a full ref name
|
||||||
WithPayload(&api.DeletePayload{
|
WithPayload(&api.DeletePayload{
|
||||||
Ref: refFullName.ShortName(),
|
Ref: refFullName.String(),
|
||||||
RefType: refFullName.RefType(),
|
RefType: refFullName.RefType(),
|
||||||
PusherType: api.PusherTypeUser,
|
PusherType: api.PusherTypeUser,
|
||||||
Repo: apiRepo,
|
Repo: apiRepo,
|
||||||
|
@ -457,6 +463,10 @@ func (n *actionsNotifier) UpdateRelease(ctx context.Context, doer *user_model.Us
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *actionsNotifier) DeleteRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) {
|
func (n *actionsNotifier) DeleteRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) {
|
||||||
|
if rel.IsTag {
|
||||||
|
// has sent same action in `PushCommits`, so skip it.
|
||||||
|
return
|
||||||
|
}
|
||||||
ctx = withMethod(ctx, "DeleteRelease")
|
ctx = withMethod(ctx, "DeleteRelease")
|
||||||
notifyRelease(ctx, doer, rel, api.HookReleaseDeleted)
|
notifyRelease(ctx, doer, rel, api.HookReleaseDeleted)
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ import (
|
||||||
|
|
||||||
actions_model "code.gitea.io/gitea/models/actions"
|
actions_model "code.gitea.io/gitea/models/actions"
|
||||||
"code.gitea.io/gitea/models/db"
|
"code.gitea.io/gitea/models/db"
|
||||||
|
git_model "code.gitea.io/gitea/models/git"
|
||||||
issues_model "code.gitea.io/gitea/models/issues"
|
issues_model "code.gitea.io/gitea/models/issues"
|
||||||
repo_model "code.gitea.io/gitea/models/repo"
|
repo_model "code.gitea.io/gitea/models/repo"
|
||||||
unit_model "code.gitea.io/gitea/models/unit"
|
unit_model "code.gitea.io/gitea/models/unit"
|
||||||
|
@ -21,6 +22,7 @@ import (
|
||||||
webhook_module "code.gitea.io/gitea/modules/webhook"
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
||||||
actions_service "code.gitea.io/gitea/services/actions"
|
actions_service "code.gitea.io/gitea/services/actions"
|
||||||
pull_service "code.gitea.io/gitea/services/pull"
|
pull_service "code.gitea.io/gitea/services/pull"
|
||||||
|
release_service "code.gitea.io/gitea/services/release"
|
||||||
repo_service "code.gitea.io/gitea/services/repository"
|
repo_service "code.gitea.io/gitea/services/repository"
|
||||||
files_service "code.gitea.io/gitea/services/repository/files"
|
files_service "code.gitea.io/gitea/services/repository/files"
|
||||||
|
|
||||||
|
@ -198,3 +200,123 @@ func TestPullRequestTargetEvent(t *testing.T) {
|
||||||
assert.Equal(t, 1, unittest.GetCount(t, &actions_model.ActionRun{RepoID: baseRepo.ID}))
|
assert.Equal(t, 1, unittest.GetCount(t, &actions_model.ActionRun{RepoID: baseRepo.ID}))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCreateDeleteRefEvent(t *testing.T) {
|
||||||
|
onGiteaRun(t, func(t *testing.T, u *url.URL) {
|
||||||
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||||||
|
|
||||||
|
// create the repo
|
||||||
|
repo, err := repo_service.CreateRepository(db.DefaultContext, user2, user2, repo_service.CreateRepoOptions{
|
||||||
|
Name: "create-delete-ref-event",
|
||||||
|
Description: "test create delete ref ci event",
|
||||||
|
AutoInit: true,
|
||||||
|
Gitignores: "Go",
|
||||||
|
License: "MIT",
|
||||||
|
Readme: "Default",
|
||||||
|
DefaultBranch: "main",
|
||||||
|
IsPrivate: false,
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotEmpty(t, repo)
|
||||||
|
|
||||||
|
// enable actions
|
||||||
|
err = repo_service.UpdateRepositoryUnits(db.DefaultContext, repo, []repo_model.RepoUnit{{
|
||||||
|
RepoID: repo.ID,
|
||||||
|
Type: unit_model.TypeActions,
|
||||||
|
}}, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// reload units
|
||||||
|
repo.Units = nil
|
||||||
|
assert.NoError(t, repo.LoadUnits(db.DefaultContext))
|
||||||
|
|
||||||
|
// add workflow file to the repo
|
||||||
|
addWorkflowToBaseResp, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, user2, &files_service.ChangeRepoFilesOptions{
|
||||||
|
Files: []*files_service.ChangeRepoFile{
|
||||||
|
{
|
||||||
|
Operation: "create",
|
||||||
|
TreePath: ".gitea/workflows/createdelete.yml",
|
||||||
|
ContentReader: strings.NewReader("name: test\non:\n [create,delete]\njobs:\n test:\n runs-on: ubuntu-latest\n steps:\n - run: echo helloworld\n"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Message: "add workflow",
|
||||||
|
OldBranch: "main",
|
||||||
|
NewBranch: "main",
|
||||||
|
Author: &files_service.IdentityOptions{
|
||||||
|
Name: user2.Name,
|
||||||
|
Email: user2.Email,
|
||||||
|
},
|
||||||
|
Committer: &files_service.IdentityOptions{
|
||||||
|
Name: user2.Name,
|
||||||
|
Email: user2.Email,
|
||||||
|
},
|
||||||
|
Dates: &files_service.CommitDateOptions{
|
||||||
|
Author: time.Now(),
|
||||||
|
Committer: time.Now(),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotEmpty(t, addWorkflowToBaseResp)
|
||||||
|
|
||||||
|
// Get the commit ID of the default branch
|
||||||
|
gitRepo, err := git.OpenRepository(git.DefaultContext, repo_model.RepoPath(user2.Name, repo.Name))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
defer gitRepo.Close()
|
||||||
|
branch, err := git_model.GetBranch(db.DefaultContext, repo.ID, repo.DefaultBranch)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// create a branch
|
||||||
|
err = repo_service.CreateNewBranchFromCommit(db.DefaultContext, user2, repo, gitRepo, branch.CommitID, "test-create-branch")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
run := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{
|
||||||
|
Title: "add workflow",
|
||||||
|
RepoID: repo.ID,
|
||||||
|
Event: "create",
|
||||||
|
Ref: "refs/heads/test-create-branch",
|
||||||
|
WorkflowID: "createdelete.yml",
|
||||||
|
CommitSHA: branch.CommitID,
|
||||||
|
})
|
||||||
|
assert.NotNil(t, run)
|
||||||
|
|
||||||
|
// create a tag
|
||||||
|
err = release_service.CreateNewTag(db.DefaultContext, user2, repo, branch.CommitID, "test-create-tag", "test create tag event")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
run = unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{
|
||||||
|
Title: "add workflow",
|
||||||
|
RepoID: repo.ID,
|
||||||
|
Event: "create",
|
||||||
|
Ref: "refs/tags/test-create-tag",
|
||||||
|
WorkflowID: "createdelete.yml",
|
||||||
|
CommitSHA: branch.CommitID,
|
||||||
|
})
|
||||||
|
assert.NotNil(t, run)
|
||||||
|
|
||||||
|
// delete the branch
|
||||||
|
err = repo_service.DeleteBranch(db.DefaultContext, user2, repo, gitRepo, "test-create-branch")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
run = unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{
|
||||||
|
Title: "add workflow",
|
||||||
|
RepoID: repo.ID,
|
||||||
|
Event: "delete",
|
||||||
|
Ref: "main",
|
||||||
|
WorkflowID: "createdelete.yml",
|
||||||
|
CommitSHA: branch.CommitID,
|
||||||
|
})
|
||||||
|
assert.NotNil(t, run)
|
||||||
|
|
||||||
|
// delete the tag
|
||||||
|
tag, err := repo_model.GetRelease(db.DefaultContext, repo.ID, "test-create-tag")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
err = release_service.DeleteReleaseByID(db.DefaultContext, repo, tag, user2, true)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
run = unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{
|
||||||
|
Title: "add workflow",
|
||||||
|
RepoID: repo.ID,
|
||||||
|
Event: "delete",
|
||||||
|
Ref: "main",
|
||||||
|
WorkflowID: "createdelete.yml",
|
||||||
|
CommitSHA: branch.CommitID,
|
||||||
|
})
|
||||||
|
assert.NotNil(t, run)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue