-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
add CLI command to register runner tokens #23762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
techknowlogick
merged 20 commits into
go-gitea:main
from
techknowlogick:cli-runner-token
Apr 17, 2023
Merged
Changes from 16 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
fe076a8
add CLI command to register runner tokens
techknowlogick a15f46e
fix lint t& fmt
techknowlogick 6e9a3b1
whoops
techknowlogick d7546ff
update cmd name
techknowlogick ff9b373
Merge remote-tracking branch 'upstream/main' into cli-runner-token
techknowlogick 2634507
update var names per feedback
techknowlogick cd9cb3a
return err per feedback
techknowlogick 021a33c
add docs
techknowlogick 83732fe
Update cmd/actions.go
techknowlogick 4c56aac
Update docs/content/doc/administration/command-line.en-us.md
techknowlogick 79f307f
Update cmd/actions.go
techknowlogick 9c109dc
Merge branch 'main' into cli-runner-token
techknowlogick 3c44ae8
move logic to internal route
techknowlogick a7a096c
fix route typo
techknowlogick 35e8a2e
fix lint
techknowlogick 743f0c7
Merge branch 'main' into cli-runner-token
techknowlogick a14ec33
Update cmd/actions.go
techknowlogick 989c153
Merge branch 'main' into cli-runner-token
techknowlogick aac5fb3
Merge branch 'main' into cli-runner-token
GiteaBot 9911554
Merge branch 'main' into cli-runner-token
GiteaBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"code.gitea.io/gitea/modules/private" | ||
"code.gitea.io/gitea/modules/setting" | ||
|
||
"github.com/urfave/cli" | ||
) | ||
|
||
var ( | ||
// CmdActions represents the available actions sub-commands. | ||
CmdActions = cli.Command{ | ||
Name: "actions", | ||
Usage: "", | ||
Description: "Commands for managing Gitea Actions", | ||
Subcommands: []cli.Command{ | ||
subcmdActionsGenRunnerToken, | ||
}, | ||
} | ||
|
||
subcmdActionsGenRunnerToken = cli.Command{ | ||
Name: "generate-runner-token", | ||
Usage: "Generate a new token for a runner to use to register with the server", | ||
Action: runGenerateActionsRunnerToken, | ||
Aliases: []string{"grt"}, | ||
Flags: []cli.Flag{ | ||
cli.StringFlag{ | ||
Name: "scope, s", | ||
Value: "", | ||
Usage: "{owner}[/{repo}]", | ||
}, | ||
}, | ||
} | ||
) | ||
|
||
func runGenerateActionsRunnerToken(c *cli.Context) error { | ||
ctx, cancel := installSignals() | ||
defer cancel() | ||
|
||
setting.InitProviderFromExistingFile() | ||
setting.LoadCommonSettings() | ||
|
||
scope := c.String("scope") | ||
|
||
respText, extra := private.GenerateActionsRunnerToken(ctx, scope) | ||
if extra.HasError() { | ||
return handleCliResponseExtra(extra) | ||
} | ||
_, _ = fmt.Printf("%s\n", respText) | ||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package private | ||
|
||
import ( | ||
"context" | ||
|
||
"code.gitea.io/gitea/modules/setting" | ||
) | ||
|
||
// Email structure holds a data for sending general emails | ||
type GenerateTokenRequest struct { | ||
Scope string | ||
} | ||
|
||
// GenerateActionsRunnerToken calls the internal GenerateActionsRunnerToken function | ||
func GenerateActionsRunnerToken(ctx context.Context, scope string) (string, ResponseExtra) { | ||
reqURL := setting.LocalURL + "api/internal/actions/generate_actions_runner_token" | ||
|
||
req := newInternalRequest(ctx, reqURL, "POST", GenerateTokenRequest{ | ||
Scope: scope, | ||
}) | ||
|
||
resp, extra := requestJSONResp(req, &responseText{}) | ||
return resp.Text, extra | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package private | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
actions_model "code.gitea.io/gitea/models/actions" | ||
repo_model "code.gitea.io/gitea/models/repo" | ||
user_model "code.gitea.io/gitea/models/user" | ||
"code.gitea.io/gitea/modules/context" | ||
"code.gitea.io/gitea/modules/json" | ||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/private" | ||
"code.gitea.io/gitea/modules/util" | ||
) | ||
|
||
// GenerateActionsRunnerToken generates a new runner token for a given scope | ||
func GenerateActionsRunnerToken(ctx *context.PrivateContext) { | ||
var genRequest private.GenerateTokenRequest | ||
rd := ctx.Req.Body | ||
defer rd.Close() | ||
|
||
if err := json.NewDecoder(rd).Decode(&genRequest); err != nil { | ||
log.Error("%v", err) | ||
ctx.JSON(http.StatusInternalServerError, private.Response{ | ||
Err: err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
owner, repo, err := parseScope(ctx, genRequest.Scope) | ||
if err != nil { | ||
log.Error("%v", err) | ||
ctx.JSON(http.StatusInternalServerError, private.Response{ | ||
Err: err.Error(), | ||
}) | ||
} | ||
|
||
token, err := actions_model.GetUnactivatedRunnerToken(ctx, owner, repo) | ||
if errors.Is(err, util.ErrNotExist) { | ||
token, err = actions_model.NewRunnerToken(ctx, owner, repo) | ||
if err != nil { | ||
err := fmt.Sprintf("error while creating runner token: %v", err) | ||
log.Error("%v", err) | ||
ctx.JSON(http.StatusInternalServerError, private.Response{ | ||
Err: err, | ||
}) | ||
return | ||
} | ||
} else if err != nil { | ||
err := fmt.Sprintf("could not get unactivated runner token: %v", err) | ||
log.Error("%v", err) | ||
ctx.JSON(http.StatusInternalServerError, private.Response{ | ||
Err: err, | ||
}) | ||
return | ||
} | ||
|
||
ctx.PlainText(http.StatusOK, token.Token) | ||
} | ||
|
||
func parseScope(ctx *context.PrivateContext, scope string) (ownerID, repoID int64, err error) { | ||
ownerID = 0 | ||
repoID = 0 | ||
if scope == "" { | ||
return ownerID, repoID, nil | ||
} | ||
|
||
ownerName, repoName, found := strings.Cut(scope, "/") | ||
|
||
u, err := user_model.GetUserByName(ctx, ownerName) | ||
if err != nil { | ||
return ownerID, repoID, err | ||
} | ||
|
||
if !found { | ||
return u.ID, repoID, nil | ||
} | ||
|
||
r, err := repo_model.GetRepositoryByName(u.ID, repoName) | ||
if err != nil { | ||
return ownerID, repoID, err | ||
} | ||
repoID = r.ID | ||
return ownerID, repoID, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.