mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-26 12:27:06 +00:00 
			
		
		
		
	Detect whether action view branch was deleted (#32764)
Fix #32761 
This commit is contained in:
		| @@ -37,6 +37,7 @@ type ActionRun struct { | |||||||
| 	TriggerUser       *user_model.User       `xorm:"-"` | 	TriggerUser       *user_model.User       `xorm:"-"` | ||||||
| 	ScheduleID        int64 | 	ScheduleID        int64 | ||||||
| 	Ref               string `xorm:"index"` // the commit/tag/… that caused the run | 	Ref               string `xorm:"index"` // the commit/tag/… that caused the run | ||||||
|  | 	IsRefDeleted      bool   `xorm:"-"` | ||||||
| 	CommitSHA         string | 	CommitSHA         string | ||||||
| 	IsForkPullRequest bool                         // If this is triggered by a PR from a forked repository or an untrusted user, we need to check if it is approved and limit permissions when running the workflow. | 	IsForkPullRequest bool                         // If this is triggered by a PR from a forked repository or an untrusted user, we need to check if it is approved and limit permissions when running the workflow. | ||||||
| 	NeedApproval      bool                         // may need approval if it's a fork pull request | 	NeedApproval      bool                         // may need approval if it's a fork pull request | ||||||
|   | |||||||
| @@ -12,6 +12,7 @@ import ( | |||||||
| 	repo_model "code.gitea.io/gitea/models/repo" | 	repo_model "code.gitea.io/gitea/models/repo" | ||||||
| 	"code.gitea.io/gitea/models/unit" | 	"code.gitea.io/gitea/models/unit" | ||||||
| 	user_model "code.gitea.io/gitea/models/user" | 	user_model "code.gitea.io/gitea/models/user" | ||||||
|  | 	"code.gitea.io/gitea/modules/container" | ||||||
| 	"code.gitea.io/gitea/modules/git" | 	"code.gitea.io/gitea/modules/git" | ||||||
| 	"code.gitea.io/gitea/modules/log" | 	"code.gitea.io/gitea/modules/log" | ||||||
| 	"code.gitea.io/gitea/modules/optional" | 	"code.gitea.io/gitea/modules/optional" | ||||||
| @@ -169,9 +170,22 @@ func GetBranch(ctx context.Context, repoID int64, branchName string) (*Branch, e | |||||||
| 	return &branch, nil | 	return &branch, nil | ||||||
| } | } | ||||||
|  |  | ||||||
| func GetBranches(ctx context.Context, repoID int64, branchNames []string) ([]*Branch, error) { | func GetBranches(ctx context.Context, repoID int64, branchNames []string, includeDeleted bool) ([]*Branch, error) { | ||||||
| 	branches := make([]*Branch, 0, len(branchNames)) | 	branches := make([]*Branch, 0, len(branchNames)) | ||||||
| 	return branches, db.GetEngine(ctx).Where("repo_id=?", repoID).In("name", branchNames).Find(&branches) |  | ||||||
|  | 	sess := db.GetEngine(ctx).Where("repo_id=?", repoID).In("name", branchNames) | ||||||
|  | 	if !includeDeleted { | ||||||
|  | 		sess.And("is_deleted=?", false) | ||||||
|  | 	} | ||||||
|  | 	return branches, sess.Find(&branches) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func BranchesToNamesSet(branches []*Branch) container.Set[string] { | ||||||
|  | 	names := make(container.Set[string], len(branches)) | ||||||
|  | 	for _, branch := range branches { | ||||||
|  | 		names.Add(branch.Name) | ||||||
|  | 	} | ||||||
|  | 	return names | ||||||
| } | } | ||||||
|  |  | ||||||
| func AddBranches(ctx context.Context, branches []*Branch) error { | func AddBranches(ctx context.Context, branches []*Branch) error { | ||||||
|   | |||||||
| @@ -245,6 +245,10 @@ func List(ctx *context.Context) { | |||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	if err := loadIsRefDeleted(ctx, runs); err != nil { | ||||||
|  | 		log.Error("LoadIsRefDeleted", err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	ctx.Data["Runs"] = runs | 	ctx.Data["Runs"] = runs | ||||||
|  |  | ||||||
| 	actors, err := actions_model.GetActors(ctx, ctx.Repo.Repository.ID) | 	actors, err := actions_model.GetActors(ctx, ctx.Repo.Repository.ID) | ||||||
| @@ -267,6 +271,34 @@ func List(ctx *context.Context) { | |||||||
| 	ctx.HTML(http.StatusOK, tplListActions) | 	ctx.HTML(http.StatusOK, tplListActions) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // loadIsRefDeleted loads the IsRefDeleted field for each run in the list. | ||||||
|  | // TODO: move this function to models/actions/run_list.go but now it will result in a circular import. | ||||||
|  | func loadIsRefDeleted(ctx *context.Context, runs actions_model.RunList) error { | ||||||
|  | 	branches := make(container.Set[string], len(runs)) | ||||||
|  | 	for _, run := range runs { | ||||||
|  | 		refName := git.RefName(run.Ref) | ||||||
|  | 		if refName.IsBranch() { | ||||||
|  | 			branches.Add(refName.ShortName()) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	if len(branches) == 0 { | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	branchInfos, err := git_model.GetBranches(ctx, ctx.Repo.Repository.ID, branches.Values(), false) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	branchSet := git_model.BranchesToNamesSet(branchInfos) | ||||||
|  | 	for _, run := range runs { | ||||||
|  | 		refName := git.RefName(run.Ref) | ||||||
|  | 		if refName.IsBranch() && !branchSet.Contains(run.Ref) { | ||||||
|  | 			run.IsRefDeleted = true | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
| type WorkflowDispatchInput struct { | type WorkflowDispatchInput struct { | ||||||
| 	Name        string   `yaml:"name"` | 	Name        string   `yaml:"name"` | ||||||
| 	Description string   `yaml:"description"` | 	Description string   `yaml:"description"` | ||||||
|   | |||||||
| @@ -19,6 +19,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" | ||||||
| 	"code.gitea.io/gitea/models/perm" | 	"code.gitea.io/gitea/models/perm" | ||||||
| 	access_model "code.gitea.io/gitea/models/perm/access" | 	access_model "code.gitea.io/gitea/models/perm/access" | ||||||
| 	repo_model "code.gitea.io/gitea/models/repo" | 	repo_model "code.gitea.io/gitea/models/repo" | ||||||
| @@ -138,6 +139,7 @@ type ViewUser struct { | |||||||
| type ViewBranch struct { | type ViewBranch struct { | ||||||
| 	Name      string `json:"name"` | 	Name      string `json:"name"` | ||||||
| 	Link      string `json:"link"` | 	Link      string `json:"link"` | ||||||
|  | 	IsDeleted bool   `json:"isDeleted"` | ||||||
| } | } | ||||||
|  |  | ||||||
| type ViewJobStep struct { | type ViewJobStep struct { | ||||||
| @@ -236,6 +238,16 @@ func ViewPost(ctx *context_module.Context) { | |||||||
| 		Name: run.PrettyRef(), | 		Name: run.PrettyRef(), | ||||||
| 		Link: run.RefLink(), | 		Link: run.RefLink(), | ||||||
| 	} | 	} | ||||||
|  | 	refName := git.RefName(run.Ref) | ||||||
|  | 	if refName.IsBranch() { | ||||||
|  | 		b, err := git_model.GetBranch(ctx, ctx.Repo.Repository.ID, refName.ShortName()) | ||||||
|  | 		if err != nil && !git_model.IsErrBranchNotExist(err) { | ||||||
|  | 			log.Error("GetBranch: %v", err) | ||||||
|  | 		} else if git_model.IsErrBranchNotExist(err) || (b != nil && b.IsDeleted) { | ||||||
|  | 			branch.IsDeleted = true | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	resp.State.Run.Commit = ViewCommit{ | 	resp.State.Run.Commit = ViewCommit{ | ||||||
| 		ShortSha: base.ShortSha(run.CommitSHA), | 		ShortSha: base.ShortSha(run.CommitSHA), | ||||||
| 		Link:     fmt.Sprintf("%s/commit/%s", run.Repo.Link(), run.CommitSHA), | 		Link:     fmt.Sprintf("%s/commit/%s", run.Repo.Link(), run.CommitSHA), | ||||||
|   | |||||||
| @@ -305,7 +305,7 @@ func SyncBranchesToDB(ctx context.Context, repoID, pusherID int64, branchNames, | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||||
| 		branches, err := git_model.GetBranches(ctx, repoID, branchNames) | 		branches, err := git_model.GetBranches(ctx, repoID, branchNames, true) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return fmt.Errorf("git_model.GetBranches: %v", err) | 			return fmt.Errorf("git_model.GetBranches: %v", err) | ||||||
| 		} | 		} | ||||||
|   | |||||||
| @@ -27,10 +27,10 @@ | |||||||
| 				</div> | 				</div> | ||||||
| 			</div> | 			</div> | ||||||
| 			<div class="flex-item-trailing"> | 			<div class="flex-item-trailing"> | ||||||
| 				{{if .RefLink}} | 				{{if .IsRefDeleted}} | ||||||
| 					<a class="ui label run-list-ref gt-ellipsis" href="{{.RefLink}}">{{.PrettyRef}}</a> | 					<span class="ui label run-list-ref gt-ellipsis tw-line-through" data-tooltip-content="{{.PrettyRef}}">{{.PrettyRef}}</span> | ||||||
| 				{{else}} | 				{{else}} | ||||||
| 					<span class="ui label run-list-ref gt-ellipsis">{{.PrettyRef}}</span> | 					<a class="ui label run-list-ref gt-ellipsis" href="{{.RefLink}}" data-tooltip-content="{{.PrettyRef}}">{{.PrettyRef}}</a> | ||||||
| 				{{end}} | 				{{end}} | ||||||
| 				<div class="run-list-item-right"> | 				<div class="run-list-item-right"> | ||||||
| 					<div class="run-list-meta">{{svg "octicon-calendar" 16}}{{DateUtils.TimeSince .Updated}}</div> | 					<div class="run-list-meta">{{svg "octicon-calendar" 16}}{{DateUtils.TimeSince .Updated}}</div> | ||||||
|   | |||||||
| @@ -429,7 +429,8 @@ export function initRepositoryActionView() { | |||||||
|           <a class="muted" :href="run.commit.pusher.link">{{ run.commit.pusher.displayName }}</a> |           <a class="muted" :href="run.commit.pusher.link">{{ run.commit.pusher.displayName }}</a> | ||||||
|         </template> |         </template> | ||||||
|         <span class="ui label tw-max-w-full" v-if="run.commit.shortSHA"> |         <span class="ui label tw-max-w-full" v-if="run.commit.shortSHA"> | ||||||
|           <a class="gt-ellipsis" :href="run.commit.branch.link">{{ run.commit.branch.name }}</a> |           <span v-if="run.commit.branch.isDeleted" class="gt-ellipsis tw-line-through" :data-tooltip-content="run.commit.branch.name">{{ run.commit.branch.name }}</span> | ||||||
|  |           <a v-else class="gt-ellipsis" :href="run.commit.branch.link" :data-tooltip-content="run.commit.branch.name">{{ run.commit.branch.name }}</a> | ||||||
|         </span> |         </span> | ||||||
|       </div> |       </div> | ||||||
|     </div> |     </div> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Lunny Xiao
					Lunny Xiao