Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support DOCUMATIC_API_KEY environment variable #21

Merged
merged 5 commits into from Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 0 additions & 12 deletions CHANGELOG.md
Expand Up @@ -24,7 +24,6 @@ Generated by **Documatic.**
* Api verification when running autolog
* Python-dotenv to setup install requires


### Changed

* Changelog even if server inactive
Expand All @@ -33,30 +32,21 @@ Generated by **Documatic.**
* Package metadata
* Issue templates


### Fixed

* Doculog error when tags present
* Changelog appending new versions to the bottom
* Error when running on non-git project




## 0.1.2 - 2021-12-28

### Fixed

* New versions are added to changelog without "v" prefix
* New versions are added to changelog in newest-first order




## 0.1.1 - 2021-12-28



## 0.1.0 - 2021-12-28

### Added
Expand All @@ -70,5 +60,3 @@ Generated by **Documatic.**
* Pyproject.toml for autolog and isort
* Func to validate api key
* Api verification when running autolog


2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -73,7 +73,7 @@ in your project root directory
with the following fields:

```
DOCULOG_API_KEY = <your-api-key>
DOCUMATIC_API_KEY = <your-api-key>
```

**IMPORTANT: DO NOT ADD `.env` TO VERSION CONTROL.
Expand Down
8 changes: 5 additions & 3 deletions doculog/changelog.py
Expand Up @@ -257,7 +257,7 @@ def __str__(self) -> str:

for section in self._sections.values():
if section.has_content():
content += str(section) + "\n\n"
content += str(section).strip() + "\n\n"

return content

Expand Down Expand Up @@ -394,13 +394,15 @@ def __str__(self) -> str:
)

if "Unreleased" in self._releases:
content += str(self._releases["Unreleased"])
content += str(self._releases["Unreleased"]).strip()

for tag_name, _ in self._tags:
vless_tag_name = tag_name.lstrip("v").strip()

if vless_tag_name in self._releases:
content += "\n\n" + str(self._releases[vless_tag_name])
content += "\n\n" + str(self._releases[vless_tag_name]).strip()

content += "\n" # newline at end of file

return content

Expand Down
13 changes: 11 additions & 2 deletions doculog/config.py
Expand Up @@ -26,6 +26,10 @@ def configure(project_root: Path) -> Dict:

def configure_api(local):
if (not local) and (not validate_key()):
if "DOCUMATIC_API_KEY" in os.environ:
print("DOCUMATIC_API_KEY invalid. Advanced features disabled")
del os.environ["DOCUMATIC_API_KEY"]

if "DOCULOG_API_KEY" in os.environ:
print("DOCULOG_API_KEY invalid. Advanced features disabled.")
del os.environ["DOCULOG_API_KEY"]
Expand Down Expand Up @@ -68,9 +72,14 @@ def parse_config(project_root: Path) -> Dict:
except (NoOptionError, ValueError):
local = False

if "DOCULOG_API_KEY" not in os.environ:
if "DOCUMATIC_API_KEY" not in os.environ and "DOCULOG_API_KEY" not in os.environ:
print(
"Environment variable DOCUMATIC_API_KEY not set. Advanced features disabled."
)

if "DOCULOG_API_KEY" in os.environ:
print(
"Environment variable DOCULOG_API_KEY not set. Advanced features disabled."
"DOCULOG_API_KEY is deprecated and will be removed in v0.2.0. Use DOCUMATIC_API_KEY environment variable to set your api key instead."
)

os.environ["DOCULOG_PROJECT_NAME"] = project_name
Expand Down
4 changes: 2 additions & 2 deletions doculog/requests.py
Expand Up @@ -46,7 +46,7 @@ def post(

hashed_project = hashlib.sha224(project_name.encode("utf-8")).hexdigest()

api_key = os.getenv("DOCULOG_API_KEY")
api_key = os.getenv("DOCUMATIC_API_KEY") or os.getenv("DOCULOG_API_KEY")

if not api_key:
return
Expand Down Expand Up @@ -94,7 +94,7 @@ def validate_key() -> bool:

hashed_project = hashlib.sha224(project_name.encode("utf-8")).hexdigest()

api_key = os.getenv("DOCULOG_API_KEY")
api_key = os.getenv("DOCUMATIC_API_KEY") or os.getenv("DOCULOG_API_KEY")

if not api_key:
return False
Expand Down
34 changes: 19 additions & 15 deletions tests/test_config.py
Expand Up @@ -176,44 +176,48 @@ def test_local_is_set_to_false_if_in_config_as_non_bool_value(self, tmp_path):

class TestValidateAPI:
@pytest.mark.parametrize("api_key", ("12345", "a2b3CDE199", "test"))
@pytest.mark.parametrize("key_name", ("DOCULOG", "DOCUMATIC"))
def test_sets_api_key_from_env_as_env_variable_if_valid_key_and_not_running_locally(
self, api_key, mocker
self, api_key, mocker, key_name
):
mocker.patch("doculog.config.validate_key", return_value=True)

os.environ["DOCULOG_API_KEY"] = api_key
os.environ[f"{key_name}_API_KEY"] = api_key

configure_api(False)
assert os.environ["DOCULOG_API_KEY"] == api_key
del os.environ["DOCULOG_API_KEY"]
assert os.environ[f"{key_name}_API_KEY"] == api_key
del os.environ[f"{key_name}_API_KEY"]

@pytest.mark.parametrize("api_key", ("12345", "a2b3CDE199", "test"))
@pytest.mark.parametrize("key_name", ("DOCULOG", "DOCUMATIC"))
def test_does_not_set_api_key_if_invalid_key_and_not_running_locally(
self, api_key, mocker
self, api_key, mocker, key_name
):
mocker.patch("doculog.config.validate_key", return_value=False)

os.environ["DOCULOG_API_KEY"] = api_key
os.environ[f"{key_name}_API_KEY"] = api_key

configure_api(False)
assert os.getenv("DOCULOG_API_KEY") is None
assert os.getenv(f"{key_name}_API_KEY") is None

@pytest.mark.parametrize("api_key", ("12345", "a2b3CDE199", "test"))
@pytest.mark.parametrize("valid_key", (True, False))
@pytest.mark.parametrize("key_name", ("DOCULOG", "DOCUMATIC"))
def test_does_sets_api_key_if_present_and_running_locally(
self, api_key, valid_key, mocker
self, api_key, valid_key, mocker, key_name
):
mocker.patch("doculog.config.validate_key", return_value=valid_key)

os.environ["DOCULOG_API_KEY"] = api_key
os.environ[f"{key_name}_API_KEY"] = api_key

configure_api(True)
assert os.getenv("DOCULOG_API_KEY") == api_key
del os.environ["DOCULOG_API_KEY"]
assert os.getenv(f"{key_name}_API_KEY") == api_key
del os.environ[f"{key_name}_API_KEY"]

def test_does_not_error_when_validating_key_if_no_key_present(self):
if "DOCULOG_API_KEY" in os.environ:
del os.environ["DOCULOG_API_KEY"]
@pytest.mark.parametrize("key_name", ("DOCULOG", "DOCUMATIC"))
def test_does_not_error_when_validating_key_if_no_key_present(self, key_name):
if f"{key_name}_API_KEY" in os.environ:
del os.environ[f"{key_name}_API_KEY"]

configure_api(False)
assert os.getenv("DOCULOG_API_KEY") is None
assert os.getenv(f"{key_name}_API_KEY") is None