Skip to content

Commit fcf52b0

Browse files
authored
Merge pull request #352 from per1234/category-order-sync
Add infrastructure for checking sync of category order metadata
2 parents 7aa859a + d0c0736 commit fcf52b0

File tree

11 files changed

+660
-2
lines changed

11 files changed

+660
-2
lines changed

.flake8

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-python/.flake8
2+
# See: https://flake8.pycqa.org/en/latest/user/configuration.html
3+
# The code style defined in this file is the official standardized style to be used in all Arduino tooling projects and
4+
# should not be modified.
5+
6+
[flake8]
7+
doctests = True
8+
# W503 and W504 are mutually exclusive. PEP 8 recommends line break before.
9+
ignore = W503
10+
max-complexity = 10
11+
max-line-length = 120
12+
select = E,W,F,C,N
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
name: Check Category Order Metadata
2+
3+
# See: https://docs.github.com/actions/using-workflows/events-that-trigger-workflows
4+
on:
5+
create:
6+
push:
7+
paths:
8+
- ".github/workflows/check-order-metadata-task.ya?ml"
9+
- "poetry.lock"
10+
- "pyproject.toml"
11+
- "Taskfile.ya?ml"
12+
- "assets/generate-category-order-metadata/*"
13+
- "content/categories/order.md"
14+
pull_request:
15+
paths:
16+
- ".github/workflows/check-order-metadata-task.ya?ml"
17+
- "poetry.lock"
18+
- "pyproject.toml"
19+
- "Taskfile.ya?ml"
20+
- "assets/generate-category-order-metadata/*"
21+
- "content/categories/order.md"
22+
schedule:
23+
# Run periodically to catch breakage caused by external changes.
24+
- cron: "0 3 * * FRI"
25+
workflow_dispatch:
26+
repository_dispatch:
27+
28+
jobs:
29+
run-determination:
30+
runs-on: ubuntu-latest
31+
outputs:
32+
result: ${{ steps.determination.outputs.result }}
33+
steps:
34+
- name: Determine if the rest of the workflow should run
35+
id: determination
36+
run: |
37+
if [[ "${{ secrets.ARDUINO_FORUM_API_KEY }}" == "" ]]; then
38+
# Required API key secret is not defined.
39+
RESULT="false"
40+
else
41+
RESULT="true"
42+
fi
43+
44+
echo "result=$RESULT" >> $GITHUB_OUTPUT
45+
46+
check-order-metadata:
47+
needs: run-determination
48+
if: needs.run-determination.outputs.result == 'true'
49+
runs-on: ubuntu-latest
50+
env:
51+
ARTIFACT_NAME: category-order-metadata
52+
CATEGORY_ORDER_METADATA_FILE_PATH: content/categories/order.md
53+
steps:
54+
- name: Checkout repository
55+
uses: actions/checkout@v3
56+
57+
- name: Install Poetry
58+
run: |
59+
pipx install \
60+
--python "$(which python)" \
61+
poetry
62+
63+
- name: Install Task
64+
uses: arduino/setup-task@v1
65+
with:
66+
repo-token: ${{ secrets.GITHUB_TOKEN }}
67+
version: 3.x
68+
69+
- name: Generate category order metadata
70+
env:
71+
ARDUINO_FORUM_API_KEY: ${{ secrets.ARDUINO_FORUM_API_KEY }}
72+
run: |
73+
task \
74+
--silent \
75+
forum:generate-category-order-metadata
76+
77+
- name: Check category order metadata
78+
id: check-category-order-metadata
79+
run: git diff --color --exit-code
80+
81+
- name: Upload corrected file to workflow artifact
82+
if: failure() && steps.check-category-order-metadata.outcome == 'failure'
83+
uses: actions/upload-artifact@v3
84+
with:
85+
if-no-files-found: error
86+
name: ${{ env.ARTIFACT_NAME }}
87+
path: ${{ env.CATEGORY_ORDER_METADATA_FILE_PATH }}
88+
89+
- name: Add artifact availability notice
90+
if: failure() && steps.check-category-order-metadata.outcome == 'failure'
91+
run: |
92+
echo "::notice file=${{ env.CATEGORY_ORDER_METADATA_FILE_PATH }}::Corrected file was saved to the ${{ env.ARTIFACT_NAME }} workflow artifact"
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/check-python-task.md
2+
name: Check Python
3+
4+
env:
5+
# See: https://github.com/actions/setup-python/tree/main#available-versions-of-python
6+
PYTHON_VERSION: "3.9"
7+
8+
# See: https://docs.github.com/actions/using-workflows/events-that-trigger-workflows
9+
on:
10+
create:
11+
push:
12+
paths:
13+
- ".github/workflows/check-python-task.ya?ml"
14+
- "**/.flake8"
15+
- "**/poetry.lock"
16+
- "**/pyproject.toml"
17+
- "**/setup.cfg"
18+
- "Taskfile.ya?ml"
19+
- "**/tox.ini"
20+
- "**.py"
21+
pull_request:
22+
paths:
23+
- ".github/workflows/check-python-task.ya?ml"
24+
- "**/.flake8"
25+
- "**/poetry.lock"
26+
- "**/pyproject.toml"
27+
- "**/setup.cfg"
28+
- "Taskfile.ya?ml"
29+
- "**/tox.ini"
30+
- "**.py"
31+
schedule:
32+
# Run periodically to catch breakage caused by external changes.
33+
- cron: "0 8 * * WED"
34+
workflow_dispatch:
35+
repository_dispatch:
36+
37+
jobs:
38+
run-determination:
39+
runs-on: ubuntu-latest
40+
outputs:
41+
result: ${{ steps.determination.outputs.result }}
42+
steps:
43+
- name: Determine if the rest of the workflow should run
44+
id: determination
45+
run: |
46+
RELEASE_BRANCH_REGEX="refs/heads/[0-9]+.[0-9]+.x"
47+
# The `create` event trigger doesn't support `branches` filters, so it's necessary to use Bash instead.
48+
if [[
49+
"${{ github.event_name }}" != "create" ||
50+
"${{ github.ref }}" =~ $RELEASE_BRANCH_REGEX
51+
]]; then
52+
# Run the other jobs.
53+
RESULT="true"
54+
else
55+
# There is no need to run the other jobs.
56+
RESULT="false"
57+
fi
58+
59+
echo "result=$RESULT" >> $GITHUB_OUTPUT
60+
61+
lint:
62+
needs: run-determination
63+
if: needs.run-determination.outputs.result == 'true'
64+
runs-on: ubuntu-latest
65+
66+
steps:
67+
- name: Checkout repository
68+
uses: actions/checkout@v3
69+
70+
- name: Install Python
71+
uses: actions/setup-python@v4
72+
with:
73+
python-version: ${{ env.PYTHON_VERSION }}
74+
75+
- name: Install Poetry
76+
run: pip install poetry
77+
78+
- name: Install Task
79+
uses: arduino/setup-task@v1
80+
with:
81+
repo-token: ${{ secrets.GITHUB_TOKEN }}
82+
version: 3.x
83+
84+
- name: Run flake8
85+
uses: liskin/gh-problem-matcher-wrap@v2
86+
with:
87+
linters: flake8
88+
run: task python:lint
89+
90+
formatting:
91+
needs: run-determination
92+
if: needs.run-determination.outputs.result == 'true'
93+
runs-on: ubuntu-latest
94+
95+
steps:
96+
- name: Checkout repository
97+
uses: actions/checkout@v3
98+
99+
- name: Install Python
100+
uses: actions/setup-python@v4
101+
with:
102+
python-version: ${{ env.PYTHON_VERSION }}
103+
104+
- name: Install Poetry
105+
run: pip install poetry
106+
107+
- name: Install Task
108+
uses: arduino/setup-task@v1
109+
with:
110+
repo-token: ${{ secrets.GITHUB_TOKEN }}
111+
version: 3.x
112+
113+
- name: Format Python code
114+
run: task python:format
115+
116+
- name: Check formatting
117+
run: git diff --color --exit-code

