Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 78055e4

Browse files
committed
Allow variables to be injected that are not defined by the schema
1 parent 5a44d84 commit 78055e4

File tree

3 files changed

+25
-37
lines changed

3 files changed

+25
-37
lines changed

coderd/projectparameter/projectparameter.go

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
// Scope targets identifiers to pull parameters from.
1717
type Scope struct {
1818
ImportJobID uuid.UUID
19-
OrganizationID sql.NullString
19+
OrganizationID string
2020
ProjectID uuid.NullUUID
2121
UserID sql.NullString
2222
WorkspaceID uuid.NullUUID
@@ -42,12 +42,10 @@ func Compute(ctx context.Context, db database.Store, scope Scope, additional ...
4242
parameterSchemasByName: map[string]database.ParameterSchema{},
4343
}
4444

45-
// All parameters for the project version!
45+
// All parameters for the import job ID!
4646
parameterSchemas, err := db.GetParameterSchemasByJobID(ctx, scope.ImportJobID)
4747
if errors.Is(err, sql.ErrNoRows) {
48-
// This occurs when the provided import job has
49-
// defined no parameters, so we have nothing to compute!
50-
return []Value{}, nil
48+
err = nil
5149
}
5250
if err != nil {
5351
return nil, xerrors.Errorf("get project parameters: %w", err)
@@ -56,15 +54,13 @@ func Compute(ctx context.Context, db database.Store, scope Scope, additional ...
5654
compute.parameterSchemasByName[projectVersionParameter.Name] = projectVersionParameter
5755
}
5856

59-
if scope.OrganizationID.Valid {
60-
// Organization parameters come first!
61-
err = compute.injectScope(ctx, database.GetParameterValuesByScopeParams{
62-
Scope: database.ParameterScopeOrganization,
63-
ScopeID: scope.OrganizationID.String,
64-
})
65-
if err != nil {
66-
return nil, err
67-
}
57+
// Organization parameters come first!
58+
err = compute.injectScope(ctx, database.GetParameterValuesByScopeParams{
59+
Scope: database.ParameterScopeOrganization,
60+
ScopeID: scope.OrganizationID,
61+
})
62+
if err != nil {
63+
return nil, err
6864
}
6965

7066
// Default project parameter values come second!
@@ -182,18 +178,16 @@ func (c *compute) injectScope(ctx context.Context, scopeParams database.GetParam
182178

183179
func (c *compute) injectSingle(scopedParameter database.ParameterValue) error {
184180
parameterSchema, hasParameterSchema := c.parameterSchemasByName[scopedParameter.Name]
185-
if !hasParameterSchema {
181+
if hasParameterSchema {
186182
// Don't inject parameters that aren't defined by the project.
187-
return nil
188-
}
189-
190-
_, hasExistingParameter := c.computedParameterByName[scopedParameter.Name]
191-
if hasExistingParameter {
192-
// If a parameter already exists, check if this variable can override it.
193-
// Injection hierarchy is the responsibility of the caller. This check ensures
194-
// project parameters cannot be overridden if already set.
195-
if !parameterSchema.AllowOverrideSource && scopedParameter.Scope != database.ParameterScopeProject {
196-
return nil
183+
_, hasExistingParameter := c.computedParameterByName[scopedParameter.Name]
184+
if hasExistingParameter {
185+
// If a parameter already exists, check if this variable can override it.
186+
// Injection hierarchy is the responsibility of the caller. This check ensures
187+
// project parameters cannot be overridden if already set.
188+
if !parameterSchema.AllowOverrideSource && scopedParameter.Scope != database.ParameterScopeProject {
189+
return nil
190+
}
197191
}
198192
}
199193

@@ -204,7 +198,7 @@ func (c *compute) injectSingle(scopedParameter database.ParameterValue) error {
204198

205199
switch scopedParameter.SourceScheme {
206200
case database.ParameterSourceSchemeData:
207-
c.computedParameterByName[parameterSchema.Name] = Value{
201+
c.computedParameterByName[scopedParameter.Name] = Value{
208202
Proto: &proto.ParameterValue{
209203
DestinationScheme: destinationScheme,
210204
Name: scopedParameter.SourceValue,

coderd/projectparameter/projectparameter_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@ func TestCompute(t *testing.T) {
1919
t.Parallel()
2020
generateScope := func() projectparameter.Scope {
2121
return projectparameter.Scope{
22-
ImportJobID: uuid.New(),
23-
OrganizationID: sql.NullString{
24-
String: uuid.New().String(),
25-
Valid: true,
26-
},
22+
ImportJobID: uuid.New(),
23+
OrganizationID: uuid.NewString(),
2724
ProjectID: uuid.NullUUID{
2825
UUID: uuid.New(),
2926
Valid: true,
@@ -124,7 +121,7 @@ func TestCompute(t *testing.T) {
124121
ID: uuid.New(),
125122
Name: parameter.Name,
126123
Scope: database.ParameterScopeOrganization,
127-
ScopeID: scope.OrganizationID.String,
124+
ScopeID: scope.OrganizationID,
128125
SourceScheme: database.ParameterSourceSchemeData,
129126
SourceValue: "nop",
130127
DestinationScheme: database.ParameterDestinationSchemeEnvironmentVariable,

coderd/provisionerdaemons.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,8 @@ func (server *provisionerdServer) AcquireJob(ctx context.Context, _ *proto.Empty
255255

256256
// Compute parameters for the workspace to consume.
257257
parameters, err := projectparameter.Compute(ctx, server.Database, projectparameter.Scope{
258-
ImportJobID: job.ID,
259-
OrganizationID: sql.NullString{
260-
String: input.OrganizationID,
261-
Valid: input.OrganizationID != "",
262-
},
258+
ImportJobID: job.ID,
259+
OrganizationID: input.OrganizationID,
263260
ProjectID: uuid.NullUUID{
264261
UUID: input.ProjectID,
265262
Valid: input.ProjectID.String() != uuid.Nil.String(),

0 commit comments

Comments
 (0)