fix not remove old records when license file not detected after branch change

This commit is contained in:
yp05327 2024-02-26 08:28:27 +00:00
parent 7d92e475e1
commit c79fbaa6e5
3 changed files with 15 additions and 22 deletions

View File

@ -43,8 +43,7 @@ func Init() error {
}
type LicenseUpdaterOptions struct {
RepoID int64
CommitID string
RepoID int64
}
func repoLicenseUpdater(items ...*LicenseUpdaterOptions) []*LicenseUpdaterOptions {
@ -67,14 +66,9 @@ func repoLicenseUpdater(items ...*LicenseUpdaterOptions) []*LicenseUpdaterOption
}
defer gitRepo.Close()
var commit *git.Commit
if opts.CommitID != "" {
commit, err = gitRepo.GetCommit(opts.CommitID)
} else {
commit, err = gitRepo.GetBranchCommit(repo.DefaultBranch)
}
commit, err := gitRepo.GetBranchCommit(repo.DefaultBranch)
if err != nil {
log.Error("repoLicenseUpdater [%d] failed: OpenRepository: %v", opts.RepoID, err)
log.Error("repoLicenseUpdater [%d] failed: GetBranchCommit: %v", opts.RepoID, err)
continue
}
if err = updateRepoLicenses(ctx, repo, commit); err != nil {
@ -258,6 +252,8 @@ func updateRepoLicenses(ctx context.Context, repo *repo_model.Repository, commit
if err != nil {
return fmt.Errorf("findLicenseFile: %w", err)
}
licenses := make([]string, 0)
if licenseFile != nil {
r, err := licenseFile.Blob().DataAsync()
if err != nil {
@ -265,15 +261,13 @@ func updateRepoLicenses(ctx context.Context, repo *repo_model.Repository, commit
}
defer r.Close()
licenses, err := detectLicense(r)
licenses, err = detectLicense(r)
if err != nil {
return fmt.Errorf("detectLicense: %w", err)
}
if err := repo_model.UpdateRepoLicenses(ctx, repo, commit.ID.String(), licenses); err != nil {
return fmt.Errorf("UpdateRepoLicenses: %v", err)
}
}
return nil
return repo_model.UpdateRepoLicenses(ctx, repo, commit.ID.String(), licenses)
}
// GetDetectedLicenseFileName returns license file name in the repository if it exists

View File

@ -38,8 +38,7 @@ func SetDefaultBranch(ctx *gitea_context.PrivateContext) {
}
if err := repo_module.AddRepoToLicenseUpdaterQueue(&repo_module.LicenseUpdaterOptions{
RepoID: ctx.Repo.Repository.ID,
CommitID: ctx.Repo.Commit.ID.String(),
RepoID: ctx.Repo.Repository.ID,
}); err != nil {
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err),

View File

@ -499,17 +499,17 @@ func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, gitR
return err
}
}
if err := repo_module.AddRepoToLicenseUpdaterQueue(&repo_module.LicenseUpdaterOptions{
RepoID: repo.ID,
CommitID: commit.ID.String(),
}); err != nil {
log.Error("AddRepoToLicenseUpdaterQueue: %v", err)
}
return nil
}); err != nil {
return err
}
if err := repo_module.AddRepoToLicenseUpdaterQueue(&repo_module.LicenseUpdaterOptions{
RepoID: repo.ID,
}); err != nil {
log.Error("AddRepoToLicenseUpdaterQueue: %v", err)
}
notify_service.ChangeDefaultBranch(ctx, repo)
return nil