2023-07-09 14:52:21 +02:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package actions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-07-22 11:55:43 +02:00
|
|
|
"crypto/subtle"
|
2023-07-09 14:52:21 +02:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
auth_model "code.gitea.io/gitea/models/auth"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
|
|
|
|
gouuid "github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
feat(cli): add `--keep-labels` flag to `forgejo actions register` (#4610)
This commit adds a new flag, `--keep-labels`, to the runner registration CLI command. If this flag is present and the runner being registered already exists, it will prevent the runners' labels from being reset.
In order to accomplish this, the signature of the `RegisterRunner` function from the `models/actions` package has been modified so that the labels argument can be nil. If it is, the part of the function that updates the record will not change the runner.
Various tests have been added for this function, for the following cases: new runner with labels, new runner without label, existing runner with labels, existing runner without labels.
The flag has been added to the CLI command, the action function has been updated to read the labels parameters through a separate function (`getLabels`), and test cases for this function have been added.
<!--
Before submitting a PR, please read the contributing guidelines:
https://codeberg.org/forgejo/forgejo/src/branch/forgejo/CONTRIBUTING.md
-->
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4610
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Emmanuel BENOÎT <tseeker@nocternity.net>
Co-committed-by: Emmanuel BENOÎT <tseeker@nocternity.net>
2024-07-22 09:33:45 +02:00
|
|
|
func RegisterRunner(ctx context.Context, ownerID, repoID int64, token string, labels *[]string, name, version string) (*ActionRunner, error) {
|
2023-07-09 14:52:21 +02:00
|
|
|
uuid, err := gouuid.FromBytes([]byte(token[:16]))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("gouuid.FromBytes %v", err)
|
|
|
|
}
|
|
|
|
uuidString := uuid.String()
|
|
|
|
|
|
|
|
var runner ActionRunner
|
|
|
|
|
|
|
|
has, err := db.GetEngine(ctx).Where("uuid=?", uuidString).Get(&runner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("GetRunner %v", err)
|
2024-07-22 11:55:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var mustUpdateSecret bool
|
|
|
|
if has {
|
|
|
|
//
|
|
|
|
// The runner exists, check if the rest of the token has changed.
|
|
|
|
//
|
|
|
|
mustUpdateSecret = subtle.ConstantTimeCompare(
|
|
|
|
[]byte(runner.TokenHash),
|
|
|
|
[]byte(auth_model.HashToken(token, runner.TokenSalt)),
|
|
|
|
) != 1
|
|
|
|
} else {
|
2023-07-09 14:52:21 +02:00
|
|
|
//
|
|
|
|
// The runner does not exist yet, create it
|
|
|
|
//
|
|
|
|
runner = ActionRunner{
|
feat(cli): add `--keep-labels` flag to `forgejo actions register` (#4610)
This commit adds a new flag, `--keep-labels`, to the runner registration CLI command. If this flag is present and the runner being registered already exists, it will prevent the runners' labels from being reset.
In order to accomplish this, the signature of the `RegisterRunner` function from the `models/actions` package has been modified so that the labels argument can be nil. If it is, the part of the function that updates the record will not change the runner.
Various tests have been added for this function, for the following cases: new runner with labels, new runner without label, existing runner with labels, existing runner without labels.
The flag has been added to the CLI command, the action function has been updated to read the labels parameters through a separate function (`getLabels`), and test cases for this function have been added.
<!--
Before submitting a PR, please read the contributing guidelines:
https://codeberg.org/forgejo/forgejo/src/branch/forgejo/CONTRIBUTING.md
-->
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4610
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Emmanuel BENOÎT <tseeker@nocternity.net>
Co-committed-by: Emmanuel BENOÎT <tseeker@nocternity.net>
2024-07-22 09:33:45 +02:00
|
|
|
UUID: uuidString,
|
|
|
|
AgentLabels: []string{},
|
2023-07-09 14:52:21 +02:00
|
|
|
}
|
|
|
|
|
2024-07-22 11:55:43 +02:00
|
|
|
if err := runner.UpdateSecret(token); err != nil {
|
|
|
|
return &runner, fmt.Errorf("can't set new runner's secret: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-07-09 14:52:21 +02:00
|
|
|
if err := CreateRunner(ctx, &runner); err != nil {
|
|
|
|
return &runner, fmt.Errorf("can't create new runner %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Update the existing runner
|
|
|
|
//
|
|
|
|
name, _ = util.SplitStringAtByteN(name, 255)
|
|
|
|
|
feat(cli): add `--keep-labels` flag to `forgejo actions register` (#4610)
This commit adds a new flag, `--keep-labels`, to the runner registration CLI command. If this flag is present and the runner being registered already exists, it will prevent the runners' labels from being reset.
In order to accomplish this, the signature of the `RegisterRunner` function from the `models/actions` package has been modified so that the labels argument can be nil. If it is, the part of the function that updates the record will not change the runner.
Various tests have been added for this function, for the following cases: new runner with labels, new runner without label, existing runner with labels, existing runner without labels.
The flag has been added to the CLI command, the action function has been updated to read the labels parameters through a separate function (`getLabels`), and test cases for this function have been added.
<!--
Before submitting a PR, please read the contributing guidelines:
https://codeberg.org/forgejo/forgejo/src/branch/forgejo/CONTRIBUTING.md
-->
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4610
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Emmanuel BENOÎT <tseeker@nocternity.net>
Co-committed-by: Emmanuel BENOÎT <tseeker@nocternity.net>
2024-07-22 09:33:45 +02:00
|
|
|
cols := []string{"name", "owner_id", "repo_id", "version"}
|
2023-07-09 14:52:21 +02:00
|
|
|
runner.Name = name
|
|
|
|
runner.OwnerID = ownerID
|
|
|
|
runner.RepoID = repoID
|
|
|
|
runner.Version = version
|
feat(cli): add `--keep-labels` flag to `forgejo actions register` (#4610)
This commit adds a new flag, `--keep-labels`, to the runner registration CLI command. If this flag is present and the runner being registered already exists, it will prevent the runners' labels from being reset.
In order to accomplish this, the signature of the `RegisterRunner` function from the `models/actions` package has been modified so that the labels argument can be nil. If it is, the part of the function that updates the record will not change the runner.
Various tests have been added for this function, for the following cases: new runner with labels, new runner without label, existing runner with labels, existing runner without labels.
The flag has been added to the CLI command, the action function has been updated to read the labels parameters through a separate function (`getLabels`), and test cases for this function have been added.
<!--
Before submitting a PR, please read the contributing guidelines:
https://codeberg.org/forgejo/forgejo/src/branch/forgejo/CONTRIBUTING.md
-->
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4610
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Emmanuel BENOÎT <tseeker@nocternity.net>
Co-committed-by: Emmanuel BENOÎT <tseeker@nocternity.net>
2024-07-22 09:33:45 +02:00
|
|
|
if labels != nil {
|
|
|
|
runner.AgentLabels = *labels
|
|
|
|
cols = append(cols, "agent_labels")
|
|
|
|
}
|
2024-07-22 11:55:43 +02:00
|
|
|
if mustUpdateSecret {
|
|
|
|
if err := runner.UpdateSecret(token); err != nil {
|
|
|
|
return &runner, fmt.Errorf("can't change runner's secret: %w", err)
|
|
|
|
}
|
|
|
|
cols = append(cols, "token_hash", "token_salt")
|
|
|
|
}
|
2023-07-09 14:52:21 +02:00
|
|
|
|
feat(cli): add `--keep-labels` flag to `forgejo actions register` (#4610)
This commit adds a new flag, `--keep-labels`, to the runner registration CLI command. If this flag is present and the runner being registered already exists, it will prevent the runners' labels from being reset.
In order to accomplish this, the signature of the `RegisterRunner` function from the `models/actions` package has been modified so that the labels argument can be nil. If it is, the part of the function that updates the record will not change the runner.
Various tests have been added for this function, for the following cases: new runner with labels, new runner without label, existing runner with labels, existing runner without labels.
The flag has been added to the CLI command, the action function has been updated to read the labels parameters through a separate function (`getLabels`), and test cases for this function have been added.
<!--
Before submitting a PR, please read the contributing guidelines:
https://codeberg.org/forgejo/forgejo/src/branch/forgejo/CONTRIBUTING.md
-->
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4610
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Emmanuel BENOÎT <tseeker@nocternity.net>
Co-committed-by: Emmanuel BENOÎT <tseeker@nocternity.net>
2024-07-22 09:33:45 +02:00
|
|
|
if err := UpdateRunner(ctx, &runner, cols...); err != nil {
|
2023-07-09 14:52:21 +02:00
|
|
|
return &runner, fmt.Errorf("can't update the runner %+v %w", runner, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &runner, nil
|
|
|
|
}
|