这是indexloc提供的服务,不要输入任何密码
Skip to content

Consider and use the protection bypass for automation secret from the plan #295

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions client/project_protection_bypass_for_automation_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func getUpdateBypassProtectionRequestBody(newValue bool, secret string) string {
return "{}"
}
return string(mustMarshal(struct {
Revoke generateBypassProtectionRequest `json:"generate"`
Generate generateBypassProtectionRequest `json:"generate"`
}{
Revoke: generateBypassProtectionRequest{
Generate: generateBypassProtectionRequest{
Secret: secret,
},
}))
Expand Down
59 changes: 50 additions & 9 deletions vercel/resource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -1624,7 +1624,8 @@ func (r *projectResource) Create(ctx context.Context, req resource.CreateRequest
}

if plan.ProtectionBypassForAutomation.ValueBool() {
protectionBypassSecret, err := r.client.UpdateProtectionBypassForAutomation(ctx, client.UpdateProtectionBypassForAutomationRequest{
plannedSecret := plan.ProtectionBypassForAutomationSecret.ValueString()
_, err := r.client.UpdateProtectionBypassForAutomation(ctx, client.UpdateProtectionBypassForAutomationRequest{
ProjectID: result.ID.ValueString(),
TeamID: result.TeamID.ValueString(),
NewValue: true,
Expand All @@ -1637,7 +1638,7 @@ func (r *projectResource) Create(ctx context.Context, req resource.CreateRequest
)
return
}
result.ProtectionBypassForAutomationSecret = types.StringValue(protectionBypassSecret)
result.ProtectionBypassForAutomationSecret = types.StringValue(plannedSecret)
result.ProtectionBypassForAutomation = types.BoolValue(true)
diags = resp.State.Set(ctx, result)
resp.Diagnostics.Append(diags...)
Expand Down Expand Up @@ -1912,22 +1913,62 @@ func (r *projectResource) Update(ctx context.Context, req resource.UpdateRequest
})
}

currentSecret := state.ProtectionBypassForAutomationSecret.ValueString()
plannedSecret := plan.ProtectionBypassForAutomationSecret.ValueString()
if state.ProtectionBypassForAutomation != plan.ProtectionBypassForAutomation {
secret := state.ProtectionBypassForAutomationSecret.ValueString()
if plan.ProtectionBypassForAutomationSecret.ValueString() != "" {
secret = plan.ProtectionBypassForAutomationSecret.ValueString()
if plan.ProtectionBypassForAutomation.ValueBool() {
// protection bypass was previously not set, and now is
_, err := r.client.UpdateProtectionBypassForAutomation(ctx, client.UpdateProtectionBypassForAutomationRequest{
ProjectID: plan.ID.ValueString(),
TeamID: plan.TeamID.ValueString(),
NewValue: true,
Secret: plannedSecret,
})
if err != nil {
resp.Diagnostics.AddError(
"Error setting protection bypass for automation",
fmt.Sprintf(
"Could not update project %s %s, unexpected error setting Protection Bypass For Automation: %s",
state.TeamID.ValueString(),
state.ID.ValueString(),
err,
),
)
return
}
} else {
// protection bypass was previously set, and now is not
_, err := r.client.UpdateProtectionBypassForAutomation(ctx, client.UpdateProtectionBypassForAutomationRequest{
ProjectID: plan.ID.ValueString(),
TeamID: plan.TeamID.ValueString(),
NewValue: false,
})
if err != nil {
resp.Diagnostics.AddError(
"Error removing protection bypass for automation",
fmt.Sprintf(
"Could not update project %s %s, unexpected error removing Protection Bypass For Automation: %s",
state.TeamID.ValueString(),
state.ID.ValueString(),
err,
),
)
return
}
}
} else if plan.ProtectionBypassForAutomation.ValueBool() && currentSecret != plannedSecret {
// protection bypass is already configured and the secret is different
_, err := r.client.UpdateProtectionBypassForAutomation(ctx, client.UpdateProtectionBypassForAutomationRequest{
ProjectID: plan.ID.ValueString(),
TeamID: plan.TeamID.ValueString(),
NewValue: plan.ProtectionBypassForAutomation.ValueBool(),
Secret: secret,
NewValue: true,
Secret: plannedSecret,
})
if err != nil {
resp.Diagnostics.AddError(
"Error updating project",
"Error updating protection bypass for automation",
fmt.Sprintf(
"Could not update project %s %s, unexpected error setting Protection Bypass For Automation: %s",
"Could not update project %s %s, unexpected error updating Protection Bypass For Automation: %s",
state.TeamID.ValueString(),
state.ID.ValueString(),
err,
Expand Down