Skip to content

Commit

Permalink
Merge branch 'main' into fix-missing-cfturnstile-ctx-variables
Browse files Browse the repository at this point in the history
  • Loading branch information
GiteaBot authored Aug 19, 2024
2 parents cf9f6d2 + ccf7366 commit a87d883
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 18 deletions.
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,8 @@ var migrations = []Migration{
NewMigration("Add index to action_task stopped log_expired", v1_23.AddIndexToActionTaskStoppedLogExpired),
// v303 -> v304
NewMigration("Add metadata column for comment table", v1_23.AddCommentMetaDataColumn),
// v304 -> v305
NewMigration("Add index for release sha1", v1_23.AddIndexForReleaseSha1),
}

// GetCurrentDBVersion returns the current db version
Expand Down
13 changes: 13 additions & 0 deletions models/migrations/v1_23/v304.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package v1_23 //nolint

import "xorm.io/xorm"

func AddIndexForReleaseSha1(x *xorm.Engine) error {
type Release struct {
Sha1 string `xorm:"INDEX VARCHAR(64)"`
}
return x.Sync(new(Release))
}
16 changes: 15 additions & 1 deletion models/repo/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type Release struct {
Target string
TargetBehind string `xorm:"-"` // to handle non-existing or empty target
Title string
Sha1 string `xorm:"VARCHAR(64)"`
Sha1 string `xorm:"INDEX VARCHAR(64)"`
NumCommits int64
NumCommitsBehind int64 `xorm:"-"`
Note string `xorm:"TEXT"`
Expand Down Expand Up @@ -537,3 +537,17 @@ func InsertReleases(ctx context.Context, rels ...*Release) error {

return committer.Commit()
}

func FindTagsByCommitIDs(ctx context.Context, repoID int64, commitIDs ...string) (map[string][]*Release, error) {
releases := make([]*Release, 0, len(commitIDs))
if err := db.GetEngine(ctx).Where("repo_id=?", repoID).
In("sha1", commitIDs).
Find(&releases); err != nil {
return nil, err
}
res := make(map[string][]*Release, len(releases))
for _, r := range releases {
res[r.Sha1] = append(res[r.Sha1], r)
}
return res, nil
}
13 changes: 13 additions & 0 deletions models/repo/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,16 @@ func TestMigrate_InsertReleases(t *testing.T) {
err := InsertReleases(db.DefaultContext, r)
assert.NoError(t, err)
}

func Test_FindTagsByCommitIDs(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())

sha1Rels, err := FindTagsByCommitIDs(db.DefaultContext, 1, "65f1bf27bc3bf70f64657658635e66094edbcb4d")
assert.NoError(t, err)
assert.Len(t, sha1Rels, 1)
rels := sha1Rels["65f1bf27bc3bf70f64657658635e66094edbcb4d"]
assert.Len(t, rels, 3)
assert.Equal(t, "v1.1", rels[0].TagName)
assert.Equal(t, "delete-tag", rels[1].TagName)
assert.Equal(t, "v1.0", rels[2].TagName)
}
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,7 @@ commit_graph.color = Color
commit.contained_in = This commit is contained in:
commit.contained_in_default_branch = This commit is part of the default branch
commit.load_referencing_branches_and_tags = Load branches and tags referencing this commit
commit.load_tags_failed = Load tags failed because of internal error
blame = Blame
download_file = Download file
normal_view = Normal View
Expand Down
12 changes: 11 additions & 1 deletion routers/web/repo/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,17 @@ func Commits(ctx *context.Context) {
return
}
ctx.Data["Commits"] = processGitCommits(ctx, commits)

