-
Notifications
You must be signed in to change notification settings - Fork 46
chore: add validation for Coder Template README files #326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
42cf35d
fix: remove .icons from namespaces
Parkreiner 583ec7f
refactor: clean up variable names
Parkreiner 20dc407
docs: add function comment
Parkreiner 4b32172
docs: add more function comments
Parkreiner af2016a
docs: add more comments
Parkreiner 3ac0c62
fix: add missing validation for GitHub usernames
Parkreiner 0c0c901
fix: add required key checks for coder resources
Parkreiner ca47cfb
docs: rewrite comment for clarity
Parkreiner 7d6dd24
refactor: split up module/template validation logic
Parkreiner 28d5af5
refactor: get majority of functionality split off
Parkreiner 4b7d152
refactor: start splitting up body validation
Parkreiner 153027e
fix: update all README files to match new submission standards
Parkreiner 45cf01b
refactor: split up functionality across more files
Parkreiner 0b79965
Merge branch 'main' into mes/template-validation-2
Parkreiner f72bb25
fix: update message
Parkreiner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"strings" | ||
|
||
"golang.org/x/xerrors" | ||
) | ||
|
||
func validateCoderModuleReadmeBody(body string) []error { | ||
var errs []error | ||
|
||
trimmed := strings.TrimSpace(body) | ||
if baseErrs := validateReadmeBody(trimmed); len(baseErrs) != 0 { | ||
errs = append(errs, baseErrs...) | ||
} | ||
|
||
foundParagraph := false | ||
terraformCodeBlockCount := 0 | ||
foundTerraformVersionRef := false | ||
|
||
lineNum := 0 | ||
isInsideCodeBlock := false | ||
isInsideTerraform := false | ||
|
||
lineScanner := bufio.NewScanner(strings.NewReader(trimmed)) | ||
for lineScanner.Scan() { | ||
lineNum++ | ||
nextLine := lineScanner.Text() | ||
|
||
// Code assumes that invalid headers would've already been handled by the base validation function, so we don't | ||
// need to check deeper if the first line isn't an h1. | ||
if lineNum == 1 { | ||
if !strings.HasPrefix(nextLine, "# ") { | ||
break | ||
} | ||
continue | ||
} | ||
|
||
if strings.HasPrefix(nextLine, "```") { | ||
isInsideCodeBlock = !isInsideCodeBlock | ||
isInsideTerraform = isInsideCodeBlock && strings.HasPrefix(nextLine, "```tf") | ||
if isInsideTerraform { | ||
terraformCodeBlockCount++ | ||
} | ||
if strings.HasPrefix(nextLine, "```hcl") { | ||
errs = append(errs, xerrors.New("all .hcl language references must be converted to .tf")) | ||
} | ||
continue | ||
} | ||
|
||
if isInsideCodeBlock { | ||
if isInsideTerraform { | ||
foundTerraformVersionRef = foundTerraformVersionRef || terraformVersionRe.MatchString(nextLine) | ||
} | ||
continue | ||
} | ||
|
||
// Code assumes that we can treat this case as the end of the "h1 section" and don't need to process any further lines. | ||
if lineNum > 1 && strings.HasPrefix(nextLine, "#") { | ||
break | ||
} | ||
|
||
// Code assumes that if we've reached this point, the only other options are: | ||
// (1) empty spaces, (2) paragraphs, (3) HTML, and (4) asset references made via [] syntax. | ||
trimmedLine := strings.TrimSpace(nextLine) | ||
isParagraph := trimmedLine != "" && !strings.HasPrefix(trimmedLine, "![") && !strings.HasPrefix(trimmedLine, "<") | ||
foundParagraph = foundParagraph || isParagraph | ||
} | ||
|
||
if terraformCodeBlockCount == 0 { | ||
errs = append(errs, xerrors.New("did not find Terraform code block within h1 section")) | ||
} else { | ||
if terraformCodeBlockCount > 1 { | ||
errs = append(errs, xerrors.New("cannot have more than one Terraform code block in h1 section")) | ||
} | ||
if !foundTerraformVersionRef { | ||
errs = append(errs, xerrors.New("did not find Terraform code block that specifies 'version' field")) | ||
} | ||
} | ||
if !foundParagraph { | ||
errs = append(errs, xerrors.New("did not find paragraph within h1 section")) | ||
} | ||
if isInsideCodeBlock { | ||
errs = append(errs, xerrors.New("code blocks inside h1 section do not all terminate before end of file")) | ||
} | ||
|
||
return errs | ||
} | ||
|
||
func validateCoderModuleReadme(rm coderResourceReadme) []error { | ||
var errs []error | ||
for _, err := range validateCoderModuleReadmeBody(rm.body) { | ||
errs = append(errs, addFilePathToError(rm.filePath, err)) | ||
} | ||
if fmErrs := validateCoderResourceFrontmatter("modules", rm.filePath, rm.frontmatter); len(fmErrs) != 0 { | ||
errs = append(errs, fmErrs...) | ||
} | ||
return errs | ||
} | ||
|
||
func validateAllCoderModuleReadmes(resources []coderResourceReadme) error { | ||
var yamlValidationErrors []error | ||
for _, readme := range resources { | ||
errs := validateCoderModuleReadme(readme) | ||
if len(errs) > 0 { | ||
yamlValidationErrors = append(yamlValidationErrors, errs...) | ||
} | ||
} | ||
if len(yamlValidationErrors) != 0 { | ||
return validationPhaseError{ | ||
phase: validationPhaseReadme, | ||
errors: yamlValidationErrors, | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func validateAllCoderModules() error { | ||
const resourceType = "modules" | ||
allReadmeFiles, err := aggregateCoderResourceReadmeFiles(resourceType) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
logger.Info(context.Background(), "processing template README files", "resource_type", resourceType, "num_files", len(allReadmeFiles)) | ||
resources, err := parseCoderResourceReadmeFiles(resourceType, allReadmeFiles) | ||
if err != nil { | ||
return err | ||
} | ||
err = validateAllCoderModuleReadmes(resources) | ||
if err != nil { | ||
return err | ||
} | ||
logger.Info(context.Background(), "processed README files as valid Coder resources", "resource_type", resourceType, "num_files", len(resources)) | ||
|
||
if err := validateCoderResourceRelativeURLs(resources); err != nil { | ||
return err | ||
} | ||
logger.Info(context.Background(), "all relative URLs for READMEs are valid", "resource_type", resourceType) | ||
return nil | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.