Skip to content

Commit 9c2c377

Browse files
authored
fix(release): build script now updates most recent archive (#6)
1 parent f7d315c commit 9c2c377

File tree

9 files changed

+5939
-1811
lines changed

9 files changed

+5939
-1811
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ jobs:
3737
node-version: '12'
3838
- name: install dependencies
3939
run: yarn install --frozen-lockfile --ignore-engines
40+
- name: build script
41+
run: yarn run build
4042
- name: release
4143
run: yarn run semantic-release
4244
env:

build/build.js

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,64 @@
33
const path = require('path')
44
const debug = require('debug')('cwe-sdk:build')
55
const { convertXmlArchiveToJson, writeJsonToFile, createCweDictionary } = require('./xmlParser')
6-
7-
debug('begin building CWE assets')
6+
const request = require('./httpClient')
7+
const AdmZip = require('adm-zip')
8+
const { rename } = require('fs').promises
89

910
// @TODO add a script that creates a copy of cwec_v4.1.xml to
1011
// its alias cwe-archive.xml
1112
const RAW_INPUT_XML_FILENAME = 'cwe-archive.xml'
1213
const RAW_OUTPUT_JSON_FILENAME = 'cwe-archive.json'
1314
const OUTPUT_JSON_DICT_FILENAME = 'cwe-dictionary.json'
1415
const OUTPUT_JSON_HIERARCHY_FILENAME = 'cwe-hierarchy.json'
16+
const ARCHIVE_DOWNLOAD_OPTIONS = {
17+
hostname: 'cwe.mitre.org',
18+
port: 443,
19+
path: '/data/xml/cwec_latest.xml.zip',
20+
method: 'GET'
21+
}
22+
let cweArchiveVersion
23+
24+
debug('begin downloading latest CWE archive')
25+
26+
updateArchive()
27+
.then(() => {
28+
debug(`archive updated to version ${cweArchiveVersion}`)
29+
debug('begin building CWE assets')
30+
31+
const rawJsonCweArchive = convertXmlArchiveToJson({
32+
cweArchiveFilepath: path.join(__dirname, '..', 'raw', RAW_INPUT_XML_FILENAME)
33+
})
34+
writeJsonToFile({
35+
jsonFilepath: path.join(__dirname, '..', 'raw', RAW_OUTPUT_JSON_FILENAME),
36+
jsonData: rawJsonCweArchive
37+
})
38+
39+
const { cweDictionary, cweHierarchy } = createCweDictionary({ cweArchive: rawJsonCweArchive })
1540

16-
const rawJsonCweArchive = convertXmlArchiveToJson({
17-
cweArchiveFilepath: path.join(__dirname, '..', 'raw', RAW_INPUT_XML_FILENAME)
18-
})
19-
writeJsonToFile({
20-
jsonFilepath: path.join(__dirname, '..', 'raw', RAW_OUTPUT_JSON_FILENAME),
21-
jsonData: rawJsonCweArchive
22-
})
41+
writeJsonToFile({
42+
jsonFilepath: path.join(__dirname, '..', 'raw', OUTPUT_JSON_DICT_FILENAME),
43+
jsonData: cweDictionary
44+
})
2345

24-
const { cweDictionary, cweHierarchy } = createCweDictionary({ cweArchive: rawJsonCweArchive })
46+
writeJsonToFile({
47+
jsonFilepath: path.join(__dirname, '..', 'raw', OUTPUT_JSON_HIERARCHY_FILENAME),
48+
jsonData: cweHierarchy
49+
})
2550

26-
writeJsonToFile({
27-
jsonFilepath: path.join(__dirname, '..', 'raw', OUTPUT_JSON_DICT_FILENAME),
28-
jsonData: cweDictionary
29-
})
51+
debug('finished')
52+
})
53+
.catch(console.error)
3054

31-
writeJsonToFile({
32-
jsonFilepath: path.join(__dirname, '..', 'raw', OUTPUT_JSON_HIERARCHY_FILENAME),
33-
jsonData: cweHierarchy
34-
})
55+
async function updateArchive() {
56+
const { data } = await request(ARCHIVE_DOWNLOAD_OPTIONS)
3557

36-
debug('finished')
58+
const zip = new AdmZip(data)
59+
const zippedFile = zip.getEntries()[0].entryName
60+
cweArchiveVersion = zippedFile.substring(zippedFile.search(/v/) + 1, zippedFile.search(/\.xml/))
61+
zip.extractEntryTo(zippedFile, 'raw', false)
62+
return rename(
63+
path.join(__dirname, '..', 'raw', zippedFile),
64+
path.join(__dirname, '..', 'raw', RAW_INPUT_XML_FILENAME)
65+
)
66+
}

build/httpClient.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const requestOriginal = require('https').request
2+
3+
module.exports = function(options) {
4+
return new Promise((resolve, reject) => {
5+
const req = requestOriginal(options, response => {
6+
const chunks = []
7+
response.on('data', data => {
8+
chunks.push(data)
9+
})
10+
11+
response.on('end', () => {
12+
resolve({ response, data: Buffer.concat(chunks) })
13+
})
14+
})
15+
req.on('error', reject)
16+
req.end()
17+
})
18+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"@semantic-release/github": "^5.4.2",
5454
"@semantic-release/npm": "^5.1.13",
5555
"@semantic-release/release-notes-generator": "^7.2.1",
56+
"adm-zip": "^0.4.16",
5657
"babel-eslint": "^10.0.1",
5758
"babel-plugin-syntax-async-functions": "^6.13.0",
5859
"babel-plugin-transform-regenerator": "^6.26.0",
@@ -218,4 +219,4 @@
218219
"raw",
219220
"src"
220221
]
221-
}
222+
}

raw/cwe-archive.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)