2016-08-13 16:11:52 -07:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
2016-12-21 10:13:17 -02:00
|
|
|
// Copyright 2016 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2016-08-13 16:11:52 -07:00
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
|
|
import (
|
2023-07-24 05:47:27 +02:00
|
|
|
"context"
|
2016-08-13 16:11:52 -07:00
|
|
|
"fmt"
|
|
|
|
|
|
2021-09-24 19:32:56 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-06 16:01:49 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2019-03-27 17:33:00 +08:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2024-01-28 04:09:51 +08:00
|
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
2017-12-31 15:45:46 +01:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-01-21 04:01:19 +08:00
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
2017-04-12 15:44:54 +08:00
|
|
|
|
2025-06-10 14:35:12 +02:00
|
|
|
"github.com/urfave/cli/v3"
|
2016-08-13 16:11:52 -07:00
|
|
|
)
|
|
|
|
|
|
2026-03-25 10:53:13 -04:00
|
|
|
func newAdminCommand() *cli.Command {
|
|
|
|
|
return &cli.Command{
|
2016-08-13 16:11:52 -07:00
|
|
|
Name: "admin",
|
2023-12-15 23:49:01 +08:00
|
|
|
Usage: "Perform common administrative operations",
|
2025-06-10 14:35:12 +02:00
|
|
|
Commands: []*cli.Command{
|
2026-03-25 10:53:13 -04:00
|
|
|
newUserCommand(),
|
|
|
|
|
newRepoSyncReleasesCommand(),
|
|
|
|
|
newRegenerateCommand(),
|
|
|
|
|
newAuthCommand(),
|
|
|
|
|
newSendMailCommand(),
|
2016-08-13 16:11:52 -07:00
|
|
|
},
|
|
|
|
|
}
|
2026-03-25 10:53:13 -04:00
|
|
|
}
|
2016-08-13 16:11:52 -07:00
|
|
|
|
2026-03-25 10:53:13 -04:00
|
|
|
func newRepoSyncReleasesCommand() *cli.Command {
|
|
|
|
|
return &cli.Command{
|
2017-12-31 15:45:46 +01:00
|
|
|
Name: "repo-sync-releases",
|
|
|
|
|
Usage: "Synchronize repository releases with tags",
|
|
|
|
|
Action: runRepoSyncReleases,
|
|
|
|
|
}
|
2026-03-25 10:53:13 -04:00
|
|
|
}
|
2018-05-16 21:35:07 -04:00
|
|
|
|
2026-03-25 10:53:13 -04:00
|
|
|
func newRegenerateCommand() *cli.Command {
|
|
|
|
|
return &cli.Command{
|
2018-05-16 21:35:07 -04:00
|
|
|
Name: "regenerate",
|
|
|
|
|
Usage: "Regenerate specific files",
|
2025-06-10 14:35:12 +02:00
|
|
|
Commands: []*cli.Command{
|
2026-03-25 10:53:13 -04:00
|
|
|
newRegenerateHooksCommand(),
|
|
|
|
|
newRegenerateKeysCommand(),
|
2018-05-16 21:35:07 -04:00
|
|
|
},
|
|
|
|
|
}
|
2026-03-25 10:53:13 -04:00
|
|
|
}
|
2018-05-16 21:35:07 -04:00
|
|
|
|
2026-03-25 10:53:13 -04:00
|
|
|
func newAuthCommand() *cli.Command {
|
|
|
|
|
return &cli.Command{
|
2018-09-12 10:46:02 -04:00
|
|
|
Name: "auth",
|
|
|
|
|
Usage: "Modify external auth providers",
|
2025-06-10 14:35:12 +02:00
|
|
|
Commands: []*cli.Command{
|
|
|
|
|
microcmdAuthAddOauth(),
|
|
|
|
|
microcmdAuthUpdateOauth(),
|
|
|
|
|
microcmdAuthAddLdapBindDn(),
|
|
|
|
|
microcmdAuthUpdateLdapBindDn(),
|
|
|
|
|
microcmdAuthAddLdapSimpleAuth(),
|
|
|
|
|
microcmdAuthUpdateLdapSimpleAuth(),
|
|
|
|
|
microcmdAuthAddSMTP(),
|
|
|
|
|
microcmdAuthUpdateSMTP(),
|
2026-03-25 10:53:13 -04:00
|
|
|
newAuthListCommand(),
|
|
|
|
|
newAuthDeleteCommand(),
|
2018-09-12 10:46:02 -04:00
|
|
|
},
|
|
|
|
|
}
|
2026-03-25 10:53:13 -04:00
|
|
|
}
|
2018-09-12 10:46:02 -04:00
|
|
|
|
2026-03-25 10:53:13 -04:00
|
|
|
func newSendMailCommand() *cli.Command {
|
|
|
|
|
return &cli.Command{
|
2020-10-24 23:38:14 +03:00
|
|
|
Name: "sendmail",
|
|
|
|
|
Usage: "Send a message to all users",
|
|
|
|
|
Action: runSendMail,
|
|
|
|
|
Flags: []cli.Flag{
|
2023-07-21 17:28:19 +08:00
|
|
|
&cli.StringFlag{
|
2025-06-10 14:35:12 +02:00
|
|
|
Name: "title",
|
|
|
|
|
Usage: "a title of a message",
|
|
|
|
|
Required: true,
|
2020-10-24 23:38:14 +03:00
|
|
|
},
|
2023-07-21 17:28:19 +08:00
|
|
|
&cli.StringFlag{
|
2020-10-24 23:38:14 +03:00
|
|
|
Name: "content",
|
|
|
|
|
Usage: "a content of a message",
|
|
|
|
|
Value: "",
|
|
|
|
|
},
|
2023-07-21 17:28:19 +08:00
|
|
|
&cli.BoolFlag{
|
|
|
|
|
Name: "force",
|
|
|
|
|
Aliases: []string{"f"},
|
|
|
|
|
Usage: "A flag to bypass a confirmation step",
|
2020-10-24 23:38:14 +03:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
2026-03-25 10:53:13 -04:00
|
|
|
}
|
2022-01-12 23:54:53 +01:00
|
|
|
|
2025-06-10 14:35:12 +02:00
|
|
|
func idFlag() *cli.Int64Flag {
|
|
|
|
|
return &cli.Int64Flag{
|
2023-09-27 20:25:38 +08:00
|
|
|
Name: "id",
|
|
|
|
|
Usage: "ID of authentication source",
|
2022-01-12 23:54:53 +01:00
|
|
|
}
|
2025-06-10 14:35:12 +02:00
|
|
|
}
|
2021-11-07 11:11:27 +08:00
|
|
|
|
2025-06-10 14:35:12 +02:00
|
|
|
func runRepoSyncReleases(ctx context.Context, _ *cli.Command) error {
|
2021-11-07 11:11:27 +08:00
|
|
|
if err := initDB(ctx); err != nil {
|
2018-01-12 23:16:49 +01:00
|
|
|
return err
|
2017-12-31 15:45:46 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-27 19:00:01 +08:00
|
|
|
if err := git.InitSimple(); err != nil {
|
2023-08-11 06:42:48 +08:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-31 15:45:46 +01:00
|
|
|
log.Trace("Synchronizing repository releases (this may take a while)")
|
|
|
|
|
for page := 1; ; page++ {
|
2025-06-02 10:33:25 -07:00
|
|
|
repos, count, err := repo_model.SearchRepositoryByName(ctx, repo_model.SearchRepoOptions{
|
2021-09-24 19:32:56 +08:00
|
|
|
ListOptions: db.ListOptions{
|
2022-06-06 16:01:49 +08:00
|
|
|
PageSize: repo_model.RepositoryListDefaultPageSize,
|
2020-01-24 19:00:29 +00:00
|
|
|
Page: page,
|
|
|
|
|
},
|
|
|
|
|
Private: true,
|
2017-12-31 15:45:46 +01:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
2022-10-24 21:29:17 +02:00
|
|
|
return fmt.Errorf("SearchRepositoryByName: %w", err)
|
2017-12-31 15:45:46 +01:00
|
|
|
}
|
|
|
|
|
if len(repos) == 0 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
log.Trace("Processing next %d repos of %d", len(repos), count)
|
|
|
|
|
for _, repo := range repos {
|
2025-10-07 02:06:51 -07:00
|
|
|
log.Trace("Synchronizing repo %s with path %s", repo.FullName(), repo.RelativePath())
|
2024-01-28 04:09:51 +08:00
|
|
|
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
|
2017-12-31 15:45:46 +01:00
|
|
|
if err != nil {
|
|
|
|
|
log.Warn("OpenRepository: %v", err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 05:47:27 +02:00
|
|
|
oldnum, err := getReleaseCount(ctx, repo.ID)
|
2017-12-31 15:45:46 +01:00
|
|
|
if err != nil {
|
|
|
|
|
log.Warn(" GetReleaseCountByRepoID: %v", err)
|
|
|
|
|
}
|
|
|
|
|
log.Trace(" currentNumReleases is %d, running SyncReleasesWithTags", oldnum)
|
|
|
|
|
|
2026-02-10 16:16:05 -08:00
|
|
|
if _, err = repo_module.SyncReleasesWithTags(ctx, repo, gitRepo); err != nil {
|
2017-12-31 15:45:46 +01:00
|
|
|
log.Warn(" SyncReleasesWithTags: %v", err)
|
2019-11-13 07:01:19 +00:00
|
|
|
gitRepo.Close()
|
2017-12-31 15:45:46 +01:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 05:47:27 +02:00
|
|
|
count, err = getReleaseCount(ctx, repo.ID)
|
2017-12-31 15:45:46 +01:00
|
|
|
if err != nil {
|
|
|
|
|
log.Warn(" GetReleaseCountByRepoID: %v", err)
|
2019-11-13 07:01:19 +00:00
|
|
|
gitRepo.Close()
|
2017-12-31 15:45:46 +01:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-07 02:06:51 -07:00
|
|
|
log.Trace("repo %s releases synchronized to tags: from %d to %d",
|
2017-12-31 15:45:46 +01:00
|
|
|
repo.FullName(), oldnum, count)
|
2019-11-13 07:01:19 +00:00
|
|
|
gitRepo.Close()
|
2017-12-31 15:45:46 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-01-12 23:16:49 +01:00
|
|
|
|
2023-07-24 05:47:27 +02:00
|
|
|
func getReleaseCount(ctx context.Context, id int64) (int64, error) {
|
2024-01-15 10:19:25 +08:00
|
|
|
return db.Count[repo_model.Release](
|
2023-07-24 05:47:27 +02:00
|
|
|
ctx,
|
2022-08-25 10:31:57 +08:00
|
|
|
repo_model.FindReleasesOptions{
|
2024-01-15 10:19:25 +08:00
|
|
|
RepoID: id,
|
2018-01-12 23:16:49 +01:00
|
|
|
IncludeTags: true,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}
|