forked from newrelic/docs-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreleaseNotes.mjs
More file actions
executable file
·218 lines (193 loc) · 5.48 KB
/
Copy pathreleaseNotes.mjs
File metadata and controls
executable file
·218 lines (193 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#! /usr/bin/env node
import { readFile } from 'fs/promises';
import { glob } from 'glob10';
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
import { unified } from 'unified10';
import remarkParse from 'remark-parse10';
import remarkMdx from 'remark-mdx2.3';
import { visit } from 'unist-util-visit4';
import { Command } from 'commander';
import getAgentName from '../src/utils/getAgentName.js';
import getEOLDate from '../src/utils/getEOLDate.js';
import { frontmatter } from './utils/frontmatter.js';
const program = new Command();
program
.description('generate agent release note JSON')
.option('-u, --upload', 'upload resulting JSON to S3')
.option('-v, --validate, validate resulting JSON')
.parse();
const options = program.opts();
const uploadToS3 = Boolean(options.upload);
const validateJSON = Boolean(options.validate);
const excerptify = async (body) => {
const Compiler = (tree) => {
let result = '';
visit(tree, (leaf, index, parent) => {
if (leaf.type === 'text' || leaf.type === 'inlineCode') {
result += leaf.value;
if (parent.children.length - 1 === index) {
result += ' ';
}
}
});
result = result.trim();
const length = result.length;
result = result.slice(0, 5000);
if (length > 5000) result += '…';
return result;
};
const vFile = await unified()
.use(remarkParse)
.use(function () {
this.Compiler = Compiler;
})
.use(remarkMdx)
.process(body);
return vFile.value;
};
const slugify = (str) => str.replace('src/content/', '').replace('.mdx', '');
const INCLUDE_AGENTS = new Set([
'.net',
'android',
'browser',
'dotnet',
'go',
'infrastructure',
'ios',
'java',
'kubernetes',
'pipeline_control_gateway',
'agent_control_deployment_chart',
'agent_control_continuous_delivery_chart',
'node',
'nodejs',
'php',
'python',
'ruby',
'sdk',
'fluentbit',
'nrdot',
'prometheus',
'streaming_for_mobile',
'streaming_for_browser',
'streaming_for_others',
'aws_firehose_log_forwarder',
'aws_lambda_log_forwarder'
]);
const generateReleaseNoteObject = async (filePath) => {
const file = await readFile(filePath, { encoding: 'utf8' });
const slug = slugify(filePath);
const { attributes, body, error } = frontmatter(file);
if (error != null) {
console.log('❌ frontmatter error:');
console.log(filePath);
console.log(error.reason);
console.log(error.mark.snippet);
throw error;
}
const output = {
agent: getAgentName(filePath) ?? null,
date: attributes.releaseDate ?? null,
downloadLink: attributes.downloadLink ?? null,
version: attributes.version ?? null,
features: attributes.features ?? null,
bugs: attributes.bugs ?? null,
security: attributes.security ?? null,
supportedOperatingSystems: attributes.supportedOperatingSystems ?? null,
description: (await excerptify(body)) ?? null,
slug,
};
if (attributes.category) {
output.category = attributes.category;
}
if (attributes.eolDate) {
output.eolDate = attributes.eolDate;
} else if (attributes.releaseDate) {
output.eolDate = getEOLDate(attributes.releaseDate);
}
return output;
};
const releaseNoteMdxs = await glob('src/content/docs/release-notes/**/*.mdx', {
ignore: '**/index.mdx',
});
const releaseNotes = (
await Promise.allSettled(releaseNoteMdxs.map(generateReleaseNoteObject))
)
.filter(({ status }) => status === 'fulfilled')
.map(({ value }) => value)
.filter(
({ date, agent }) => Boolean(date && agent) && INCLUDE_AGENTS.has(agent)
);
console.error('📦 release notes JSON generated');
const validateReleaseNotesAgents = (releaseNotes) => {
// this set excludes 'sdk', 'node' and '.net' from the one above
const JSON_AGENTS = new Set([
'android',
'browser',
'dotnet',
'go',
'infrastructure',
'ios',
'java',
'nodejs',
'php',
'python',
'ruby',
]);
const errors = [];
JSON_AGENTS.forEach((agent) => {
const agentsCount = releaseNotes.filter((note) => note.agent === agent)
.length;
if (agentsCount < 1) {
const message = `\n😵 No release notes found for ${agent}`;
errors.push(message);
} else {
console.error(`🕵️ Found ${agentsCount} release notes for ${agent}`);
}
});
const requiredData = [
'agent',
'date',
'version',
'description',
'slug',
'eolDate',
];
releaseNotes.forEach((note) => {
requiredData.forEach((key) => {
if (!note[key]) {
const message = `\n😵 Missing ${key} data for: \n ${JSON.stringify(
note
)}`;
errors.push(message);
}
});
});
if (errors.length > 0) {
errors.forEach((error) => console.error(error));
process.exitCode = 1;
} else {
console.error(`✨ Release notes JSON validated`);
}
};
if (uploadToS3) {
const client = new S3Client({ region: 'us-east-2' });
const putCommand = new PutObjectCommand({
Body: JSON.stringify(releaseNotes),
Bucket: 'docs-release-notes',
ContentType: 'application/json',
Key: 'release-notes.json',
});
console.error('🌎 uploading release notes JSON to S3');
client
.send(putCommand)
.then(() => console.error('✨ successfully uploaded release notes to S3!'))
.catch((err) => {
console.error('😵 failed to upload release notes to S3');
console.error(err);
});
} else if (validateJSON) {
validateReleaseNotesAgents(releaseNotes);
} else {
console.log(JSON.stringify(releaseNotes));
}