Skip to content

Commit ec9137b

Browse files
feat: validate organization existence before repository creation
- Added OrgExists method to check if organization exists on Gitea server - Implemented interactive prompt to fallback to personal account when organization not found - Added trimming of organization name whitespace to prevent validation issues
1 parent c22904b commit ec9137b

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

pkg/repository/creator.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,32 @@ func (c *Creator) ensureInitialCommit() error {
189189
func (c *Creator) ensureRemoteRepository() (*gitea.Repository, error) {
190190
logger := otelzap.Ctx(c.rc.Ctx)
191191

192-
owner := c.opts.Organization
192+
owner := strings.TrimSpace(c.opts.Organization)
193+
c.opts.Organization = owner
194+
195+
if owner != "" {
196+
exists, err := c.giteaClient.OrgExists(owner)
197+
if err != nil {
198+
return nil, err
199+
}
200+
if !exists {
201+
if c.opts.NonInteractive {
202+
return nil, fmt.Errorf("organization %s not found on Gitea; create it first or choose an existing organization", owner)
203+
}
204+
205+
personalOwner := c.giteaClient.Username()
206+
if !promptYesNo(fmt.Sprintf("Organization %q was not found on Gitea. Create the repository under your personal account (%s) instead?", owner, personalOwner), false) {
207+
return nil, fmt.Errorf("organization %s not found on Gitea; create it first or choose an existing organization", owner)
208+
}
209+
210+
logger.Warn("Organization not found on Gitea; falling back to personal namespace",
211+
zap.String("organization", owner),
212+
zap.String("username", personalOwner))
213+
c.opts.Organization = ""
214+
owner = ""
215+
}
216+
}
217+
193218
if owner == "" {
194219
owner = c.giteaClient.Username()
195220
}

pkg/repository/gitea.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ func (c *GiteaClient) RepoExists(owner, repo string) (*gitea.Repository, bool, e
5050
return r, true, nil
5151
}
5252

53+
// OrgExists checks if the specified organization is present on the server.
54+
func (c *GiteaClient) OrgExists(org string) (bool, error) {
55+
if strings.TrimSpace(org) == "" {
56+
return false, nil
57+
}
58+
59+
_, resp, err := c.client.GetOrg(org)
60+
if err != nil {
61+
if resp != nil && resp.StatusCode == http.StatusNotFound {
62+
return false, nil
63+
}
64+
return false, fmt.Errorf("failed to query organization %s: %w", org, err)
65+
}
66+
return true, nil
67+
}
68+
5369
// CreateUserRepo creates a repository in the authenticated user's namespace.
5470
func (c *GiteaClient) CreateUserRepo(opts *gitea.CreateRepoOption) (*gitea.Repository, error) {
5571
repo, resp, err := c.client.CreateRepo(*opts)

0 commit comments

Comments
 (0)