generated from ludeeus/integration_blueprint
-
-
Notifications
You must be signed in to change notification settings - Fork 125
144 lines (120 loc) · 5.85 KB
/
new_device.yaml
File metadata and controls
144 lines (120 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
name: New Device
on:
issues:
types: [opened, edited]
jobs:
create-device-pull-request:
if: ${{ contains(github.event.issue.labels.*.name, 'new-device') }}
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v6
- name: Parse device data
id: device-data
uses: issue-ops/parser@v5
with:
body: ${{ github.event.issue.body }}
issue-form-template: new_device_request.yaml
- name: Install jq
run: sudo apt install jq
- name: Set up python 3.12
uses: actions/setup-python@v6
with:
python-version: "3.12"
- name: Update JSON file
id: update-json
uses: jannekem/run-python-script-action@v1
with:
script: |
import re
import json
# Load the existing JSON library file
with open("library/library.json",'r') as f:
devices_json = json.loads(f.read())
devices = devices_json.get('devices')
# Remove the "battery_quantity" key from the device dictionary if it's 1
new_device = ${{ steps.device-data.outputs.json }}
# Convert battery_quantity field to a numeric
numeric_quantity = int(new_device["battery_quantity"])
del new_device["battery_quantity"]
# Add numeric "battery_quantity" key if it's more than 1
if numeric_quantity > 1:
new_device["battery_quantity"] = numeric_quantity
if new_device.get("model_id", "MISSING").strip() == "":
del new_device["model_id"]
if new_device.get("hw_version", "MISSING").strip() == "":
del new_device["hw_version"]
# Fix common typo in battery type
if new_device.get("battery_type", "").lower() == "rechargable":
new_device["battery_type"] = "Rechargeable"
# Check for duplicates and replace old entry with new one, flagging manual entries
duplicate_found = False
manual_found = False
for i, device in enumerate(devices):
if device["manufacturer"] == new_device["manufacturer"] and device["model"] == new_device["model"] and device.get("model_id", "") == new_device.get("model_id", "") and device.get("hw_version", "") == new_device.get("hw_version", ""):
if device["battery_type"].upper() == "MANUAL":
manual_found = True
break
devices[i] = new_device
duplicate_found = True
break
# If no duplicate found and not currently manual, add the new device to the JSON library file
if not duplicate_found and not manual_found:
devices.append(new_device)
# Save manufacturer and model for later use
set_env("BRANCH", "_".join(re.findall(r"\w+",f"{new_device['manufacturer']} {new_device['model']} {new_device.get('model_id', '')} {new_device.get('hw_version', '')})".lower())))
set_env("MANUFACTURER", new_device['manufacturer'])
set_env("MODEL", new_device['model'])
set_env("MODEL_ID", new_device.get('model_id', ''))
set_env("HW_VERSION", new_device.get('hw_version', ''))
set_env("BQT", f"{numeric_quantity}x {new_device['battery_type']}")
if duplicate_found:
set_env("MODE", "updates")
set_env("TITLE_MODE", "Update")
else:
set_env("MODE", "adds")
set_env("TITLE_MODE", "Add")
set_env("MANUAL", str(manual_found))
if not manual_found:
with open("library/library.json", "w") as f:
f.write(json.dumps(devices_json, indent=4))
- name: Rename Issue
run: |
curl --request PATCH \
--url https://api.github.com/repos/${{github.repository}}/issues/${{github.event.issue.number}} \
--header 'Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
--header 'Content-Type: application/json' \
--data '{
"title": "Device: ${{ env.MANUFACTURER }} - ${{ env.MODEL }}"
}'
- name: Validate JSON
uses: docker://orrosenblatt/validate-json-action:latest
if: "${{ env.MANUAL != 'True' }}"
env:
INPUT_SCHEMA: library/schema.json
INPUT_JSONS: library/library.json
- name: Create pull request
uses: peter-evans/create-pull-request@v8
if: "${{ env.MANUAL != 'True' }}"
with:
commit-message: "${{ env.TITLE_MODE }} device: ${{ env.MODEL }} by ${{ env.MANUFACTURER }}"
title: "${{ env.TITLE_MODE }} device: ${{ env.MANUFACTURER }} - ${{ env.MODEL }}"
body: |
This pull request ${{ env.MODE }} the device information for:
Manufacturer: ${{ env.MANUFACTURER }}
Model: ${{ env.MODEL }}
Model ID: ${{ env.MODEL_ID }}
Hardware: ${{ env.HW_VERSION }}
Battery: ${{ env.BQT }}
It closes issue #${{ github.event.issue.number }}
If Battery Notes is useful to you please
[](https://www.buymeacoffee.com/codechimp)
branch: "device-${{ env.BRANCH }}"
- name: Close Issue
run: gh issue close --comment "$BODY" ${{github.event.issue.number}}
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BODY: >
Thanks for the contribution. We're auto-closing this issue. If it's a new device, a pull request will be created that will be reviewed and merged.
If Battery Notes is useful to you please
[](https://www.buymeacoffee.com/codechimp)