Skip to content

Commit 48e08f8

Browse files
iliakurNouemanKHAL
andauthored
ddev doesn't force writing github uname and token to config file (#22691)
* ddev doesn't force writing github uname and token to config file * add changelog * Pass github creds to the legacy config * Apply suggestions from code review Co-authored-by: NouemanKHAL <noueman.khalikine@datadoghq.com> * fix formatting --------- Co-authored-by: NouemanKHAL <noueman.khalikine@datadoghq.com>
1 parent 2fd1664 commit 48e08f8

File tree

4 files changed

+81
-10
lines changed

4 files changed

+81
-10
lines changed

ddev/changelog.d/22691.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Stop writing the GitHub username and token to the config file. Read them directly from environment variables instead.

ddev/src/ddev/cli/application.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ def initialize_old_cli(self):
9494
self.__config['color'] = not self.console.no_color
9595
self.__config['dd_api_key'] = self.config.orgs.get('default', {}).get('api_key', '')
9696
self.__config['dd_app_key'] = self.config.orgs.get('default', {}).get('app_key', '')
97+
# Ensure GitHub config is available for old CLI commands that need it
98+
self.__config['github'] = {
99+
'user': self.config.github.user,
100+
'token': self.config.github.token,
101+
}
97102
# Make sure that envvar overrides of repo make it into config.
98103
self.__config['repo'] = self.repo.name
99104
# Transfer the -x/--here flag to the old CLI.

ddev/src/ddev/config/model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ def user(self):
487487

488488
self._field_user = user
489489
else:
490-
self._field_user = self.raw_data['user'] = get_github_user()
490+
self._field_user = get_github_user()
491491

492492
return self._field_user
493493

@@ -506,7 +506,7 @@ def token(self):
506506

507507
self._field_token = token
508508
else:
509-
self._field_token = self.raw_data['token'] = get_github_token()
509+
self._field_token = get_github_token()
510510

511511
return self._field_token
512512

ddev/tests/config/test_model.py

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ def test_default():
3535
'log_url': os.getenv('DD_LOGS_CONFIG_LOGS_DD_URL', ''),
3636
},
3737
},
38-
'github': {
39-
'user': get_github_user(),
40-
'token': get_github_token(),
41-
},
38+
'github': {},
4239
'pypi': {
4340
'user': '',
4441
'auth': '',
@@ -714,10 +711,7 @@ def test_default(self):
714711
assert config.github.user == config.github.user == get_github_user()
715712
assert config.github.token == config.github.token == get_github_token()
716713
assert config.raw_data == {
717-
'github': {
718-
'user': get_github_user(),
719-
'token': get_github_token(),
720-
},
714+
'github': {},
721715
}
722716

723717
def test_not_table(self, helpers):
@@ -1409,3 +1403,74 @@ def test_styles_spinner_set_lazy_error(self, helpers):
14091403
),
14101404
):
14111405
_ = config.terminal.styles.spinner
1406+
1407+
1408+
class TestGitHubConfig:
1409+
def test_default_github_config_empty_raw_data(self):
1410+
config = RootConfig({})
1411+
config.parse_fields()
1412+
1413+
# GitHub config should be empty in raw_data when not explicitly set
1414+
assert config.raw_data['github'] == {}
1415+
1416+
# But properties should still work via environment variables
1417+
assert config.github.user == get_github_user()
1418+
assert config.github.token == get_github_token()
1419+
1420+
# After accessing properties, raw_data should still be empty
1421+
assert config.raw_data['github'] == {}
1422+
1423+
def test_explicit_github_config_in_raw_data(self):
1424+
config = RootConfig({'github': {'user': 'explicit_user', 'token': 'explicit_token'}})
1425+
1426+
# When explicitly set, values should be in raw_data
1427+
assert config.raw_data['github']['user'] == 'explicit_user'
1428+
assert config.raw_data['github']['token'] == 'explicit_token'
1429+
1430+
# Properties should return explicit values
1431+
assert config.github.user == 'explicit_user'
1432+
assert config.github.token == 'explicit_token'
1433+
1434+
def test_partial_github_config_explicit_user_only(self):
1435+
config = RootConfig({'github': {'user': 'explicit_user'}})
1436+
1437+
# Only explicitly set field should be in raw_data
1438+
assert config.raw_data['github']['user'] == 'explicit_user'
1439+
assert 'token' not in config.raw_data['github']
1440+
1441+
# Properties should work - explicit user, env var token
1442+
assert config.github.user == 'explicit_user'
1443+
assert config.github.token == get_github_token()
1444+
1445+
# raw_data should still only have explicit field
1446+
assert config.raw_data['github']['user'] == 'explicit_user'
1447+
assert 'token' not in config.raw_data['github']
1448+
1449+
def test_partial_github_config_explicit_token_only(self):
1450+
config = RootConfig({'github': {'token': 'explicit_token'}})
1451+
1452+
# Only explicitly set field should be in raw_data
1453+
assert 'user' not in config.raw_data['github']
1454+
assert config.raw_data['github']['token'] == 'explicit_token'
1455+
1456+
# Properties should work - env var user, explicit token
1457+
assert config.github.user == get_github_user()
1458+
assert config.github.token == 'explicit_token'
1459+
1460+
# raw_data should still only have explicit field
1461+
assert 'user' not in config.raw_data['github']
1462+
assert config.raw_data['github']['token'] == 'explicit_token'
1463+
1464+
def test_github_config_with_environment_variables(self, monkeypatch):
1465+
# Mock environment variables
1466+
monkeypatch.setenv('DD_GITHUB_USER', 'env_user')
1467+
monkeypatch.setenv('DD_GITHUB_TOKEN', 'env_token')
1468+
1469+
config = RootConfig({})
1470+
1471+
# Properties should return environment variable values
1472+
assert config.github.user == 'env_user'
1473+
assert config.github.token == 'env_token'
1474+
1475+
# raw_data should still be empty
1476+
assert config.raw_data['github'] == {}

0 commit comments

Comments
 (0)