2016-08-14 01:11:52 +02:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
2016-12-21 13:13:17 +01:00
|
|
|
// Copyright 2016 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2016-08-14 01:11:52 +02:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2023-07-24 05:47:27 +02:00
|
|
|
"context"
|
2016-08-14 01:11:52 +02:00
|
|
|
"fmt"
|
|
|
|
|
2021-09-24 13:32:56 +02:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-06 10:01:49 +02:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2019-03-27 10:33:00 +01:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2017-12-31 15:45:46 +01:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-01-20 21:01:19 +01:00
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
2017-04-12 09:44:54 +02:00
|
|
|
|
2023-07-21 11:28:19 +02:00
|
|
|
"github.com/urfave/cli/v2"
|
2016-08-14 01:11:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-11-04 12:42:18 +01:00
|
|
|
// CmdAdmin represents the available admin sub-command.
|
2023-07-21 11:28:19 +02:00
|
|
|
CmdAdmin = &cli.Command{
|
2016-08-14 01:11:52 +02:00
|
|
|
Name: "admin",
|
2018-01-12 23:16:49 +01:00
|
|
|
Usage: "Command line interface to perform common administrative operations",
|
2023-07-21 11:28:19 +02:00
|
|
|
Subcommands: []*cli.Command{
|
2020-10-16 04:48:38 +02:00
|
|
|
subcmdUser,
|
2017-12-31 15:45:46 +01:00
|
|
|
subcmdRepoSyncReleases,
|
2018-05-17 03:35:07 +02:00
|
|
|
subcmdRegenerate,
|
2018-09-12 16:46:02 +02:00
|
|
|
subcmdAuth,
|
2020-10-24 22:38:14 +02:00
|
|
|
subcmdSendMail,
|
2016-08-14 01:11:52 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-07-21 11:28:19 +02:00
|
|
|
subcmdRepoSyncReleases = &cli.Command{
|
2017-12-31 15:45:46 +01:00
|
|
|
Name: "repo-sync-releases",
|
|
|
|
Usage: "Synchronize repository releases with tags",
|
|
|
|
Action: runRepoSyncReleases,
|
|
|
|
}
|
2018-05-17 03:35:07 +02:00
|
|
|
|
2023-07-21 11:28:19 +02:00
|
|
|
subcmdRegenerate = &cli.Command{
|
2018-05-17 03:35:07 +02:00
|
|
|
Name: "regenerate",
|
|
|
|
Usage: "Regenerate specific files",
|
2023-07-21 11:28:19 +02:00
|
|
|
Subcommands: []*cli.Command{
|
2018-05-17 03:35:07 +02:00
|
|
|
microcmdRegenHooks,
|
|
|
|
microcmdRegenKeys,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-07-21 11:28:19 +02:00
|
|
|
subcmdAuth = &cli.Command{
|
2018-09-12 16:46:02 +02:00
|
|
|
Name: "auth",
|
|
|
|
Usage: "Modify external auth providers",
|
2023-07-21 11:28:19 +02:00
|
|
|
Subcommands: []*cli.Command{
|
2018-09-12 16:46:02 +02:00
|
|
|
microcmdAuthAddOauth,
|
|
|
|
microcmdAuthUpdateOauth,
|
2023-09-27 14:25:38 +02:00
|
|
|
microcmdAuthAddLdapBindDn,
|
|
|
|
microcmdAuthUpdateLdapBindDn,
|
|
|
|
microcmdAuthAddLdapSimpleAuth,
|
|
|
|
microcmdAuthUpdateLdapSimpleAuth,
|
2022-01-12 23:54:53 +01:00
|
|
|
microcmdAuthAddSMTP,
|
|
|
|
microcmdAuthUpdateSMTP,
|
2018-09-12 16:46:02 +02:00
|
|
|
microcmdAuthList,
|
|
|
|
microcmdAuthDelete,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-07-21 11:28:19 +02:00
|
|
|
subcmdSendMail = &cli.Command{
|
2020-10-24 22:38:14 +02:00
|
|
|
Name: "sendmail",
|
|
|
|
Usage: "Send a message to all users",
|
|
|
|
Action: runSendMail,
|
|
|
|
Flags: []cli.Flag{
|
2023-07-21 11:28:19 +02:00
|
|
|
&cli.StringFlag{
|
2020-10-24 22:38:14 +02:00
|
|
|
Name: "title",
|
|
|
|
Usage: `a title of a message`,
|
|
|
|
Value: "",
|
|
|
|
},
|
2023-07-21 11:28:19 +02:00
|
|
|
&cli.StringFlag{
|
2020-10-24 22:38:14 +02:00
|
|
|
Name: "content",
|
|
|
|
Usage: "a content of a message",
|
|
|
|
Value: "",
|
|
|
|
},
|
2023-07-21 11:28:19 +02:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "force",
|
|
|
|
Aliases: []string{"f"},
|
|
|
|
Usage: "A flag to bypass a confirmation step",
|
2020-10-24 22:38:14 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-01-12 23:54:53 +01:00
|
|
|
|
2023-09-27 14:25:38 +02:00
|
|
|
idFlag = &cli.Int64Flag{
|
|
|
|
Name: "id",
|
|
|
|
Usage: "ID of authentication source",
|
2022-01-12 23:54:53 +01:00
|
|
|
}
|
2016-08-14 01:11:52 +02:00
|
|
|
)
|
|
|
|
|
2021-04-09 09:40:34 +02:00
|
|
|
func runRepoSyncReleases(_ *cli.Context) error {
|
2021-11-07 04:11:27 +01:00
|
|
|
ctx, cancel := installSignals()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := initDB(ctx); err != nil {
|
2018-01-12 23:16:49 +01:00
|
|
|
return err
|
2017-12-31 15:45:46 +01:00
|
|
|
}
|
|
|
|
|
2023-08-11 00:42:48 +02:00
|
|
|
if err := git.InitSimple(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-31 15:45:46 +01:00
|
|
|
log.Trace("Synchronizing repository releases (this may take a while)")
|
|
|
|
for page := 1; ; page++ {
|
2022-11-19 09:12:33 +01:00
|
|
|
repos, count, err := repo_model.SearchRepositoryByName(ctx, &repo_model.SearchRepoOptions{
|
2021-09-24 13:32:56 +02:00
|
|
|
ListOptions: db.ListOptions{
|
2022-06-06 10:01:49 +02:00
|
|
|
PageSize: repo_model.RepositoryListDefaultPageSize,
|
2020-01-24 20:00:29 +01: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 {
|
|
|
|
log.Trace("Synchronizing repo %s with path %s", repo.FullName(), repo.RepoPath())
|
2022-03-29 21:13:41 +02:00
|
|
|
gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
|
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)
|
|
|
|
|
2023-09-25 15:17:37 +02: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 08:01:19 +01: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 08:01:19 +01:00
|
|
|
gitRepo.Close()
|
2017-12-31 15:45:46 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace(" repo %s releases synchronized to tags: from %d to %d",
|
|
|
|
repo.FullName(), oldnum, count)
|
2019-11-13 08:01:19 +01: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) {
|
2022-08-25 04:31:57 +02:00
|
|
|
return repo_model.GetReleaseCountByRepoID(
|
2023-07-24 05:47:27 +02:00
|
|
|
ctx,
|
2018-01-12 23:16:49 +01:00
|
|
|
id,
|
2022-08-25 04:31:57 +02:00
|
|
|
repo_model.FindReleasesOptions{
|
2018-01-12 23:16:49 +01:00
|
|
|
IncludeTags: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|