diff --git a/modules/repository/license.go b/modules/repository/license.go index fc9e9b8543..1f447703d8 100644 --- a/modules/repository/license.go +++ b/modules/repository/license.go @@ -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 diff --git a/routers/private/default_branch.go b/routers/private/default_branch.go index 597a10f7f8..0ed11963c6 100644 --- a/routers/private/default_branch.go +++ b/routers/private/default_branch.go @@ -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), diff --git a/services/repository/branch.go b/services/repository/branch.go index c0f302e2eb..f6c5ed4448 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -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