theonedev/onedev · error · UnauthorizedException

Import target already exists. You need to have project manag

Error message

Import target already exists. You need to have project management privilege over it

What it means

When importing Gitea repositories, the target OneDev project is created/located via ProjectService.setup. If the project already exists and the current user lacks manage privilege on it, an UnauthorizedException is thrown rather than silently importing into someone else's project.

Source

Thrown at server-plugin/server-plugin-import-gitea/src/main/java/io/onedev/server/plugin/imports/gitea/ImportServer.java:524

			ProjectImportOption option, boolean dryRun, TaskLogger logger) {
		Client client = newClient();
		try {
			Map<String, Optional<Long>> userIds = new HashMap<>();
			ImportResult result = new ImportResult();
			for (var giteaRepository : repositories.getImportRepositories()) {
				OneDev.getInstance(TransactionService.class).run(() -> {
					try {
						String oneDevProjectPath = giteaRepository;
						if (repositories.getParentOneDevProject() != null)
							oneDevProjectPath = repositories.getParentOneDevProject() + "/" + oneDevProjectPath;

						logger.log("Importing from '" + giteaRepository + "' to '" + oneDevProjectPath + "'...");

						ProjectService projectService = OneDev.getInstance(ProjectService.class);
						Project project = projectService.setup(SecurityUtils.getSubject(), oneDevProjectPath);

						if (!project.isNew() && !SecurityUtils.canManageProject(project)) {
							throw new UnauthorizedException("Import target already exists. " +
									"You need to have project management privilege over it");
						}

						String apiEndpoint = getApiEndpoint("/repos/" + giteaRepository);
						JsonNode repoNode = JerseyUtils.get(client, apiEndpoint, logger);

						project.setDescription(repoNode.get("description").asText(null));
						project.setIssueManagement(repoNode.get("has_issues").asBoolean());

						if (project.isNew() || project.getDefaultBranch() == null) {
							logger.log("Cloning code...");

							URIBuilder builder = new URIBuilder(repoNode.get("clone_url").asText());
							builder.setUserInfo("git", getAccessToken());

							SecretMasker.push(text -> Strings.CS.replace(text, getAccessToken(), "******"));
							try {
								if (dryRun) {

View on GitHub (pinned to d44925c47c)

Solutions

  1. Choose a different target project path for the import
  2. Ask a project administrator to grant your account Project Management privilege on the existing project
  3. Delete or rename the conflicting existing project if it is obsolete
  4. Re-run the import as a user with sufficient privileges

Example fix

// before
oneDevProjectPath = "team/api-server"  // exists, owned by others
// after
oneDevProjectPath = "team/api-server-imported"  // or get manage permission first
Defensive patterns

Strategy: try-catch

Validate before calling

Project existing = OneDev.getInstance(ProjectManager.class).findProjectByPath(oneDevProjectPath);
if (existing != null && !SecurityUtils.canManageProject(existing))
    throw new ValidationException("No manage privilege over existing project " + oneDevProjectPath);

Try / catch

try { importServer.importProjects(...); } catch (UnauthorizedException e) { logger.error("Import blocked: {}", e.getMessage()); /* choose new path or request permission */ }

Prevention

When it happens

Trigger: Running importProjects when oneDevProjectPath already exists in OneDev and the authenticated user does not have Project Management permission over that existing project.

Common situations: Importing into a project path that another team already created; running the import with an account that has only read access; path collisions after a renamed/preceding import.

Understand the failure class

Background: "You do not have permission" / 403 Forbidden errors: authenticated but not allowed — causes and fixes across open-source libraries — this error's family across 31 libraries.

Related errors


AI-assisted analysis of theonedev/onedev@d44925c47c (2026-09-06). Data as JSON: /api/errors/d1f5247b937d5537. Report an issue: GitHub.