-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.R
65 lines (53 loc) · 2.01 KB
/
sync.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
library(gert)
library(gdrive.automation)
set_github_pat <- function(force_new = FALSE,
token_file = ".secrets/github-PAT.rds",
decrypt_env_var = "GH_PAT_KEY")
{
token <- Sys.getenv("GITHUB_PAT")
if (token != "" && !force_new)
{
return(invisible(token))
}
if (gargle::secret_has_key(decrypt_env_var))
{
token <- gargle::secret_read_rds(token_file,
key = decrypt_env_var)
}
Sys.setenv(GITHUB_PAT = token)
invisible(token)
}
update_data <- function(path = tempdir())
{
# set up git config
git_config_global_set("user.name", "Raven Bot")
git_config_global_set("user.email", "[email protected]")
set_github_pat(force_new = TRUE)
# clone dashboard repo
repo_path <- file.path(path, "unit-dashboard")
repo <- git_clone("https://github.com/c4r-io/unit-dashboard",
path = repo_path)
# update data file
data_file <- "unit_data.RDS"
data_path <- file.path(repo_path, data_file)
result <- download_unit_data(data_path)
unit_data <- result$unit_data
# sync unit names to projections spreadsheet
projections_spreadsheet <- "https://docs.google.com/spreadsheets/d/1oSe2HgRCSevMiko1Vzky-w9tBj6ry229-aLiARi9dSI/edit?usp=sharing"
projections_data <- googlesheets4::read_sheet(projections_spreadsheet)
target_unit_names <- unit_data[match(projections_data$`unit-id`, unit_data$`unit-id`), "Unit"]
if (!identical(projections_data$Unit, target_unit_names[[1]]))
{
googlesheets4::range_write(projections_spreadsheet, target_unit_names, range = "D1", reformat = FALSE)
}
# add, commit, push new data file
git_add(files = data_file, repo = repo)
commit_msg <- paste("auto-update data;",
format(Sys.time(), "%F %T %Z", tz = "America/New_York"))
git_commit(message = commit_msg, repo = repo)
git_push(repo = repo)
return()
}
process_roadmaps()
set_github_pat()
update_data()