Skip to content

Commit

Permalink
♻️ split compile & deploy; use pytest; re #29
Browse files Browse the repository at this point in the history
  • Loading branch information
t4k committed Oct 24, 2023
1 parent 9fcb506 commit 0b73f25
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 252 deletions.
11 changes: 7 additions & 4 deletions workflow-compile.py → .github/workflows/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

from pathlib import Path

import plac


def main(
file: "modified file from which to build artifacts", # type: ignore
libguides_groups: '{"groups":[{"slug":"foo","id":"999"},{…}]}', # type: ignore
github_commit: ("optional github commit path", "option", "g"), # type: ignore
):
print(f"🐞 file: {file}")
Expand Down Expand Up @@ -62,14 +63,16 @@ def main(
f.write(html)
elif component == "header" or component == "footer":
# NOTE libguides_groups is set in a GitHub Actions secret
slugs = [g["slug"] for g in json.loads(libguides_groups)["groups"]]
slugs = [g["slug"] for g in json.loads(os.environ.get("GROUPS"))["groups"]]
slugs.append("system")
variants = list(slugs)
print(f"🐞 variants: {variants}")
variant = (
file.split(".")[0].split("-")[-1]
if file.split(".")[0].split("-")[-1] in variants
else None
)
print(f"🐞 variant: {variant}")
# NOTE avoid redundant artifact creation
if os.path.isfile(f'artifacts/{file.split("/")[-1]}') or os.path.isfile(
f"artifacts/{component}--{variant}.html"
Expand All @@ -83,6 +86,7 @@ def main(
f.write(html)
else:
# NOTE header-wrapper.shtm triggers this condition, for example
print(f"🐞 variants: {variants}")
for variant in variants:
print(f"🐞 variant: {variant}")
# reset output by copying html content into it
Expand Down Expand Up @@ -140,5 +144,4 @@ def parse_nested_includes(wrapper, variant=None):


if __name__ == "__main__":
# fmt: off
import plac; plac.call(main)
plac.call(main)
37 changes: 37 additions & 0 deletions .github/workflows/compile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Compile Artifacts

on:
workflow_call:

jobs:
compile:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 2
- name: Set up Python ${{ vars.PYTHON_VERSION }}
uses: actions/setup-python@v4
with:
python-version: ${{ vars.PYTHON_VERSION }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install plac
curl --silent --show-error --location https://github.com/sass/dart-sass/releases/download/${{ vars.DART_SASS_VERSION }}/dart-sass-${{ vars.DART_SASS_VERSION }}-linux-x64.tar.gz | tar --extract --gzip
mv dart-sass/sass /usr/local/bin
- name: Create artifacts directory
run: mkdir -p artifacts
- name: Compile artifacts from changed files
env:
GROUPS: ${{ secrets.LIBGUIDES_GROUPS }}
run: for f in $(git diff-tree --no-commit-id --name-only -r ${{ github.sha }});
do
python .github/workflows/compile.py "$f" --github-commit ${{ github.repository }}/commit/${{ github.sha }};
done
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
name: compile-${{ github.sha }}
path: artifacts
180 changes: 180 additions & 0 deletions .github/workflows/deploy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import json
import os
import sys

from playwright.sync_api import Page, TimeoutError as PlaywrightTimeoutError


def test_deploy(page: Page):
for item in os.scandir("artifacts"):
try:
page.goto("/libapps/login.php")
page.fill("#s-libapps-email", os.environ.get("USERNAME"))
page.fill("#s-libapps-password", os.environ.get("PASSWORD"))
page.click("#s-libapps-login-button")
page.click("#s-lib-app-anchor")
page.click("#s-lib-app-menu a:text('LibGuides')")
if item.name.endswith(".css") or item.name.endswith(".js"):
page.click("#s-lg-admin-command-bar a:text('Admin')")
page.click("#s-lg-admin-command-bar a:text('Look & Feel')")
page.click("#s-lib-admin-tabs a:text('Custom JS/CSS')")
page.click("#s-lg-include-files_link")
page.set_input_files("#include_file", item.path)
elif item.name.endswith(".html"):
target = item.name.split("-")[0]
with open(item) as f:
html = f.read()
if target == "template":
page.click("#s-lg-admin-command-bar a:text('Admin')")
page.click("#s-lg-admin-command-bar a:text('Look & Feel')")
page.click("#s-lib-admin-tabs a:text('Page Layout')")
if item.name.split("-")[1] == "guide":
page.click("#s-lib-admin-tabs a:text('Guide')")
page.click("#s-lg-guide-templates_link")
page.click("#select2-chosen-2")
# NOTE template must already exist
# TODO account for template not found condition
page.fill("#s2id_autogen2_search", item.name.split(".")[0])
page.press("#s2id_autogen2_search", "Enter")
# NOTE template takes time to load after select
page.wait_for_load_state("networkidle")
page.fill("#template_code", html)
page.click("#btn-save-template")
# NOTE must wait for success before moving on
page.wait_for_selector("#btn-save-template.btn-success")
if item.name.split("-")[1] == "search":
page.click("#s-lib-admin-tabs a:text('Search')")
page.click("#s-lg-tpl_link")
page.click("#select2-chosen-3")
# NOTE template must already exist
# TODO account for template not found condition
page.fill("#s2id_autogen3_search", item.name.split(".")[0])
page.press("#s2id_autogen3_search", "Enter")
# NOTE template takes time to load after select
page.wait_for_load_state("networkidle")
page.fill("#template_code", html)
page.click("#btn-save-template")
# NOTE must wait for success before moving on
page.wait_for_selector("#btn-save-template.btn-success")
elif target == "widget":
page.click("#s-lg-admin-command-bar a:text('Content')")
page.click("#s-lg-admin-command-bar a:text('Assets')")
page.fill(
"#name", item.name.split("-", maxsplit=2)[-1].split(".")[0]
)
page.click(
"#lg-admin-asset-filter .datatable-filter__button--submit"
)
try:
page.wait_for_selector(
"#s-lg-admin-datatable-content_info:text('showing 1 to 1 of 1 entries')"
)
page.click("#s-lg-admin-datatable-content a i.fa-edit")
except PlaywrightTimeoutError:
page.click("#s-lg-page-content button:text('Add Content Item')")
page.click("#s-lg-page-content a:text('Media / Widget')")
page.fill(
"#widget_name",
item.name.split("-", maxsplit=2)[-1].split(".")[0],
)
page.fill("#embed_code", html)
page.click("#s-lib-alert-btn-first")
page.wait_for_selector(
"td:text('"
+ item.name.split("-", maxsplit=2)[-1].split(".")[0]
+ "')"
)
elif target == "head":
variant = item.name.split(".")[0].split("-")[-1]
if variant == "system":
page.goto("/libguides/lookfeel.php?action=1")
page.fill("#jscss_code", html)
page.click("#s-lg-btn-save-jscss")
# NOTE must wait for success before moving on
page.wait_for_selector("#s-lg-btn-save-jscss.btn-success")
elif variant == "libanswers":
# NOTE JS/CSS files are uploaded in LibGuides
page.click("#s-lib-app-anchor")
page.click("#s-lib-app-menu a:text('LibAnswers')")
page.click("#s-la-cmd-bar-collapse a:text('Admin')")
page.click("#s-la-cmd-bar-collapse a:text('System Settings')")
page.click(".nav-tabs a:text('Look & Feel')")
page.fill("#instmetafield", html)
page.click("#instmetabut")
# NOTE must wait for success before moving on
page.wait_for_selector("#s-ui-notification :text('Success')")
elif variant == "libcal":
# NOTE JS/CSS files are uploaded in LibGuides
page.click("#s-lib-app-anchor")
page.click("#s-lib-app-menu a:text('LibCal')")
page.click("#s-lc-app-menu-adm a") # Admin
page.click("#s-lc-app-menu-adm a:text('Look & Feel')")
page.fill("#instmeta", html)
page.click("#instmeta ~ button")
else:
for group in json.loads(os.environ.get("GROUPS"))["groups"]:
if variant == group["slug"]:
page.goto(
f'/libguides/groups.php?action=3&group_id={group["id"]}'
)
page.fill("#jscss_code", html)
page.click("#s-lg-btn-save-jscss")
# NOTE must wait for success before moving on
page.wait_for_selector(
"#s-lg-btn-save-jscss.btn-success"
)
elif target == "header":
variant = item.name.split(".")[0].split("-")[-1]
if variant == "system":
page.goto("/libguides/lookfeel.php?action=0")
page.fill("#banner_html", html)
page.click("#banner_html + .btn-primary")
# TODO LibAnswers & LibCal
else:
for group in json.loads(os.environ.get("GROUPS"))["groups"]:
if variant == group["slug"]:
page.goto(
f'/libguides/groups.php?action=2&group_id={group["id"]}'
)
page.fill("#banner_html", html)
page.click("#banner_html + .btn-primary")
# NOTE must wait for success before moving on
page.wait_for_selector("#banner_html + .btn-success")
elif target == "footer":
variant = item.name.split(".")[0].split("-")[-1]
if variant == "system":
page.goto("/libguides/lookfeel.php?action=0")
page.click("#s-lg-footer_link")
page.fill("#footer_code", html)
page.click("#s-lg-btn-save-footer")
# NOTE LibAnswers uses the same system footer
page.click("#s-lib-app-anchor")
page.click("#s-lib-app-menu a:text('LibAnswers')")
page.click("#s-la-cmd-bar-collapse a:text('Admin')")
page.click("#s-la-cmd-bar-collapse a:text('System Settings')")
page.click(".nav-tabs a:text('Look & Feel')")
page.fill("#instfooterfield", html)
page.click("#instfooterbut")
# NOTE LibCal uses the same system footer
page.click("#s-la-app-anchor")
page.click("#s-la-app-menu a:text('LibCal')")
page.click("#s-lc-app-menu-adm a") # Admin
page.click("#s-lc-app-menu-adm a:text('Look & Feel')")
page.fill("#instfooter", html)
page.click("#instfooter ~ button")
else:
for group in json.loads(os.environ.get("GROUPS"))["groups"]:
if variant == group["slug"]:
page.goto(
f'/libguides/groups.php?action=2&group_id={group["id"]}'
)
page.click("#s-lg-footer_link")
page.fill("#footer_code", html)
page.click("#s-lg-btn-save-footer")
# NOTE must wait for success before moving on
page.wait_for_selector(
"#s-lg-btn-save-footer.btn-success"
)
except PlaywrightTimeoutError:
page.close()
sys.exit(f"PLAYWRIGHT_TIMEOUT: {item.name}", end="")
47 changes: 47 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Deploy Components

on:
push:
paths:
- "**.scss"
- "*.js"
- "*.html"
- "*.shtm"

jobs:
compile:
uses: ./.github/workflows/compile.yml
secrets: inherit
deploy:
runs-on: ubuntu-latest
needs: compile
container:
image: mcr.microsoft.com/playwright/python:v${{ vars.PLAYWRIGHT_VERSION }}
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 2
- name: Set up Python ${{ vars.PYTHON_VERSION }}
uses: actions/setup-python@v4
with:
python-version: ${{ vars.PYTHON_VERSION }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install playwright==${{ vars.PLAYWRIGHT_VERSION }} pytest-playwright
- uses: actions/download-artifact@v3
with:
name: compile-${{ github.sha }}
path: artifacts
- name: Deploy components
id: deploy
env:
USERNAME: ${{ secrets.ADMIN_USERNAME }}
PASSWORD: ${{ secrets.ADMIN_PASSWORD }}
GROUPS: ${{ secrets.LIBGUIDES_GROUPS }}
run: pytest --slowmo 100 --video on --output playwright --base-url ${{ secrets.ADMIN_BASE_URL }} -sv .github/workflows/deploy.py
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
name: deploy-${{ github.sha }}
path: playwright
51 changes: 0 additions & 51 deletions .github/workflows/workflow.yml

This file was deleted.

Loading

0 comments on commit 0b73f25

Please sign in to comment.