Skip to content

Commit

Permalink
feat: add ignored labels option (#29)
Browse files Browse the repository at this point in the history
* feat: add ignored labels option

* feat: add pull request labeling process

* docs: update documentation
  • Loading branch information
adrianiy committed Oct 17, 2021
1 parent b9ac996 commit 9887eef
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 12 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ This project was born to automate the communication between developers and final
- The parser will use Pull Request labels for:
- **Filter** wich labels will be used in `RELEASE-NOTES.md`. i.e: `type/releas-note`
- **Mark** Release note section as feature, bug, refactor, etc.
- If you set `publish: true` flag all changes will be commited to your repo.
- If you set `publish: true`
- Flag all changes will be commited to your repo.
- Tag pull requests with `in-release-note` label.


## Supported Repos
Expand Down Expand Up @@ -62,6 +64,7 @@ We support `.yml` and `.json` formats with these options:
| out | `'.'` | Base path where `RELEASE-NOTES` will be generated |
| name | `RELEASE-NOTES` | Output file name |
| labels | `[ 'release-note' ]` | Only PRs with these labels will be used in generation process |
| ignoredLabels | `[ 'in-release-note' ]` | PRs with these labels will be ignored |
| split | `false` | If `true` one file will be generated per iteration, and will be stored under a `release_notes` folder in `out` directory |
| publish | `false` | If `true` the output file will be commited to repo |
| message | `chore: update RELEASE-NOTES` | Commit message |
Expand Down
3 changes: 3 additions & 0 deletions src/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export interface Configuration {
name?: string;
// Only PRs with this labels will be included in MD
labels?: string[];
// PRs with these labels will be ignores
ignoredLabels: string[];
// Split Release-Notes on file per Relase
// This option will create a folder in `out` dir.
split?: boolean;
Expand All @@ -36,6 +38,7 @@ const defaultConfiguration: Configuration = {
out: '.',
name: 'RELEASE-NOTES',
labels: ['release-note'],
ignoredLabels: ['in-release-note'],
publish: false,
message: 'chore: update RELEASE-NOTES',
branch: 'main',
Expand Down
3 changes: 2 additions & 1 deletion src/connector/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ export abstract class Connector {
abstract getLatestRelease(): Promise<Release>;
abstract getPullRequests(since?: string): Promise<PullRequest[]>;
abstract publishChanges(file: string): Promise<void>;
abstract updatePullRequest(pullRequest: PullRequest): Promise<void>;

async connect(): Promise<void> {
if (this._customAuth || this._interactive) {
const { token } = await inquirer.prompt([
{
name: 'token',
message: `Enter your acces token. If empty we'll use env (${this._defaultOptions.token}): `,
message: `Enter your acces token. If empty we'll use env (${this._defaultOptions.token}) or config. token: `,
type: 'password',
},
]);
Expand Down
18 changes: 16 additions & 2 deletions src/connector/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,21 @@ export class GitHubConnector extends Connector {
const labels = this._getLabelFilter();
const query = prQuery.loc!.source.body;
const created = since ? `created:>${since}` : '';
const queryString = `repo:${this._owner}/${this._repo} is:closed is:pr ${labels} ${created}`;
const queryString = `repo:${this._owner}/${this._repo} is:open is:pr ${labels} ${created}`;
const response: PullRequestResponse[] = await this._paginatedResponse<PullRequestResponse>(query, { queryString });

return response.map(this._parsePullRequest);
}

updatePullRequest = async (pullRequest: PullRequest): Promise<void> => {
await this._connection.rest.issues.addLabels({
owner: this._owner,
repo: this._repo,
issue_number: pullRequest.number,
labels: ['in-release-note'],
});
};

async publishChanges(file: string): Promise<void> {
const filePath = file.replace('./', '');
const sha = await this._getSha(filePath);
Expand All @@ -84,7 +93,12 @@ export class GitHubConnector extends Connector {
}

private _getLabelFilter(): string {
return this._configuration.labels?.map((value: string) => `label:${value}`).join(' ') || '';
const { labels, ignoredLabels } = this._configuration;

const labelFilter = labels?.map((value: string) => `label:${value}`).join(' ') || '';
const ignoredLabelsFilter = ignoredLabels?.map((value: string) => `-label:${value}`).join(' ') || '';

return `${labelFilter} ${ignoredLabelsFilter}`;
}

private _parsePullRequest(pr: PullRequestResponse): PullRequest {
Expand Down
12 changes: 5 additions & 7 deletions src/connector/models/pullRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ export interface LabelNode {
export interface Labels {
nodes: LabelNode[];
}
export interface PullRequestResponse {
title: string;
body: string;
labels: Labels;
createdAt: string;
}
export interface PullRequest {
number: number;
title: string;
body: string;
labels: string[];
labels: string[] | any;
createdAt: string;
}
export interface PullRequestResponse extends PullRequest {
labels: Labels;
}
1 change: 1 addition & 0 deletions src/connector/queries/pull_requests.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ query PRQuery($queryString: String!, $cursor: String) {
cursor
node {
... on PullRequest {
number
title
body
labels(first: 100) {
Expand Down
23 changes: 23 additions & 0 deletions src/generator/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export abstract class Generator {
const list = await this._getPullRequestList();
const markdown = this._parsePullRequests(list);

await this._labelPullRequests(list);

this._storeMarkdown(markdown);
}

Expand Down Expand Up @@ -75,6 +77,27 @@ export abstract class Generator {
return pullRequestsList;
}

protected async _labelPullRequests(pullRequests: PullRequest[]): Promise<void> {
if (this._configuration.publish) {
let willPublish = true;
if (this._interactive) {
const { response } = await inquirer.prompt([
{
name: 'response',
type: 'confirm',
message: 'Do you want to label pull requests with in-release-note label?',
},
]);

willPublish = response;
}

if (willPublish) {
await Promise.all(pullRequests.map(this._connector.updatePullRequest));
}
}
}

protected _loadMarkdown(): string {
try {
const file = fs.readFileSync(path.join(this._filePath), 'utf8');
Expand Down
2 changes: 1 addition & 1 deletion src/generator/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ export class GithubGenerator extends Generator {
const icon = pr.labels.find((label: string) => decoration[label]) || '';
const date = format(new Date(pr.createdAt), 'yyyy-MM-dd');

return `## ${decoration[icon]}${pr.title} \n###### ${date}\n\n${pr.body}\n`;
return `## ${decoration[icon] || ''}${pr.title} \n###### ${date}\n\n${pr.body}\n`;
};
}

0 comments on commit 9887eef

Please sign in to comment.