Skip to content

Commit

Permalink
Add wildcard path support (#16)
Browse files Browse the repository at this point in the history
* Add wildcard path support

* Fix npm audit issues

* 1.4.0
  • Loading branch information
timja authored Apr 28, 2021
1 parent 00be888 commit 4753883
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 22 deletions.
8 changes: 6 additions & 2 deletions lib/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const StreamZip = require('node-stream-zip');
const util = require('util');
const xml2js = require('xml2js');
const config = require('./config.js');
const wcmatch = require('wildcard-match');

module.exports = {
fetch: () => {
Expand All @@ -27,7 +28,10 @@ module.exports = {

zip.on('entry', async function (entry) {
entries.push(entry.name);
let ok = !!applicable.find(file => entry.name.startsWith(file));
let ok = !!applicable.find(file => {
const isMatch = wcmatch(file, { separator: '|' })
return isMatch(entry.name) || entry.name.startsWith(file);
});
if (!ok) {
this.emit('error', new Error(util.format('No permissions for %s', entry.name)));
return
Expand Down Expand Up @@ -56,7 +60,7 @@ module.exports = {
log.info(util.format('Parsed %s with url=%s tag=%s GAV=%s:%s:%s', entry.name, url, tag, groupId, artifactId, version));
const expectedPath = groupId.replace(/[.]/g, '/') + '/' + artifactId + '/' + version + '/' + artifactId + '-' + version + '.pom';
if (tag !== hash) {
this.emit('error', new Error('Wrong commit hash in /project/scm/tag'));
this.emit('error', new Error(`Wrong commit hash in /project/scm/tag, expected ${tag}, got ${hash}`));
return
} else if (!url.match('^https?://github[.]com/.+$')) {
this.emit('error', new Error('Wrong URL in /project/scm/url'));
Expand Down
31 changes: 18 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "incrementals-publisher",
"version": "1.3.0",
"version": "1.4.0",
"description": "",
"private": true,
"main": "index.js",
Expand Down Expand Up @@ -33,8 +33,9 @@
"express-async-wrap": "^1.0.0",
"express-winston": "^4.0.5",
"helmet": "^4.1.0",
"node-fetch": "^2.6.0",
"node-fetch": "^2.6.1",
"node-stream-zip": "^1.11.3",
"wildcard-match": "^5.1.0",
"winston": "^3.3.3",
"xml2js": "^0.4.23"
}
Expand Down
Binary file modified test/fixtures-good-archive.zip
Binary file not shown.
5 changes: 5 additions & 0 deletions test/fixtures-permissions-missing-path.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jenkinsci/bom": [
"io/jenkins/tools/invalid-path"
]
}
5 changes: 5 additions & 0 deletions test/fixtures-permissions-wildcard.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jenkinsci/bom": [
"io/jenkins/tools/bom/bom-*"
]
}
5 changes: 5 additions & 0 deletions test/fixtures-permissions.json
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,11 @@
],
"jenkinsci/bom": [
"io/jenkins/tools/bom/bom",
"io/jenkins/tools/bom/bom-2.222.x",
"io/jenkins/tools/bom/bom-2.235.x",
"io/jenkins/tools/bom/bom-2.249.x",
"io/jenkins/tools/bom/bom-2.263.x",
"io/jenkins/tools/bom/bom-2.277.x",
"io/jenkins/tools/bom/parent"
],
"jenkinsci/boot-clj-plugin": [
Expand Down
8 changes: 4 additions & 4 deletions test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const {IncrementalsPlugin} = require('../IncrementalsPlugin.js');
const permissions = require('../lib/permissions');

const urlResults = {
'https://repo.jenkins-ci.org/incrementals/io/jenkins/tools/bom/bom/2.176.1-rc63.55219da23b98/bom-2.176.1-rc63.55219da23b98.pom': {
'https://repo.jenkins-ci.org/incrementals/io/jenkins/tools/bom/bom-2.222.x/29-rc793.5055257e4d28/bom-2.222.x-29-rc793.5055257e4d28.pom': {
status: 404
},
'https://ci.jenkins.io/job/Tools/job/bom/job/PR-22/5/api/json?tree=actions[revision[hash,pullHash]]': {
Expand All @@ -18,7 +18,7 @@ const urlResults = {
"_class": "jenkins.scm.api.SCMRevisionAction",
"revision": {
"_class": "org.jenkinsci.plugins.github_branch_source.PullRequestSCMRevision",
"pullHash": "55219da23b98739fa6b793b21b91555b36162856"
"pullHash": "5055257e4d28adea76fc34fdde4e025347405bae"
}
}]
}
Expand All @@ -34,7 +34,7 @@ const urlResults = {
status: 200,
results: () => require('./fixtures-permissions.json')
},
'https://fake-repo.jenkins-ci.org/incrementals/io/jenkins/tools/bom/bom/2.176.1-rc63.55219da23b98/bom-2.176.1-rc63.55219da23b98.pom': {
'https://fake-repo.jenkins-ci.org/incrementals/io/jenkins/tools/bom/bom-2.222.x/29-rc793.5055257e4d28/bom-2.222.x-29-rc793.5055257e4d28.pom': {
status: 404,
results: () => 'Not found'
}
Expand Down Expand Up @@ -147,7 +147,7 @@ describe('Handling incremental publisher webhook events', function() {
it('should output an error', async function() {
data.body.build_url = 'https://ci.jenkins.io/job/Tools/job/bom/job/PR-22/5/';
await run();
assert.equal(ctx.res.body, 'Invalid archive retrieved from Jenkins, perhaps the plugin is not properly incrementalized?\nError: This is my error from https://ci.jenkins.io/job/Tools/job/bom/job/PR-22/5/artifact/**/*55219da23b98*/*55219da23b98*/*zip*/archive.zip');
assert.equal(ctx.res.body, 'Invalid archive retrieved from Jenkins, perhaps the plugin is not properly incrementalized?\nError: This is my error from https://ci.jenkins.io/job/Tools/job/bom/job/PR-22/5/artifact/**/*5055257e4d28*/*5055257e4d28*/*zip*/archive.zip');
assert.equal(ctx.res.status, 400);
});
});
Expand Down
57 changes: 56 additions & 1 deletion test/permissions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,43 @@ describe('The Permissions helpers', function() {
}
)
})
it('Fails with no permissions error', async function() {
const folderMetadataParsed = {
owner: 'jenkinsci',
repo: 'bom'
}
const buildMetadataParsed = {
hash: '5055257e4d28adea76fc34fdde4e025347405bae'
}

const repoPath = path.join(folderMetadataParsed.owner, folderMetadataParsed.repo);
const entries = [];
let perms = {
status: 200,
json: () => require('./fixtures-permissions-missing-path.json')
}
assert.rejects(
() => Permissions.verify(
{ info: () => true },
repoPath,
path.resolve('./test/fixtures-good-archive.zip'),
entries,
perms,
buildMetadataParsed.hash
),
{
name: 'Error',
message: 'ZIP error: Error: No permissions for io/jenkins/tools/bom/bom-2.222.x/29-rc793.5055257e4d28/bom-2.222.x-29-rc793.5055257e4d28.pom'
}
)
})
it('Succeeds with good pom', async function() {
const folderMetadataParsed = {
owner: 'jenkinsci',
repo: 'bom'
}
const buildMetadataParsed = {
hash: '55219da23b98739fa6b793b21b91555b36162856'
hash: '5055257e4d28adea76fc34fdde4e025347405bae'
}

const repoPath = path.join(folderMetadataParsed.owner, folderMetadataParsed.repo);
Expand All @@ -58,4 +88,29 @@ describe('The Permissions helpers', function() {
);
assert.equal(response, true)
})
it('Succeeds with wildcard path', async function() {
const folderMetadataParsed = {
owner: 'jenkinsci',
repo: 'bom'
}
const buildMetadataParsed = {
hash: '5055257e4d28adea76fc34fdde4e025347405bae'
}

const repoPath = path.join(folderMetadataParsed.owner, folderMetadataParsed.repo);
const entries = [];
let perms = {
status: 200,
json: () => require('./fixtures-permissions-wildcard.json')
};
const response = await Permissions.verify(
{ info: () => true },
repoPath,
path.resolve('./test/fixtures-good-archive.zip'),
entries,
perms,
buildMetadataParsed.hash
);
assert.equal(response, true)
})
});

0 comments on commit 4753883

Please sign in to comment.