Skip to content

Avoid hardcoded links in READMEs#938

Merged
mrodm merged 44 commits intoelastic:mainfrom
mrodm:avoid_hardcoded_links
Sep 7, 2022
Merged

Avoid hardcoded links in READMEs#938
mrodm merged 44 commits intoelastic:mainfrom
mrodm:avoid_hardcoded_links

Conversation

@mrodm
Copy link
Copy Markdown
Contributor

@mrodm mrodm commented Aug 23, 2022

Closes #948

This PR adds a new function ("url") available into Readme templates to render links based on the event and fields functions. All links will be defined in a common file that will be the source of truth.

This intended to be used in integrations repository where a specific csv file links_table.csv YAML file links_table.yml containing all the available links is defined.
This file should be placed at the root of the integrations repository and it should follow this pattern:

links:
  link_key: http://url1.test
  foo_key: http://url2.test

Being there at the root of the repository, this file (all the links) are available and shared by all the packages defined in the repository in building time (elastic-package build).

Example of usages in the README using the above file as basis:

  • README template:
    Interesting links:
    - {{ url "link_key" }}
    - {{ url "foo_key" "howto guide" }}
    
  • README generated:
    Interesting links:
    - http://url1.test
    - [howto guide](http://url2.test)
    

@mrodm mrodm added the Team:Ecosystem Label for the Packages Ecosystem team label Aug 23, 2022
@mrodm mrodm self-assigned this Aug 23, 2022
@elasticmachine
Copy link
Copy Markdown
Collaborator

elasticmachine commented Aug 23, 2022

💚 Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview preview

Expand to view the summary

Build stats

  • Start Time: 2022-09-07T14:21:26.310+0000

  • Duration: 32 min 4 sec

Test stats 🧪

Test Results
Failed 0
Passed 800
Skipped 0
Total 800

🤖 GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

@mrodm mrodm force-pushed the avoid_hardcoded_links branch from 03d8a78 to 4ca14dd Compare August 23, 2022 09:58
@elasticmachine
Copy link
Copy Markdown
Collaborator

elasticmachine commented Aug 23, 2022

🌐 Coverage report

Name Metrics % (covered/total) Diff
Packages 100.0% (33/33) 💚
Files 67.213% (82/122) 👍 0.264
Classes 62.941% (107/170) 👍 0.517
Methods 49.351% (342/693) 👎 -0.275
Lines 32.949% (3084/9360) 👎 -0.34
Conditionals 100.0% (0/0) 💚

@mrodm
Copy link
Copy Markdown
Contributor Author

mrodm commented Aug 25, 2022

/test

@mrodm mrodm marked this pull request as ready for review August 25, 2022 13:55
@mrodm mrodm requested a review from a team August 25, 2022 13:55
Copy link
Copy Markdown
Contributor

@mtojek mtojek left a comment

Choose a reason for hiding this comment

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

I'm not sure if I fully understand what's the purposes of this PR. Do you want to add a new file to a package? Is it necessary to be a .csv file?

If so, I would start with a package-spec issue to decide about the form of the file first.

@mrodm
Copy link
Copy Markdown
Contributor Author

mrodm commented Aug 26, 2022

I'm not sure if I fully understand what's the purposes of this PR. Do you want to add a new file to a package? Is it necessary to be a .csv file?

If so, I would start with a package-spec issue to decide about the form of the file first.

I see that the description of the PR lacked some information regarding this proposal as a first step to solve elastic/integrations#2652. I'll give some more information here (and I'll update the description accordingly afterwards).

With this PR, my proposal is adding a file at the root of "integrations" repository that contains all the links to elastic documentation pages. Being there, all these links will be available and shared by all the packages in building time (elastic-package build). Similarly, as License file.

As that file would be out of the package scope, at least with this design, it should not be added to the package-spec.

@mtojek
Copy link
Copy Markdown
Contributor

mtojek commented Aug 26, 2022

Yeah, but keep in mind that Elastic packages shouldn't have a strong coupling between with elastic/integrations repository. They can be stored in any place.

EDIT:

I'm wondering if we can decouple links_table.csv from elastic-package and integrations, and make it more generic. I'm thinking about a dedicated ENV which contains a path to the links_table.csv (assuming that we want that format instead of YAML). Whenever elastic-package finds the {{ url ... }}, it will look up the ENV to read the value from there.

@mrodm
Copy link
Copy Markdown
Contributor Author

mrodm commented Aug 26, 2022

I'm wondering if we can decouple links_table.csv from elastic-package and integrations, and make it more generic. I'm thinking about a dedicated ENV which contains a path to the links_table.csv (assuming that we want that format instead of YAML). Whenever elastic-package finds the {{ url ... }}, it will look up the ENV to read the value from there.

That way is way more flexible. Avoiding hard-coding the filename and the path where it should be located will help to avoid issues. Maybe later it needs to be changed to some other folder that file or just being renamed, and that should be independent of elastic-package.

In case ENV is not defined, it could be kept looking in the root of the repository.

I will look into it to update this PR accordingly. Thanks for the suggestion!

About csv vs YAML, as first step I just wanted to follow the same structure it was shown as example in the main issue (example). But definetly, this can be changed later to YAML format. Probably easier to read.

Just some other possible format using YAML:

links:
  link_key: http://url1.test
  foo_key: http://url2.test

@mrodm
Copy link
Copy Markdown
Contributor Author

mrodm commented Sep 5, 2022

Could you please add another test package which uses the links feature?

I've added new tests for generateReadme function instead of adding a new package under test/packages to try to test all the usages of the functions available in readme (fields, event and now url). These tests along with the ones to test linksDefinitionsFilePath would probably cover this.

Would it be better to add a new package? Would that test something else ?

EDIT:
Added a new packages under test/packages (readme_links) with just a manifest and the readme docs to be able to test this rendering. test-check-packages.sh script has been updated to include the ELASTIC_PACKAGE_LINKS_FILE_PATH definition.

@mrodm mrodm requested a review from mtojek September 5, 2022 16:51
Copy link
Copy Markdown
Contributor

@mtojek mtojek left a comment

Choose a reason for hiding this comment

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

Would it be better to add a new package? Would that test something else?

If you ask me, more tests are always better. The benefit of adding a package is that we can catch if the README.md changed. With unit tests, you won't catch it if the calling function suddenly disappears.

If you don't want to create a test package, you can modify an existing one.

Comment on lines +92 to +93
foo,http://url.com.test
help,http://other.url.com/help
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It should YAML right?

Comment on lines +109 to +110
foo,http://url.com.test
help,http://other.url.com/help
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same question

}

func renderReadme(fileName, packageRoot, templatePath string) ([]byte, error) {
func renderReadme(fileName, packageRoot, templatePath string, linksMap linkMap) ([]byte, error) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah, I wouldn't preoptimize it. If it becomes a bottleneck in the future, we can refactor it.

@mrodm mrodm requested a review from mtojek September 6, 2022 15:53
Copy link
Copy Markdown
Contributor

@mtojek mtojek left a comment

Choose a reason for hiding this comment

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

LGTM! Well done.

@mrodm
Copy link
Copy Markdown
Contributor Author

mrodm commented Sep 7, 2022

/test

Copy link
Copy Markdown
Member

@jsoriano jsoriano left a comment

Choose a reason for hiding this comment

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

👍

@mrodm
Copy link
Copy Markdown
Contributor Author

mrodm commented Sep 7, 2022

/test

@mrodm mrodm merged commit 20e8351 into elastic:main Sep 7, 2022
@mrodm mrodm deleted the avoid_hardcoded_links branch September 7, 2022 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Team:Ecosystem Label for the Packages Ecosystem team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a new function to render links in README files (_dev/build/docs/*md)

4 participants