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

Emit <!--Start PulumiCodeChooser --> instead of {{% examples %}} #1689

Merged
merged 29 commits into from
Mar 7, 2024

Conversation

guineveresaenger
Copy link
Contributor

@guineveresaenger guineveresaenger commented Feb 21, 2024

IMPORTANT This cannot be merged until the bridge has updated pulumi/pulumi to a version that includes these changes.

After that, merging will be safe.

Note: pulumi/pulumi#15475 needs to be merged first for these changes to be safe.

This PR aims to remove the nested "Example Usage" logic from the bridge, as discussed here.

The issue this is aimed at fixing is pulumi/pulumi-aws#2624 but there should be a myriad of other issues that will be addressed by this change.

In a nutshell, this PR removes any {{% example(s) }} injection from the Description fields of a resource and instead detects any and all code conversions from TF/HCL --> pulumi languages, and surrounds them with an HTML comment.
This is currently

const (
	startPulumiCodeChooser = "<!--Start PulumiCodeChooser -->"
	endPulumiCodeChooser   = "<!--End PulumiCodeChooser -->"
)

Marking up any converted code blocks using the new format will serve only as a marker for registry docsgen to surround this converted code with a language chooser.

I refrained from going full Markdown parser because I believe it would be an even larger rewrite. Instead, we detect code fences in the docstring input, and append text and converted code section to the output, with the following caveats:

  • If a code block is part of a Section (i.e. has a Markdown header), if we encounter errors in code conversion, the new functionality strips the entire section automatically. Non-headered code blocks with such an error will also continue to be stripped.
  • Using indices rather than line splitting allows us to get rid of the awkward <break> placeholders for the Import section.
  • The Import section no longer needs special handling in convertExamplesInner: it is valid code and will be retained as such
  • With a little bit of extra parsing of the code fence syntax highlighter, we can capture a few more cases of perfectly valid json or yaml that we have elided previously as "failure to convert from TF".
  • With the removal of examples, titles (which never actually made it into the schema) are now obsolete. We will refer to resource paths when logging conversion failures. We have done so all along when there was no exampleTitle, and this change affects only the wording of logging output.
  • The isFrontMatter variable was removed as well. To the best of my knowledge, Hugo front matter does not get piped through the bridge, and is generated by the registry instead.
  • Handwritten docs continue to bypass convertExamplesInner, with an added caveat that both third party and our own docs may add the old example shortcodes. This change will allow us to remove some of these. Registry docsgen will continue to support the shortcode way of doing things, since native providers conform to that expectation separately.

@guineveresaenger guineveresaenger requested review from iwahbe and a team February 21, 2024 04:38
Copy link
Contributor

@VenelinMartinov VenelinMartinov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks for waddling through this and making the changes!

I had a few questions to help me understand the code and I think it'd be great if we can split up the new parsing logic into a few functions to make it easier to understand!

Also, do you think it'd make sense to add a few more regression tests? Maybe from some non-aws providers or some of the more visited docs pages?

Lastly, do the new HTML comments require a PR somewhere else before we can ship this?

pkg/tfgen/docs.go Outdated Show resolved Hide resolved
// to reconvert them. But we need to surround them in the examples shortcode for rendering on the registry
// to reconvert them.
//
//TODO: This only works if the incoming docs already have an {{% example }} shortcode, and if they are
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this introduce a regression in this PR? Or is this a TODO for the future?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a regression. This is an issue where third party (and internal) docs writers are working with the current status quo and marking up their docs with shortcodes.
See here for an internal example.
The TODO here is to write code to handle expected legacy behavior on users' behalf, or ask them to migrate.
Currently, the registry will continue to accept old shortcode behavior, as many native providers expect to use them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you open an issue for this and link it with the TODO.

