Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
feat: impl dist-tag hooks (#1612)
Browse files Browse the repository at this point in the history
* feat: impl dist-tag hooks

* test: add tag assert
  • Loading branch information
killagu authored Jan 14, 2021
1 parent dffe589 commit 33dd355
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
37 changes: 37 additions & 0 deletions controllers/registry/package/dist_tag.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var packageService = require('../../../services/package');
var hook = require('../../../services/hook');

function ok() {
return {
Expand Down Expand Up @@ -34,6 +35,19 @@ exports.update = function* () {
for (var tag in tags) {
var version = tags[tag];
yield packageService.addModuleTag(name, tag, version);

// hooks
const envelope = {
event: 'package:dist-tag',
name: name,
tag: tag,
type: 'package',
version: version,
hookOwner: null,
payload: null,
change: null,
};
hook.trigger(envelope);
}
this.status = 201;
this.body = ok();
Expand All @@ -60,6 +74,18 @@ exports.set = function* () {
yield packageService.addModuleTag(name, tag, version);
this.status = 201;
this.body = ok();
// hooks
const envelope = {
event: 'package:dist-tag',
name: name,
tag: tag,
type: 'package',
version: version,
hookOwner: null,
payload: null,
change: null,
};
hook.trigger(envelope);
};

// DELETE /-/package/:pkg/dist-tags/:tag -- Remove tag from dist-tags
Expand All @@ -77,4 +103,15 @@ exports.destroy = function* () {
}
yield packageService.removeModuleTagsByNames(name, tag);
this.body = ok();
// hooks
const envelope = {
event: 'package:dist-tag:rm',
name: name,
tag: tag,
type: 'package',
hookOwner: null,
payload: null,
change: null,
};
hook.trigger(envelope);
};
74 changes: 74 additions & 0 deletions test/controllers/registry/package/dist_tag.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

var assert = require('assert');
var request = require('supertest');
var mm = require('mm');
var pedding = require('pedding');
Expand Down Expand Up @@ -117,6 +118,27 @@ describe('test/controllers/registry/package/dist_tag.test.js', function () {
.expect(201, done);
});
});

it('should fire globalHook', function (done) {
done = pedding(2, done);
mm(config, 'globalHook', function* (envelope) {
assert(envelope.version === '1.0.1');
assert(envelope.name === '@cnpmtest/dist_tag_test_module_set');
assert(envelope.type === 'package');
assert(envelope.event === 'package:dist-tag');
assert(envelope.tag === 'exists');
done();
})
request(app)
.put('/-/package/@cnpmtest/dist_tag_test_module_set/dist-tags/exists')
.set('authorization', utils.otherUserAuth)
.set('content-type', 'application/json')
.send(JSON.stringify('1.0.1'))
.expect({
ok: 'dist-tags updated'
})
.expect(201, done);
});
});

describe('destroy()', function () {
Expand Down Expand Up @@ -178,6 +200,25 @@ describe('test/controllers/registry/package/dist_tag.test.js', function () {
})
.expect(200, done);
});

it('should fire globalHook', function (done) {
done = pedding(2, done);
mm(config, 'globalHook', function* (envelope) {
assert(envelope.name === '@cnpmtest/dist_tag_test_module_destroy');
assert(envelope.type === 'package');
assert(envelope.event === 'package:dist-tag:rm');
assert(envelope.tag === 'next');
done();
})
request(app)
.delete('/-/package/@cnpmtest/dist_tag_test_module_destroy/dist-tags/next')
.set('authorization', utils.otherUserAuth)
.set('content-type', 'application/json')
.expect({
ok: 'dist-tags updated'
})
.expect(200, done);
});
});

describe('save()', function () {
Expand Down Expand Up @@ -243,6 +284,39 @@ describe('test/controllers/registry/package/dist_tag.test.js', function () {
}, done);
});
});

it('should fire globalHook', function (done) {
done = pedding(3, done);
mm(config, 'globalHook', function* (envelope) {
if (envelope.tag === 'latest') {
assert(envelope.version === '1.0.1');
assert(envelope.name === '@cnpmtest/dist_tag_test_module_save');
assert(envelope.type === 'package');
assert(envelope.event === 'package:dist-tag');
assert(envelope.tag === 'latest');
done();
}
if (envelope.tag === 'new') {
assert(envelope.version === '1.0.1');
assert(envelope.name === '@cnpmtest/dist_tag_test_module_save');
assert(envelope.type === 'package');
assert(envelope.event === 'package:dist-tag');
assert(envelope.tag === 'new');
done();
}
})
request(app)
.put('/-/package/@cnpmtest/dist_tag_test_module_save/dist-tags')
.set('authorization', utils.otherUserAuth)
.send({
latest: '1.0.1',
new: '1.0.1'
})
.expect({
ok: 'dist-tags updated'
})
.expect(201, done);
});
});

describe('update()', function () {
Expand Down

0 comments on commit 33dd355

Please sign in to comment.