MarkEdmondson1234 / googleCloudRunner

Easy R scripts on Google Cloud Platform via Cloud Run, Cloud Build and Cloud Scheduler
https://code.markedmondson.me/googleCloudRunner/
Other
80 stars 25 forks source link

regex failure when passing in sourceToBuild #172

Open muschellij2 opened 2 years ago

muschellij2 commented 2 years ago

I don't know what the purpose of the failed regex created in cr_buildtrigger_repo so I'll just show the (what I believe) to be standard use case for using googleCloudRunner::cr_buildtrigger with a GitHub repo. Making a cr_trigger_repo output with the source as the sourceToBuild as well as trigger:

repo_name = "StreamlineDataScience/targets-minimal"
github_secret = "ssh-deploy-key"

sourceToBuild = googleCloudRunner::cr_buildtrigger_repo(
  repo_name = repo_name,
  github_secret = github_secret,
  type = "github"
)
googleCloudRunner::cr_buildtrigger(
  build = "blah.yaml",
  name = "blah", 
  trigger = sourceToBuild,
  sourceToBuild = sourceToBuild)
#> Error: !grepl("[^A-Za-z0-9/.]", ref) is not TRUE

Drilldown

This drills down in different evaluations of the sub functions that are gone through before the error:

# inside cr_buildtrigger
if (!is.null(sourceToBuild)) {
  assertthat::assert_that(googleCloudRunner:::is.buildtrigger_repo(sourceToBuild))
  sourceToBuild <- googleCloudRunner:::as.gitRepoSource(sourceToBuild)
}
#> Error: !grepl("[^A-Za-z0-9/.]", ref) is not TRUE

# googleCloudRunner:::as.gitRepoSource
x = sourceToBuild
ref <- paste0("refs/heads/", x$repo$branchName)
ref
#> [1] "refs/heads/"

# 181
# GitRepoSource
!grepl("[^A-Za-z0-9/.]", ref) # regex not allowed
#> [1] TRUE

I don't know why this is excluded in your code as I don't see anything related to this and the docs don't indicate this: https://cloud.google.com/build/docs/api/reference/rest/v1/projects.locations.triggers#BuildTrigger.GitRepoSource

Created on 2022-02-11 by the reprex package (v2.0.1)

MarkEdmondson1234 commented 2 years ago

The default for cr_buildtrigger_repo() includes a Regex for the git branch, which is fine for the trigger but sourceToBuild does not accept regex only specific branches. Some useful error messages are needed here and perhaps the default .* changed or even altered when it's being used as a sourceToBuild although it's not standard anymore what the main/master branch is called.

Short term the fix is to specify the branch or tag for the sourceToBuild