Skip to content

Commit 6579516

Browse files
sbfnksbfnk-bot
andcommitted
Process all open publications issues in single workflow run
- Loops through all open issues with "publications" label - Collects selected papers from each issue - Adds all papers to papers.bib in one batch - Closes each issue with "processed" label - Logs progress for easier debugging Co-Authored-By: sbfnk-bot <sbfnk-bot@users.noreply.github.com>
1 parent ee6c5f8 commit 6579516

File tree

1 file changed

+76
-61
lines changed

1 file changed

+76
-61
lines changed

snippets/update_publications.R

Lines changed: 76 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,110 @@
1-
# Find the latest open publications issue
1+
# Find all open publications issues
22
open_issues <- gh::gh(
33
"/repos/{gh_repo}/issues",
44
gh_repo = gh_repository,
55
state = "open",
66
labels = "publications",
77
sort = "created",
8-
direction = "desc",
9-
per_page = 1
8+
direction = "asc",
9+
per_page = 100
1010
)
1111

1212
if (length(open_issues) == 0) {
13-
message("No open publications issue found")
13+
message("No open publications issues found")
1414
quit(save = "no")
1515
}
1616

17-
issue <- open_issues[[1]]
18-
issue_number <- issue$number
17+
message(sprintf("Found %d open publications issue(s)", length(open_issues)))
1918

20-
last_comment_pubs <- issue$body_html
21-
if (is.null(last_comment_pubs)) {
22-
# Fetch with HTML body
23-
issue <- gh::gh(
24-
"/repos/{gh_repo}/issues/{issue_number}",
25-
gh_repo = gh_repository,
26-
issue_number = issue_number,
27-
.accept = "application/vnd.github.v3.full+json"
28-
)
29-
last_comment_pubs <- issue$body_html
30-
}
31-
32-
last_comment_pubs <- xml2::read_html(last_comment_pubs)
33-
34-
# Extract BibTeX from the code block inside <details>
35-
# GitHub renders markdown code fences as <pre><code> elements
36-
bib_text <- last_comment_pubs |>
37-
xml2::xml_find_first("//details//pre") |>
38-
xml2::xml_text()
19+
# Process each issue and collect selected papers
20+
all_selected_bibs <- list()
3921

40-
# If no <pre> found, try the <details> element directly (fallback)
41-
if (is.na(bib_text) || nchar(trimws(bib_text)) == 0) {
42-
bib_text <- last_comment_pubs |>
43-
xml2::xml_find_first("//details") |>
44-
xml2::xml_text()
45-
}
46-
47-
# Clean up any leading/trailing whitespace
48-
bib_text <- trimws(bib_text)
49-
50-
if (is.na(bib_text) || nchar(bib_text) == 0) {
51-
message("No BibTeX content found in issue")
52-
quit(save = "no")
53-
}
22+
for (issue in open_issues) {
23+
issue_number <- issue$number
24+
message(sprintf("Processing issue #%d", issue_number))
5425

55-
write(bib_text, "bibentries_previous_month.bib")
26+
# Get HTML body
27+
issue_html <- issue$body_html
28+
if (is.null(issue_html)) {
29+
issue <- gh::gh(
30+
"/repos/{gh_repo}/issues/{issue_number}",
31+
gh_repo = gh_repository,
32+
issue_number = issue_number,
33+
.accept = "application/vnd.github.v3.full+json"
34+
)
35+
issue_html <- issue$body_html
36+
}
5637

57-
bibentries_previous_month <- bibtex::read.bib("bibentries_previous_month.bib")
38+
parsed_html <- xml2::read_html(issue_html)
5839

59-
selected <- last_comment_pubs |>
60-
xml2::xml_find_first("//ul") |>
61-
xml2::xml_find_all(".//input") |>
62-
xml2::xml_has_attr("checked")
40+
# Extract BibTeX from the code block inside <details>
41+
bib_text <- parsed_html |>
42+
xml2::xml_find_first("//details//pre") |>
43+
xml2::xml_text()
6344

64-
if (!any(selected)) {
65-
message("No papers selected, closing issue")
45+
# Fallback to <details> directly
46+
if (is.na(bib_text) || nchar(trimws(bib_text)) == 0) {
47+
bib_text <- parsed_html |>
48+
xml2::xml_find_first("//details") |>
49+
xml2::xml_text()
50+
}
51+
52+
bib_text <- trimws(bib_text)
53+
54+
if (is.na(bib_text) || nchar(bib_text) == 0) {
55+
message(sprintf(" No BibTeX content found in issue #%d, closing", issue_number))
56+
gh::gh(
57+
"PATCH /repos/{gh_repo}/issues/{issue_number}",
58+
gh_repo = gh_repository,
59+
issue_number = issue_number,
60+
state = "closed",
61+
labels = list("publications", "processed")
62+
)
63+
next
64+
}
65+
66+
# Write and read bib entries
67+
write(bib_text, "bibentries_temp.bib")
68+
bibentries <- bibtex::read.bib("bibentries_temp.bib")
69+
70+
# Find selected (checked) papers
71+
selected <- parsed_html |>
72+
xml2::xml_find_first("//ul") |>
73+
xml2::xml_find_all(".//input") |>
74+
xml2::xml_has_attr("checked")
75+
76+
if (!any(selected)) {
77+
message(sprintf(" No papers selected in issue #%d, closing", issue_number))
78+
} else {
79+
selected_bibentries <- bibentries[selected]
80+
message(sprintf(" Found %d selected paper(s)", length(selected_bibentries)))
81+
all_selected_bibs <- c(all_selected_bibs, selected_bibentries)
82+
}
83+
84+
# Close the issue and add "processed" label
6685
gh::gh(
6786
"PATCH /repos/{gh_repo}/issues/{issue_number}",
6887
gh_repo = gh_repository,
6988
issue_number = issue_number,
7089
state = "closed",
7190
labels = list("publications", "processed")
7291
)
73-
quit(save = "no")
7492
}
7593

76-
selected_bibentries <- bibentries_previous_month[selected]
94+
# Clean up temp file
95+
unlink("bibentries_temp.bib")
96+
97+
if (length(all_selected_bibs) == 0) {
98+
message("No papers selected across all issues")
99+
quit(save = "no")
100+
}
77101

78-
bib_new <- vapply(selected_bibentries, format, style = "Bibtex", character(1))
102+
message(sprintf("Adding %d paper(s) to papers.bib", length(all_selected_bibs)))
79103

104+
# Add all selected papers to bib file
105+
bib_new <- vapply(all_selected_bibs, format, style = "Bibtex", character(1))
80106
bib_old <- readLines("_data/papers.bib")
81-
82107
bib <- paste(c(bib_old, bib_new), collapse = "\n")
83-
84108
write(bib, "_data/papers.bib")
85109

86110
## filter latest version
@@ -120,12 +144,3 @@ df <- df |>
120144
dplyr::select(-n, -id, -n_pp)
121145

122146
bib2df::df2bib(df, file = "_data/papers.bib")
123-
124-
# Close the issue and add "processed" label
125-
gh::gh(
126-
"PATCH /repos/{gh_repo}/issues/{issue_number}",
127-
gh_repo = gh_repository,
128-
issue_number = issue_number,
129-
state = "closed",
130-
labels = list("publications", "processed")
131-
)

0 commit comments

Comments
 (0)