-
Notifications
You must be signed in to change notification settings - Fork 9
ENT-13814: Added syntax-description and GH Action to update it weekly to replace policy_language.py #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENT-13814: Added syntax-description and GH Action to update it weekly to replace policy_language.py #59
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,69 @@ | ||||||||||||||||||||||
| name: Update syntax-description | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| on: | ||||||||||||||||||||||
| schedule: | ||||||||||||||||||||||
| - cron: "0 7 * * 1" # Run every Monday at 7am UTC | ||||||||||||||||||||||
| # | | | | | | ||||||||||||||||||||||
| # | | | | day of the week (0-6) (Sunday to Saturday) | ||||||||||||||||||||||
| # | | | month (1-12) | ||||||||||||||||||||||
| # | | day of the month (1-31) | ||||||||||||||||||||||
| # | hour (0-23) | ||||||||||||||||||||||
| # minute (0-59) | ||||||||||||||||||||||
| workflow_dispatch: # Enables manual trigger | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| jobs: | ||||||||||||||||||||||
| update_syntax_desc: | ||||||||||||||||||||||
| name: Update syntax-description | ||||||||||||||||||||||
| runs-on: ubuntu-24.04 | ||||||||||||||||||||||
| permissions: | ||||||||||||||||||||||
| contents: write | ||||||||||||||||||||||
| pull-requests: write | ||||||||||||||||||||||
| steps: | ||||||||||||||||||||||
| - name: Checks-out repository | ||||||||||||||||||||||
| uses: actions/checkout@v4 | ||||||||||||||||||||||
| with: | ||||||||||||||||||||||
| ref: "main" | ||||||||||||||||||||||
| - name: Set up Python 3.12 | ||||||||||||||||||||||
| uses: actions/setup-python@v5 | ||||||||||||||||||||||
| with: | ||||||||||||||||||||||
| python-version: "3.12" | ||||||||||||||||||||||
| - name: Install dependencies | ||||||||||||||||||||||
| run: | | ||||||||||||||||||||||
| python -m pip install --upgrade pip | ||||||||||||||||||||||
| python -m pip install cf-remote | ||||||||||||||||||||||
| - name: Install cfengine | ||||||||||||||||||||||
| run: | | ||||||||||||||||||||||
| cf-remote --version master download --edition community ubuntu24 amd64 hub | ||||||||||||||||||||||
| sudo dpkg -i ~/.cfengine/cf-remote/packages/cfengine-community*.deb | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| - name: Extract new syntax-description | ||||||||||||||||||||||
| run: | | ||||||||||||||||||||||
| ( | ||||||||||||||||||||||
| sudo cf-promises --syntax-description json | ||||||||||||||||||||||
| ) > new.json | ||||||||||||||||||||||
| echo "" >> new.json | ||||||||||||||||||||||
| - name: Set Git user | ||||||||||||||||||||||
| run: | | ||||||||||||||||||||||
| git config user.name 'github-actions[bot]' | ||||||||||||||||||||||
| git config user.email 'github-actions[bot]@users.noreply.github.com' | ||||||||||||||||||||||
| - name: Update contents of syntax-description | ||||||||||||||||||||||
| run: | | ||||||||||||||||||||||
| if ! cmp -s new.json ./src/cfengine_cli/syntax-description.json; then | ||||||||||||||||||||||
| cat new.json > ./src/cfengine_cli/syntax-description.json | ||||||||||||||||||||||
| echo "CHANGES_DETECTED=true" >> $GITHUB_ENV | ||||||||||||||||||||||
| rm new.json | ||||||||||||||||||||||
| fi | ||||||||||||||||||||||
|
Comment on lines
+51
to
+55
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
| - name: Create Pull Request | ||||||||||||||||||||||
| if: env.CHANGES_DETECTED == 'true' | ||||||||||||||||||||||
| uses: cfengine/create-pull-request@v6 | ||||||||||||||||||||||
| with: | ||||||||||||||||||||||
| title: Updated syntax-description.json | ||||||||||||||||||||||
| body: Automated update to syntax-description.json [the `update-syntax-description` workflow](https://github.com/cfengine/cfengine-cli/tree/main/.github/workflows/update-syntax-description.yml). | ||||||||||||||||||||||
| reviewers: | | ||||||||||||||||||||||
| simonthalvorsen | ||||||||||||||||||||||
| olehermanse | ||||||||||||||||||||||
| larsewi | ||||||||||||||||||||||
| nickanderson | ||||||||||||||||||||||
| craigcomstock | ||||||||||||||||||||||
| branch: update-syntax-description | ||||||||||||||||||||||
| branch-suffix: timestamp | ||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,17 +37,56 @@ | |
| from cfbs.validate import validate_config | ||
| from cfbs.cfbs_config import CFBSConfig | ||
| from cfbs.utils import find | ||
| from cfengine_cli.policy_language import ( | ||
| DEPRECATED_PROMISE_TYPES, | ||
| ALLOWED_BUNDLE_TYPES, | ||
| BUILTIN_PROMISE_TYPES, | ||
| BUILTIN_FUNCTIONS, | ||
| ) | ||
| from cfengine_cli.utils import UserError | ||
|
|
||
| LINT_EXTENSIONS = (".cf", ".json") | ||
|
|
||
|
|
||
| def _load_syntax_description(path: str | None = None) -> dict: | ||
| """Load and return the parsed syntax-description.json file.""" | ||
| if path is None: | ||
| path = os.path.join(os.path.dirname(__file__), "syntax-description.json") | ||
| with open(path, "r") as f: | ||
| return json.load(f) | ||
|
|
||
|
|
||
| def _derive_syntax_sets(data: dict) -> tuple: | ||
| """Derive the four sets used for linting from a loaded syntax-description dict. | ||
|
|
||
| Returns: (ALLOWED_BUNDLE_TYPES, BUILTIN_PROMISE_TYPES, BUILTIN_FUNCTIONS, DEPRECATED_PROMISE_TYPES) | ||
| """ | ||
| builtin_body_types = set(data.get("bodyTypes", {}).keys()) | ||
|
|
||
| allowed_bundle_types = data.get("bundleTypes", {}).keys() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned below, linting will not work correctly if this one is empty, so it might be appropriate to assert / fail early when the syntax data is invalid / missing. |
||
|
|
||
| builtin_promise_types = set(data.get("promiseTypes", {}).keys()) | ||
|
|
||
| builtin_functions = set(data.get("functions", {}).keys()) | ||
|
|
||
| deprecated_promise_types = { | ||
| "defaults", | ||
| "guest_environments", | ||
| } # Has to be hardcoded, not tagged in syntax-description.json | ||
|
|
||
| return ( | ||
| builtin_body_types, | ||
| allowed_bundle_types, | ||
| builtin_promise_types, | ||
| builtin_functions, | ||
| deprecated_promise_types, | ||
| ) | ||
|
Comment on lines
+53
to
+77
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the future, we will want to access the syntax data in more complicated ways, not just lists of keys. I don't think this approach will scale nicely, please use a class instead; class SyntaxData:
def __init__(self, path):
self._data = get_json(path)
# Maybe add some assertions here, linting will not work correctly
# if for example allowed bundle types is empty.
[...]
def get_bundle_types(self):
[...]
# In the future we might choose to do something like;
def is_valid_attribute(self, promise_type, attribute_name, attribute_value):
[...] |
||
|
|
||
|
|
||
| _SYNTAX_DATA = _load_syntax_description() | ||
| ( | ||
| _, | ||
| ALLOWED_BUNDLE_TYPES, | ||
| BUILTIN_PROMISE_TYPES, | ||
| BUILTIN_FUNCTIONS, | ||
| DEPRECATED_PROMISE_TYPES, | ||
| ) = _derive_syntax_sets(_SYNTAX_DATA) | ||
|
|
||
|
|
||
| def _qualify(name: str, namespace: str) -> str: | ||
| """If name is already qualified (contains ':'), return as-is. Otherwise prepend namespace.""" | ||
| assert '"' not in namespace | ||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would probably prefer to put the JSON / yaml in as separate commits to avoid doing too many things in one commit.