-
Notifications
You must be signed in to change notification settings - Fork 0
272 lines (238 loc) · 12.2 KB
/
create-json.yml
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# # Convert info from a Github issue from the onboarding form into json.
name: create JSON
on:
issues:
types: [opened]
jobs:
create-json:
if: contains(github.event.issue.labels.*.name, 'Onboarding Request')
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.API_TOKEN_GITHUB }}
steps:
- name: checkout toolbox_web_templating repo
uses: actions/checkout@v3
with:
repository: noaa-fisheries-integrated-toolbox/toolbox_web_templating
ref: dev
- name: install R
uses: r-lib/actions/setup-r@v2
- name: install libcurl
run: sudo apt-get install -y libcurl4-openssl-dev
- name: install R packages
run: |
install.packages("gh")
install.packages("jsonlite")
install.packages("rlang")
shell: Rscript {0}
- name: run R script
run: |
library(gh)
library(jsonlite)
issue_info <- gh("/repos/{owner}/{repo}/issues/{issue_number}",
owner = "noaa-fisheries-integrated-toolbox",
repo = "onboard-and-update",
issue_number = ${{ github.event.issue.number }}
)
issue_body <- issue_info$body
# break up different form sections
issue_body <- strsplit(issue_body, "### ", fixed = TRUE)[[1]]
# break up headers from content
issue_body <- strsplit(issue_body, "(\n\n)|(\r\n\r\n)")
# rm no response components
issue_body <- lapply(issue_body, function(x) {
nr <- grep("_No response_", x, fixed = TRUE)
if(length(nr) > 0) {
return(NULL)
}
x
})
# remove nulls and anything of length 0
issue_body <- issue_body[lengths(issue_body) != 0]
# convert the JSON component names
issue_body <- lapply(issue_body, function(x) {
json_title <- tolower(x[1])
json_title <- gsub(" ", "_", json_title, fixed = TRUE)
json_title <- gsub("(", "", json_title, fixed = TRUE)
json_title <- gsub(")", "", json_title, fixed = TRUE)
x[1] <- json_title
x
})
# make names the title instead of a part of the list
names(issue_body) <- unlist(lapply(issue_body, function(x) x[1]))
issue_body <- lapply(issue_body, function(x) x[-1])
# Get rid of move source code, comments and code of conduct, not needed
# in the json
issue_body$move_source_code <- NULL
issue_body$comments <- NULL
issue_body$code_of_conduct <- NULL
# Now make changes needed for specific elements to be correct in JSON.
# Format boolean inputs
issue_body$active_development <-
ifelse(issue_body$active_development == "Yes",TRUE, FALSE)
issue_body$noaa_internal <-
ifelse(issue_body$noaa_internal == "Yes", TRUE, FALSE)
issue_body$uses_github_releases <-
ifelse(issue_body$uses_github_releases == "Yes", TRUE, FALSE)
# format toolbox drawers and keywords
format_checkboxes <- function(original_checkboxes) {
tmp_boxes <- strsplit(original_checkboxes, "\n", fixed = TRUE)[[1]]
tmp_boxes <- grep("[X]", tmp_boxes, fixed = TRUE, value = TRUE)
tmp_boxes <- strsplit(tmp_boxes, "- [X] ", fixed = TRUE)
tmp_boxes <- unlist(lapply(tmp_boxes, function(x) x[2]))
tmp_boxes
}
issue_body$toolbox_drawers <-
format_checkboxes(issue_body$toolbox_drawers)
issue_body$keywords <-
format_checkboxes(issue_body$keywords)
# format other array inputs
if(!is.null(issue_body$references)) {
tmp_refs <- issue_body$references
tmp_refs <- strsplit(tmp_refs, "(\\r)|(\\n)")[[1]]
issue_body$references <- tmp_refs
}
if(!is.null(issue_body$associated_tools)) {
tmp_tools <- issue_body$associated_tools
tmp_tools <- strsplit(tmp_tools, ",", fixed = TRUE)[[1]]
tmp_tools <- gsub("(\n)|(\r)", "", tmp_tools)
tmp_tools <- strsplit(tmp_tools, " http", fixed = TRUE)
tmp_tools <- lapply(tmp_tools, function(x) {
x[2] <- paste0("http", x[2])
names(x) <- c("name", "link")
x <- as.list(x)
# mark as singletons
x[["name"]] <- unbox(x[["name"]])
x[["link"]] <- unbox(x[["link"]])
x
})
issue_body$associated_tools <- tmp_tools
}
if(!is.null(issue_body$user_organizations)) {
tmp_orgs <- issue_body$user_organizations
tmp_orgs <- strsplit(tmp_orgs, ",\\s*")[[1]]
issue_body$user_organizations <- tmp_orgs
}
if(!is.null(issue_body$software_badges)) {
tmp_badges <- issue_body$software_badges
tmp_badges <- strsplit(tmp_badges, ",\\s*")[[1]]
issue_body$software_badges <- tmp_badges
}
# mark singletons before converting to JSON
issue_body$active_development <- unbox(issue_body$active_development)
issue_body$tool_name <- unbox(issue_body$tool_name)
issue_body$tool_abbreviation <- unbox(issue_body$tool_abbreviation)
issue_body$authors <- unbox(issue_body$authors)
issue_body$contributors <- unbox(issue_body$contributors)
issue_body$tool_logo <- unbox(issue_body$tool_logo)
issue_body$noaa_internal <- unbox(issue_body$noaa_internal)
issue_body$maintainer_name <- unbox(issue_body$maintainer_name)
issue_body$maintainer_email <- unbox(issue_body$maintainer_email)
issue_body$background_text <- unbox(issue_body$background_text)
issue_body$citation <- unbox(issue_body$citation)
issue_body$online_app_link <- unbox(issue_body$online_app_link)
issue_body$executable_link <- unbox(issue_body$executable_link)
issue_body$website_link <- unbox(issue_body$website_link)
issue_body$documentation_link <- unbox(issue_body$documentation_link)
issue_body$source_code_link <- unbox(issue_body$source_code_link)
issue_body$installation_instructions <- unbox(issue_body$installation_instructions)
issue_body$uses_github_releases <- unbox(issue_body$uses_github_releases)
issue_body$static_version_number <- unbox(issue_body$static_version_number)
issue_body$release_badge <- unbox(issue_body$release_badge)
# to write to a file:
#jsonlite::write_json(issue_body, "test.JSON", pretty = TRUE)
txt <- jsonlite::toJSON(issue_body, pretty = TRUE)
txt <- gsub("\\r", "", txt, fixed = TRUE)
markdown_txt <- paste("Automatically generated JSON for the onboarding request: ", "```",
txt, "```", sep = "\n"
)
# post json as a comment on the issue
gh("POST /repos/{owner}/{repo}/issues/{issue_number}/comments",
owner = "noaa-fisheries-integrated-toolbox",
repo = "onboard-and-update",
issue_number = ${{ github.event.issue.number }},
body = markdown_txt)
# # open an issue in the templating repo - this works great!
# issue_external <- paste(
# paste0("Onboarding request from https://github.com/noaa-fisheries-integrated-toolbox/onboard-and-update/issues/", issue_info$number),
# "Automatically generated JSON for the onboarding request: ", "```",
# txt, "```", sep = "\n")
# issue_title <- paste0("Onboard new tool from onboard-and-update: # ", issue_info$number)
# gh::gh("POST /repos/{owner}/{repo}/issues",
# owner = "noaa-fisheries-integrated-toolbox",
# repo = "toolbox_web_templating",
# title = issue_title,
# body = issue_external)
# write json to file
newjson <- "newjson"
dir.create(newjson)
writeLines(txt, file.path(newjson,paste0(issue_body$tool_abbreviation, ".json")))
shell: Rscript {0}
# - name: add new json to the toolbox web templating
# run: |
# # add json file(s)
# json <- list.files("newjson")
# json <- grep(".json", json, fixed = TRUE, value = TRUE)
# file.copy(from = file.path("newjson", json), to = file.path("model_list_dir", json), overwrite = FALSE)
# # add json file name to browse_config.json
# config <- jsonlite::read_json(file.path("Browse_config.json"), simplifyVector = TRUE)
# json_no_file_ext <- gsub(".json", "", json, fixed = TRUE)
# config$list_of_models <- c(config$list_of_models, json_no_file_ext)
# jsonlite::write_json(config, file.path("Browse_config.json"), pretty = TRUE, simplifyVector = TRUE, auto_unbox = TRUE)
# shell: Rscript {0}
# This had some issues.
# - name: submit pull request to the toolbox_web_templating repo
# uses: peter-evans/create-pull-request@v4
# with:
# token: API_TOKEN_GITHUB
# path: templating
# add-paths: templating/Browse_config.json, templating/model_list_dir/*.json
# committer: Kathryn Doering <kathryn.doering@noaa.gov>
# author: Kathryn Doering <kathryn.doering@noaa.gov>
# commit-message: 'Onboard new tool'
# branch: onboard-new-tool
# base: dev
# title: 'Onboard new tool'
# body: Auto-generated from https://github.com/noaa-fisheries-integrated-toolbox/onboard-and-update
# reviewers: k-doering-NOAA
# This didn't work perfectly, unfortunately. Generates an error when run, even though it does what we want
# - name: Pushes file to repo branch
# continue-on-error: true
# uses: dmnemec/copy_file_to_another_repo_action@main
# env:
# API_TOKEN_GITHUB: ${{ secrets.API_TOKEN_GITHUB }}
# with:
# source_file: 'Browse_config.json'
# destination_repo: 'noaa-fisheries-integrated-toolbox/toolbox_web_templating'
# destination_folder: '.'
# user_email: 'kathryn.doering@noaa.gov'
# user_name: 'k-doering-NOAA'
# commit_message: 'onboard new tool, config file'
# destination_branch: 'dev'
# destination_branch_create: 'onboarding${{github.event.issue.number}}'
# - name: Pushes file to repo branch
# continue-on-error: true
# uses: dmnemec/copy_file_to_another_repo_action@main
# env:
# API_TOKEN_GITHUB: ${{ secrets.API_TOKEN_GITHUB }}
# with:
# source_file: 'model_list_dir'
# destination_repo: 'noaa-fisheries-integrated-toolbox/toolbox_web_templating'
# destination_folder: '.'
# user_email: 'kathryn.doering@noaa.gov'
# user_name: 'k-doering-NOAA'
# commit_message: 'onboard new tool, model json files'
# destination_branch: 'onboarding${{github.event.issue.number}}'
# for some reason, this does not open the pull request
# - name: 'Create Pull Request'
# continue-on-error: true
# uses: actions/github-script@v6
# with:
# github-token: ${{ secrets.API_TOKEN_GITHUB }}
# script: |
# github.rest.pulls.create({owner: 'noaa-fisheries-integrated-toolbox',
# repo: 'toolbox_web_templating',
# title: 'Onboard new tool',
# head: 'onboarding${context.issue.number}',
# body: 'Info based on https://github.com/noaa-fisheries-integrated-toolbox/onboard-and-update/issues/${context.issue.number}'
# base: 'dev'});