Skip to content

Commit 9dfc878

Browse files
committed
First attempt at automating DOI addition to blog posts
1 parent bc427bf commit 9dfc878

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

.github/workflows/add-doi.yaml

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
name: Query API for New Posts
2+
3+
on:
4+
workflow_dispatch:
5+
schedule:
6+
- cron: "0 0 * * *" # Runs at midnight every day
7+
push:
8+
paths:
9+
- "posts/**" # Trigger only if there are changes in the posts/ folder
10+
11+
jobs:
12+
query_api:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout repository
17+
uses: actions/checkout@v2
18+
19+
- name: Get changed files
20+
id: changed-files
21+
uses: actions/github-script@v6
22+
with:
23+
script: |
24+
const changedFiles = await github.paginate(
25+
github.rest.repos.compareCommits,
26+
{
27+
owner: context.repo.owner,
28+
repo: context.repo.repo,
29+
base: context.payload.before,
30+
head: context.sha,
31+
},
32+
(response) => response.data.files
33+
);
34+
const postFiles = changedFiles
35+
.filter(file => file.filename.startsWith('posts/') && file.status !== 'removed')
36+
.map(file => file.filename);
37+
return postFiles.join(' ');
38+
39+
- name: Set up R
40+
uses: r-lib/actions/setup-r@v2
41+
42+
- name: Install dependencies
43+
run: |
44+
install.packages(c("yaml", "httr"))
45+
46+
- name: Find new posts and query API
47+
env:
48+
CHANGED_FILES: ${{ steps.changed-files.outputs.result }}
49+
run: |
50+
Rscript -e '
51+
library(yaml)
52+
library(httr)
53+
54+
posts_dir <- "posts/"
55+
base_api_url <- "https://rogue-scholar.org/api/communities/epiverse_trace/records?q="
56+
changed_files <- strsplit(Sys.getenv("CHANGED_FILES"), " ")[[1]]
57+
58+
for (file_path in changed_files) {
59+
if (grepl("\\.qmd$", file_path)) {
60+
content <- readLines(file_path)
61+
yaml_start <- grep("^---$", content)[1]
62+
yaml_end <- grep("^---$", content)[2]
63+
metadata <- yaml.load(paste(content[(yaml_start + 1):(yaml_end - 1)], collapse = "\n"))
64+
if ("DOI" %in% metadata$categories) {
65+
api_url <- paste0(base_api_url, URLencode(metadata$title, reserved = TRUE))
66+
response <- GET(api_url)
67+
result <- content(response, "parsed")
68+
doi <- result$hits$hits[[1]]$pids$doi$identifier
69+
# Add DOI to metadata
70+
metadata$doi <- doi
71+
# Reconstruct the file content with updated metadata
72+
new_yaml <- c("---", as.yaml(metadata, handlers = list(
73+
logical = verbatim_logical)), "---")
74+
new_content <- c(new_yaml, content[(yaml_end + 1):length(content)])
75+
writeLines(new_content, file_path)
76+
cat("Updated", file_path, "with DOI:", doi, "\n")
77+
}
78+
}
79+
}
80+
'
81+
82+
- uses: peter-evans/create-pull-request@10db75894f6d53fc01c3bb0995e95bd03e583a62
83+
id: cpr
84+
with:
85+
token: ${{ secrets.GITHUB_TOKEN }}
86+
commit-message: Add DOIs to blog posts
87+
committer: epiverse-trace-bot <epiverse-trace-bot@users.noreply.github.com>
88+
author: epiverse-trace-bot <epiverse-trace-bot@users.noreply.github.com>
89+
branch: automated-update-dois
90+
branch-suffix: timestamp
91+
add-paths: posts
92+
title: Automated update of DOI posts
93+
delete-branch: true
94+
body: |
95+
This PR updates the blog posts with DOIs.
96+
It is generated automatically by a GitHub Actions workflow.
97+
Please review the DOIs for accuracy and merge if things look fine.
98+
99+
The original workflow can be found under `.github/workflows/add-doi.yaml`.

0 commit comments

Comments
 (0)