Skip to content

Commit

Permalink
feat: add ability to create repos on behalf of other user
Browse files Browse the repository at this point in the history
  • Loading branch information
acch committed Dec 19, 2024
1 parent 9a4fd8f commit 0fee3a7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
29 changes: 29 additions & 0 deletions examples/resources/forgejo_repository/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,28 @@ resource "forgejo_repository" "org_non_defaults" {
trust_model = "collaborator"
}

resource "forgejo_repository" "user_defaults" {
owner = {
login = "test_user_1"
}
name = "user_tftest_defaults"
}
resource "forgejo_repository" "user_non_defaults" {
owner = {
login = "test_user_1"
}
name = "user_tftest_non_defaults"
description = "Terraform Test Repo owned by user with non-default attributes"
# website = "http://localhost:3000"
private = true
template = true
default_branch = "custom"
issue_labels = "Default"
auto_init = false
readme = "Default"
trust_model = "collaborator"
}

output "personal_debug_defaults" {
value = forgejo_repository.personal_defaults
}
Expand All @@ -63,3 +85,10 @@ output "org_debug_defaults" {
output "org_debug_non_defaults" {
value = forgejo_repository.org_non_defaults
}

output "user_debug_defaults" {
value = forgejo_repository.user_defaults
}
output "user_debug_non_defaults" {
value = forgejo_repository.user_non_defaults
}
42 changes: 31 additions & 11 deletions internal/provider/repository_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -913,20 +913,21 @@ func (r *repositoryResource) Create(ctx context.Context, req resource.CreateRequ
_, res, _ := r.client.GetOrg(owner.UserName.ValueString())
if res.StatusCode == 404 {
// Use Forgejo client to check if owner is user
// TODO: check to see if owner is user or raise an error

resp.Diagnostics.AddError(
"Owner not found",
fmt.Sprintf(
"Neither organization nor user with name %s exists.",
owner.UserName.String(),
),
)
_, res, _ = r.client.GetUserInfo(owner.UserName.ValueString())
if res.StatusCode == 404 {
resp.Diagnostics.AddError(
"Owner not found",
fmt.Sprintf(
"Neither organization nor user with name %s exists.",
owner.UserName.String(),
),
)

return
return
}

// User exists -> user repository
// ownerType = "user"
ownerType = "user"
} else {
// Org exists -> org repository
ownerType = "org"
Expand All @@ -948,6 +949,12 @@ func (r *repositoryResource) Create(ctx context.Context, req resource.CreateRequ
case "personal":
// Use Forgejo client to create new personal repository
rep, res, err = r.client.CreateRepo(opts)
case "user":
// Use Forgejo client to create new user repository
rep, res, err = r.client.AdminCreateRepo(
owner.UserName.ValueString(),
opts,
)
}
if err != nil {
tflog.Error(ctx, "Error", map[string]any{
Expand All @@ -956,6 +963,19 @@ func (r *repositoryResource) Create(ctx context.Context, req resource.CreateRequ

var msg string
switch res.StatusCode {
case 403:
msg = fmt.Sprintf(
"Repository with owner %s and name %s forbidden: %s",
owner.UserName.String(),
data.Name.String(),
err,
)
case 404:
msg = fmt.Sprintf(
"Repository owner with name %s not found: %s",
owner.UserName.String(),
err,
)
case 409:
msg = fmt.Sprintf(
"Repository with name %s already exists: %s",
Expand Down

0 comments on commit 0fee3a7

Please sign in to comment.