fix(license): normalize license identifiers and default to Unlicense
Gitea's repository template engine resolves license templates by exact case-sensitive filename against its internal asset registry (e.g., 'Unlicense', 'MIT', 'Apache-2.0'). When passed 'UNLICENSE', Gitea returned an unhandled HTTP 500 error: 'initRepository: prepareRepoCommit: getLicense[UNLICENSE]: file does not exist'. - Add GITEA_LICENSES mapping and normalize_license() helper - Change default license identifier from 'UNLICENSE' to 'Unlicense' - Normalize license argument in GiteaClient.create_repo - Add unit tests for license casing and client payloads - Update README CLI documentation and examples
This commit is contained in:
1 parent
aff2098f7f
commit
c950ff2e73
3 files changed
+99
-8
No files matched your search
@@ -40,7 +40,7 @@ Options:
|
||||
--migrate [URL] Migrate an existing GitHub repo to Gitea with automated push mirroring (URL or auto-detect from local origin)
|
||||
-p, --private Make repository private (default: public)
|
||||
-d, --description Optional repository description string
|
||||
-l, --license License template identifier (default: UNLICENSE, pass "" to disable)
|
||||
-l, --license License template identifier (default: Unlicense, pass "" to disable)
|
||||
-t, --template Optional Gitea template repository ('[owner/]template-name')
|
||||
-M, --no-mirror Disable automatic offsite push mirroring to GitHub
|
||||
--no-clone Skip cloning the repository locally after creation
|
||||
@@ -50,7 +50,7 @@ Options:
|
||||
### Examples
|
||||
|
||||
```bash
|
||||
# 1. Standard greenfield creation (Public Gitea + GitHub push mirror + UNLICENSE + auto-clone)
|
||||
# 1. Standard greenfield creation (Public Gitea + GitHub push mirror + Unlicense + auto-clone)
|
||||
new-repo service-api
|
||||
|
||||
# 2. Provision under a Gitea organization (via shorthand or -o/--org flag; auto-creates org if missing)
|
||||
|
||||
+56
-5
@@ -216,6 +216,56 @@ class SecretProvider:
|
||||
)
|
||||
|
||||
|
||||
# ==============================================================================
|
||||
# License Registry & Normalization
|
||||
# ==============================================================================
|
||||
|
||||
GITEA_LICENSES: dict[str, str] = {
|
||||
"0bsd": "0BSD",
|
||||
"agpl-3.0": "AGPL-3.0",
|
||||
"apache-2.0": "Apache-2.0",
|
||||
"bsd-2-clause": "BSD-2-Clause",
|
||||
"bsd-3-clause": "BSD-3-Clause",
|
||||
"bsd-3-clause-clear": "BSD-3-Clause-Clear",
|
||||
"bsl-1.0": "BSL-1.0",
|
||||
"cc-by-4.0": "CC-BY-4.0",
|
||||
"cc-by-sa-4.0": "CC-BY-SA-4.0",
|
||||
"cc0-1.0": "CC0-1.0",
|
||||
"epl-1.0": "EPL-1.0",
|
||||
"epl-2.0": "EPL-2.0",
|
||||
"eupl-1.2": "EUPL-1.2",
|
||||
"gpl-2.0": "GPL-2.0",
|
||||
"gpl-3.0": "GPL-3.0",
|
||||
"isc": "ISC",
|
||||
"lgpl-2.1": "LGPL-2.1",
|
||||
"lgpl-3.0": "LGPL-3.0",
|
||||
"mit": "MIT",
|
||||
"mit-0": "MIT-0",
|
||||
"mpl-2.0": "MPL-2.0",
|
||||
"mulanpsl-2.0": "MulanPSL-2.0",
|
||||
"ofl-1.1": "OFL-1.1",
|
||||
"osl-3.0": "OSL-3.0",
|
||||
"upl-1.0": "UPL-1.0",
|
||||
"unlicense": "Unlicense",
|
||||
"wtfpl": "WTFPL",
|
||||
"zlib": "Zlib",
|
||||
}
|
||||
|
||||
|
||||
def normalize_license(license_name: str | None) -> str:
|
||||
"""Normalize license identifier to match Gitea's case-sensitive template registry.
|
||||
|
||||
Gitea resolves license templates by exact filename in its internal assets.
|
||||
For example, 'UNLICENSE' fails with a 500 error while 'Unlicense' succeeds.
|
||||
"""
|
||||
if not license_name:
|
||||
return ""
|
||||
clean = license_name.strip()
|
||||
if not clean:
|
||||
return ""
|
||||
return GITEA_LICENSES.get(clean.lower(), clean)
|
||||
|
||||
|
||||
# ==============================================================================
|
||||
# API Clients
|
||||
# ==============================================================================
|
||||
@@ -328,7 +378,7 @@ class GiteaClient:
|
||||
private: bool = False,
|
||||
description: str = "",
|
||||
auto_init: bool = True,
|
||||
license_name: str = "UNLICENSE",
|
||||
license_name: str = "Unlicense",
|
||||
org: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Create a new repository under the authenticated user or specified organization."""
|
||||
@@ -339,8 +389,9 @@ class GiteaClient:
|
||||
"auto_init": auto_init,
|
||||
"default_branch": "main",
|
||||
}
|
||||
if license_name:
|
||||
payload["license"] = license_name
|
||||
normalized_license = normalize_license(license_name)
|
||||
if normalized_license:
|
||||
payload["license"] = normalized_license
|
||||
|
||||
endpoint = f"/api/v1/orgs/{org}/repos" if org else "/api/v1/user/repos"
|
||||
return self._request("POST", endpoint, payload)
|
||||
@@ -821,8 +872,8 @@ def parse_args() -> argparse.Namespace:
|
||||
"-l",
|
||||
"--license",
|
||||
type=str,
|
||||
default="UNLICENSE",
|
||||
help="License identifier template (default: UNLICENSE, pass empty string to disable)",
|
||||
default="Unlicense",
|
||||
help="License identifier template (default: Unlicense, pass empty string to disable)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-t",
|
||||
|
||||
+41
-1
@@ -14,6 +14,7 @@ from new_repo import (
|
||||
GitHubClient,
|
||||
SecretProvider,
|
||||
determine_target,
|
||||
normalize_license,
|
||||
parse_args,
|
||||
parse_github_target,
|
||||
reconfigure_local_remotes,
|
||||
@@ -33,7 +34,7 @@ class TestArgParsing(unittest.TestCase):
|
||||
self.assertIsNone(args.org)
|
||||
self.assertIsNone(args.github_org)
|
||||
self.assertFalse(args.private)
|
||||
self.assertEqual(args.license, "UNLICENSE")
|
||||
self.assertEqual(args.license, "Unlicense")
|
||||
self.assertIsNone(args.template)
|
||||
self.assertFalse(args.no_mirror)
|
||||
self.assertFalse(args.no_clone)
|
||||
@@ -90,6 +91,45 @@ class TestArgParsing(unittest.TestCase):
|
||||
self.assertIsNone(args.migrate)
|
||||
|
||||
|
||||
class TestLicenseNormalization(unittest.TestCase):
|
||||
"""Test license identifier normalization against Gitea's registry."""
|
||||
|
||||
def test_unlicense_casing_variants(self) -> None:
|
||||
self.assertEqual(normalize_license("UNLICENSE"), "Unlicense")
|
||||
self.assertEqual(normalize_license("unlicense"), "Unlicense")
|
||||
self.assertEqual(normalize_license("Unlicense"), "Unlicense")
|
||||
|
||||
def test_common_licenses_case_insensitivity(self) -> None:
|
||||
self.assertEqual(normalize_license("mit"), "MIT")
|
||||
self.assertEqual(normalize_license("MIT"), "MIT")
|
||||
self.assertEqual(normalize_license("apache-2.0"), "Apache-2.0")
|
||||
self.assertEqual(normalize_license("APACHE-2.0"), "Apache-2.0")
|
||||
self.assertEqual(normalize_license("gpl-3.0"), "GPL-3.0")
|
||||
self.assertEqual(normalize_license("bsd-3-clause"), "BSD-3-Clause")
|
||||
|
||||
def test_empty_and_whitespace_licenses(self) -> None:
|
||||
self.assertEqual(normalize_license(""), "")
|
||||
self.assertEqual(normalize_license(" "), "")
|
||||
self.assertEqual(normalize_license(None), "")
|
||||
|
||||
def test_unrecognized_license_passthrough(self) -> None:
|
||||
self.assertEqual(normalize_license("Custom-Proprietary"), "Custom-Proprietary")
|
||||
|
||||
def test_gitea_client_create_repo_normalizes_license(self) -> None:
|
||||
client = GiteaClient("https://gitea.local", "kiskaadee", "secret-token")
|
||||
with patch.object(client, "_request", return_value={}) as mock_req:
|
||||
client.create_repo("my-repo", license_name="UNLICENSE")
|
||||
mock_req.assert_called_once()
|
||||
payload = mock_req.call_args[0][2]
|
||||
self.assertEqual(payload["license"], "Unlicense")
|
||||
|
||||
with patch.object(client, "_request", return_value={}) as mock_req:
|
||||
client.create_repo("my-repo-no-license", license_name="")
|
||||
mock_req.assert_called_once()
|
||||
payload = mock_req.call_args[0][2]
|
||||
self.assertNotIn("license", payload)
|
||||
|
||||
|
||||
class TestOrganizationAndTargetResolution(unittest.TestCase):
|
||||
"""Test resolution of repository names, org/repo shorthands, and flat local paths."""
|
||||
|
||||
|
||||
Reference in new issue
Block a user