From 2d52aca9eb58a2003d9b4252c8e4700a2d81584e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Sun, 20 Feb 2022 13:00:01 +0100 Subject: [PATCH] Add Latest Tag sensor to GitHub integration (#66932) --- homeassistant/components/github/coordinator.py | 12 ++++++++++++ homeassistant/components/github/sensor.py | 9 +++++++++ tests/components/github/fixtures/graphql.json | 10 ++++++++++ 3 files changed, 31 insertions(+) diff --git a/homeassistant/components/github/coordinator.py b/homeassistant/components/github/coordinator.py index 9769c944f16..ae3c9b5dc87 100644 --- a/homeassistant/components/github/coordinator.py +++ b/homeassistant/components/github/coordinator.py @@ -68,6 +68,18 @@ query ($owner: String!, $repository: String!) { url tag: tagName } + refs( + first: 1 + refPrefix: "refs/tags/" + orderBy: {field: TAG_COMMIT_DATE, direction: DESC} + ) { + tags: nodes { + name + target { + url: commitUrl + } + } + } } } """ diff --git a/homeassistant/components/github/sensor.py b/homeassistant/components/github/sensor.py index 08e8a49af1f..75284b2ccd3 100644 --- a/homeassistant/components/github/sensor.py +++ b/homeassistant/components/github/sensor.py @@ -126,6 +126,15 @@ SENSOR_DESCRIPTIONS: tuple[GitHubSensorEntityDescription, ...] = ( "number": data["pull_request"]["pull_requests"][0]["number"], }, ), + GitHubSensorEntityDescription( + key="latest_tag", + name="Latest Tag", + avabl_fn=lambda data: data["refs"]["tags"], + value_fn=lambda data: data["refs"]["tags"][0]["name"][:255], + attr_fn=lambda data: { + "url": data["refs"]["tags"][0]["target"]["url"], + }, + ), ) diff --git a/tests/components/github/fixtures/graphql.json b/tests/components/github/fixtures/graphql.json index d8fe86f6c95..8fe1b17022a 100644 --- a/tests/components/github/fixtures/graphql.json +++ b/tests/components/github/fixtures/graphql.json @@ -43,6 +43,16 @@ "name": "v1.0.0", "url": "https://github.com/octocat/Hello-World/releases/v1.0.0", "tag": "v1.0.0" + }, + "refs": { + "tags": [ + { + "name": "v1.0.0", + "target": { + "url": "https://github.com/octocat/Hello-World/commit/6dcb09b5b57875f334f61aebed695e2e4193db5e" + } + } + ] } } }