Skip to content

Commit c243c04

Browse files
committed
Improve how we work with changed vs added files
1 parent 0435a13 commit c243c04

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ jobs:
5151
- name: Publish to Hashnode
5252
uses: actions/publish-github-to-hashnode@v1
5353
with:
54+
added-files: ${{ steps.changed-files.outputs.added_files }}
55+
changed-and-modified-files: ${{ steps.changed-files.outputs.all_changed_and_modified_files }}
56+
deleted-files: ${{ steps.changed-files.outputs.deleted_files }}
5457
access-token: ${{ secrets.HASHNODE_ACCESS_TOKEN }}
55-
changed-files: ${{ steps.changed-files.outputs.all_changed_files }}
5658
publication-host: 'blog.mydomain.com' # Your publication host
5759
posts-directory: 'content/posts' # The directory within your repository containing the markdown files, if different from the root directory
5860
```
@@ -149,8 +151,10 @@ jobs:
149151
id: publish
150152
uses: actions/publish-github-to-hashnode@v1
151153
with:
154+
added-files: ${{ steps.changed-files.outputs.added_files }}
155+
changed-and-modified-files: ${{ steps.changed-files.outputs.all_changed_and_modified_files }}
156+
deleted-files: ${{ steps.changed-files.outputs.deleted_files }}
152157
access-token: ${{ secrets.HASHNODE_ACCESS_TOKEN }}
153-
changed-files: ${{ steps.changed-files.outputs.all_changed_files }}
154158
publication-host: 'blog.mydomain.com'
155159
posts-directory: 'content/posts'
156160

action.yml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ branding:
66
color: "green"
77

88
inputs:
9-
access-token:
10-
description: "Your Hashnode API Personal Access Token. See: https://hashnode.com/settings/developer"
9+
added-files:
10+
description: "The list of added files in the repository."
1111
required: true
12-
changed-files:
13-
description: "The list of changed files in the repository."
12+
changed-and-modified-files:
13+
description: "The list of changed or modified files in the repository."
1414
required: true
1515
deleted-files:
1616
description: "The list of deleted files in the repository."
1717
required: true
18+
access-token:
19+
description: "Your Hashnode API Personal Access Token. See: https://hashnode.com/settings/developer"
20+
required: true
1821
publication-host:
1922
description: "The publication host (e.g., blog.mydomain.com)."
2023
required: true
@@ -36,12 +39,14 @@ runs:
3639
ACCESS_TOKEN: ${{ inputs.access-token }}
3740
POSTS_DIRECTORY: ${{ inputs.posts-directory }}
3841
PUBLICATION_HOST: ${{ inputs.publication-host }}
39-
CHANGED_FILES: ${{ inputs.changed-files }}
42+
ADDED_FILES: ${{ inputs.added-files }}
43+
CHANGED_AND_MODIFIED_FILES: ${{ inputs.changed-and-modified-files }}
4044
DELETED_FILES: ${{ inputs.deleted-files }}
4145
PYTHONUNBUFFERED: "1"
4246
args:
4347
- ${{ inputs.access-token }}
4448
- ${{ inputs.posts-directory }}
4549
- ${{ inputs.publication-host }}
46-
- ${{ inputs.changed-files }}
50+
- ${{ inputs.added-files }}
51+
- ${{ inputs.changed-and-modified-files }}
4752
- ${{ inputs.deleted-files }}

entrypoint.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def process_markdown(file_path: Path) -> Tuple[Dict[str, Any], str]:
128128
return post.metadata, post.content
129129

130130

131-
def validate_frontmatter(metadata: Dict[str, Any]) -> None:
131+
def get_validated_frontmatter(metadata: Dict[str, Any]) -> None:
132132
"""Validate that the frontmatter contains the required fields."""
133133
required_fields = ["title"]
134134
for field in required_fields:
@@ -152,6 +152,8 @@ def validate_frontmatter(metadata: Dict[str, Any]) -> None:
152152
if "publishedAt" not in metadata:
153153
metadata["publishedAt"] = datetime.now().isoformat()
154154

155+
return metadata
156+
155157

156158
def convert_path_to_posix(path: Union[str, Path]) -> str:
157159
"""Convert a path to a POSIX path."""
@@ -185,7 +187,12 @@ def handle_post( # pylint: disable=too-many-arguments
185187
) -> None:
186188
"""Handle a markdown post file."""
187189
metadata, content = process_markdown(file_path)
188-
validate_frontmatter(metadata)
190+
191+
try:
192+
metadata = get_validated_frontmatter(metadata)
193+
except ValueError as e:
194+
results["errors"].append({"file": file_path, "error": str(e)})
195+
return results
189196

190197
content = update_image_urls(content, base_path, repo, branch)
191198

@@ -264,20 +271,31 @@ def main():
264271
publication_host = os.environ["PUBLICATION_HOST"]
265272

266273
# Convert the space-separated strings to lists
267-
changed_files = os.environ.get("CHANGED_FILES", "").split()
274+
added_files = os.environ.get("ADDED_FILES", "").split()
275+
changed_and_modified_files = os.environ.get("CHANGED_AND_MODIFIED_FILES", "").split()
268276
deleted_files = os.environ.get("DELETED_FILES", "").split()
269277

270278
repo = os.environ["GITHUB_REPOSITORY"]
271279
branch = os.environ["GITHUB_REF"].split("/")[-1]
272-
added_files = [Path(f) for f in changed_files if f]
280+
added_files = [Path(f) for f in added_files if f]
281+
changed_and_modified_files = [Path(f) for f in changed_and_modified_files if f]
273282
deleted_files = [Path(f) for f in deleted_files if f]
274283

275284
headers = {"Authorization": f"Bearer {access_token}"}
276285
publication_id = get_publication_id(publication_host, headers)
277286

278-
results = {"added": [], "modified": [], "deleted": []}
287+
results = {
288+
"input_added_files": added_files,
289+
"input_changed_and_modified_files": changed_and_modified_files,
290+
"input_deleted_files": deleted_files,
291+
"added": [],
292+
"modified": [],
293+
"deleted": [],
294+
"errors": [],
295+
}
279296

280-
for file_path in added_files:
297+
all_changed_files = added_files + changed_and_modified_files
298+
for file_path in all_changed_files:
281299
if file_path.is_relative_to(posts_directory) and file_path.suffix == ".md":
282300
results = handle_post(
283301
file_path, file_path.parent, repo, branch, publication_id, headers, results, added_files

0 commit comments

Comments
 (0)