diff --git a/CHANGELOG.md b/CHANGELOG.md index 09fcea2..fb09f72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to this project are documented here. The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and the project uses [Semantic Versioning](https://semver.org/). +## [Unreleased] + +### Added + +- **Reconfigure** can now replace the access token. The new token is checked against the enabled modules and must belong to the same GitHub account; leave the field empty to keep the current one. + ## [1.4.0] - 2026-11-23 ### Added diff --git a/README.md b/README.md index 645b9a9..6514ede 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,8 @@ You need **one** token. It serves every module you enable. A classic token with The setup checks the token against every module you select and tells you what is missing. The full endpoint list is in the [permission matrix](docs/permission-matrix.md). +To replace the token, open the integration, choose **Reconfigure** and enter the new token. It must belong to the same GitHub account. Leave the field empty to keep the current token. + > [!IMPORTANT] > Fine-grained tokens belong to a single owner and cannot read Packages. Use a classic token if you need more. diff --git a/custom_components/github_pulse/config_flow.py b/custom_components/github_pulse/config_flow.py index ab853a4..f819d3f 100644 --- a/custom_components/github_pulse/config_flow.py +++ b/custom_components/github_pulse/config_flow.py @@ -93,7 +93,10 @@ {probatio.Required(CONF_ACCESS_TOKEN): _TOKEN_SELECTOR} ) STEP_RECONFIGURE_SCHEMA = probatio.Schema( - {probatio.Required(CONF_MODULES): _MODULES_SELECTOR} + { + probatio.Required(CONF_MODULES): _MODULES_SELECTOR, + probatio.Optional(CONF_ACCESS_TOKEN): _TOKEN_SELECTOR, + } ) @@ -330,7 +333,7 @@ async def async_step_reauth_confirm( async def async_step_reconfigure( self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: - """Change which modules are enabled, keeping the stored token.""" + """Change which modules are enabled and, optionally, replace the token.""" errors: dict[str, str] = {} placeholders: dict[str, str] = {} entry = self._get_reconfigure_entry() @@ -340,24 +343,30 @@ async def async_step_reconfigure( if not modules: errors["base"] = "no_modules" else: + new_token = user_input.get(CONF_ACCESS_TOKEN, "").strip() + token = new_token or entry.data[CONF_ACCESS_TOKEN] verified = await _verify( self.hass, - entry.data[CONF_ACCESS_TOKEN], + token, modules, expected_login=entry.data[CONF_OWNER], ) errors = verified.errors placeholders |= verified.placeholders if verified.info: + updates: dict[str, Any] = {CONF_MODULES: modules} + if new_token: + updates[CONF_ACCESS_TOKEN] = new_token + updates[CONF_TOKEN_TYPE] = verified.info.token_type.value return self.async_update_reload_and_abort( - entry, data_updates={CONF_MODULES: modules} + entry, data_updates=updates ) return self.async_show_form( step_id="reconfigure", data_schema=self.add_suggested_values_to_schema( STEP_RECONFIGURE_SCHEMA, - user_input or {CONF_MODULES: entry.data[CONF_MODULES]}, + {CONF_MODULES: (user_input or entry.data)[CONF_MODULES]}, ), errors=errors, description_placeholders=placeholders, diff --git a/custom_components/github_pulse/translations/de.json b/custom_components/github_pulse/translations/de.json index 2b4d502..4806fa8 100644 --- a/custom_components/github_pulse/translations/de.json +++ b/custom_components/github_pulse/translations/de.json @@ -20,10 +20,11 @@ } }, "reconfigure": { - "title": "Erfasste Daten ändern", - "description": "Das gespeicherte Token wird für jedes gewählte Modul geprüft.", + "title": "Erfassung oder Token ändern", + "description": "Das Token wird für jedes gewählte Modul geprüft. Lass das Token leer, um das aktuelle zu behalten; ein neues Token muss zum selben GitHub-Konto gehören.", "data": { - "modules": "Was möchtest du erfassen?" + "modules": "Was möchtest du erfassen?", + "access_token": "Neues Zugriffstoken (optional)" } } }, @@ -38,7 +39,7 @@ "abort": { "already_configured": "Dieses GitHub-Konto ist bereits eingerichtet.", "reauth_successful": "Das neue Token wurde gespeichert.", - "reconfigure_successful": "Die erfassten Module wurden aktualisiert." + "reconfigure_successful": "Die Einstellungen wurden aktualisiert." } }, "options": { diff --git a/custom_components/github_pulse/translations/en.json b/custom_components/github_pulse/translations/en.json index e10d3fe..7e38e6c 100644 --- a/custom_components/github_pulse/translations/en.json +++ b/custom_components/github_pulse/translations/en.json @@ -20,10 +20,11 @@ } }, "reconfigure": { - "title": "Change what is tracked", - "description": "The stored token is checked against every module you select.", + "title": "Change tracking or token", + "description": "The stored token is checked against every module you select. Leave the token empty to keep the current one; a new token must belong to the same GitHub account.", "data": { - "modules": "What do you want to track?" + "modules": "What do you want to track?", + "access_token": "New access token (optional)" } } }, @@ -38,7 +39,7 @@ "abort": { "already_configured": "This GitHub account is already configured.", "reauth_successful": "The new token was saved.", - "reconfigure_successful": "The tracked modules were updated." + "reconfigure_successful": "The settings were updated." } }, "options": { diff --git a/tests/test_config_flow_lifecycle.py b/tests/test_config_flow_lifecycle.py index d6ecbed..9e309fc 100644 --- a/tests/test_config_flow_lifecycle.py +++ b/tests/test_config_flow_lifecycle.py @@ -115,6 +115,72 @@ async def test_reconfigure_changes_the_modules( assert entry.data[CONF_ACCESS_TOKEN] == "ghp_old" +async def test_reconfigure_replaces_the_token( + hass: HomeAssistant, patch_session: Callable[[FakeSession], None] +) -> None: + patch_session(FakeSession(USER_OK, GRAPHQL_OK)) + entry = _entry(hass) + + result = await entry.start_reconfigure_flow(hass) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {CONF_MODULES: ["profile"], CONF_ACCESS_TOKEN: " ghp_new "}, + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reconfigure_successful" + assert entry.data[CONF_ACCESS_TOKEN] == "ghp_new" + assert entry.data[CONF_TOKEN_TYPE] == "classic" + assert entry.data[CONF_OWNER] == "octocat" + + +async def test_reconfigure_keeps_the_token_when_the_field_is_blank( + hass: HomeAssistant, patch_session: Callable[[FakeSession], None] +) -> None: + patch_session(FakeSession(USER_OK, GRAPHQL_OK)) + entry = _entry(hass) + + result = await entry.start_reconfigure_flow(hass) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {CONF_MODULES: ["profile"], CONF_ACCESS_TOKEN: " "} + ) + + assert result["reason"] == "reconfigure_successful" + assert entry.data[CONF_ACCESS_TOKEN] == "ghp_old" + + +async def test_reconfigure_rejects_a_token_of_another_account( + hass: HomeAssistant, patch_session: Callable[[FakeSession], None] +) -> None: + patch_session(FakeSession(OTHER_USER)) + entry = _entry(hass) + + result = await entry.start_reconfigure_flow(hass) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {CONF_MODULES: ["profile"], CONF_ACCESS_TOKEN: "ghp_new"}, + ) + + assert result["errors"] == {"base": "wrong_account"} + assert entry.data[CONF_ACCESS_TOKEN] == "ghp_old" + + +async def test_reconfigure_rejects_an_invalid_new_token( + hass: HomeAssistant, patch_session: Callable[[FakeSession], None] +) -> None: + patch_session(FakeSession(BAD_CREDENTIALS)) + entry = _entry(hass) + + result = await entry.start_reconfigure_flow(hass) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {CONF_MODULES: ["profile"], CONF_ACCESS_TOKEN: "ghp_bad"}, + ) + + assert result["errors"] == {"base": "invalid_auth"} + assert entry.data[CONF_ACCESS_TOKEN] == "ghp_old" + + async def test_reconfigure_rejects_a_module_the_token_cannot_serve( hass: HomeAssistant, patch_session: Callable[[FakeSession], None] ) -> None: