Skip to content

Commit aae96cc

Browse files
yp05327lunny
andauthored
Fix invalid link of the commit status when ref is tag (#29752) (#29908)
Backport #29752 --------- Co-authored-by: Lunny Xiao <[email protected]>
1 parent 5f7b6b5 commit aae96cc

File tree

2 files changed

+135
-3
lines changed

2 files changed

+135
-3
lines changed

services/actions/notifier.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,12 @@ func (*actionsNotifier) MergePullRequest(ctx context.Context, doer *user_model.U
516516
}
517517

518518
func (n *actionsNotifier) PushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
519+
commitID, _ := git.NewIDFromString(opts.NewCommitID)
520+
if commitID.IsZero() {
521+
log.Trace("new commitID is empty")
522+
return
523+
}
524+
519525
ctx = withMethod(ctx, "PushCommits")
520526

521527
apiPusher := convert.ToUser(ctx, pusher, nil)
@@ -548,9 +554,9 @@ func (n *actionsNotifier) CreateRef(ctx context.Context, pusher *user_model.User
548554
apiRepo := convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeNone})
549555

550556
newNotifyInput(repo, pusher, webhook_module.HookEventCreate).
551-
WithRef(refFullName.ShortName()). // FIXME: should we use a full ref name
557+
WithRef(refFullName.String()).
552558
WithPayload(&api.CreatePayload{
553-
Ref: refFullName.ShortName(),
559+
Ref: refFullName.String(),
554560
Sha: refID,
555561
RefType: refFullName.RefType(),
556562
Repo: apiRepo,
@@ -567,7 +573,7 @@ func (n *actionsNotifier) DeleteRef(ctx context.Context, pusher *user_model.User
567573

568574
newNotifyInput(repo, pusher, webhook_module.HookEventDelete).
569575
WithPayload(&api.DeletePayload{
570-
Ref: refFullName.ShortName(),
576+
Ref: refFullName.String(),
571577
RefType: refFullName.RefType(),
572578
PusherType: api.PusherTypeUser,
573579
Repo: apiRepo,
@@ -624,6 +630,10 @@ func (n *actionsNotifier) UpdateRelease(ctx context.Context, doer *user_model.Us
624630
}
625631

626632
func (n *actionsNotifier) DeleteRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) {
633+
if rel.IsTag {
634+
// has sent same action in `PushCommits`, so skip it.
635+
return
636+
}
627637
ctx = withMethod(ctx, "DeleteRelease")
628638
notifyRelease(ctx, doer, rel, api.HookReleaseDeleted)
629639
}

tests/integration/actions_trigger_test.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
actions_model "code.gitea.io/gitea/models/actions"
1313
"code.gitea.io/gitea/models/db"
14+
git_model "code.gitea.io/gitea/models/git"
1415
issues_model "code.gitea.io/gitea/models/issues"
1516
repo_model "code.gitea.io/gitea/models/repo"
1617
unit_model "code.gitea.io/gitea/models/unit"
@@ -19,6 +20,7 @@ import (
1920
actions_module "code.gitea.io/gitea/modules/actions"
2021
"code.gitea.io/gitea/modules/git"
2122
pull_service "code.gitea.io/gitea/services/pull"
23+
release_service "code.gitea.io/gitea/services/release"
2224
repo_service "code.gitea.io/gitea/services/repository"
2325
files_service "code.gitea.io/gitea/services/repository/files"
2426

@@ -194,3 +196,123 @@ func TestPullRequestTargetEvent(t *testing.T) {
194196
assert.Equal(t, 1, unittest.GetCount(t, &actions_model.ActionRun{RepoID: baseRepo.ID}))
195197
})
196198
}
199+
200+
func TestCreateDeleteRefEvent(t *testing.T) {
201+
onGiteaRun(t, func(t *testing.T, u *url.URL) {
202+
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
203+
204+
// create the repo
205+
repo, err := repo_service.CreateRepository(db.DefaultContext, user2, user2, repo_service.CreateRepoOptions{
206+
Name: "create-delete-ref-event",
207+
Description: "test create delete ref ci event",
208+
AutoInit: true,
209+
Gitignores: "Go",
210+
License: "MIT",
211+
Readme: "Default",
212+
DefaultBranch: "main",
213+
IsPrivate: false,
214+
})
215+
assert.NoError(t, err)
216+
assert.NotEmpty(t, repo)
217+
218+
// enable actions
219+
err = repo_service.UpdateRepositoryUnits(db.DefaultContext, repo, []repo_model.RepoUnit{{
220+
RepoID: repo.ID,
221+
Type: unit_model.TypeActions,
222+
}}, nil)
223+
assert.NoError(t, err)
224+
225+
// reload units
226+
repo.Units = nil
227+
assert.NoError(t, repo.LoadUnits(db.DefaultContext))
228+
229+
// add workflow file to the repo
230+
addWorkflowToBaseResp, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, user2, &files_service.ChangeRepoFilesOptions{
231+
Files: []*files_service.ChangeRepoFile{
232+
{
233+
Operation: "create",
234+
TreePath: ".gitea/workflows/createdelete.yml",
235+
ContentReader: strings.NewReader("name: test\non:\n [create,delete]\njobs:\n test:\n runs-on: ubuntu-latest\n steps:\n - run: echo helloworld\n"),
236+
},
237+
},
238+
Message: "add workflow",
239+
OldBranch: "main",
240+
NewBranch: "main",
241+
Author: &files_service.IdentityOptions{
242+
Name: user2.Name,
243+
Email: user2.Email,
244+
},
245+
Committer: &files_service.IdentityOptions{
246+
Name: user2.Name,
247+
Email: user2.Email,
248+
},
249+
Dates: &files_service.CommitDateOptions{
250+
Author: time.Now(),
251+
Committer: time.Now(),
252+
},
253+
})
254+
assert.NoError(t, err)
255+
assert.NotEmpty(t, addWorkflowToBaseResp)
256+
257+
// Get the commit ID of the default branch
258+
gitRepo, err := git.OpenRepository(git.DefaultContext, repo_model.RepoPath(user2.Name, repo.Name))
259+
assert.NoError(t, err)
260+
defer gitRepo.Close()
261+
branch, err := git_model.GetBranch(db.DefaultContext, repo.ID, repo.DefaultBranch)
262+
assert.NoError(t, err)
263+
264+
// create a branch
265+
err = repo_service.CreateNewBranchFromCommit(db.DefaultContext, user2, repo, gitRepo, branch.CommitID, "test-create-branch")
266+
assert.NoError(t, err)
267+
run := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{
268+
Title: "add workflow",
269+
RepoID: repo.ID,
270+
Event: "create",
271+
Ref: "refs/heads/test-create-branch",
272+
WorkflowID: "createdelete.yml",
273+
CommitSHA: branch.CommitID,
274+
})
275+
assert.NotNil(t, run)
276+
277+
// create a tag
278+
err = release_service.CreateNewTag(db.DefaultContext, user2, repo, branch.CommitID, "test-create-tag", "test create tag event")
279+
assert.NoError(t, err)
280+
run = unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{
281+
Title: "add workflow",
282+
RepoID: repo.ID,
283+
Event: "create",
284+
Ref: "refs/tags/test-create-tag",
285+
WorkflowID: "createdelete.yml",
286+
CommitSHA: branch.CommitID,
287+
})
288+
assert.NotNil(t, run)
289+
290+
// delete the branch
291+
err = repo_service.DeleteBranch(db.DefaultContext, user2, repo, gitRepo, "test-create-branch")
292+
assert.NoError(t, err)
293+
run = unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{
294+
Title: "add workflow",
295+
RepoID: repo.ID,
296+
Event: "delete",
297+
Ref: "main",
298+
WorkflowID: "createdelete.yml",
299+
CommitSHA: branch.CommitID,
300+
})
301+
assert.NotNil(t, run)
302+
303+
// delete the tag
304+
tag, err := repo_model.GetRelease(db.DefaultContext, repo.ID, "test-create-tag")
305+
assert.NoError(t, err)
306+
err = release_service.DeleteReleaseByID(db.DefaultContext, repo, tag, user2, true)
307+
assert.NoError(t, err)
308+
run = unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{
309+
Title: "add workflow",
310+
RepoID: repo.ID,
311+
Event: "delete",
312+
Ref: "main",
313+
WorkflowID: "createdelete.yml",
314+
CommitSHA: branch.CommitID,
315+
})
316+
assert.NotNil(t, run)
317+
})
318+
}

0 commit comments

Comments
 (0)