Onboarding Request: Spatial Economics Toolbox for Fisheries (FishSET) #63
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# # 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'}); | |