commitIDs := make([]string, 0, len(commits))
for _, c := range commits {
commitIDs = append(commitIDs, c.ID.String())
}
commitsTagsMap, err := repo_model.FindTagsByCommitIDs(ctx, ctx.Repo.Repository.ID, commitIDs...)
if err != nil {
log.Error("FindTagsByCommitIDs: %v", err)
ctx.Flash.Error(ctx.Tr("repo.commit.load_tags_failed"))
} else {
ctx.Data["CommitsTagsMap"] = commitsTagsMap
}
ctx.Data["Username"] = ctx.Repo.Owner.Name
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
ctx.Data["CommitCount"] = commitsCount
Expand Down
2 changes: 1 addition & 1 deletion services/actions/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ func (n *actionsNotifier) ForkRepository(ctx context.Context, doer *user_model.U
// Add to hook queue for created repo after session commit.
if u.IsOrganization() {
newNotifyInput(repo, doer, webhook_module.HookEventRepository).
WithRef(oldRepo.DefaultBranch).
WithRef(git.RefNameFromBranch(oldRepo.DefaultBranch).String()).
WithPayload(&api.RepositoryPayload{
Action: api.HookRepoCreated,
Repository: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeOwner}),
Expand Down
25 changes: 15 additions & 10 deletions services/actions/notifier_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type notifyInput struct {
Event webhook_module.HookEventType

// optional
Ref string
Ref git.RefName
Payload api.Payloader
PullRequest *issues_model.PullRequest
}
Expand All @@ -89,7 +89,7 @@ func (input *notifyInput) WithDoer(doer *user_model.User) *notifyInput {
}

func (input *notifyInput) WithRef(ref string) *notifyInput {
input.Ref = ref
input.Ref = git.RefName(ref)
return input
}

Expand All @@ -101,7 +101,7 @@ func (input *notifyInput) WithPayload(payload api.Payloader) *notifyInput {
func (input *notifyInput) WithPullRequest(pr *issues_model.PullRequest) *notifyInput {
input.PullRequest = pr
if input.Ref == "" {
input.Ref = pr.GetGitRefName()
input.Ref = git.RefName(pr.GetGitRefName())
}
return input
}
Expand Down Expand Up @@ -144,20 +144,25 @@ func notify(ctx context.Context, input *notifyInput) error {
defer gitRepo.Close()

ref := input.Ref
if ref != input.Repo.DefaultBranch && actions_module.IsDefaultBranchWorkflow(input.Event) {
if ref.BranchName() != input.Repo.DefaultBranch && actions_module.IsDefaultBranchWorkflow(input.Event) {
if ref != "" {
log.Warn("Event %q should only trigger workflows on the default branch, but its ref is %q. Will fall back to the default branch",
input.Event, ref)
}
ref = input.Repo.DefaultBranch
ref = git.RefNameFromBranch(input.Repo.DefaultBranch)
}
if ref == "" {
log.Warn("Ref of event %q is empty, will fall back to the default branch", input.Event)
ref = input.Repo.DefaultBranch
ref = git.RefNameFromBranch(input.Repo.DefaultBranch)
}

commitID, err := gitRepo.GetRefCommitID(ref.String())
if err != nil {
return fmt.Errorf("gitRepo.GetRefCommitID: %w", err)
}

// Get the commit object for the ref
commit, err := gitRepo.GetCommit(ref)
commit, err := gitRepo.GetCommit(commitID)
if err != nil {
return fmt.Errorf("gitRepo.GetCommit: %w", err)
}
Expand All @@ -168,7 +173,7 @@ func notify(ctx context.Context, input *notifyInput) error {

var detectedWorkflows []*actions_module.DetectedWorkflow
actionsConfig := input.Repo.MustGetUnit(ctx, unit_model.TypeActions).ActionsConfig()
shouldDetectSchedules := input.Event == webhook_module.HookEventPush && git.RefName(input.Ref).BranchName() == input.Repo.DefaultBranch
shouldDetectSchedules := input.Event == webhook_module.HookEventPush && input.Ref.BranchName() == input.Repo.DefaultBranch
workflows, schedules, err := actions_module.DetectWorkflows(gitRepo, commit,
input.Event,
input.Payload,
Expand Down Expand Up @@ -220,12 +225,12 @@ func notify(ctx context.Context, input *notifyInput) error {
}

if shouldDetectSchedules {
if err := handleSchedules(ctx, schedules, commit, input, ref); err != nil {
if err := handleSchedules(ctx, schedules, commit, input, ref.String()); err != nil {
return err
}
}

return handleWorkflows(ctx, detectedWorkflows, commit, input, ref)
return handleWorkflows(ctx, detectedWorkflows, commit, input, ref.String())
}

func skipWorkflows(input *notifyInput, commit *git.Commit) bool {
Expand Down
5 changes: 5 additions & 0 deletions templates/repo/commits_list.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@
{{if IsMultilineCommitMessage .Message}}
<pre class="commit-body tw-hidden">{{RenderCommitBody $.Context .Message ($.Repository.ComposeMetas ctx)}}</pre>
{{end}}
{{if $.CommitsTagsMap}}
{{range (index $.CommitsTagsMap .ID.String)}}
{{- template "repo/tag/name" dict "RepoLink" $.Repository.Link "TagName" .TagName "IsRelease" (not .IsTag) -}}
{{end}}
{{end}}
</td>
{{if .Committer}}
<td class="text right aligned">{{TimeSince .Committer.When ctx.Locale}}</td>
Expand Down
4 changes: 1 addition & 3 deletions templates/repo/graph/commits.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@
</a>
{{end}}
{{else if eq $refGroup "tags"}}
<a class="ui labelled basic tiny button" href="{{$.RepoLink}}/src/tag/{{.ShortName|PathEscape}}">
{{svg "octicon-tag"}} {{.ShortName}}
</a>
{{- template "repo/tag/name" dict "RepoLink" $.Repository.Link "TagName" .ShortName -}}
{{else if eq $refGroup "remotes"}}
<a class="ui labelled basic tiny button" href="{{$.RepoLink}}/src/commit/{{$commit.Rev|PathEscape}}">
{{svg "octicon-cross-reference"}} {{.ShortName}}
Expand Down
3 changes: 3 additions & 0 deletions templates/repo/tag/name.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<a class="ui label basic tiny button{{if .IsRelease}} primary{{end}}" href="{{.RepoLink}}/src/tag/{{.TagName|PathEscape}}">
{{svg "octicon-tag"}} {{.TagName}}
</a>
4 changes: 2 additions & 2 deletions tests/integration/actions_trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ func TestCreateDeleteRefEvent(t *testing.T) {
Title: "add workflow",
RepoID: repo.ID,
Event: "delete",
Ref: "main",
Ref: "refs/heads/main",
WorkflowID: "createdelete.yml",
CommitSHA: branch.CommitID,
})
Expand All @@ -442,7 +442,7 @@ func TestCreateDeleteRefEvent(t *testing.T) {
Title: "add workflow",
RepoID: repo.ID,
Event: "delete",
Ref: "main",
Ref: "refs/heads/main",
WorkflowID: "createdelete.yml",
CommitSHA: branch.CommitID,
})
Expand Down

0 comments on commit a87d883

Please sign in to comment.