|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bufio" |
4 | 5 | "errors" |
5 | 6 | "fmt" |
6 | 7 | "net/url" |
| 8 | + "regexp" |
7 | 9 | "strings" |
8 | 10 | ) |
9 | 11 |
|
@@ -94,25 +96,126 @@ func validateCoderResourceTags(tags []string) error { |
94 | 96 | return nil |
95 | 97 | } |
96 | 98 |
|
97 | | -func validateCoderResourceChanges(resource coderResourceReadme) []error { |
| 99 | +// Todo: This is a holdover from the validation logic used by the Coder Modules |
| 100 | +// repo. It gives us some assurance, but realistically, we probably want to |
| 101 | +// parse any Terraform code snippets, and make some deeper guarantees about how |
| 102 | +// it's structured. Just validating whether it *can* be parsed as Terraform |
| 103 | +// would be a big improvement. |
| 104 | +var terraformVersionRe = regexp.MustCompile("^\\bversion\\s+=") |
| 105 | + |
| 106 | +// This validation function definitely has risks of false positives right now, |
| 107 | +// but realistically, it's not going to cause problems for launch. The most |
| 108 | +// foolproof way to rebuild this would be to parse each README into an AST, and |
| 109 | +// then parse each Terraform code block as Terraform |
| 110 | +func validateCoderResourceReadmeBody(body string) []error { |
| 111 | + trimmed := strings.TrimSpace(body) |
| 112 | + var errs []error |
| 113 | + errs = append(errs, validateReadmeBody(trimmed)...) |
| 114 | + |
| 115 | + foundParagraph := false |
| 116 | + terraformCodeBlockCount := 0 |
| 117 | + foundTerraformVersionRef := false |
| 118 | + |
| 119 | + lineNum := 0 |
| 120 | + isInsideCodeBlock := false |
| 121 | + isInsideTerraform := false |
| 122 | + |
| 123 | + lineScanner := bufio.NewScanner(strings.NewReader(trimmed)) |
| 124 | + for lineScanner.Scan() { |
| 125 | + lineNum++ |
| 126 | + nextLine := lineScanner.Text() |
| 127 | + |
| 128 | + // Code assumes that invalid headers would've already been handled by |
| 129 | + // the base validation function, so we don't need to check deeper if the |
| 130 | + // first line isn't an h1 |
| 131 | + if lineNum == 1 && !strings.HasPrefix(nextLine, "# ") { |
| 132 | + break |
| 133 | + } |
| 134 | + |
| 135 | + if nextLine == "```" { |
| 136 | + if !isInsideCodeBlock { |
| 137 | + errs = append(errs, fmt.Errorf("line %d: found stray ``` (either an extra code block terminator, or a code block header without a specified language)", lineNum)) |
| 138 | + break |
| 139 | + } |
| 140 | + |
| 141 | + isInsideCodeBlock = false |
| 142 | + isInsideTerraform = false |
| 143 | + continue |
| 144 | + } |
| 145 | + |
| 146 | + if isInsideTerraform { |
| 147 | + foundTerraformVersionRef = foundTerraformVersionRef || terraformVersionRe.MatchString(nextLine) |
| 148 | + continue |
| 149 | + } |
| 150 | + if isInsideCodeBlock { |
| 151 | + continue |
| 152 | + } |
| 153 | + |
| 154 | + // Code assumes that we can treat this case as the end of the "h1 |
| 155 | + // section" and don't need to process any further lines |
| 156 | + if strings.HasPrefix(nextLine, "#") { |
| 157 | + break |
| 158 | + } |
| 159 | + |
| 160 | + // This is meant to catch cases like ```tf, ```hcl, and ```js |
| 161 | + if strings.HasPrefix(nextLine, "```") { |
| 162 | + isInsideCodeBlock = true |
| 163 | + isInsideTerraform = strings.HasPrefix(nextLine, "```tf") |
| 164 | + if isInsideTerraform { |
| 165 | + terraformCodeBlockCount++ |
| 166 | + } |
| 167 | + |
| 168 | + if strings.HasPrefix(nextLine, "```hcl") { |
| 169 | + errs = append(errs, fmt.Errorf("line %d: all .hcl language references must be converted to .tf", lineNum)) |
| 170 | + } |
| 171 | + |
| 172 | + continue |
| 173 | + } |
| 174 | + |
| 175 | + // Code assumes that if we've reached this point, the only other options |
| 176 | + // are: (1) empty spaces, (2) paragraphs, (3) HTML, and (4) asset |
| 177 | + // references made via [] syntax |
| 178 | + trimmedLine := strings.TrimSpace(nextLine) |
| 179 | + isParagraph := trimmedLine != "" && !strings.HasPrefix(trimmedLine, "[") && !strings.HasPrefix(trimmedLine, "<") |
| 180 | + foundParagraph = foundParagraph || isParagraph |
| 181 | + } |
| 182 | + |
| 183 | + if terraformCodeBlockCount == 0 { |
| 184 | + errs = append(errs, errors.New("did not find Terraform code block within h1 section")) |
| 185 | + } else { |
| 186 | + if terraformCodeBlockCount > 1 { |
| 187 | + errs = append(errs, errors.New("cannot have more than one Terraform code block in h1 section")) |
| 188 | + } |
| 189 | + if !foundTerraformVersionRef { |
| 190 | + errs = append(errs, errors.New("did not find Terraform code block that specifies 'version' field")) |
| 191 | + } |
| 192 | + } |
| 193 | + if !foundParagraph { |
| 194 | + errs = append(errs, errors.New("did not find paragraph within h1 section")) |
| 195 | + } |
| 196 | + |
| 197 | + return errs |
| 198 | +} |
| 199 | + |
| 200 | +func validateCoderResourceReadme(rm coderResourceReadme) []error { |
98 | 201 | var errs []error |
99 | 202 |
|
100 | | - if err := validateReadmeBody(resource.body); err != nil { |
101 | | - errs = append(errs, addFilePathToError(resource.filePath, err)) |
| 203 | + for _, err := range validateCoderResourceReadmeBody(rm.body) { |
| 204 | + errs = append(errs, addFilePathToError(rm.filePath, err)) |
102 | 205 | } |
103 | 206 |
|
104 | | - if err := validateCoderResourceDisplayName(resource.frontmatter.DisplayName); err != nil { |
105 | | - errs = append(errs, addFilePathToError(resource.filePath, err)) |
| 207 | + if err := validateCoderResourceDisplayName(rm.frontmatter.DisplayName); err != nil { |
| 208 | + errs = append(errs, addFilePathToError(rm.filePath, err)) |
106 | 209 | } |
107 | | - if err := validateCoderResourceDescription(resource.frontmatter.Description); err != nil { |
108 | | - errs = append(errs, addFilePathToError(resource.filePath, err)) |
| 210 | + if err := validateCoderResourceDescription(rm.frontmatter.Description); err != nil { |
| 211 | + errs = append(errs, addFilePathToError(rm.filePath, err)) |
109 | 212 | } |
110 | | - if err := validateCoderResourceTags(resource.frontmatter.Tags); err != nil { |
111 | | - errs = append(errs, addFilePathToError(resource.filePath, err)) |
| 213 | + if err := validateCoderResourceTags(rm.frontmatter.Tags); err != nil { |
| 214 | + errs = append(errs, addFilePathToError(rm.filePath, err)) |
112 | 215 | } |
113 | 216 |
|
114 | | - for _, err := range validateCoderResourceIconURL(resource.frontmatter.IconURL) { |
115 | | - errs = append(errs, addFilePathToError(resource.filePath, err)) |
| 217 | + for _, err := range validateCoderResourceIconURL(rm.frontmatter.IconURL) { |
| 218 | + errs = append(errs, addFilePathToError(rm.filePath, err)) |
116 | 219 | } |
117 | 220 |
|
118 | 221 | return errs |
|
0 commit comments