Skip to content

Commit

Permalink
Make the code simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Dec 10, 2024
1 parent 450b309 commit affe773
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
7 changes: 7 additions & 0 deletions models/git/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ func GetBranches(ctx context.Context, repoID int64, branchNames []string) ([]*Br
return branches, db.GetEngine(ctx).Where("repo_id=?", repoID).In("name", branchNames).Find(&branches)
}

func GetExistBranches(ctx context.Context, repoID int64, branchNames []string) ([]*Branch, error) {
branches := make([]*Branch, 0, len(branchNames))
return branches, db.GetEngine(ctx).Where("repo_id=?", repoID).
And("is_deleted=?", false).
In("name", branchNames).Find(&branches)
}

func AddBranches(ctx context.Context, branches []*Branch) error {
for _, branch := range branches {
if _, err := db.GetEngine(ctx).Insert(branch); err != nil {
Expand Down
18 changes: 6 additions & 12 deletions routers/web/repo/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,28 +281,22 @@ func loadIsRefDeleted(ctx *context.Context, runs actions_model.RunList) error {
refName := git.RefName(run.Ref)
if refName.IsBranch() {
branchName := refName.ShortName()
run.IsRefDeleted = true // assume it's deleted then if it's found in the database it's not deleted
branchRuns[branchName] = append(branchRuns[branchName], run)
branches.Add(branchName)
}
}
if len(branches) == 0 {
return nil
}
var branchInfos []*git_model.Branch
if err := db.GetEngine(ctx).Where("repo_id = ?", ctx.Repo.Repository.ID).
In("name", branches.Values()).Find(&branchInfos); err != nil {
branchInfos, err := git_model.GetExistBranches(ctx, ctx.Repo.Repository.ID, branches.Values())
if err != nil {
return err
}
for branch, branchRun := range branchRuns {
exist := false
for _, branchInfo := range branchInfos {
if branchInfo.Name == branch {
exist = !branchInfo.IsDeleted
break
}
}
for _, branchInfo := range branchInfos {
branchRun := branchRuns[branchInfo.Name]
for _, br := range branchRun {
br.IsRefDeleted = !exist
br.IsRefDeleted = false
}
}
return nil
Expand Down

0 comments on commit affe773

Please sign in to comment.