Skip to content

Commit

Permalink
feat: add notification customization config
Browse files Browse the repository at this point in the history
* feat: add notification customization config

* feat: add notification customizer method

* chore: update git ignore

* docs: update README
  • Loading branch information
adrianiy committed Oct 30, 2021
1 parent 0efaae4 commit 04a3f7d
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

node_modules
dist
notes

# config files
.env
Expand Down
38 changes: 35 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<img alt="RNG" src="./assets/RNG.svg">
</p>

# :rocket: RELEASE NOTES GENERATOR (RNG)
<center><h1 align="center">:rocket: RELEASE NOTES GENERATOR :rocket:</h1></center>

**RELEASE NOTES GENERATOR** automates the creation of a customized release note.

Expand Down Expand Up @@ -85,6 +85,7 @@ We support `.yml` and `.json` formats with these options:
| title | `RELEASE NOTES` | Title used in output markdown |
| decoration | [Decoration object](#decoration-object) | Icon decoration for each issue type |
| webhooks | `{}` | List of [webhooks](#webhooks) to execute |
| notification | [Notification customization](#customized-notification) | Object that allows you to customize your webhook notification |


##### Webhooks
Expand Down Expand Up @@ -133,6 +134,37 @@ This is the result markdown for a pr tagged with `enhancement` label:
## :zap: Issue title
```

##### Customized notification

You can customize your webhook notification using `notification.style` attribute.

This is an object where each key is a htm tag like `<div>` with a style object as value.

By default we modify some classes to make notification more readable, we encourage yo to use this yml example as a base for your customizations

```yml
notification:
style:
h1:
font-size: 3rem
margin-top: 2rem
h2:
font-size: 2rem
margin-top: 3rem
h3:
font-size: 1.8rem
margin: 2rem 0
h6:
font-size: .9em
opacity: .7
p:
font-size: 1.4rem
li:
margin-bottom: 8px
pre:
margin-bottom: .7rem
```

##### CI Configuration

This configuration depends entirely on your necessities, just keep in mind that PRs are parsed since last release so you'll need to execute **RNG** before new release step.
Expand All @@ -159,8 +191,8 @@ If you are trying to push to a **protected_branch** you can create a personal ac
// rng --help

Comandos:
bin.js generate Generates Release Note markdown [alias: gen]
bin.js publish Pubish Release Note in your repo [alias: pub]
rng generate Generates Release Note markdown [alias: gen]
rng publish Pubish Release Note in your repo [alias: pub]

Opciones:
--version Muestra número de versión [booleano]
Expand Down
40 changes: 39 additions & 1 deletion RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,42 @@
# RELEASE NOTES
# RELEASE NOTES

# :rocket: Release 1.6.0
###### 2021-10-21



Use last n releases
###### 2021-10-21

### Changes
<!-- Specify changes you've done in your PR, be as specific as you can! :) -->

New option to process last `N` releases is available!! 🥳

```yml
useLast: 2
```

Will use the creation date of the 2nd latest release to process pull requests





Move section decoration to config
###### 2021-10-21

### Changes
<!-- Specify changes you've done in your PR, be as specific as you can! :) -->

Release sections have not the required relevance as they are decorated with `##` as any other issue.

To solve this we move section decorators `#` to configuration object, this allow users to configure as many `#` as they want.







# :rocket: Release 1.6.0
Expand Down
21 changes: 21 additions & 0 deletions src/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ export interface Webhook {
actions?: unknown[];
}

export interface Customization {
[key: string]: Record<string, string>;
}

export interface Notification {
style: Customization;
}

export interface Configuration {
// GITHUB authorization token
token?: string;
Expand Down Expand Up @@ -51,6 +59,8 @@ export interface Configuration {
decoration?: Record<string, string>;
// Webhooks lis
webhooks?: Record<string, Webhook>;
// Notification configuration
notification?: Notification;
}

const defaultConfiguration: Configuration = {
Expand All @@ -74,6 +84,17 @@ const defaultConfiguration: Configuration = {
style: '## :nailcare: ',
documentation: '## :book: ',
},
notification: {
style: {
h1: { 'font-size': '3rem', 'margin-top': '2rem' },
h2: { 'font-size': '2rem', 'margin-top': '3rem' },
h3: { 'font-size': '1.8rem', 'margin': '2rem 0' },
h6: { 'font-size': '.9em', 'opacity': '.7' },
p: { 'font-size': '1.4rem' },
li: { 'margin-bottom': '8px' },
pre: { 'margin-bottom': '.7rem' },
},
},
};

const searchExistentFileExt = (fileName: string): string | undefined => {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class PluginLoader {
}
willExecute &&
this._webhooks?.map((webhook: Webhook) => {
webhook.execute(this._filePath);
webhook.execute(this._filePath, this._configuration.notification!.style);
});
}
}
Expand Down
31 changes: 25 additions & 6 deletions src/plugins/webhooks/teams.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Webhook as IWebhook } from 'configuration/configuration';
import { Customization, Webhook as IWebhook } from 'configuration/configuration';
import { Connector } from 'connector/connector';
import { Webhook } from './webhook';
import assert from 'assert';
Expand All @@ -14,15 +14,12 @@ export class TeamsWebhook extends Webhook {
assert(webhook?.url !== undefined, 'An url must be configured in temas webhook');
}

async execute(filePath: string): Promise<void> {
async execute(filePath: string, customization?: Customization): Promise<void> {
const text = fs.readFileSync(filePath, 'utf8');

const html = await this._connector.renderMarkdown(text);

const activityText = html
.replace(/<li>/g, '<li style="margin-bottom: 8px">')
.replace(/<h6>/g, '<h6 style="opacity: .7; font-size: .9em">')
.replace(/<pre>/g, '<pre style="margin: 8px 0">');
const activityText = this._customizeHtml(html, customization);

this._verbose && logger.info('Publishing notification using teams webhook...');

Expand All @@ -44,4 +41,26 @@ export class TeamsWebhook extends Webhook {
}),
);
}

private _customizeHtml(html: string, customization?: Customization): string {
if (customization) {
return Object.entries(customization).reduce((acc, curr) => {
const [tag, styleObj] = curr;
const regexp = new RegExp(`<${tag}>`, 'g');
const style = this._getStyleString(styleObj);

return acc.replace(regexp, `<${tag} style=${JSON.stringify(style)}>`);
}, html);
} else {
return html;
}
}

private _getStyleString(styleObject: Record<string, string>): string {
return Object.entries(styleObject).reduce((acc, curr) => {
const [property, value] = curr;

return `${acc}; ${property}: ${value}`;
}, '');
}
}
4 changes: 2 additions & 2 deletions src/plugins/webhooks/webhook.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Webhook as IWebhook } from 'configuration/configuration';
import { Customization, Webhook as IWebhook } from 'configuration/configuration';
import { Connector } from 'connector/connector';

export abstract class Webhook {
Expand All @@ -12,5 +12,5 @@ export abstract class Webhook {
this._verbose = verbose!;
}

abstract execute(filePath: string): Promise<void>;
abstract execute(filePath: string, customization: Customization): Promise<void>;
}

0 comments on commit 04a3f7d

Please sign in to comment.