Skip to content

Commit

Permalink
Improve scripts/new_article.py: leaf bundle, tests, comments (#26)
Browse files Browse the repository at this point in the history
* Handle leaf bundle articles in new_article.py script

* Check that response code is 200 when sending notification

* Minor comment change

* Wrap new_article.py's code in a function

* Pass new artciles paths in parameters

* Make content base location configurable in new_article.py

* Add tests for new_article script

* Update .gitignore

* Add pytest workflow
  • Loading branch information
ctmbl authored Mar 18, 2024
1 parent db45586 commit c5774c1
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 20 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Pytest

on:
# Runs on pull requests to check that the website is building without errors
pull_request:

# Only run if the push to main
push:
branches:
- main

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

jobs:
# Build job
build:
runs-on: ubuntu-latest
steps:
# Checkout repo
- name: 🛒 Checkout
uses: actions/checkout@v3

# Install pytest
- name: 🛠️ Install pytest
run: |
python3 -m pip install pytest pytest-mock
# Run tests
- name: 🚀 Run pytest
run: |
cd ./scripts/
pytest
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# env files
.env.prod
.env.dev

Expand All @@ -10,3 +11,6 @@ certbot/*
# hugo build
src/public
build/blog/*

# python
**/__pycache__
60 changes: 40 additions & 20 deletions scripts/new_article.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,45 @@
import os
import sys
import re
import requests
import yaml

for file_path in sys.argv[1:]:
# Check that this is an article file
if re.match("^src/content/posts/.+\.md$", file_path):
# Read YAML Header
with open(file_path, "r") as f:
raw_txt = f.read()
data = yaml.safe_load(raw_txt.split("---")[1])

# Get rid of python objects, only keep basic types
for key in data:
if type(data[key]) not in [int, str, float, bool]:
data[key] = str(data[key])

# Add URL info
file_name = file_path.split("/")[-1][:-3]
data["url"] = f"https://iscsc.fr/posts/{file_name}"

# Finally send Data
requests.post("http://iscsc.fr:8001/new-blog", json=data)
print(file_path, file_name, data)
ARTICLE_FILE_BASE_PATH = "src/content/posts/"

def main(files_paths):
for file_path in files_paths:
# Check that this is an article file
if re.match(f"^{ARTICLE_FILE_BASE_PATH}.+\.md$", file_path):
## Read YAML Header
with open(file_path, "r") as f:
raw_txt = f.read()
data = yaml.safe_load(raw_txt.split("---")[1])

## Get rid of python objects, only keep basic types
for key in data:
if type(data[key]) not in [int, str, float, bool]:
data[key] = str(data[key])

# we have to deal with both possibilities of new article:
# - an article as a .md file which URL is the name
# - a leaf bundle article (https://gohugo.io/content-management/page-bundles/#leaf-bundles):
# it's an article which name is the folder's name and body is in a index.md in this directory
dirname, basename = os.path.split(file_path)
if basename == "index.md":
# leaf bundle: name is directory name
file_name = os.path.basename(dirname)
else:
# direct article file: name is file name
file_name = basename[:-3] # get rid of the `.md`

## Add URL info:
data["url"] = f"https://iscsc.fr/posts/{file_name}"

## Finally send Data
req = requests.post("http://iscsc.fr:8001/new-blog", json=data)
print(file_path, file_name, data)
assert(req.status_code == 200)


if __name__ == "__main__":
main(sys.argv[1:])
68 changes: 68 additions & 0 deletions scripts/test_new_article.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import pytest

import new_article

### DISCLAIMER:
# Whereas other extensions are allowed by HUGO:
# "The extension can be .html, .json or any valid MIME type"
# We only accept Markdown articles and so only parse these
###


@pytest.fixture
def mock_requests_post(mocker):
mock_post = mocker.MagicMock()
fake_response = mocker.Mock()

fake_response.status_code = 200
mock_post.return_value = fake_response

mocker.patch("requests.post", mock_post)
mocker.patch("new_article.ARTICLE_FILE_BASE_PATH", "test_resources/")

yield mock_post


def test_new_article_file(mock_requests_post):
new_article.main(["test_resources/article_1.md"])

mock_requests_post.assert_called_once_with(
'http://iscsc.fr:8001/new-blog',
json={
'title': 'article title',
'summary': 'article summary',
'date': '2024-02-19 10:52:09+01:00',
'lastUpdate': '2024-02-19 10:52:09+01:00',
'tags': "['some', 'tags']",
'author': 'ctmbl',
'draft': False,
'url': 'https://iscsc.fr/posts/article_1'
}
)

def test_new_leaf_bundle_article(mock_requests_post):
new_article.main(["test_resources/leaf_bundle/index.md"])

mock_requests_post.assert_called_once_with(
'http://iscsc.fr:8001/new-blog',
json={
'title': 'leaf bundle title',
'summary': 'leaf bundle summary',
'date': '2024-02-19 10:52:09+01:00',
'lastUpdate': '2024-02-19 10:52:09+01:00',
'tags': "['leaf', 'bundle']",
'author': 'ctmbl',
'draft': False,
'url': 'https://iscsc.fr/posts/leaf_bundle'
}
)

def test_new_branch_bundle():
# not yet implemented
# https://gohugo.io/content-management/page-bundles/#branch-bundles
pass

def test_headless_bundle():
# not yet implemented
# https://gohugo.io/content-management/page-bundles/#headless-bundle
pass
9 changes: 9 additions & 0 deletions scripts/test_resources/article_1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: "article title"
summary: "article summary"
date: 2024-02-19T10:52:09+01:00
lastUpdate: 2024-02-19T10:52:09+01:00
tags: ["some","tags"]
author: ctmbl
draft: false
---
9 changes: 9 additions & 0 deletions scripts/test_resources/leaf_bundle/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: "leaf bundle title"
summary: "leaf bundle summary"
date: 2024-02-19T10:52:09+01:00
lastUpdate: 2024-02-19T10:52:09+01:00
tags: ["leaf","bundle"]
author: ctmbl
draft: false
---

0 comments on commit c5774c1

Please sign in to comment.