Skip to content
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

atlasaction: codeblock with markdown #281

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions atlasaction/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ var (
},
"join": strings.Join,
"codeblock": func(lang, code string) string {
return fmt.Sprintf("<pre lang=%q><code>%s</code></pre>", lang, code)
return fmt.Sprintf("\n\n```%s\n%s\n```\n\n", lang, strings.Trim(code, "\n"))
},
"details": func(label, details string) string {
return fmt.Sprintf("<details><summary>%s</summary>%s</details>", label, details)
Expand All @@ -1154,7 +1154,6 @@ var (
// clicking on the image to view the full size.
return fmt.Sprintf(`<picture><source media="(prefers-color-scheme: light)" srcset=%q><img %s/></picture>`, src, attrs), nil
},
"trimRight": strings.TrimRight,
}).
ParseFS(comments, "comments/*.tmpl"),
)
Expand Down
4 changes: 2 additions & 2 deletions atlasaction/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1728,7 +1728,7 @@ func TestApplyTemplateGeneration(t *testing.T) {
End: must(time.Parse(time.RFC3339, "2024-06-16T15:27:38.963743+03:00")),
},
// language=markdown
expected: "<h2><picture><source media=\"(prefers-color-scheme: light)\" srcset=\"https://release.ariga.io/images/assets/success.svg?v=1\"><img width=\"22px\" height=\"22px\" src=\"https://release.ariga.io/images/assets/success.svg?v=1\"/></picture> Migration Passed</h2><h4><code>atlas migrate apply</code> Summary:</h4>\n<table>\n <tr>\n <th>Parameter</th>\n <th>Details</th>\n </tr>\n <tr>\n <td>Migration Directory</td>\n <td><code>testdata/migrations</code></td>\n </tr>\n <tr>\n <td>Database URL</td>\n <td><code>sqlite://file?_fk=1&mode=memory</code></td>\n </tr>\n <tr>\n <td>Migrate to Version</td>\n <td>\n <code>20221108173658</code>\n </td>\n </tr>\n <tr>\n <td>SQL Summary</td>\n <td>2 migration files, 3 statements passed</td>\n </tr>\n <tr>\n <td>Total Time</td>\n <td>54.297ms</td>\n </tr>\n</table><h4>Version 20221108173626.sql:</h4>\n<table>\n <tr>\n <th>Status</th>\n <th>Executed Statements</th>\n <th>Execution Time</th>\n <th>Error</th>\n <th>Error Statement</th>\n </tr>\n <tr><td><div align=\"center\"><picture><source media=\"(prefers-color-scheme: light)\" srcset=\"https://release.ariga.io/images/assets/success.svg?v=1\"><img width=\"20px\" height=\"20px\" src=\"https://release.ariga.io/images/assets/success.svg?v=1\"/></picture></div></td><td>2</td>\n <td>25.765ms</td><td>-</td><td>-</td></tr>\n</table><details><summary>📄 View SQL Statements</summary><pre lang=\"sql\"><code>CREATE TABLE `dept_emp_latest_date` (`emp_no` int NOT NULL, `from_date` date NULL, `to_date` date NULL) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci COMMENT \"VIEW\";\nCREATE TABLE `employees` (`emp_no` int NOT NULL, `birth_date` date NOT NULL, `first_name` varchar(14) NOT NULL, `last_name` varchar(16) NOT NULL, `gender` enum('M','F') NOT NULL, `hire_date` date NOT NULL, PRIMARY KEY (`emp_no`)) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci;</code></pre></details><h4>Version 20221108173658.sql:</h4>\n<table>\n <tr>\n <th>Status</th>\n <th>Executed Statements</th>\n <th>Execution Time</th>\n <th>Error</th>\n <th>Error Statement</th>\n </tr>\n <tr><td><div align=\"center\"><picture><source media=\"(prefers-color-scheme: light)\" srcset=\"https://release.ariga.io/images/assets/success.svg?v=1\"><img width=\"20px\" height=\"20px\" src=\"https://release.ariga.io/images/assets/success.svg?v=1\"/></picture></div></td><td>1</td>\n <td>23.4ms</td><td>-</td><td>-</td></tr>\n</table><details><summary>📄 View SQL Statements</summary><pre lang=\"sql\"><code>CREATE TABLE `employees` (`emp_no` int NOT NULL, `birth_date` date NOT NULL, `first_name` varchar(14) NOT NULL, `last_name` varchar(16) NOT NULL, `gender` enum('M','F') NOT NULL, `hire_date` date NOT NULL, PRIMARY KEY (`emp_no`)) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci;</code></pre></details>",
expected: "<h2><picture><source media=\"(prefers-color-scheme: light)\" srcset=\"https://release.ariga.io/images/assets/success.svg?v=1\"><img width=\"22px\" height=\"22px\" src=\"https://release.ariga.io/images/assets/success.svg?v=1\"/></picture> Migration Passed</h2><h4><code>atlas migrate apply</code> Summary:</h4>\n<table>\n <tr>\n <th>Parameter</th>\n <th>Details</th>\n </tr>\n <tr>\n <td>Migration Directory</td>\n <td><code>testdata/migrations</code></td>\n </tr>\n <tr>\n <td>Database URL</td>\n <td><code>sqlite://file?_fk=1&mode=memory</code></td>\n </tr>\n <tr>\n <td>Migrate to Version</td>\n <td>\n <code>20221108173658</code>\n </td>\n </tr>\n <tr>\n <td>SQL Summary</td>\n <td>2 migration files, 3 statements passed</td>\n </tr>\n <tr>\n <td>Total Time</td>\n <td>54.297ms</td>\n </tr>\n</table><h4>Version 20221108173626.sql:</h4>\n<table>\n <tr>\n <th>Status</th>\n <th>Executed Statements</th>\n <th>Execution Time</th>\n <th>Error</th>\n <th>Error Statement</th>\n </tr>\n <tr><td><div align=\"center\"><picture><source media=\"(prefers-color-scheme: light)\" srcset=\"https://release.ariga.io/images/assets/success.svg?v=1\"><img width=\"20px\" height=\"20px\" src=\"https://release.ariga.io/images/assets/success.svg?v=1\"/></picture></div></td><td>2</td>\n <td>25.765ms</td><td>-</td><td>-</td></tr>\n</table><details><summary>📄 View SQL Statements</summary>\n\n```sql\nCREATE TABLE `dept_emp_latest_date` (`emp_no` int NOT NULL, `from_date` date NULL, `to_date` date NULL) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci COMMENT \"VIEW\";\nCREATE TABLE `employees` (`emp_no` int NOT NULL, `birth_date` date NOT NULL, `first_name` varchar(14) NOT NULL, `last_name` varchar(16) NOT NULL, `gender` enum('M','F') NOT NULL, `hire_date` date NOT NULL, PRIMARY KEY (`emp_no`)) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci;\n```\n\n</details><h4>Version 20221108173658.sql:</h4>\n<table>\n <tr>\n <th>Status</th>\n <th>Executed Statements</th>\n <th>Execution Time</th>\n <th>Error</th>\n <th>Error Statement</th>\n </tr>\n <tr><td><div align=\"center\"><picture><source media=\"(prefers-color-scheme: light)\" srcset=\"https://release.ariga.io/images/assets/success.svg?v=1\"><img width=\"20px\" height=\"20px\" src=\"https://release.ariga.io/images/assets/success.svg?v=1\"/></picture></div></td><td>1</td>\n <td>23.4ms</td><td>-</td><td>-</td></tr>\n</table><details><summary>📄 View SQL Statements</summary>\n\n```sql\nCREATE TABLE `employees` (`emp_no` int NOT NULL, `birth_date` date NOT NULL, `first_name` varchar(14) NOT NULL, `last_name` varchar(16) NOT NULL, `gender` enum('M','F') NOT NULL, `hire_date` date NOT NULL, PRIMARY KEY (`emp_no`)) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci;\n```\n\n</details>",
},
{
name: "2 files, 1 statement error",
Expand Down Expand Up @@ -1794,7 +1794,7 @@ func TestApplyTemplateGeneration(t *testing.T) {
Error: "sql/migrate: executing statement \"create Table Err?\" from version \"20240616125213\": Error 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1",
},
// language=markdown
expected: "<h2><picture><source media=\"(prefers-color-scheme: light)\" srcset=\"https://release.ariga.io/images/assets/error.svg?v=1\"><img width=\"22px\" height=\"22px\" src=\"https://release.ariga.io/images/assets/error.svg?v=1\"/></picture> Migration Failed</h2><h4><code>atlas migrate apply</code> Summary:</h4>\n<table>\n <tr>\n <th>Parameter</th>\n <th>Details</th>\n </tr>\n <tr>\n <td>Migration Directory</td>\n <td><code>testdata/migrations</code></td>\n </tr>\n <tr>\n <td>Database URL</td>\n <td><code>mysql://localhost:3306/test?parseTime=true</code></td>\n </tr>\n <tr>\n <td>Migrate from Version</td>\n <td><code>20221108143624</code></td>\n </tr>\n <tr>\n <td>Migrate to Version</td>\n <td>\n <code>20221108173658</code>\n </td>\n </tr>\n <tr>\n <td>SQL Summary</td>\n <td>2 migration files, 2 statements passed, 1 failed</td>\n </tr>\n <tr>\n <td>Total Time</td>\n <td>54.297ms</td>\n </tr>\n</table><h4>Version 20221108173626.sql:</h4>\n<table>\n <tr>\n <th>Status</th>\n <th>Executed Statements</th>\n <th>Execution Time</th>\n <th>Error</th>\n <th>Error Statement</th>\n </tr>\n <tr><td><div align=\"center\"><picture><source media=\"(prefers-color-scheme: light)\" srcset=\"https://release.ariga.io/images/assets/success.svg?v=1\"><img width=\"20px\" height=\"20px\" src=\"https://release.ariga.io/images/assets/success.svg?v=1\"/></picture></div></td><td>1</td>\n <td>25.765ms</td><td>-</td><td>-</td></tr>\n</table><details><summary>📄 View SQL Statements</summary><pre lang=\"sql\"><code>CREATE TABLE Persons ( PersonID int );</code></pre></details><h4>Version 20221108173658.sql:</h4>\n<table>\n <tr>\n <th>Status</th>\n <th>Executed Statements</th>\n <th>Execution Time</th>\n <th>Error</th>\n <th>Error Statement</th>\n </tr>\n <tr><td><div align=\"center\"><picture><source media=\"(prefers-color-scheme: light)\" srcset=\"https://release.ariga.io/images/assets/error.svg?v=1\"><img width=\"20px\" height=\"20px\" src=\"https://release.ariga.io/images/assets/error.svg?v=1\"/></picture></div></td><td>1</td>\n <td>23.4ms</td><td>Error 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1</td><td><details><summary>📄 View</summary><pre lang=\"sql\"><code>create Table Err?</code></pre></details></td></tr>\n</table><details><summary>📄 View SQL Statements</summary><pre lang=\"sql\"><code>create Table Err?</code></pre></details>",
expected: "<h2><picture><source media=\"(prefers-color-scheme: light)\" srcset=\"https://release.ariga.io/images/assets/error.svg?v=1\"><img width=\"22px\" height=\"22px\" src=\"https://release.ariga.io/images/assets/error.svg?v=1\"/></picture> Migration Failed</h2><h4><code>atlas migrate apply</code> Summary:</h4>\n<table>\n <tr>\n <th>Parameter</th>\n <th>Details</th>\n </tr>\n <tr>\n <td>Migration Directory</td>\n <td><code>testdata/migrations</code></td>\n </tr>\n <tr>\n <td>Database URL</td>\n <td><code>mysql://localhost:3306/test?parseTime=true</code></td>\n </tr>\n <tr>\n <td>Migrate from Version</td>\n <td><code>20221108143624</code></td>\n </tr>\n <tr>\n <td>Migrate to Version</td>\n <td>\n <code>20221108173658</code>\n </td>\n </tr>\n <tr>\n <td>SQL Summary</td>\n <td>2 migration files, 2 statements passed, 1 failed</td>\n </tr>\n <tr>\n <td>Total Time</td>\n <td>54.297ms</td>\n </tr>\n</table><h4>Version 20221108173626.sql:</h4>\n<table>\n <tr>\n <th>Status</th>\n <th>Executed Statements</th>\n <th>Execution Time</th>\n <th>Error</th>\n <th>Error Statement</th>\n </tr>\n <tr><td><div align=\"center\"><picture><source media=\"(prefers-color-scheme: light)\" srcset=\"https://release.ariga.io/images/assets/success.svg?v=1\"><img width=\"20px\" height=\"20px\" src=\"https://release.ariga.io/images/assets/success.svg?v=1\"/></picture></div></td><td>1</td>\n <td>25.765ms</td><td>-</td><td>-</td></tr>\n</table><details><summary>📄 View SQL Statements</summary>\n\n```sql\nCREATE TABLE Persons ( PersonID int );\n```\n\n</details><h4>Version 20221108173658.sql:</h4>\n<table>\n <tr>\n <th>Status</th>\n <th>Executed Statements</th>\n <th>Execution Time</th>\n <th>Error</th>\n <th>Error Statement</th>\n </tr>\n <tr><td><div align=\"center\"><picture><source media=\"(prefers-color-scheme: light)\" srcset=\"https://release.ariga.io/images/assets/error.svg?v=1\"><img width=\"20px\" height=\"20px\" src=\"https://release.ariga.io/images/assets/error.svg?v=1\"/></picture></div></td><td>1</td>\n <td>23.4ms</td><td>Error 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1</td><td><details><summary>📄 View</summary>\n\n```sql\ncreate Table Err?\n```\n\n</details></td></tr>\n</table><details><summary>📄 View SQL Statements</summary>\n\n```sql\ncreate Table Err?\n```\n\n</details>",
},
{
name: "no work migration",
Expand Down
9 changes: 1 addition & 8 deletions atlasaction/comments/schema-plan.tmpl
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
<h3>Atlas detected changes to the desired schema</h3>
<h4>Migration Plan {{- with .Plan.File.Link }} ({{- . | link "View on Atlas Cloud" -}}){{ end }}</h4>

{{/* Respect double new lines as they are skipped in <pre/code> blocks. */}}
{{- with .Plan.File.Migration }}
```sql
{{ trimRight . "\n" }}
```
{{- end }}

{{- with .Plan.File.Migration }}{{ codeblock "sql" . }}{{- end -}}
<h4>Atlas lint results</h4>
{{- template "schema-lint" . -}}
<hr>
Expand Down
27 changes: 21 additions & 6 deletions atlasaction/testdata/github/schema-apply-envs.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,14 @@ _GitHubActionsFileCommandDelimeter_
</tr>
<tr><td><div align="center"><picture><source media="(prefers-color-scheme: light)" srcset="https://release.ariga.io/images/assets/success.svg?v=1"><img width="20px" height="20px" src="https://release.ariga.io/images/assets/success.svg?v=1"/></picture></div></td><td>1</td>
<td>234µs</td><td>-</td><td>-</td></tr>
</table><details><summary>📄 View Pre-planned SQL Statements</summary><pre lang="sql"><code>-- Add column "c2" to table: "t4"
</table><details><summary>📄 View Pre-planned SQL Statements</summary>

```sql
-- Add column "c2" to table: "t4"
ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;
</code></pre></details>
```

</details>
<h2><picture><source media="(prefers-color-scheme: light)" srcset="https://release.ariga.io/images/assets/success.svg?v=1"><img width="22px" height="22px" src="https://release.ariga.io/images/assets/success.svg?v=1"/></picture> Migration Passed</h2><h4><code>atlas schema apply</code> Summary:</h4><table>
<tr>
<td>Database URL</td>
Expand Down Expand Up @@ -96,9 +101,14 @@ ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;
</tr>
<tr><td><div align="center"><picture><source media="(prefers-color-scheme: light)" srcset="https://release.ariga.io/images/assets/success.svg?v=1"><img width="20px" height="20px" src="https://release.ariga.io/images/assets/success.svg?v=1"/></picture></div></td><td>1</td>
<td>315µs</td><td>-</td><td>-</td></tr>
</table><details><summary>📄 View Pre-planned SQL Statements</summary><pre lang="sql"><code>-- Add column "c2" to table: "t4"
</table><details><summary>📄 View Pre-planned SQL Statements</summary>

```sql
-- Add column "c2" to table: "t4"
ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;
</code></pre></details>
```

</details>
<h2><picture><source media="(prefers-color-scheme: light)" srcset="https://release.ariga.io/images/assets/success.svg?v=1"><img width="22px" height="22px" src="https://release.ariga.io/images/assets/success.svg?v=1"/></picture> Migration Passed</h2><h4><code>atlas schema apply</code> Summary:</h4><table>
<tr>
<td>Database URL</td>
Expand Down Expand Up @@ -131,6 +141,11 @@ ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;
</tr>
<tr><td><div align="center"><picture><source media="(prefers-color-scheme: light)" srcset="https://release.ariga.io/images/assets/success.svg?v=1"><img width="20px" height="20px" src="https://release.ariga.io/images/assets/success.svg?v=1"/></picture></div></td><td>1</td>
<td>221µs</td><td>-</td><td>-</td></tr>
</table><details><summary>📄 View Pre-planned SQL Statements</summary><pre lang="sql"><code>-- Add column "c2" to table: "t4"
</table><details><summary>📄 View Pre-planned SQL Statements</summary>

```sql
-- Add column "c2" to table: "t4"
ALTER TABLE `t4` ADD COLUMN `c2` integer NOT NULL;
</code></pre></details>
```

</details>
9 changes: 7 additions & 2 deletions atlasaction/testdata/github/schema-apply-lint.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@ _GitHubActionsFileCommandDelimeter_
<tr>
<td>To Hash</td>
<td><code>Rl4lBdMkvFoGQ4xu+3sYCeogTVnamJ7bmDoq9pMXcjw=</code></td>
</tr><tr><td>Error</td><td>Rejected by review policy: errors or warnings were found</td></tr></table><details><summary>📄 View SQL Statements</summary><pre lang="sql"><code>-- Disable the enforcement of foreign-keys constraints
</tr><tr><td>Error</td><td>Rejected by review policy: errors or warnings were found</td></tr></table><details><summary>📄 View SQL Statements</summary>

```sql
-- Disable the enforcement of foreign-keys constraints
PRAGMA foreign_keys = off;
-- Drop "t4" table
DROP TABLE `t4`;
-- Enable back the enforcement of foreign-keys constraints
PRAGMA foreign_keys = on;
</code></pre></details><h4>Atlas lint results</h4><table>
```

</details><h4>Atlas lint results</h4><table>
<thead>
<tr>
<th>Status</th>
Expand Down
1 change: 0 additions & 1 deletion atlasaction/testdata/gitlab/schema-plan.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ schema plan --format {{ json . }} --context {"repo":"my-project","branch":"test-
<h3>Atlas detected changes to the desired schema</h3>
<h4>Migration Plan (<a href="http://test.atlasgo.cloud/schemas/141733920769/plans/210453397511" target="_blank">View on Atlas Cloud</a>)</h4>


```sql
-- Create "1" table
CREATE TABLE `1` (
Expand Down
Loading