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