Skip to content

Commit 98217b0

Browse files
authored
Fix invalid link of the commit status when ref is tag (#29752)
Fix #29731 Caused by #24634 Also remove fixme. ps: we can not fix the existed runs, as wrong refs are all recorded in DB, and we can not know whether they are branch or tag: ![image](https://github.com/go-gitea/gitea/assets/18380374/cb7cf266-f73f-419a-be1a-4689fdd1952a)
1 parent 17d7ab5 commit 98217b0

File tree

2 files changed

+132
-3
lines changed

2 files changed

+132
-3
lines changed

services/actions/notifier.go

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

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

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

549555
newNotifyInput(repo, pusher, webhook_module.HookEventCreate).
550-
WithRef(refFullName.ShortName()). // FIXME: should we use a full ref name
556+
WithRef(refFullName.String()).
551557
WithPayload(&api.CreatePayload{
552-
Ref: refFullName.ShortName(),
558+
Ref: refFullName.String(),
553559
Sha: refID,
554560
RefType: refFullName.RefType(),
555561
Repo: apiRepo,
@@ -566,7 +572,7 @@ func (n *actionsNotifier) DeleteRef(ctx context.Context, pusher *user_model.User
566572

567573
newNotifyInput(repo, pusher, webhook_module.HookEventDelete).
568574
WithPayload(&api.DeletePayload{
569-
Ref: refFullName.ShortName(),
575+
Ref: refFullName.String(),
570576
RefType: refFullName.RefType(),
571577
PusherType: api.PusherTypeUser,
572578
Repo: apiRepo,
@@ -623,6 +629,10 @@ func (n *actionsNotifier) UpdateRelease(ctx context.Context, doer *user_model.Us
623629
}
624630

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

tests/integration/actions_trigger_test.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@ import (
1212

1313
actions_model "code.gitea.io/gitea/models/actions"
1414
"code.gitea.io/gitea/models/db"
15+
git_model "code.gitea.io/gitea/models/git"
1516
issues_model "code.gitea.io/gitea/models/issues"
1617
repo_model "code.gitea.io/gitea/models/repo"
1718
unit_model "code.gitea.io/gitea/models/unit"
1819
"code.gitea.io/gitea/models/unittest"
1920
user_model "code.gitea.io/gitea/models/user"
2021
actions_module "code.gitea.io/gitea/modules/actions"
2122
"code.gitea.io/gitea/modules/git"
23+
"code.gitea.io/gitea/modules/gitrepo"
2224
"code.gitea.io/gitea/modules/setting"
2325
"code.gitea.io/gitea/modules/test"
2426
pull_service "code.gitea.io/gitea/services/pull"
27+
release_service "code.gitea.io/gitea/services/release"
2528
repo_service "code.gitea.io/gitea/services/repository"
2629
files_service "code.gitea.io/gitea/services/repository/files"
2730

@@ -324,3 +327,119 @@ func TestSkipCI(t *testing.T) {
324327
assert.Equal(t, 1, unittest.GetCount(t, &actions_model.ActionRun{RepoID: repo.ID}))
325328
})
326329
}
330+
331+
func TestCreateDeleteRefEvent(t *testing.T) {
332+
onGiteaRun(t, func(t *testing.T, u *url.URL) {
333+
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
334+
335+
// create the repo
336+
repo, err := repo_service.CreateRepository(db.DefaultContext, user2, user2, repo_service.CreateRepoOptions{
337+
Name: "create-delete-ref-event",
338+
Description: "test create delete ref ci event",
339+
AutoInit: true,
340+
Gitignores: "Go",
341+
License: "MIT",
342+
Readme: "Default",
343+
DefaultBranch: "main",
344+
IsPrivate: false,
345+
})
346+
assert.NoError(t, err)
347+
assert.NotEmpty(t, repo)
348+
349+
// enable actions
350+
err = repo_service.UpdateRepositoryUnits(db.DefaultContext, repo, []repo_model.RepoUnit{{
351+
RepoID: repo.ID,
352+
Type: unit_model.TypeActions,
353+
}}, nil)
354+
assert.NoError(t, err)
355+
356+
// add workflow file to the repo
357+
addWorkflowToBaseResp, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, user2, &files_service.ChangeRepoFilesOptions{
358+
Files: []*files_service.ChangeRepoFile{
359+
{
360+
Operation: "create",
361+
TreePath: ".gitea/workflows/createdelete.yml",
362+
ContentReader: strings.NewReader("name: test\non:\n [create,delete]\njobs:\n test:\n runs-on: ubuntu-latest\n steps:\n - run: echo helloworld\n"),
363+
},
364+
},
365+
Message: "add workflow",
366+
OldBranch: "main",
367+
NewBranch: "main",
368+
Author: &files_service.IdentityOptions{
369+
Name: user2.Name,
370+
Email: user2.Email,
371+
},
372+
Committer: &files_service.IdentityOptions{
373+
Name: user2.Name,
374+
Email: user2.Email,
375+
},
376+
Dates: &files_service.CommitDateOptions{
377+
Author: time.Now(),
378+
Committer: time.Now(),
379+
},
380+
})
381+
assert.NoError(t, err)
382+
assert.NotEmpty(t, addWorkflowToBaseResp)
383+
384+
// Get the commit ID of the default branch
385+
gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo)
386+
assert.NoError(t, err)
387+
defer gitRepo.Close()
388+
branch, err := git_model.GetBranch(db.DefaultContext, repo.ID, repo.DefaultBranch)
389+
assert.NoError(t, err)
390+
391+
// create a branch
392+
err = repo_service.CreateNewBranchFromCommit(db.DefaultContext, user2, repo, gitRepo, branch.CommitID, "test-create-branch")
393+
assert.NoError(t, err)
394+
run := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{
395+
Title: "add workflow",
396+
RepoID: repo.ID,
397+
Event: "create",
398+
Ref: "refs/heads/test-create-branch",
399+
WorkflowID: "createdelete.yml",
400+
CommitSHA: branch.CommitID,
401+
})
402+
assert.NotNil(t, run)
403+
404+
// create a tag
405+
err = release_service.CreateNewTag(db.DefaultContext, user2, repo, branch.CommitID, "test-create-tag", "test create tag event")
406+
assert.NoError(t, err)
407+
run = unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{
408+
Title: "add workflow",
409+
RepoID: repo.ID,
410+
Event: "create",
411+
Ref: "refs/tags/test-create-tag",
412+
WorkflowID: "createdelete.yml",
413+
CommitSHA: branch.CommitID,
414+
})
415+
assert.NotNil(t, run)
416+
417+
// delete the branch
418+
err = repo_service.DeleteBranch(db.DefaultContext, user2, repo, gitRepo, "test-create-branch")
419+
assert.NoError(t, err)
420+
run = unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{
421+
Title: "add workflow",
422+
RepoID: repo.ID,
423+
Event: "delete",
424+
Ref: "main",
425+
WorkflowID: "createdelete.yml",
426+
CommitSHA: branch.CommitID,
427+
})
428+
assert.NotNil(t, run)
429+
430+
// delete the tag
431+
tag, err := repo_model.GetRelease(db.DefaultContext, repo.ID, "test-create-tag")
432+
assert.NoError(t, err)
433+
err = release_service.DeleteReleaseByID(db.DefaultContext, repo, tag, user2, true)
434+
assert.NoError(t, err)
435+
run = unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{
436+
Title: "add workflow",
437+
RepoID: repo.ID,
438+
Event: "delete",
439+
Ref: "main",
440+
WorkflowID: "createdelete.yml",
441+
CommitSHA: branch.CommitID,
442+
})
443+
assert.NotNil(t, run)
444+
})
445+
}

0 commit comments

Comments
 (0)