Skip to content
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
1 change: 1 addition & 0 deletions docs/resources/repository.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ import {
- `allow_rebase` (Boolean) Allowed to rebase then fast-forward? **Note**: This setting is only effective if `has_pull_requests` is `true`.
- `allow_rebase_explicit` (Boolean) Allowed to rebase then create merge commit? **Note**: This setting is only effective if `has_pull_requests` is `true`.
- `allow_squash_merge` (Boolean) Allowed to create squash commit? **Note**: This setting is only effective if `has_pull_requests` is `true`.
- `archive_on_destroy` (Boolean) Archive the repo instead of delete?
- `archived` (Boolean) Is the repository archived?
- `auth_token` (String, Sensitive) API token for authenticating with migrate / clone URL. **Note**: This setting is only effective if `clone_addr` is set.
- `auto_init` (Boolean) Whether the repository should be auto-intialized?
Expand Down
105 changes: 69 additions & 36 deletions internal/provider/repository_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ type repositoryResourceModel struct {
Milestones types.Bool `tfsdk:"milestones"`
Labels types.Bool `tfsdk:"labels"`
Service types.String `tfsdk:"service"`
ArchiveOnDestroy types.Bool `tfsdk:"archive_on_destroy"`
}

// from is a helper function to load an API struct into Terraform data model.
Expand Down Expand Up @@ -980,6 +981,12 @@ Note: Managing user repositories requires administrative privileges!`,
),
},
},
"archive_on_destroy": schema.BoolAttribute{
Description: "Archive the repo instead of delete?",
Computed: true,
Optional: true,
Default: booldefault.StaticBool(false),
},
},
}
}
Expand Down Expand Up @@ -1503,48 +1510,74 @@ func (r *repositoryResource) Delete(ctx context.Context, req resource.DeleteRequ
return
}

tflog.Info(ctx, "Delete repository", map[string]any{
"owner": data.Owner.ValueString(),
"name": data.Name.ValueString(),
})

// Use Forgejo client to delete existing repository
res, err := r.client.DeleteRepo(
data.Owner.ValueString(),
data.Name.ValueString(),
var (
res *forgejo.Response
err error
)
if err != nil {
var msg string
if res == nil {
msg = fmt.Sprintf("Unknown error with nil response: %s", err)
} else {
tflog.Error(ctx, "Error", map[string]any{
"status": res.Status,
})

switch res.StatusCode {
case 403:
msg = fmt.Sprintf(
"Repository with owner %s and name %s forbidden: %s",
data.Owner.String(),
data.Name.String(),
err,
)
case 404:
msg = fmt.Sprintf(
"Repository with owner %s and name %s not found: %s",
data.Owner.String(),
data.Name.String(),
err,
)
default:
msg = fmt.Sprintf("Unknown error: %s", err)
}
if data.ArchiveOnDestroy.ValueBool() {
tflog.Info(ctx, "Archive repository", map[string]any{
"owner": data.Owner.ValueString(),
"name": data.Name.ValueString(),
})

archive := true
opts := forgejo.EditRepoOption{
Archived: &archive,
}
resp.Diagnostics.AddError("Unable to delete repository", msg)

_, res, err = r.client.EditRepo(
data.Owner.ValueString(),
data.Name.ValueString(),
opts,
)
} else {
tflog.Info(ctx, "Delete repository", map[string]any{
"owner": data.Owner.ValueString(),
"name": data.Name.ValueString(),
})

// Use Forgejo client to delete existing repository
res, err = r.client.DeleteRepo(
data.Owner.ValueString(),
data.Name.ValueString(),
)
}

if err == nil {
return
}

var msg string
if res == nil {
msg = fmt.Sprintf("Unknown error with nil response: %s", err)
} else {
tflog.Error(ctx, "Error", map[string]any{
"status": res.Status,
})

switch res.StatusCode {
case 403:
msg = fmt.Sprintf(
"Repository with owner %s and name %s forbidden: %s",
data.Owner.String(),
data.Name.String(),
err,
)
case 404:
msg = fmt.Sprintf(
"Repository with owner %s and name %s not found: %s",
data.Owner.String(),
data.Name.String(),
err,
)
case 422:
msg = fmt.Sprintf("Input validation error: %s", err)
default:
msg = fmt.Sprintf("Unknown error: %s", err)
}
}
resp.Diagnostics.AddError("Unable to delete repository", msg)
}

// ImportState reads an existing resource and adds it to Terraform state on success.
Expand Down
Loading