Skip to content

Commit 5545d8c

Browse files
committed
feat: Allow archiving on destroy.
1 parent 61411d8 commit 5545d8c

File tree

2 files changed

+66
-31
lines changed

2 files changed

+66
-31
lines changed

docs/resources/repository.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ import {
176176
- `allow_rebase` (Boolean) Allowed to rebase then fast-forward? **Note**: This setting is only effective if `has_pull_requests` is `true`.
177177
- `allow_rebase_explicit` (Boolean) Allowed to rebase then create merge commit? **Note**: This setting is only effective if `has_pull_requests` is `true`.
178178
- `allow_squash_merge` (Boolean) Allowed to create squash commit? **Note**: This setting is only effective if `has_pull_requests` is `true`.
179+
- `archive_on_destroy` (Boolean) Archive the repo instead of delete?
179180
- `archived` (Boolean) Is the repository archived?
180181
- `auth_token` (String, Sensitive) API token for authenticating with migrate / clone URL. **Note**: This setting is only effective if `clone_addr` is set.
181182
- `auto_init` (Boolean) Whether the repository should be auto-intialized?

internal/provider/repository_resource.go

Lines changed: 65 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ type repositoryResourceModel struct {
105105
Milestones types.Bool `tfsdk:"milestones"`
106106
Labels types.Bool `tfsdk:"labels"`
107107
Service types.String `tfsdk:"service"`
108+
ArchiveOnDestroy types.Bool `tfsdk:"archive_on_destroy"`
108109
}
109110

110111
// from is a helper function to load an API struct into Terraform data model.
@@ -979,6 +980,12 @@ Note: Managing user repositories requires administrative privileges!`,
979980
),
980981
},
981982
},
983+
"archive_on_destroy": schema.BoolAttribute{
984+
Description: "Archive the repo instead of delete?",
985+
Computed: true,
986+
Optional: true,
987+
Default: booldefault.StaticBool(false),
988+
},
982989
},
983990
}
984991
}
@@ -1490,44 +1497,71 @@ func (r *repositoryResource) Delete(ctx context.Context, req resource.DeleteRequ
14901497
return
14911498
}
14921499

1493-
tflog.Info(ctx, "Delete repository", map[string]any{
1494-
"owner": data.Owner.ValueString(),
1495-
"name": data.Name.ValueString(),
1496-
})
1497-
1498-
// Use Forgejo client to delete existing repository
1499-
res, err := r.client.DeleteRepo(
1500-
data.Owner.ValueString(),
1501-
data.Name.ValueString(),
1500+
var (
1501+
res *forgejo.Response
1502+
err error
15021503
)
1503-
if err != nil {
1504-
tflog.Error(ctx, "Error", map[string]any{
1505-
"status": res.Status,
1504+
if data.ArchiveOnDestroy.ValueBool() {
1505+
tflog.Info(ctx, "Archive repository", map[string]any{
1506+
"owner": data.Owner.ValueString(),
1507+
"name": data.Name.ValueString(),
15061508
})
15071509

1508-
var msg string
1509-
switch res.StatusCode {
1510-
case 403:
1511-
msg = fmt.Sprintf(
1512-
"Repository with owner %s and name %s forbidden: %s",
1513-
data.Owner.String(),
1514-
data.Name.String(),
1515-
err,
1516-
)
1517-
case 404:
1518-
msg = fmt.Sprintf(
1519-
"Repository with owner %s and name %s not found: %s",
1520-
data.Owner.String(),
1521-
data.Name.String(),
1522-
err,
1523-
)
1524-
default:
1525-
msg = fmt.Sprintf("Unknown error: %s", err)
1510+
archive := true
1511+
opts := forgejo.EditRepoOption{
1512+
Archived: &archive,
15261513
}
1527-
resp.Diagnostics.AddError("Unable to delete repository", msg)
15281514

1515+
_, res, err = r.client.EditRepo(
1516+
data.Owner.ValueString(),
1517+
data.Name.ValueString(),
1518+
opts,
1519+
)
1520+
} else {
1521+
tflog.Info(ctx, "Delete repository", map[string]any{
1522+
"owner": data.Owner.ValueString(),
1523+
"name": data.Name.ValueString(),
1524+
})
1525+
1526+
// Use Forgejo client to delete existing repository
1527+
res, err = r.client.DeleteRepo(
1528+
data.Owner.ValueString(),
1529+
data.Name.ValueString(),
1530+
)
1531+
}
1532+
1533+
if err == nil {
15291534
return
15301535
}
1536+
1537+
tflog.Error(ctx, "Error", map[string]any{
1538+
"status": res.Status,
1539+
})
1540+
1541+
var msg string
1542+
switch res.StatusCode {
1543+
case 403:
1544+
msg = fmt.Sprintf(
1545+
"Repository with owner %s and name %s forbidden: %s",
1546+
data.Owner.String(),
1547+
data.Name.String(),
1548+
err,
1549+
)
1550+
case 404:
1551+
msg = fmt.Sprintf(
1552+
"Repository with owner %s and name %s not found: %s",
1553+
data.Owner.String(),
1554+
data.Name.String(),
1555+
err,
1556+
)
1557+
case 422:
1558+
msg = fmt.Sprintf("Input validation error: %s", err)
1559+
default:
1560+
msg = fmt.Sprintf("Unknown error: %s", err)
1561+
}
1562+
resp.Diagnostics.AddError("Unable to delete repository", msg)
1563+
1564+
return
15311565
}
15321566

15331567
// ImportState reads an existing resource and adds it to Terraform state on success.

0 commit comments

Comments
 (0)