Skip to content

Commit 4d8d3e8

Browse files
committed
feat: Allow archiving on destroy.
1 parent 15a5cbf commit 4d8d3e8

File tree

3 files changed

+296
-36
lines changed

3 files changed

+296
-36
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: 69 additions & 36 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.
@@ -980,6 +981,12 @@ Note: Managing user repositories requires administrative privileges!`,
980981
),
981982
},
982983
},
984+
"archive_on_destroy": schema.BoolAttribute{
985+
Description: "Archive the repo instead of delete?",
986+
Computed: true,
987+
Optional: true,
988+
Default: booldefault.StaticBool(false),
989+
},
983990
},
984991
}
985992
}
@@ -1503,48 +1510,74 @@ func (r *repositoryResource) Delete(ctx context.Context, req resource.DeleteRequ
15031510
return
15041511
}
15051512

1506-
tflog.Info(ctx, "Delete repository", map[string]any{
1507-
"owner": data.Owner.ValueString(),
1508-
"name": data.Name.ValueString(),
1509-
})
1510-
1511-
// Use Forgejo client to delete existing repository
1512-
res, err := r.client.DeleteRepo(
1513-
data.Owner.ValueString(),
1514-
data.Name.ValueString(),
1513+
var (
1514+
res *forgejo.Response
1515+
err error
15151516
)
1516-
if err != nil {
1517-
var msg string
1518-
if res == nil {
1519-
msg = fmt.Sprintf("Unknown error with nil response: %s", err)
1520-
} else {
1521-
tflog.Error(ctx, "Error", map[string]any{
1522-
"status": res.Status,
1523-
})
15241517

1525-
switch res.StatusCode {
1526-
case 403:
1527-
msg = fmt.Sprintf(
1528-
"Repository with owner %s and name %s forbidden: %s",
1529-
data.Owner.String(),
1530-
data.Name.String(),
1531-
err,
1532-
)
1533-
case 404:
1534-
msg = fmt.Sprintf(
1535-
"Repository with owner %s and name %s not found: %s",
1536-
data.Owner.String(),
1537-
data.Name.String(),
1538-
err,
1539-
)
1540-
default:
1541-
msg = fmt.Sprintf("Unknown error: %s", err)
1542-
}
1518+
if data.ArchiveOnDestroy.ValueBool() {
1519+
tflog.Info(ctx, "Archive repository", map[string]any{
1520+
"owner": data.Owner.ValueString(),
1521+
"name": data.Name.ValueString(),
1522+
})
1523+
1524+
archive := true
1525+
opts := forgejo.EditRepoOption{
1526+
Archived: &archive,
15431527
}
1544-
resp.Diagnostics.AddError("Unable to delete repository", msg)
15451528

1529+
_, res, err = r.client.EditRepo(
1530+
data.Owner.ValueString(),
1531+
data.Name.ValueString(),
1532+
opts,
1533+
)
1534+
} else {
1535+
tflog.Info(ctx, "Delete repository", map[string]any{
1536+
"owner": data.Owner.ValueString(),
1537+
"name": data.Name.ValueString(),
1538+
})
1539+
1540+
// Use Forgejo client to delete existing repository
1541+
res, err = r.client.DeleteRepo(
1542+
data.Owner.ValueString(),
1543+
data.Name.ValueString(),
1544+
)
1545+
}
1546+
1547+
if err == nil {
15461548
return
15471549
}
1550+
1551+
var msg string
1552+
if res == nil {
1553+
msg = fmt.Sprintf("Unknown error with nil response: %s", err)
1554+
} else {
1555+
tflog.Error(ctx, "Error", map[string]any{
1556+
"status": res.Status,
1557+
})
1558+
1559+
switch res.StatusCode {
1560+
case 403:
1561+
msg = fmt.Sprintf(
1562+
"Repository with owner %s and name %s forbidden: %s",
1563+
data.Owner.String(),
1564+
data.Name.String(),
1565+
err,
1566+
)
1567+
case 404:
1568+
msg = fmt.Sprintf(
1569+
"Repository with owner %s and name %s not found: %s",
1570+
data.Owner.String(),
1571+
data.Name.String(),
1572+
err,
1573+
)
1574+
case 422:
1575+
msg = fmt.Sprintf("Input validation error: %s", err)
1576+
default:
1577+
msg = fmt.Sprintf("Unknown error: %s", err)
1578+
}
1579+
}
1580+
resp.Diagnostics.AddError("Unable to delete repository", msg)
15481581
}
15491582

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

0 commit comments

Comments
 (0)