Skip to content

Commit 66f11d5

Browse files
committed
feat: Allow archiving on destroy.
1 parent 4ee5e40 commit 66f11d5

File tree

2 files changed

+70
-36
lines changed

2 files changed

+70
-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.
@@ -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
}
@@ -1502,48 +1509,74 @@ func (r *repositoryResource) Delete(ctx context.Context, req resource.DeleteRequ
15021509
return
15031510
}
15041511

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

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

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

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

0 commit comments

Comments
 (0)