Repository: WeblateOrg/weblate

Issue:
docker: Enable WEBLATE_GITLAB_CREDENTIALS environment variable
### Describe the problem

Right now it seems I can use gitlab_username and gitlab_token variables. But when I try to use gitlab_credentials:

>       WEBLATE_GITLAB_CREDENTIALS: "git.duniter.org": {username: weblate,token: XXXXXXXXXXXXXXX}

I get this error:

> ERROR: yaml.scanner.ScannerError: mapping values are not allowed here
>   in "./docker-compose.override.yml", line 17, column 52


### Describe the solution you'd like

Add weblate_gitlab_credentials support

### Describe alternatives you've considered

_No response_

### Screenshots

_No response_

### Additional context

_No response_


Discussion:
The error you get is from the docker-compose YAML parser, to have a valid YAML syntax it should be:

```yaml
  WEBLATE_GITLAB_CREDENTIALS: "git.duniter.org: username: XXXXXXXX,token: XXXXXXXXXXXXXXX"
```

Still, this setting is currently not parsed from the environment, see https://docs.weblate.org/en/latest/admin/install/docker.html#docker-environment-variables for list of supported variables (you can use WEBLATE_GITLAB_USERNAME and WEBLATE_GITLAB_TOKEN).

PS: Posting question to multiple places is not a good practice. This duplicates https://github.com/WeblateOrg/weblate/discussions/8128
This issue seems to be a good fit for newbie contributors. You are welcome to contribute to Weblate! Don't hesitate to ask any questions you would have while implementing this.

You can learn about how to get started in our [contributors documentation](https://docs.weblate.org/en/latest/contributing/index.html).
I would like to work on this. Can you please assign this to me?
@jatinmark Yes, you're welcome.
You can now configure WEBLATE_GITLAB_CREDENTIALS for a single host by setting WEBLATE_GITLAB_HOST, WEBLATE_GITLAB_USERNAME and WEBLATE_GITLAB_TOKEN, see https://github.com/WeblateOrg/weblate/commit/b299b88af9dd20c6c8bef68ef1d5125def0de471. Multiple host configuration is still not supported via environment variables.
Has Jatin closed this issue or Is it still open? I can work on this
Yes , you can work on this issue .
Hello,

I would like to enable the WEBLATE_GITLAB_CREDENTIALS environment variable in the docker-compose.yml file. Currently, the file is not able to parse the value of this variable.

To fix the issue, I propose modifying the syntax of the WEBLATE_GITLAB_CREDENTIALS variable as follows:

environment:

      DJANGO_SETTINGS_MODULE: weblate.settings_docker

      WEBLATE_SITE_DOMAIN: ${WEBLATE_HOST}

      # Enable the WEBLATE_GITLAB_CREDENTIALS variable here:

      WEBLATE_GITLAB_CREDENTIALS: '"git.duniter.org": {username: $USERNAME,token: $TOKEN}'
Just to clarify it: There is currently no support for `WEBLATE_GITLAB_CREDENTIALS` in Docker. This issue is about adding it.

If you are configuring a single Gitlab instance, use WEBLATE_GITLAB_HOST, WEBLATE_GITLAB_USERNAME and WEBLATE_GITLAB_TOKEN.

Resolving patch:
diff --git a/weblate/utils/environment.py b/weblate/utils/environment.py
--- a/weblate/utils/environment.py
+++ b/weblate/utils/environment.py
@@ -4,6 +4,7 @@
 
 from __future__ import annotations
 
+import ast
 import os
 
 
@@ -90,6 +91,8 @@ def get_env_credentials(
     name: str,
 ) -> dict[str, dict[str, str]]:
     """Parses VCS integration credentials."""
+    if credentials := get_env_str(f"WEBLATE_{name}_CREDENTIALS"):
+        return ast.literal_eval(credentials)
     username = os.environ.get(f"WEBLATE_{name}_USERNAME")
     token = os.environ.get(f"WEBLATE_{name}_TOKEN")
     host = os.environ.get(f"WEBLATE_{name}_HOST")


Test patch:
diff --git a/weblate/utils/tests/test_environment.py b/weblate/utils/tests/test_environment.py
--- a/weblate/utils/tests/test_environment.py
+++ b/weblate/utils/tests/test_environment.py
@@ -83,6 +83,20 @@ def test_get_env_credentials(self):
         del os.environ["WEBLATE_TEST_TOKEN"]
         del os.environ["WEBLATE_TEST_HOST"]
 
+        os.environ["WEBLATE_TEST_CREDENTIALS"] = "{invalid-syntax}"
+        with self.assertRaises(ValueError):
+            get_env_credentials("TEST")
+
+        os.environ[
+            "WEBLATE_TEST_CREDENTIALS"
+        ] = '{"host": {"username": "user", "token": "token"}}'
+        self.assertEqual(
+            get_env_credentials("TEST"),
+            {"host": {"username": "user", "token": "token"}},
+        )
+
+        del os.environ["WEBLATE_TEST_CREDENTIALS"]
+
     def test_get_env_ratelimit(self):
         os.environ["RATELIMIT_ANON"] = "1/hour"
         self.assertEqual(