Metrics actually exploit the possibility of integrating HTML and CSS into SVGs, so basically creating these images is as simple as designing static web pages. It can even handle animations and transparency.

SVGs are templated through [EJS framework](https://github.com/mde/ejs) to make the whole rendering process easier thanks to variables, conditional and loop statements. Only drawback is that it tends to make syntax coloration a bit confused because templates are often misinterpreted as HTML tags markers (`<%= "EJS templating syntax" %>`).
Images (and custom fonts) are encoded into base64 to prevent cross-origin requests, while also removing any external dependencies, although it tends to increase files sizes.
Since SVG renders differently depending on OS and browsers (system fonts, CSS support, ...), it's pretty hard to compute dynamically height. Previously, it was computed with ugly formulas, but as it wasn't scaling really well (especially since the introduction of variable content length plugins). It was often resulting in large empty blank spaces or really badly cropped image.
To solve this, metrics now spawns a [puppeteer](https://github.com/puppeteer/puppeteer) instance and directly render SVG in a browser environment (with all animations disabled). An hidden "marker" element is placed at the end of the image, and is used to resize image through its Y-offset.
Additional bonus of using pupeeter is that it can take screenshots, making it easy to convert SVGs to PNG output.
Finally, SVGs image can be optimized through [svgo](https://github.com/svg/svgo), which helps to remove unused attributes and blank space, while also reducing a bit the file size.
</details>
<details>
<summary>💬 Gathering external data from GitHub APIs and Third-Party services</summary>
Metrics mostly use GitHub APIs since it is its primary target. Most of the time, data are retrieved through GraphQL to save APIs requests, but it sometimes fallback on REST for other features. Octokit SDKs are used to make it easier.
As for other external services (Twitter, Spotify, PageSpeed, ...), metrics use their respective APIs, usually making https requests through [axios](https://github.com/axios/axios) and by following their documentation. It would be overkill to install entire SDKs for these since plugins rarely uses more than 2/3 calls.
In last resort, pupeeter is seldom used to scrap websites, though its use tends to make things slow and unstable (as it'll break upon HTML structural changes).
</details>
<details>
<summary>💬 Web instance and GitHub action similarities</summary>
Historically, metrics used to be only a web service without any customization possible. The single input was a GitHub username, and was composed of what is now `base` content (along with `languages` and `followup` plugin, which is why they can be computed without any additional queries). That's why `base` content is handled a bit differently from plugins.
As it gathered more and more plugins over time, generating a single user's metrics was becoming costly both in terms of resources but also in APIs requests. It was thus decided to switch to GitHub Action. At first, it was just a way to explore possibilities of this GitHub feature, but now it's basically the full-experience of metrics (unless you use your own self-hosted instance).
Both web instance and Action actually use the same entrypoint so they basically have the same features.
Action just format inputs into a query-like object (similarly to when url params are parsed by web instance), from which metrics compute the rendered image. It also makes testing easier, as test cases can be reused since only inputs differs.
</details>
<details>
<summary>💬 Testing and mocking</summary>
Testing is done through [jest](https://github.com/facebook/jest) framework.
While the best would be to work with real data during testing, to avoid consuming too much APIs requests for testing (and to be more planet friendly), they're [mocked](https://github.com/lowlighter/metrics/blob/master/source/app/mocks.mjs) using [JavaScript Proxies](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) and [Faker.js](https://github.com/marak/Faker.js/). Basically function calls are "trapped" and send randomly generated data from Faker.js if we're in a development environment.
</details>
## 👨💻 Extending metrics
## 👨💻 Extending metrics
This section explain how to create new templates and plugins, and contains references about used packages and project structure.
This section explains how to create new templates and plugins. It also contains references about used packages, global project structure and list which contributions on are accepted.
It also lists which contributions on main repository are accepted.
<details>
### 🤝 Accepted contributions
<summary>🤝 Accepted contributions</summary>
Thanks for wanting to help metrics growing!
Thanks for your interest in [metrics](https://github.com/lowlighter/metrics) and wanting to help it growing!
Review below which contributions are accepted:
Review below which contributions are accepted:
<table>
<table>
<tr>
<tr>
<th>Section</th>
<th>Section</th>
<th>Examples</th>
<th colspan="2">Addition</th>
<th>Addition</th>
<th colspan="2">Editions</th>
<th>Editions</th>
</tr>
</tr>
<tr>
<tr>
<td>🧩 Plugins</td>
<td>🧩 Plugins</td>
<td></td>
<td>✔️</td>
<td>✔️</td>
<td>✔️</td>
<td>✔️</td>
<td>
<ul>
<li>New plugins are welcomed provided they're not redundant with existing plugins</li>
<li>New features for existing plugins are allowed but must be optional</li>
</ul>
</td>
</tr>
</tr>
<tr>
<tr>
<td>🖼️ Templates</td>
<td>🖼️ Templates</td>
<td></td>
<td>❌</td>
<td>❌</td>
<td>⭕</td>
<td>⭕</td>
<td>
<ul>
<li>New templates are not allowed (use <a href="https://github.com/lowlighter/metrics/blob/master/source/templates/community/README.md">📕 Community templates</a> instead)</li>
<li>Templates editions are allowed for new features additions (but must remain consistent with current visuals)</li>
</ul>
</td>
</tr>
</tr>
<tr>
<tr>
<td>🧪 Tests</td>
<td>🧪 Tests</td>
<td><code>tests/metrics.test.js</code></td>
<td>✔️</td>
<td>✔️</td>
<td>✔️</td>
<td>✔️</td>
<td>
<ul>
<li>Everything that make metrics more stable is welcomed!</li>
<li>Workflows, license, readmes, etc. usually don't need to be edited</li>
</ul>
</td>
</tr>
</tr>
</table>
</table>
**Legend**
**Legend**
* ✔️: Contributions welcomed!
* ✔️: Contributions welcomed!
* ⭕: Contributions welcomed, but must be discussed first with a maintainer
* ⭕: Contributions welcomed, but must be discussed first
* ❌: Only maintainers can manage these files
* ❌: Only maintainers can manage these files
Before working on something, ensure that it isn't listed in [In progress](https://github.com/lowlighter/metrics/projects/1#column-12158618) and that no open pull requests (including drafts) already implement what you want to do.
Before working on something, ensure that it isn't already [in progress](https://github.com/lowlighter/metrics/projects/1#column-12158618) and that it will not duplicate any open pull requests (including drafts).
If you're unsure, always open an issue first to get insights and gather feedback.
If it's listed in [Roadmap and todos](https://github.com/lowlighter/metrics/projects/1) be sure to let maintainers that you're working on it. As metrics remains a side project, things being working on can change from one day to another.
Even if your changes don't get merged in [lowlighter/metrics](https://github.com/lowlighter/metrics), please don't be too sad.
Metrics is designed to be highly customizable, so you can always decide to generate metrics on your forked repository 🙂!
If you're unsure, always open an issue to obtain insights and feedback 🙂
And even if your changes don't get merged in [lowlighter/metrics](https://github.com/lowlighter/metrics), please don't be too sad.
Metrics is designed to be highly customizable, so you can always decide to generate metrics on your forked repository!
</details>
</details>
<details>
<summary>🖼️ Templates</summary>
Templates require you to be comfortable with HTML, CSS and JavaScript ([EJS](https://github.com/mde/ejs) flavored).
Metrics does not really accept contributions on [default templates](https://github.com/lowlighter/metrics/tree/master/source/templates) in order to avoid bloating main repository with a lot of templates and to keep visual consistency across all version, but fear not! Users will still be able to use your custom templates thanks to [community templates](source/templates/community)!
If you make something awesome, don't hesitate to share it!
<details>
<summary>💬 Creating a new template from scratch</summary>
Find a cool name for your template and run:
```shell
npm run quickstart -- template <template_name>
```
It will create a new folder in [`source/templates`](https://github.com/lowlighter/metrics/tree/master/source/templates) with the following files:
- A `README.md` to describe your template and document it
- An `image.svg` with base structure for rendering
- A `partials/` folder where you'll be able to implement parts of your template
- A `partials/_.json` with a JSON array listing these parts in the order you want them displayed (unless overridden by user with `config_order` option)
If needed, you can also create the following optional files:
- A `fonts.css` containing base64 encoded custom fonts
- A `styles.css` with custom CSS that'll style your template
- A `template.mjs` with additional data processing and formatting at template-level
- When your template is used through `setup_community_templates` on official releases, this is disabled by default unless user trusts it by appending `+trust` at the end of source
If inexistent, these will fallback to [`classic`](https://github.com/lowlighter/metrics/tree/master/source/templates/classic) template files.
Templates are auto-loaded based on their folder existence, so there's no need to register them somewhere.
</details>
<details>
<summary>💬 Creating <code>image.svg</code> and partials</summary>
The base structure for rendering looks like below:
-`fonts` and `style` variables will both be populated with the same content as your `fonts.css` and `styles.css` files
- (or thos of `classic` template files if inexistent)
-`partials` variable will be populated with `partials/_.json` content
- Main loop will iterate over this array to include all defined partials
-`#metrics-end` is a special HTML tag which must remain at the bottom of SVG template
- This is used to compute dynamically height through a [puppeteer](https://github.com/puppeteer/puppeteer) headless instance
- SVG height must also be set to a high number so it doesn't get cropped accidentally while [puppeteer](https://github.com/puppeteer/puppeteer) compute [element.getBoundingClientRect()](https://developer.mozilla.org/fr/docs/Web/API/Element/getBoundingClientRect)
As you can see, we exploit the fact that SVG images are able to render HTML and CSS content so designing partials is the same as creating static web pages.
[EJS](https://github.com/mde/ejs) framework is also used to programmatically create content through the help of templating tags (`<% %>`).
</details>
<details>
<summary>💬 Adding custom fonts</summary>
⚠️ This significantly increases rendered metrics filesize and thus not recommended
When using this feature, you should aim to restrict used charset to avoid including useless data.
Here's a quick step-by-step tutorial to create base64 encoded fonts:
- 1. Find a font on [fonts.google.com](https://fonts.google.com)
- Select regular, bold, italic and bold+italic fonts
- Open `embed` tab and extract `href`
- 2. Open extracted `href` in a browser and append `&text=` parameter with list of used characters
- e.g. `&text=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`
- 3. Download each font file from urls present in generated stylesheet
- 4. Convert them into base64 with `woff` format on [transfonter.org](https://transfonter.org)
- 5. Download archive and extract it
- 6. Copy content of generated stylesheet to `fonts.css`
- 7. Update your template `style.css` to use the new font
</details>
</details>
<details>
<summary>🧩 Plugins</summary>
Plugins lets add new features with additional content to rendered metrics and are coded with [JavaScript modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules).
New plugins are welcomed, but maintainers have no obligation to maintain them.
It means you (as author) may be notified about open issues regarding related plugin.
<details>
<summary>💬 Creating a new plugin from scratch</summary>
Find a cool name for your plugin and run:
```shell
npm run quickstart -- plugin <plugin_name>
```
It will create a new folder in [`source/plugins`](https://github.com/lowlighter/metrics/tree/master/source/plugins) with the following files:
- A `README.md` to describe your plugin and document it
- An `index.mjs` with minimal plugin code
- A `metadata.yml` which list plugin attributes and inputs
- A `tests.yml` for unit tests
Here are some guidelines to follow about plugins:
- They should never be dependent on output produced by other plugins (though allowed to re-use core and base data)
- It allows parallelization of plugins execution
- It avoids creating inter-dependencies which makes it confusing for both developers and users
- Use of new external dependencies should be avoided
- Adding new libraries to use only ~5% of its possibilities is just a waste
- For APIs, most of the time a few HTTP calls instead of installing a full SDK wrapper is more than sufficient
-`imports` probably already contains a library or a function that can help you achieving what you want
- It also add more unstability as it external changes are
- Use of raw commands should be avoided when (spawning sub-process)
- It allows metrics to be platform agnostic (i.e. working on most OS)
- If mandatory:
- Use [`which`](https://linux.die.net/man/1/which) detect whether command is available
- For Windows, wrap command with [WSL](https://docs.microsoft.com/windows/wsl/about)
- Errors should be handled gracefully with error messages
- Plugins arguments should **NEVER** be directly edited from inside a plugin
- These are used by all plugins, including core and base so it would create unattended side effects
- They should let end user with some customization options (limit entries, detailed output, etc.)
You'll also need to an unused [emoji](https://emojipedia.org) to use as your plugin icon.
Plugins are auto-loaded based on their folder existence, so there's no need to register them somewhere.
</details>
<details>
<summary>💬 Implementing <code>index.mjs</code> and gathering new data from external APIs</summary>
Default exported function in `index.mjs` will receive the following inputs:
-`login`, set to GitHub login
-`q`, with query parameters (formatted with dots (`.`) instead of underscores (`_`) and without `plugin_` prefix)
-`imports`, with libraries and utilitaries
-`imports.url` for [NodeJS `url` library](https://nodejs.org/api/url.html)
-`imports.os` for [NodeJS `os` library](https://nodejs.org/api/os.html)
-`imports.fs` for [NodeJS `fs` library](https://nodejs.org/api/fs.html)
-`imports.paths` for [NodeJS `paths` library](https://nodejs.org/api/paths.html)
-`imports.util` for [NodeJS `util` library](https://nodejs.org/api/util.html)
-`imports.imgb64` for [renanbastos93/image-to-base64](https://github.com/renanbastos93/image-to-base64)
-`imports.axios` for [axios/axios](https://github.com/axios/axios)
-`imports.puppeteer` for [puppeteer/puppeteer](https://github.com/puppeteer/puppeteer)
-`imports.run` is an helper to run raw command
-`imports.shuffle` is an helper to shuffle array
-`imports.__module` is an helper to find `__dirname` from a module `import.meta.url`
- And more...
-`data` and `computed`, with all data gathered from core and base
-`graphql` and `rest`, with authenticated [octokit clients](https://github.com/octokit) (for GitHub APIs)
-`queries`, with autoloaded GraphQL queries and replacers
-`account`, set to account type ("user" or "organization")
Second input contains configuration settings from [settings.json](https://github.com/lowlighter/metrics/blob/master/settings.example.json) (which is mostly used by web instances) and all also user inputs of type `token`.
As said previously, plugins arguments should **NEVER** be directly edited from it, since these are used by all plugins, including core and base so it would create unattended side effects.
As for data gathering:
- Related to GitHub, use `graphql` (for [GraphQL API](https://docs.github.com/en/graphql)) or `rest` [REST API](https://docs.github.com/en/rest)
- From Third-Party services, use [`imports.axios`](https://github.com/axios/axios) to make APIs calls
- In last resort, use `imports.puppeteer`
For GraphQL queries, use `queries` which will auto-load all queries from `queries` directory and will lets you create custom queries on the fly.
`metadata.yml` is a mandatory file which describes what inputs are allowed, which entities are supported, etc.
Here's an example:
```yaml
name:"🧩 Plugin name (with emoji icon)"
cost:Estimates how many GitHub requests is used during plugin execution ("N/A" for Third-Party services)
supports:
- user # Support users account
- organization # Support organizations account
- repository # Support repositories metrics
inputs:
# A comment detailing input purposes
# An input must have at least a "description" and a "default" (used to generated GitHub Action `action.yml`)
plugin_input:
description:Short description (few words)
type:boolean
default:no
```
Because of GitHub Actions limitations, only strings and numbers are actually supported by `action.yml` inputs.
Metrics apply additional post-processing to handle inputs.
Supported input types are `boolean`, `string`, `number`, `array` and `json`.
- Allowed values for `string` and `array` may be restricted using `values` attribute
- Special default values `.user.login`, `.user.twitter` and `.user.website` will respectively be replaced by user's login, Twitter username and website (not available when `token` is set to `NOT_NEEDED` by user )
- Lower and upper limits for `number` may be set using `min` and `max` attribute
- Array `format` attribute define how string should be splitted (`comma-separated` or `space-separated`)
You can additionally specify an `example` which will also be used in web instance input placeholder.
Inputs will be available through `imports.metadata.plugins.name.inputs` with correct typing and default values (`plugin_` prefix will be dropped, and all underscored (`_`) will be changed to dots (`.`) instead):
- If it failed, an error message instead will be displayed instead
- If it succeeded, second section in render.
Additional CSS rules may be added to `style.css` of edited template, but ensure it does not break other plugins rendering.
</details>
<details>
<summary>💬 Fast prototyping and testing</summary>
The easiest way to test and prototype your plugin is to setup a web instance. See [documentation](https://github.com/lowlighter/metrics#%EF%B8%8F-deploying-your-own-web-instance-15-min-setup-depending-on-your-sysadmin-knowledge) for more informations about that.
Open a browser and try to generate metrics with new your plugin enabled to see if it works as expected:
Once ready, define test cases in your plugin directory `tests.yml`.
These tests will be run with:
- Metrics action
- Metrics web instance
- Metrics web instance placeholder (rendered by browser)
Most APIs (including GitHub) usually have a rate-limit to ensure quality of service.
This is why APIs output must be mocked and added in [`source/app/mocks/api/`](/source/app/mocks/api) in order for tests to be able to be performed anytime.
Files from these directories are auto-loaded, so you just need to create new functions (see other mocked data for examples).
Finally, edit [source/app/web/statics/app.placeholder.js](https://github.com/lowlighter/metrics/blob/master/source/app/web/statics/app.placeholder.js) to add mocked result (but this time from metrics server) so users will be able to render placeholder preview in web instance.
</details>
<details>
<summary>💬 Submitting a pull request</summary>
Ensure that:
-`metadata.yml` is correctly filled
-`tests.yml` has defined test cases
-`mocks/api` has mocked data for external APIs
-`app.placeholder.js` has been updated with mocked plugin output
-`README.md` of plugin explain how plugin works
-`<table>` tag **MUST** remain present (along with `<img width="900" height="1" alt="">`) as these are extracted for global `README.md`
-`npm run linter` outputs no errors
-`npm test` is successful
Use `config.output` option to render a PNG version of your plugin:
And finally open a new [pull request](https://github.com/lowlighter/metrics/pulls) and ensure that all builds succeed.
Global `README.md`, `plugins/README.md`, `templates/README.md`, `action.yml` and `settings.example.json` are automatically rebuild by GitHub action, do not edit them manually.
```markdown
### 🧩 Your plugin name
<table>
<td align="center">
<img src="">
<img width="900" height="1" alt="">
</td>
</table>
#### ℹ️ Examples workflows
[➡️ Available options for this plugin](metadata.yml)
'''yaml
- uses: lowlighter/metrics@latest
with:
# ... other options
plugin_custom: yes
'''
```
Note that you **must** keep .
</details>
<details>
<details>
<summary>🗂️ Project structure</summary>
<summary>🗂️ Project structure</summary>
@@ -206,433 +522,62 @@ Below is a list of used packages.
</details>
</details>
## 🎬 Behind the scenes
This section explore some topics which explain globally how metrics was designed and how it works.
Templates requires you to be comfortable with HTML, CSS and JavaScript ([EJS](https://github.com/mde/ejs) flavored).
Metrics actually exploit the possibility of integrating HTML and CSS into SVGs, so basically creating these images is as simple as designing static web pages. It can even handle animations and transparency.
Metrics does not really accept contributions on [default templates](https://github.com/lowlighter/metrics/tree/master/source/templates) in order to avoid bloating main repository with a lot of templates, but fear not! Users will still be able to use your custom templates thanks to [community templates](source/templates/community)!

If you make something awesome, don't hesistate to share it!
SVGs are templated through [EJS framework](https://github.com/mde/ejs) to make the whole rendering process easier thanks to variables, conditional and loop statements. Only drawback is that it tends to make syntax coloration a bit confused because templates are often misinterpreted as HTML tags markers (`<%= "EJS templating syntax" %>`).
For a quick start, use:
Images (and custom fonts) are encoded into base64 to prevent cross-origin requests, while also removing any external dependencies, although it tends to increase files sizes.
```shell
npm run quickstart -- template <template_name>
```
<details>
Since SVG renders differently depending on OS and browsers (system fonts, CSS support, ...), it's pretty hard to compute dynamically height. Previously, it was computed with ugly formulas, but as it wasn't scaling really well (especially since the introduction of variable content length plugins). It was often resulting in large empty blank spaces or really badly cropped image.
<summary>💬 Creating a new template from scratch</summary>
Find a cool name for your template and create an eponym folder in [`source/templates`](https://github.com/lowlighter/metrics/tree/master/source/templates).
To solve this, metrics now spawns a [puppeteer](https://github.com/puppeteer/puppeteer) instance and directly render SVG in a browser environment (with all animations disabled). An hidden "marker" element is placed at the end of the image, and is used to resize image through its Y-offset.
-`README.md` will contain template description and documentation
-`image.svg` will contain the base render structure of your template
-`partials/` is a folder that'll contain parts of your template (called "partials")
-`partials/_.json` is a JSON array which lists your partials (these will be displayed in the same order as listed, unless if overriden by user with `config_order` option)
The following files are optional:
Additional bonus of using pupeeter is that it can take screenshots, making it easy to convert SVGs to PNG output.
-`fonts.css` can contain your custom fonts (base64 encoded) if needed
-`styles.css` can contain your CSS that'll style your template
-`template.mjs` can contain additional data processing and formatting at template-level
Optional files will fallback to the one defined in [`classic`](https://github.com/lowlighter/metrics/tree/master/source/templates/classic) template if unexistant.
Finally, SVGs image can be optimized through [svgo](https://github.com/svg/svgo), which helps to remove unused attributes and blank space, while also reducing a bit the file size.
Note that by default, `template.mjs` is skipped when using official release with community templates, to prevent malicious code to leaks token and credentials.
</details>
</details>
<details>
<details>
<summary>💬 Creating a <code>README.md</code></summary>
<summary>💬 Gathering external data from GitHub APIs and Third-Party services</summary>
Your `README.md` will document your template and explain how it works.
Metrics mostly use GitHub APIs since it is its primary target. Most of the time, data are retrieved through GraphQL to save APIs requests, but it sometimes fallback on REST for other features. Octokit SDKs are used to make it easier.
It must contain at least the following:
```markdown
### 📕 My custom template
<table>
As for other external services (Twitter, Spotify, PageSpeed, ...), metrics use their respective APIs, usually making https requests through [axios](https://github.com/axios/axios) and by following their documentation. It would be overkill to install entire SDKs for these since plugins rarely uses more than 2/3 calls.
<td align="center">
<img src="">
<img width="900" height="1" alt="">
</td>
</table>
#### ℹ️ Examples workflows
In last resort, pupeeter is seldom used to scrap websites, though its use tends to make things slow and unstable (as it'll break upon HTML structural changes).
`fonts` and `style` variables will be populated with the same content as your `fonts.css` and `styles.css` files.
Like said previously, if these does not exists, it'll contain the same content as the `classic` template files.
The main loop will iterate on `partials` variable which contains your partials index set in `_.json`.
Finally, you may have noticed that `height` is set to a very high number, and that there is a `#metrics-end` element at the bottom of the SVG template. This is because rendered height is computed dynamically through a [puppeteer](https://github.com/puppeteer/puppeteer) browser instance which locate `#metrics-end` and use its *y-coordinate* and `config_padding` to set final height. So you should leave it like this to ensure your rendered image will be correctly sized.
</details>
</details>
<details>
<details>
<summary>💬 Customizing templates with partials</summary>
<summary>💬 Web instance and GitHub action similarities</summary>
Partials are sections that'll be displayed in rendered metrics.
Historically, metrics used to be only a web service without any customization possible. The single input was a GitHub username, and was composed of what is now `base` content (along with `languages` and `followup` plugin, which is why they can be computed without any additional queries). That's why `base` content is handled a bit differently from plugins.
It's just HTML with CSS which can be templated through [EJS](https://github.com/mde/ejs) framework.
As it gathered more and more plugins over time, generating a single user's metrics was becoming costly both in terms of resources but also in APIs requests. It was thus decided to switch to GitHub Action. At first, it was just a way to explore possibilities of this GitHub feature, but now it's basically the full-experience of metrics (unless you use your own self-hosted instance).
Basically, you can use JavaScript statements in templating tags (`<% %>`) to display variables content and to programmatically create content.
Both web instance and Action actually use the same entrypoint so they basically have the same features.
Action just format inputs into a query-like object (similarly to when url params are parsed by web instance), from which metrics compute the rendered image. It also makes testing easier, as test cases can be reused since only inputs differs.
</details>
</details>
<details>
<details>
<summary>💬 Using custom fonts</summary>
<summary>💬 Testing and mocking</summary>
This is actually not recommended because it drastically increases the size of generated metrics, but it should also make your rendering more consistant. The trick is to actually restrict the charset used to keep file size small.
Testing is done through [jest](https://github.com/facebook/jest) framework.
Below is a simplified process on how to generate base64 encoded fonts to use in metrics:
While the best would be to work with real data during testing, to avoid consuming too much APIs requests for testing (and to be more planet friendly), they're [mocked](https://github.com/lowlighter/metrics/blob/master/source/app/mocks.mjs) using [JavaScript Proxies](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) and [Faker.js](https://github.com/marak/Faker.js/). Basically function calls are "trapped" and send randomly generated data from Faker.js if we're in a development environment.
- 1. Find a font on [fonts.google.com](https://fonts.google.com/)
- Select regular, bold, italic and bold+italic fonts
- Open `embed` tab and extract the `href`
- 2. Open extracted `href` and append `&text=` params with used characters from SVG
- e.g. `&text=%26%27"%7C%60%5E%40°%3F!%23%24%25()*%2B%2C-.%2F0123456789%3A%3B<%3D>ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5D_abcdefghijklmnopqrstuvwxyz%7B%7D~─└├▇□✕`
- 3. Download each font file from url links from the generated stylesheet
- 4. Convert them into base64 with `woff` extension on [transfonter.org]https://transfonter.org/) and download archive
- 5. Extract archive and copy the content of the generated stylesheet to `fonts.css`
- 6. Update your template
- Include `<defs><style><%= fonts %></style></defs>` to your `image.svg`
- Edit your `style.css` to use yout new font
</details>
</details>
<details>
<summary>🧩 Plugins</summary>
Plugins are self-sufficient and independant code functions that gather additional data from GitHub APIs or external sources.
<details>
<summary>💬 Plugin guidelines</summary>
- A plugin should never be dependent on others plugins
- But they're allowed to use data gathered by main metrics function
- Avoid the need of new external dependencies (like SDKs)
- Most of the time, SDKs are overkill when a few HTTP calls do the trick
-`imports` probably contains a library that can help you achieving what you want
- Avoid using raw command when possible (like spawning sub-process)
- Sub-process should be platform agnostic (i.e. working on most OS)
- Errors should always be handled gracefully by displaying an error message when it fails
- When possible, try to display explicit error messages
</details>
For a quick start, use:
```shell
npm run quickstart -- plugin <plugin_name>
```
<details>
<summary>💬 Creating a new plugin</summary>
Find a cool word to name your plugin and create an eponym folder in [`source/plugins`](https://github.com/lowlighter/metrics/tree/master/source/plugins) folder.
You'll also need to find an unused [emoji](https://emojipedia.org) that you'll be able to use as your plugin icon.
Then create an `index.mjs` in your plugin folder and paste the following code:
-`imports` contains libraries and utilitaries that are shared amongst plugins
-`imports.url` refers to [NodeJS `url` library](https://nodejs.org/api/url.html)
-`imports.os` refers to [NodeJS `os` library](https://nodejs.org/api/os.html)
-`imports.fs` refers to [NodeJS `fs` library](https://nodejs.org/api/fs.html)
-`imports.paths` refers to [NodeJS `paths` library](https://nodejs.org/api/paths.html)
-`imports.util` refers to [NodeJS `util` library](https://nodejs.org/api/util.html)
-`imports.imgb64` refers to [renanbastos93/image-to-base64](https://github.com/renanbastos93/image-to-base64)
-`imports.axios` refers to [axios/axios](https://github.com/axios/axios)
-`imports.puppeteer` refers to [puppeteer/puppeteer](https://github.com/puppeteer/puppeteer)
-`imports.run` is an helper to run raw command
-`imports.shuffle` is an helper to shuffle array
-`imports.__module` is an helper to find `__dirname` from a module url
- And more...
-`data` and `computed` contains all data (and computed data) gathered by various APIs from main metrics function
-`graphql` and `rest` contains [octokit clients](https://github.com/octokit) for GitHub API
-`queries` contains autoloaded GraphQL queries with replacers
-`account` contains the type of account being worked on ("user" or "organization")
The second input contains configuration settings from [settings.json](https://github.com/lowlighter/metrics/blob/master/settings.example.json), which is mostly used by web instances.
Content of these parameters should **never** be edited directly, as your plugin should only return a new result.
Plugins are autoloaded so you do not need to do anything special to register them.
</details>
<details>
<summary>💬 Gathering new data from GitHub APIs and from Third-Party services</summary>
For GitHub related data, always try to use their [GraphQL API](https://docs.github.com/en/graphql) or their [REST API](https://docs.github.com/en/rest) when possible. Use `puppeteer` in last resort.
When using GraphQL API, `queries` object autoloads queries from your plugin `queries` directory and will replace all strings prefixed by a dollar sign (`$`) with eponym variables.
For REST API, check out their [documentation](https://octokit.github.io/rest.js/v18/).
As for Third-Party services, always prefer using their APIs (you can use [`imports.axios`](https://github.com/axios/axios) for easy HTTP requests) when they exists before having recourse to [`imports.puppeteer`](https://github.com/puppeteer/puppeteer).
New external dependencies should be avoided at all costs, especially since most of the time it's overkill to setup a new SDK.
</details>
<details>
<summary>💬 Creating a partial to display your plugin result</summary>
Create new files in `partials` of `source/templates` you want to support with `.ejs` extension.
<%= plugins./* your plugin name */.error.message %>
</div>
</section>
<% } else { %>
<section>
<%# Do stuff in there -%>
</section>
<% } %>
</div>
</section>
<% } %>
```
Let's explain what it does.
First conditional statement will ensure that your partial only execute when your plugin is enabled.
The nested one will check if your plugin resulted in an error, and if that's the case, it'll display an error message instead.
Else, if it's successful, you'll get the second section in render.
Plugins errors should always be handled gracefully when possible.
If you need additional CSS rules, edits the `style.css` of edited template.
</details>
<details>
<summary>💬 Fast prototyping with web instance</summary>
The easiest way to test and prototype your plugin is to use a web instance.
Configure a [settings.json](https://github.com/lowlighter/metrics/blob/master/settings.example.json) with a valid GitHub token and with debug mode enabled.
Then start a web instance with `npm start` (you may have to run `npm install` if that's the first time you use the web instance).
Then try to generate your metrics in your browser with your GitHub user and your plugin enabled, and see if it works as expected:
<summary>💬 Registering plugin options in <code>metadata.yml</code></summary>
`metadata.yml` is a special file that will be used to parse user inputs and to generate final `action.yml`
```yaml
name:"🧩 Your plugin name"
# Estimate of how many GitHub requests will be used
cost:N/A
# Supported modes
supports:
- user
- organization
- repository
# Inputs list
inputs:
# Enable or disable plugin
plugin_custom:
description:Your custom plugin
type:boolean
default:no
```
The following types are supported:
```yaml
string:
type:string
select:
type:string
values:
- allowed-value-1
- allowed-value-2
- ...
boolean:
type:boolean
number:
type:number
ranged:
type:number
min:0
max:100
array:
type:array
format:comma-separated
array_select:
type:array
format:comma-separated
values:
- allowed-value-1
- allowed-value-2
- ...
json:
type:json
```
</details>
<details>
<summary>💬 Create mocked data and tests</summary>
Creating tests for your plugin ensure that external changes don't break it.
You can define your tests cases in `tests.yml` in your plugin directory, which will automatically test your plugin with:
- Metrics action
- Metrics web instance
- Metrics web instance placeholder (rendered by browser)
As most of APIs (including GitHub) usually have a rate-limit to ensure quality of their service.
To bypass these restrictions but still perform tests, you must mock their data which simulates APIs call returns.
Add them in [`source/app/mocks/api/`](/source/app/mocks/api) folder.
If you're using `axios` or GitHub GraphQL API, these files are autoloaded so you just need to create new functions (see other mocked data for examples).
If you're using GitHub REST API, add your mocks in [`source/app/mocks/rest`](/source/app/mocks/rest) with same path as octokit.
</details>
<details>
<summary>💬 Creating a <code>README.md</code></summary>
Your `README.md` will document your plugin and explain how it works.
It must contain at least the following:
```markdown
### 🧩 Your plugin name
<table>
<td align="center">
<img src="">
<img width="900" height="1" alt="">
</td>
</table>
#### ℹ️ Examples workflows
[➡️ Available options for this plugin](metadata.yml)
'''yaml
- uses: lowlighter/metrics@latest
with:
# ... other options
plugin_custom: yes
'''
```
Note that you **must** keep `<table>` tags as these will be extracted to autogenerated global `README.md` with your example.
<% { let previous = null; for (const [date, value] of Object.entries(plugins.stargazers.total.dates)) { const p = 0.05+0.95*(value-plugins.stargazers.total.min)/(plugins.stargazers.total.max-plugins.stargazers.total.min); const [y, m, d] = date.split("-").map(Number) %>
<% { let previous = null; for (const [date, value] of Object.entries(plugins.stargazers.total.dates)) { const p = 0.05+0.95*(value-plugins.stargazers.total.min)/(plugins.stargazers.total.max-plugins.stargazers.total.min || 1); const [y, m, d] = date.split("-").map(Number) %>
<% { let previous = null; for (const [date, value] of Object.entries(plugins.stargazers.increments.dates)) { const p = value/plugins.stargazers.increments.max; const [y, m, d] = date.split("-").map(Number) %>
<% { let previous = null; for (const [date, value] of Object.entries(plugins.stargazers.increments.dates)) { const p = value/(plugins.stargazers.increments.max || 1); const [y, m, d] = date.split("-").map(Number) %>
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.