.github/workflows/check-toc-task.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ on:
1313
- ".github/workflows/check-toc-task.ya?ml"
1414
- "package.json"
1515
- "package-lock.json"
16+
- "Taskfile.ya?ml"
1617
- "content/categories/staff/moderation/_topics/moderator-instructions/1.md"
1718
pull_request:
1819
paths:
1920
- ".github/workflows/check-toc-task.ya?ml"
2021
- "package.json"
2122
- "package-lock.json"
23+
- "Taskfile.ya?ml"
2224
- "content/categories/staff/moderation/_topics/moderator-instructions/1.md"
2325
schedule:
2426
# Run periodically to catch breakage caused by external changes.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Arduino Forum Assets
22

3+
[![Check Category Order Metadata](https://github.com/arduino/forum-assets/actions/workflows/check-order-metadata-task.yml/badge.svg)](https://github.com/arduino/forum-assets/actions/workflows/check-order-metadata-task.yml)
34
[![Check General Formatting status](https://github.com/arduino/forum-assets/actions/workflows/check-general-formatting-task.yml/badge.svg)](https://github.com/arduino/forum-assets/actions/workflows/check-general-formatting-task.yml)
45
[![Check Files status](https://github.com/arduino/forum-assets/actions/workflows/check-files-task.yml/badge.svg)](https://github.com/arduino/forum-assets/actions/workflows/check-files-task.yml)
56
[![Check License status](https://github.com/arduino/forum-assets/actions/workflows/check-license.yml/badge.svg)](https://github.com/arduino/forum-assets/actions/workflows/check-license.yml)
67
[![Check Markdown status](https://github.com/arduino/forum-assets/actions/workflows/check-markdown-task.yml/badge.svg)](https://github.com/arduino/forum-assets/actions/workflows/check-markdown-task.yml)
78
[![Check Prettier Formatting status](https://github.com/arduino/forum-assets/actions/workflows/check-prettier-formatting-task.yml/badge.svg)](https://github.com/arduino/forum-assets/actions/workflows/check-prettier-formatting-task.yml)
9+
[![Check Python status](https://github.com/arduino/forum-assets/actions/workflows/check-python-task.yml/badge.svg)](https://github.com/arduino/forum-assets/actions/workflows/check-python-task.yml)
810
[![Check Taskfiles status](https://github.com/arduino/forum-assets/actions/workflows/check-taskfiles.yml/badge.svg)](https://github.com/arduino/forum-assets/actions/workflows/check-taskfiles.yml)
911
[![Check ToC status](https://github.com/arduino/forum-assets/actions/workflows/check-toc-task.yml/badge.svg)](https://github.com/arduino/forum-assets/actions/workflows/check-toc-task.yml)
1012
[![Check Workflows status](https://github.com/arduino/forum-assets/actions/workflows/check-workflows-task.yml/badge.svg)](https://github.com/arduino/forum-assets/actions/workflows/check-workflows-task.yml)

Taskfile.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ tasks:
5454
# This is an "umbrella" task used to call any documentation generation processes the project has.
5555
# It can be left empty if there are none.
5656

57+
forum:generate-category-order-metadata:
58+
desc: Generate the category order metadata file
59+
deps:
60+
- task: poetry:install-deps
61+
cmds:
62+
- |
63+
if [[ "$ARDUINO_FORUM_API_KEY" == "" ]]; then
64+
echo "ARDUINO_FORUM_API_KEY environment variable not set."
65+
echo "This task requires a forum.arduino.cc Discourse API key with \"categories > list\" scope."
66+
exit 1
67+
fi
68+
69+
poetry run \
70+
python assets/generate-category-order-metadata/generate-category-order-metadata.py
71+
5772
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-files-task/Taskfile.yml
5873
general:check-filenames:
5974
desc: Check for non-portable filenames
@@ -287,6 +302,22 @@ tasks:
287302
cmds:
288303
- poetry install --no-root
289304

305+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-python-task/Taskfile.yml
306+
python:format:
307+
desc: Format Python files
308+
deps:
309+
- task: poetry:install-deps
310+
cmds:
311+
- poetry run black .
312+
313+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-python-task/Taskfile.yml
314+
python:lint:
315+
desc: Lint Python code
316+
deps:
317+
- task: poetry:install-deps
318+
cmds:
319+
- poetry run flake8 --show-source
320+
290321
# Make a temporary file named according to the passed TEMPLATE variable and print the path passed to stdout
291322
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/windows-task/Taskfile.yml
292323
utility:mktemp-file:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Category Order Metadata Generator
2+
3+
This Python script uses the Arduino Forum's [Discourse API](https://docs.discourse.org/#tag/Categories/operation/listCategories) to generate the forum category order metadata file (`order.md`).
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[186]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import os
2+
import requests
3+
import json
4+
import pathlib
5+
6+
7+
def get_category_list(categories_data, level):
8+
category_list = ""
9+
for category_data in categories_data:
10+
if category_data["id"] in skip_list:
11+
continue
12+
13+
category_list += "{indent}- {name}\n".format(indent=" " * level, name=category_data["name"])
14+
15+
if "subcategory_list" in category_data:
16+
category_list += get_category_list(categories_data=category_data["subcategory_list"], level=level + 1)
17+
18+
return category_list
19+
20+
21+
response = requests.get(
22+
url="https://forum.arduino.cc/categories.json",
23+
params={"include_subcategories": "true"},
24+
headers={"Api-Key": os.environ["ARDUINO_FORUM_API_KEY"], "Api-Username": "system"},
25+
)
26+
27+
script_location = pathlib.PurePath(os.path.dirname(os.path.realpath(__file__)))
28+
with open(file=script_location.joinpath("category-skip-list.json"), encoding="utf-8") as skip_list_file:
29+
skip_list = json.load(fp=skip_list_file)
30+
31+
category_list = get_category_list(categories_data=response.json()["category_list"]["categories"], level=0)
32+
33+
with open(file="content/categories/order.md", mode="w", encoding="utf-8") as order_file:
34+
order_file.write(category_list)

0 commit comments

Comments
 (0)