Skip to content
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

Closes #184 Automatically updating the date of new blog posts #222

Merged
merged 12 commits into from
Sep 16, 2024
1 change: 1 addition & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Thank you for your Pull Request! We have developed this task checklist to help w
Please check off each taskbox as an acknowledgment that you completed the task or check off that it is not relevant to your Pull Request. This checklist is part of the Github Action workflows and the Pull Request will not be merged into the `main` branch until you have checked off each task.

- [ ] Place Closes #<insert_issue_number> into the beginning of your Pull Request Title (Use Edit button in top-right if you need to update), and make sure the corresponding issue is linked in the Development section on the right hand side
- [ ] Ensure your new post folder is of the form `"posts/zzz_DO_NOT_EDIT_<your post title>"`. This is so that the post date can be auto-updated upon the merge into `main`.
- [ ] Run the script from `CICD.R` line by line to first check the spelling in your post and then to make sure your code is compatible with our code-style. Address any incongruences by following the instructions in the file!
- [ ] Choose (possibly several) `tag(s)` or categories from the current list: ` c("Metadata", "SDTM", "ADaM", "TLG", "Shiny", "Community", "Conferences", "Submissions", "Technical")` for your blog post. If you cannot find anything that fits your blog post, propose a new tag to the maintainers! Note: if you use a tag not from this list, the "Check Post Tags" CICD pipeline will error. We occasionally tidy up all `tags` for consistency.
- [ ] Add a short description for your blog post in the `description` field at the top of the markdown document.
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ name: Quarto Publish

jobs:
build-deploy:
needs: Update-post-dates
runs-on: ubuntu-latest
permissions:
contents: write
Expand Down
33 changes: 33 additions & 0 deletions .github/workflows/update_post_dates.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Update post dates

on:
push:
branches:
- 'main'

jobs:
Update-post-dates:
runs-on: ubuntu-latest
container:
image: "rocker/tidyverse:4.2.1"
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: main # replace with the branch you want to checkout

- name: Run update_post_dates
run: Rscript R/update_post_dates.R # running the R script with Rscript

- name: Configure Git safe directory
run: git config --global --add safe.directory /__w/blog/blog

- name: Commit results
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git add .
git commit -m "Auto-update blog post date"
git push origin main
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion R/create_blogpost.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ print(allowed_tags)
# Fill in the info, e.g.:
create_post(
post_name = "My Blog", # needs to be character vector (length 1)
post_date = "2024-12-01", # needs to be character vector (length 1)
post_date = "2024-12-01", # needs to be length 1 character vector and will be auto-updated when your post is merged
description = "", # you can fill the description in later as well
author = c("My Name"), # one or more authors are permitted
cover_image = "pharmaverse", # chose one of the above (see line 8)
Expand Down
2 changes: 1 addition & 1 deletion R/help_create_blogpost.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ create_post <- function(post_name,

# Prepare values
snake_name <- gsub(" ", "_", tolower(gsub("(.)([A-Z])", "\\1 \\2", post_name)))
short_name <- paste(post_date, snake_name, sep = "_")
short_name <- paste("zzz_DO_NOT_EDIT", snake_name, sep = "_")

if (short_name != short_name %>% stringr::str_trunc(30)) {
message("For the folder creation:")
Expand Down
1 change: 1 addition & 0 deletions R/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ Some of these files help in creating/developing blog-posts, others are used by o
- `help_create_blogpost.R`: script containing the function(s) used by `create_blogpost.R`
- `switch.R`: Used by CICD spellcheck workflow.
- `check_post_tags.R`: Used by Check-Post-Tags CICD workflow
- `update_post_dates.R`: Used by Update-Post-Dates CICD workflow
53 changes: 53 additions & 0 deletions R/update_post_dates.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Get list of post folders where date has to be edited ----
post_folders <- list.files("posts", recursive = FALSE, pattern = "zzz_DO_NOT_EDIT")

# Prepare date to replace in a blog post and folder ----
formatted_date <- format(Sys.Date(), "%Y-%m-%d")

for (folder in post_folders) {
# Update date in quarto document ----

## Find qmd file with blog post ----
blog_post <- list.files(file.path("posts", folder), recursive = FALSE, pattern = "*.qmd")
blog_post_path <- file.path("posts", folder, blog_post)

## Read the quarto file ----
lines <- readLines(blog_post_path)

## Identify the YAML front matter boundaries ----
yaml_start <- which(lines == "---")[1]
yaml_end <- which(lines == "---")[2]

## Check file is formatted correctly ----
if (is.na(yaml_start) || is.na(yaml_end) || yaml_start >= yaml_end) {
cli::cli_abort("YAML front matter not found or improperly formatted.")
}

## Extract and parse the existing YAML front matter ----
yaml_content <- paste(lines[(yaml_start + 1):(yaml_end - 1)], collapse = "\n")
yaml_list <- yaml::yaml.load(yaml_content)

## Modify the post date ----
yaml_list$date <- formatted_date

cli::cli_inform(paste("Date updated on post:", yaml_list$title))

## Convert the modified YAML back to a string ----
new_yaml_content <- yaml::as.yaml(yaml_list)

## Reassemble the RMarkdown file with the new YAML front matter ----
new_lines <- c(
lines[1:yaml_start],
strsplit(new_yaml_content, "\n")[[1]],
lines[yaml_end:length(lines)]
)

## Write the modified RMarkdown file back to disk ----
writeLines(new_lines, blog_post_path)

# Update date in folder ----
new_folder_name <- stringr::str_replace(folder, "zzz_DO_NOT_EDIT", formatted_date)
file.rename(file.path("posts", folder), file.path("posts", new_folder_name))

cli::cli_inform(paste("Folder renamed for post:", yaml_list$title))
}
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ After that you can go wild, but we do ask that it is kept short!

- Source the script `help_create_blogpost.R` from within the `create_blogpost.R` file.

- Enter your information into the `create_post` function.
Please note that we do not currently allow users to specify their own categories (or `tags`), so you must choose (possibly several) from this list: `c("Metadata", "SDTM", "ADaM", "TLG", "Shiny", "Community", "Conferences", "Submissions", "Technical")`.
- Enter your information into the `create_post` function. For the post date, feel free to put a placeholder date as it will be automatically replaced once the post is merged onto `main`.
Please note that we do not currently allow users to specify their own categories (or `tags`), so you must choose (possibly several) from this list: `c("Metadata", "SDTM", "ADaM", "TLG", "Shiny", "Community", "Conferences", "Submissions", "Technical")`.

- Running that function will create a subfolder with a quarto file (`*.qmd`) in the `posts/` folder.
Both should be named based on the supplied `post_date` and `post_name` information.
The folder will be named `zzz_DO_NOT_EDIT_<your post name>`, and the `zzz_DO_NOT_EDIT` portion of the folder name
will also be automatically replaced once the post is merged onto `main`.

- Open the newly created folder and start working within the `*.qmd` file.

Expand Down
5 changes: 3 additions & 2 deletions inst/template/template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ title: [TITLE]
author:
[AUTHOR]
description: [DESCR]
# Note that the date below will be auto-updated when the post is merged.
date: [DATE]
# please do not use any non-default categories.
# Please do not use any non-default categories.
# You can find the default categories in the repository README.md
categories: [[TAG]]
# feel free to change the image
# Feel free to change the image
image: [IMG]

---
Expand Down