inCodeBlock = false
}
} else {
// Keep track of header locations. These should never be inside code blocks.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add an error if this assumption is violated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I was trying to say was "we never look for a header inside a code block" - describing behavior, not assumption.
The new code is a bit more clear - we never look for a header if we are inside a code block.

pkg/tfgen/docs.go Outdated Show resolved Hide resolved
pkg/tfgen/docs.go Outdated Show resolved Hide resolved
pkg/tfgen/docs.go Outdated Show resolved Hide resolved
header, wroteHeader := section[0], false
isFrontMatter, isExampleUsage := !strings.HasPrefix(header, "## "), header == "## Example Usage"
// Now we know where the headers and code fences are.
textStart := 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's quite a few interactions and context one needs to keep when reading here, would this also make sense to be split into a few functions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This 🙏

if output.Len() > 0 {
fprintf(output, "\n")
} else {
// Take already-valid code blocks as-is.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this covered by a regression test?

@VenelinMartinov
Copy link
Contributor

Okay, I think you've opened pulumi/pulumi#15475 to parse the new HTML comments, great!

@@ -25,5 +25,5 @@
"ips": "The set of IPs on the Address Map.",
"memberships": "Zones and Accounts which will be assigned IPs on this Address Map."
},
"Import": "## Import\n\n```sh\u003cbreak\u003e $ pulumi import MISSING_TOK example \u003caccount_id\u003e/\u003caddress_map_id\u003e \u003cbreak\u003e```\u003cbreak\u003e\u003cbreak\u003e"
"Import": "## Import\n\n```sh\n$ pulumi import MISSING_TOK example \u003caccount_id\u003e/\u003caddress_map_id\u003e\n```\n\n"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remaining issue with MISSING_TOK? Should that be an error instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to file an issue here but it's out of scope for this change I think!

pkg/tfgen/docs.go Show resolved Hide resolved
pkg/tfgen/docs.go Outdated Show resolved Hide resolved
pkg/tfgen/docs.go Outdated Show resolved Hide resolved
pkg/tfgen/docs.go Outdated Show resolved Hide resolved
pkg/tfgen/docs.go Outdated Show resolved Hide resolved
pkg/tfgen/docs.go Outdated Show resolved Hide resolved
pkg/tfgen/docs.go Outdated Show resolved Hide resolved
pkg/tfgen/docs.go Outdated Show resolved Hide resolved
pkg/tfgen/docs.go Outdated Show resolved Hide resolved
Copy link
Contributor

@VenelinMartinov VenelinMartinov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Member

@iwahbe iwahbe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

github-merge-queue bot pushed a commit to pulumi/pulumi that referenced this pull request Feb 23, 2024
This pull request depends on corresponding changes in the
pulumi-terraform bridge.

The bridge will mark up each code section it finds (not just ones found
under an "Example Usage" header) with an HTML comment - currently this
will be `<!--Begin Code Chooser -->` and `<!--End Code Chooser -->` but
the name is subject to change.

In this PR, we are adding logic to detect if an incoming Description
block has an `{{% examples }}` shortcode markup: if yes, we will process
it as before. If no, we will switch to new functionality that relies on
documentation with code sections having no short codes, no "Examples
Section" logic, and will generate every code section it finds in an
incoming schema that is in the Description/Content section of a registry
page with a chooser and language choosables.

This work is part of pulumi/pulumi-aws#2624.


Edit: unit tests are added

This PR is a prerequisite for
pulumi/pulumi-terraform-bridge#1689. It aims to
support both legacy and new behavior.



<!--- 
Thanks so much for your contribution! If this is your first time
contributing, please ensure that you have read the
[CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md)
documentation.
-->

# Description

<!--- Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context. -->

Fixes # (issue)

## Checklist

- [x] I have run `make tidy` to update any new dependencies
- [x] I have run `make lint` to verify my code passes the lint check
  - [x] I have formatted my code using `gofumpt`

<!--- Please provide details if the checkbox below is to be left
unchecked. -->
- [x] I have added tests that prove my fix is effective or that my
feature works
<!--- 
User-facing changes require a CHANGELOG entry.
-->
- [ ] I have run `make changelog` and committed the
`changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the
Pulumi Cloud,
then the service should honor older versions of the CLI where this
change would not exist.
You must then bump the API version in
/pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi
Cloud API version
<!-- @pulumi employees: If yes, you must submit corresponding changes in
the service repo. -->
@guineveresaenger guineveresaenger changed the title Refactor: convertExamplesInner [DO NOT MERGE] Refactor: convertExamplesInner Feb 23, 2024
@VenelinMartinov
Copy link
Contributor

VenelinMartinov commented Feb 26, 2024

Is this blocked by #1699? From the description there it looks like we'd be regressing in some providers if we merge this.

@t0yv0
Copy link
Member

t0yv0 commented Feb 28, 2024

Note: pulumi/pulumi#15475 needs to be merged first for these changes to be safe.

I don't know about 1699.

@iwahbe
Copy link
Member

iwahbe commented Feb 28, 2024

Note: pulumi/pulumi#15475 needs to be merged first for these changes to be safe.

I don't know about 1699.

I don't believe that it is blocked on #1699.

@VenelinMartinov
Copy link
Contributor

VenelinMartinov commented Feb 28, 2024

Yup, I confirmed it with the docs team - #1699 is not a blocker but the bridge has yet to pick up pulumi/pulumi#15475

…re they are written in. Still needs: how have we been adding short codes to hand written docs?
…r the TF markdown with less nesting and fewer tracking variables. This also removes the need for <break> placeholders in the import section processing.

However, every Description entry now ends with a double newline and I'm not yet sure why.
Copy link
Contributor

@VenelinMartinov VenelinMartinov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@iwahbe iwahbe changed the title [DO NOT MERGE] Refactor: convertExamplesInner Emit <!--Start PulumiCodeChooser --> instead of {{% examples %}} Mar 7, 2024
Copy link

codecov bot commented Mar 7, 2024

Codecov Report

Attention: Patch coverage is 70.81081% with 54 lines in your changes are missing coverage. Please review.

Project coverage is 59.19%. Comparing base (b364656) to head (532a693).

Files Patch % Lines
pkg/tfgen/docs.go 68.78% 53 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1689      +/-   ##
==========================================
- Coverage   59.75%   59.19%   -0.56%     
==========================================
  Files         301      308       +7     
  Lines       42046    42471     +425     
==========================================
+ Hits        25123    25141      +18     
- Misses      15490    15903     +413     
+ Partials     1433     1427       -6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@iwahbe iwahbe merged commit cf13280 into master Mar 7, 2024
9 checks passed
@iwahbe iwahbe deleted the guin/missing-examples branch March 7, 2024 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants