|
| 1 | +# Setup and Usage --------------------------------------------------------- |
| 2 | +# |
| 3 | +# Will need to get a GitHub PAT to use some of these commands. |
| 4 | +# See [here](https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/). Once you have the GitHub PAT, you will need to save this PAT in your |
| 5 | +# home directory ("~/") in a file called ".Renviron". Inside the file, type out: |
| 6 | +# |
| 7 | +# GITHUB_PAT= # paste the PAT number after the equal sign |
| 8 | +# |
| 9 | +# NOTE: Keep this PAT **ONLY ON YOUR COMPUTER**. Don't save it to GitHub. |
| 10 | +# |
| 11 | +# This script can be used in a few ways. Either: |
| 12 | +# 1. Open the main.Rproj, open the scripts/announcements.R, and "Source" the file |
| 13 | +# using the button or the shortcut Ctrl-Shift-S. |
| 14 | +# 2. Open R console and run the command "source('announcements.R')" when in the |
| 15 | +# script folder. |
| 16 | +# 3. Open the main.Rproj, run "source('scripts/announcements.R')" in the console. |
| 17 | + |
| 18 | +# Installation and Loading Packages --------------------------------------- |
| 19 | +# Running these lines will install the necessary packages. |
| 20 | + |
| 21 | +if (!require(devtools)) install.packages("devtools") |
| 22 | +devtools::install_dev_deps() |
| 23 | +library(tidyverse) |
| 24 | +library(lubridate) |
| 25 | +library(glue) |
| 26 | +library(assertr) |
| 27 | + |
| 28 | +# Importing and Filtering the Event Data ---------------------------------- |
| 29 | + |
| 30 | +session_details <- read_csv(here::here("_data", "events.csv"), comment = "#") %>% |
| 31 | + arrange(date) %>% |
| 32 | + # drop sessions that are not set (NA in date) |
| 33 | + filter(!is.na(date)) |
| 34 | + |
| 35 | +# Find any existing posts, take the date, and filter out those sessions from the |
| 36 | +# session_details dataframe. |
| 37 | +keep_only_new_sessions <- function() { |
| 38 | + existing_post_dates <- fs::dir_ls(here::here("_posts"), regexp = ".md$|.markdown$") %>% |
| 39 | + str_extract("[0-9]{4}-[0-9]{2}-[0-9]{2}") |
| 40 | + |
| 41 | + session_details %>% |
| 42 | + filter(!as.character(date) %in% existing_post_dates) |
| 43 | +} |
| 44 | + |
| 45 | +new_sessions <- keep_only_new_sessions() |
| 46 | + |
| 47 | +# Create a GitHub Issue of the session ------------------------------------ |
| 48 | + |
| 49 | +post_gh_issue <- function(title, body, labels) { |
| 50 | + # Will need to set up a GitHub PAT via (I think) the function |
| 51 | + # devtools::github_pat() in the console. |
| 52 | + devtools:::rule("Posting GitHub Issues") |
| 53 | + cat("Posting `", title, "`\n\n") |
| 54 | + if (!devtools:::yesno("Are you sure you want to post this event as an Issue?")) { |
| 55 | + gh::gh( |
| 56 | + "POST /repos/:owner/:repo/issues", |
| 57 | + owner = "uoftcoders", |
| 58 | + repo = "Events", |
| 59 | + title = title, |
| 60 | + body = body, |
| 61 | + labels = array(c(labels)) |
| 62 | + ) |
| 63 | + usethis:::done("Event posted as an Issue to UofTCoders/Events.") |
| 64 | + return(invisible()) |
| 65 | + } else { |
| 66 | + message("Event not posted to Issue.") |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +# Format as eg August 23 |
| 71 | +day_month <- function(.date, add_name = TRUE) { |
| 72 | + date_format <- "%B %e" # as August 23 |
| 73 | + if (add_name) date_format <- "%A, %B %e" # as Monday, August 23 |
| 74 | + trimws(format(as.Date(.date), format = date_format)) |
| 75 | +} |
| 76 | + |
| 77 | +gh_issue_info <- function(.data) { |
| 78 | + content <- .data %>% |
| 79 | + mutate(needs_packages = ifelse( |
| 80 | + !is.na(packages), |
| 81 | + str_c( |
| 82 | + "Please also install these packages: ", |
| 83 | + str_replace_all(packages, " ", ", "), "." |
| 84 | + ), |
| 85 | + "" |
| 86 | + )) %>% |
| 87 | + mutate_at(vars(start_time, end_time), funs(strftime(., format = "%H:%M"))) %>% |
| 88 | + glue_data( |
| 89 | + " |
| 90 | + {description} |
| 91 | +
|
| 92 | + - **Where**: MADLab at [Gerstein Science Information Centre](https://goo.gl/maps/2916Y54jQkx) |
| 93 | + - **When**: {day_month(date)}, from {start_time}-{end_time} pm |
| 94 | + - **Instructor**: TBA |
| 95 | + - **Skill level**: {skill_level} |
| 96 | +
|
| 97 | + *Installation instructions*: You will need to install the appropriate programs. See the {program_language} section of the [installation instructions page](?link?). {needs_packages} |
| 98 | +
|
| 99 | + **Directions:** MADLab is located in [Gerstein Science Information Centre](https://goo.gl/maps/2916Y54jQkx), Room B112 at the south end of the first lower level. Once you go through the main entrance of Gerstein, take a right turn down a corridor (across from the admin desk or just past the reading room), then take the stairs down and follow the signs to MADLab, the door should be open 10-15 minutes before the lesson. |
| 100 | + " |
| 101 | + ) |
| 102 | + |
| 103 | + .data %>% |
| 104 | + mutate(content = content, title = str_c(title, " - ", day_month(date, add_name = FALSE))) %>% |
| 105 | + select(title, content, skill_level, gh_labels) |
| 106 | +} |
| 107 | + |
| 108 | +create_gh_issues <- function(.data) { |
| 109 | + .data %>% |
| 110 | + gh_issue_info() %>% |
| 111 | + pmap( ~ post_gh_issue(..1, ..2, c(..3, ..4))) |
| 112 | +} |
| 113 | + |
| 114 | +create_gh_issues(new_sessions) |
| 115 | + |
| 116 | +# Create files in _posts/ ------------------------------------------------- |
| 117 | +# Adds the new sessions/events to the _posts folder. |
| 118 | + |
| 119 | +create_new_posts_with_content <- function(.data) { |
| 120 | + new_post_filenames <- |
| 121 | + glue_data(new_sessions, "{here::here('_posts')}/{date}-{key}.md") |
| 122 | + |
| 123 | + # Get the GitHub Issue URL for the event. |
| 124 | + gh_issue_number <- gh::gh("GET /repos/:owner/:repo/issues", |
| 125 | + owner = "uoftcoders", |
| 126 | + repo = "Events") %>% |
| 127 | + map_dfr(~ data_frame(title = .$title, url = .$html_url)) %>% |
| 128 | + mutate(title = str_remove(title, " - .* [1-9]?$")) |
| 129 | + |
| 130 | + new_post_content <- .data %>% |
| 131 | + left_join(gh_issue_number, by = "title") %>% |
| 132 | + glue_data( |
| 133 | + ' |
| 134 | + --- |
| 135 | + title: "{title}" |
| 136 | + text: "{description}" |
| 137 | + location: "MADLab at Gerstein" |
| 138 | + link: "{url}" |
| 139 | + date: "{as.Date(date)}" |
| 140 | + startTime: "{start_time}" |
| 141 | + endTime: "{end_time}" |
| 142 | + --- |
| 143 | + ' |
| 144 | + ) |
| 145 | + |
| 146 | + # Save post content to file |
| 147 | + fs::dir_create(here::here("_posts")) |
| 148 | + map2(new_post_content, new_post_filenames, ~ write_lines(x = .x, path = .y)) |
| 149 | + usethis:::done("Markdown posts created in _posts/ folder.") |
| 150 | + return(invisible()) |
| 151 | +} |
| 152 | + |
| 153 | +create_new_posts_with_content(new_sessions) |
| 154 | + |
| 155 | +# Create emails for sessions ---------------------------------------------- |
| 156 | + |
| 157 | +create_new_emails_for_session <- function(.data) { |
| 158 | + email_content <- new_sessions %>% |
| 159 | + mutate(needs_packages = ifelse( |
| 160 | + !is.na(packages), |
| 161 | + str_c( |
| 162 | + "Please also install these packages: ", |
| 163 | + str_replace_all(packages, " ", ", "), "." |
| 164 | + ), |
| 165 | + "" |
| 166 | + )) %>% |
| 167 | + mutate_at(vars(start_time, end_time), funs(strftime(., format = "%H:%M"))) %>% |
| 168 | + glue_data( |
| 169 | + " |
| 170 | + <add this to the email subject> {title} - {day_month(date)} |
| 171 | +
|
| 172 | + {description} |
| 173 | +
|
| 174 | + **When**: {day_month(date)}, from {start_time}-{end_time} |
| 175 | + **Where**: MADLab at [Gerstein Science Information Centre](https://goo.gl/maps/2916Y54jQkx) |
| 176 | + **Skill level**: {skill_level} |
| 177 | + **What to bring:** |
| 178 | +
|
| 179 | + *Installation instructions*: You will need to install the appropriate programs. See the {program_language} section of the [installation instructions page](https://au-oc.github.io/content/installation). {needs_packages} |
| 180 | + " |
| 181 | + ) |
| 182 | + |
| 183 | + fs::dir_create(here::here("_emails")) |
| 184 | + email_file <- glue_data(.data, "{here::here('_emails')}/{date}-{key}.md") |
| 185 | + usethis:::done("Files with email contents created.") |
| 186 | + usethis:::todo("Open them and copy over into an email.") |
| 187 | + |
| 188 | + map2(email_content, email_file, ~ write_lines(x = .x, path = .y)) |
| 189 | + return(invisible()) |
| 190 | +} |
| 191 | + |
| 192 | +create_new_emails_for_session(new_sessions) |
0 commit comments