mirror of
https://codeberg.org/forgejo/forgejo
synced 2025-10-18 22:50:36 +02:00
Fixes #9433. ``` $ ./gitea admin user create --username blah --must-change-password false Hint: boolean false must be specified as a single arg, eg. '--restricted=false', not '--restricted false' Command error: unexpected arguments: false ``` **Breaking**: CLI sub-commands that only have flags would previously ignore anything that might be considered an "extra" argument, and would proceed without any errors. I've manually tested this change on the single `admin user create` command with positive (ensuring cmd still works) and negative (ensuring errors are reported) test cases. I've attempted to ensure the change is applied only to commands which don't use the CLI `Args()` and avoided touching them, including: - `admin user must-change-password` takes a list of users - `doctor recreate-tables` takes a list of tables - `embedded [list/view/extract]` use a pattern of resources to operate upon - git repo hook subcommands, and the ssh serv command, use arguments and have been omitted from the change ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests - I added test coverage for Go changes... - [ ] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] I do not want this change to show in the release notes. - [ ] I want the title to show in the release notes with a link to this pull request. - [x] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9458 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net> Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
180 lines
4 KiB
Go
180 lines
4 KiB
Go
// Copyright 2016 The Gogs Authors. All rights reserved.
|
|
// Copyright 2016 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"forgejo.org/models/db"
|
|
repo_model "forgejo.org/models/repo"
|
|
"forgejo.org/modules/git"
|
|
"forgejo.org/modules/gitrepo"
|
|
"forgejo.org/modules/log"
|
|
repo_module "forgejo.org/modules/repository"
|
|
|
|
"github.com/urfave/cli/v3"
|
|
)
|
|
|
|
// CmdAdmin represents the available admin sub-command.
|
|
func cmdAdmin() *cli.Command {
|
|
return &cli.Command{
|
|
Name: "admin",
|
|
Usage: "Perform common administrative operations",
|
|
Commands: []*cli.Command{
|
|
subcmdUser(),
|
|
subcmdRepoSyncReleases(),
|
|
subcmdRegenerate(),
|
|
subcmdAuth(),
|
|
subcmdSendMail(),
|
|
},
|
|
}
|
|
}
|
|
|
|
func subcmdRepoSyncReleases() *cli.Command {
|
|
return &cli.Command{
|
|
Name: "repo-sync-releases",
|
|
Usage: "Synchronize repository releases with tags",
|
|
Before: noDanglingArgs,
|
|
Action: runRepoSyncReleases,
|
|
}
|
|
}
|
|
|
|
func subcmdRegenerate() *cli.Command {
|
|
return &cli.Command{
|
|
Name: "regenerate",
|
|
Usage: "Regenerate specific files",
|
|
Commands: []*cli.Command{
|
|
microcmdRegenHooks,
|
|
microcmdRegenKeys,
|
|
},
|
|
}
|
|
}
|
|
|
|
func subcmdAuth() *cli.Command {
|
|
return &cli.Command{
|
|
Name: "auth",
|
|
Usage: "Modify external auth providers",
|
|
Commands: []*cli.Command{
|
|
microcmdAuthAddOauth(),
|
|
microcmdAuthUpdateOauth(),
|
|
microcmdAuthAddLdapBindDn(),
|
|
microcmdAuthUpdateLdapBindDn(),
|
|
microcmdAuthAddLdapSimpleAuth(),
|
|
microcmdAuthUpdateLdapSimpleAuth(),
|
|
microcmdAuthAddSMTP(),
|
|
microcmdAuthUpdateSMTP(),
|
|
microcmdAuthList(),
|
|
microcmdAuthDelete(),
|
|
},
|
|
}
|
|
}
|
|
|
|
func subcmdSendMail() *cli.Command {
|
|
return &cli.Command{
|
|
Name: "sendmail",
|
|
Usage: "Send a message to all users",
|
|
Before: noDanglingArgs,
|
|
Action: runSendMail,
|
|
Flags: []cli.Flag{
|
|
&cli.StringFlag{
|
|
Name: "title",
|
|
Usage: `a title of a message`,
|
|
Value: "",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "content",
|
|
Usage: "a content of a message",
|
|
Value: "",
|
|
},
|
|
&cli.BoolFlag{
|
|
Name: "force",
|
|
Aliases: []string{"f"},
|
|
Usage: "A flag to bypass a confirmation step",
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func idFlag() *cli.Int64Flag {
|
|
return &cli.Int64Flag{
|
|
Name: "id",
|
|
Usage: "ID of authentication source",
|
|
}
|
|
}
|
|
|
|
func runRepoSyncReleases(ctx context.Context, c *cli.Command) error {
|
|
ctx, cancel := installSignals(ctx)
|
|
defer cancel()
|
|
|
|
if err := initDB(ctx); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := git.InitSimple(ctx); err != nil {
|
|
return err
|
|
}
|
|
|
|
log.Trace("Synchronizing repository releases (this may take a while)")
|
|
for page := 1; ; page++ {
|
|
repos, count, err := repo_model.SearchRepositoryByName(ctx, &repo_model.SearchRepoOptions{
|
|
ListOptions: db.ListOptions{
|
|
PageSize: repo_model.RepositoryListDefaultPageSize,
|
|
Page: page,
|
|
},
|
|
Private: true,
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("SearchRepositoryByName: %w", err)
|
|
}
|
|
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())
|
|
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
|
|
if err != nil {
|
|
log.Warn("OpenRepository: %v", err)
|
|
continue
|
|
}
|
|
|
|
oldnum, err := getReleaseCount(ctx, repo.ID)
|
|
if err != nil {
|
|
log.Warn(" GetReleaseCountByRepoID: %v", err)
|
|
}
|
|
log.Trace(" currentNumReleases is %d, running SyncReleasesWithTags", oldnum)
|
|
|
|
if err = repo_module.SyncReleasesWithTags(ctx, repo, gitRepo); err != nil {
|
|
log.Warn(" SyncReleasesWithTags: %v", err)
|
|
gitRepo.Close()
|
|
continue
|
|
}
|
|
|
|
count, err = getReleaseCount(ctx, repo.ID)
|
|
if err != nil {
|
|
log.Warn(" GetReleaseCountByRepoID: %v", err)
|
|
gitRepo.Close()
|
|
continue
|
|
}
|
|
|
|
log.Trace(" repo %s releases synchronized to tags: from %d to %d",
|
|
repo.FullName(), oldnum, count)
|
|
gitRepo.Close()
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func getReleaseCount(ctx context.Context, id int64) (int64, error) {
|
|
return db.Count[repo_model.Release](
|
|
ctx,
|
|
repo_model.FindReleasesOptions{
|
|
RepoID: id,
|
|
IncludeTags: true,
|
|
},
|
|
)
|
|
}
|