Skip to content

Commit afa90f9

Browse files
committed
refactor: pass full semantic-release context to hooks implementation
1 parent 5b7b962 commit afa90f9

File tree

5 files changed

+64
-32
lines changed

5 files changed

+64
-32
lines changed

index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const publishGitHub = require('./lib/publish');
33

44
let verified;
55

6-
async function verifyConditions(pluginConfig, {options, logger}) {
6+
async function verifyConditions(pluginConfig, context) {
7+
const {options} = context;
78
// If the GitHub publish plugin is used and has `assets` configured, validate it now in order to prevent any release if the configuration is wrong
89
if (options.publish) {
910
const publishPlugin = (Array.isArray(options.publish) ? options.publish : [options.publish]).find(
@@ -14,16 +15,16 @@ async function verifyConditions(pluginConfig, {options, logger}) {
1415
}
1516
}
1617

17-
await verifyGitHub(pluginConfig, options, logger);
18+
await verifyGitHub(pluginConfig, context);
1819
verified = true;
1920
}
2021

21-
async function publish(pluginConfig, {nextRelease, options, logger}) {
22+
async function publish(pluginConfig, context) {
2223
if (!verified) {
23-
await verifyGitHub(pluginConfig, options, logger);
24+
await verifyGitHub(pluginConfig, context);
2425
verified = true;
2526
}
26-
await publishGitHub(pluginConfig, options, nextRelease, logger);
27+
await publishGitHub(pluginConfig, context);
2728
}
2829

2930
module.exports = {verifyConditions, publish};

lib/publish.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const globAssets = require('./glob-assets.js');
99
const resolveConfig = require('./resolve-config');
1010
const getClient = require('./get-client');
1111

12-
module.exports = async (pluginConfig, {branch, repositoryUrl}, {version, gitHead, gitTag, notes}, logger) => {
12+
module.exports = async (pluginConfig, {options: {branch, repositoryUrl}, nextRelease: {gitTag, notes}, logger}) => {
1313
const {githubToken, githubUrl, githubApiPathPrefix, assets} = resolveConfig(pluginConfig);
1414
const {name: repo, owner} = parseGithubUrl(repositoryUrl);
1515
const github = getClient(githubToken, githubUrl, githubApiPathPrefix);

lib/verify.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const SemanticReleaseError = require('@semantic-release/error');
55
const resolveConfig = require('./resolve-config');
66
const getClient = require('./get-client');
77

8-
module.exports = async (pluginConfig, {repositoryUrl}, logger) => {
98
const {githubToken, githubUrl, githubApiPathPrefix, assets} = resolveConfig(pluginConfig);
9+
module.exports = async (pluginConfig, {options: {repositoryUrl}, logger}) => {
1010

1111
if (!githubToken) {
1212
throw new SemanticReleaseError('No github token specified.', 'ENOGHTOKEN');

test/publish.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ test.serial('Publish a release', async t => {
5454
})
5555
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl});
5656

57-
await publish(pluginConfig, options, nextRelease, t.context.logger);
57+
await publish(pluginConfig, {options, nextRelease, logger: t.context.logger});
5858

5959
t.deepEqual(t.context.log.args[0], ['Published GitHub release: %s', releaseUrl]);
6060
t.true(github.isDone());
@@ -91,7 +91,7 @@ test.serial('Publish a release with one asset', async t => {
9191
.post(`${uploadUri}?name=${escape('.dotfile')}&label=${escape('A dotfile with no ext')}`)
9292
.reply(200, {browser_download_url: assetUrl});
9393

94-
await publish(pluginConfig, options, nextRelease, t.context.logger);
94+
await publish(pluginConfig, {options, nextRelease, logger: t.context.logger});
9595

9696
t.deepEqual(t.context.log.args[0], ['Published GitHub release: %s', releaseUrl]);
9797
t.deepEqual(t.context.log.args[1], ['Published file %s', assetUrl]);
@@ -139,7 +139,7 @@ test.serial('Publish a release with one asset and custom github url', async t =>
139139
.post(`${uploadUri}?name=${escape('upload.txt')}&label=${escape('A text file')}`)
140140
.reply(200, {browser_download_url: assetUrl});
141141

142-
await publish(pluginConfig, options, nextRelease, t.context.logger);
142+
await publish(pluginConfig, {options, nextRelease, logger: t.context.logger});
143143

144144
t.deepEqual(t.context.log.args[0], ['Published GitHub release: %s', releaseUrl]);
145145
t.deepEqual(t.context.log.args[1], ['Published file %s', assetUrl]);
@@ -169,7 +169,7 @@ test.serial('Publish a release with an array of missing assets', async t => {
169169
})
170170
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl});
171171

172-
await publish(pluginConfig, options, nextRelease, t.context.logger);
172+
await publish(pluginConfig, {options, nextRelease, logger: t.context.logger});
173173

174174
t.deepEqual(t.context.log.args[0], ['Published GitHub release: %s', releaseUrl]);
175175
t.deepEqual(t.context.error.args[0], [

test/verify.test.js

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ test.serial('Verify package, token and repository access', async t => {
3939
.reply(200, {permissions: {push: true}});
4040

4141
await t.notThrows(
42-
verify({assets}, {repositoryUrl: `git+https://othertesturl.com/${owner}/${repo}.git`}, t.context.logger)
42+
verify(
43+
{assets},
44+
{options: {repositoryUrl: `git+https://othertesturl.com/${owner}/${repo}.git`}, logger: t.context.logger}
45+
)
4346
);
4447
t.true(github.isDone());
4548
});
@@ -57,8 +60,7 @@ test.serial('Verify package, token and repository access and custom URL with pre
5760
await t.notThrows(
5861
verify(
5962
{githubUrl, githubApiPathPrefix},
60-
{repositoryUrl: `git@othertesturl.com:${owner}/${repo}.git`},
61-
t.context.logger
63+
{options: {repositoryUrl: `git@othertesturl.com:${owner}/${repo}.git`}, logger: t.context.logger}
6264
)
6365
);
6466

@@ -76,7 +78,10 @@ test.serial('Verify package, token and repository access and custom URL without
7678
.reply(200, {permissions: {push: true}});
7779

7880
await t.notThrows(
79-
verify({githubUrl}, {repositoryUrl: `git@othertesturl.com:${owner}/${repo}.git`}, t.context.logger)
81+
verify(
82+
{githubUrl},
83+
{options: {repositoryUrl: `git@othertesturl.com:${owner}/${repo}.git`}, logger: t.context.logger}
84+
)
8085
);
8186

8287
t.true(github.isDone());
@@ -93,7 +98,9 @@ test.serial('Verify package, token and repository with environment variables', a
9398
.get(`/repos/${owner}/${repo}`)
9499
.reply(200, {permissions: {push: true}});
95100

96-
await t.notThrows(verify({}, {repositoryUrl: `git@othertesturl.com:${owner}/${repo}.git`}, t.context.logger));
101+
await t.notThrows(
102+
verify({}, {options: {repositoryUrl: `git@othertesturl.com:${owner}/${repo}.git`}, logger: t.context.logger})
103+
);
97104

98105
t.true(github.isDone());
99106
t.deepEqual(t.context.log.args[0], ['Verify GitHub authentication (%s)', 'https://othertesturl.com:443/prefix']);
@@ -110,7 +117,9 @@ test.serial('Verify package, token and repository access with alternative enviro
110117
.get(`/repos/${owner}/${repo}`)
111118
.reply(200, {permissions: {push: true}});
112119

113-
await t.notThrows(verify({}, {repositoryUrl: `git@othertesturl.com:${owner}/${repo}.git`}, t.context.logger));
120+
await t.notThrows(
121+
verify({}, {options: {repositoryUrl: `git@othertesturl.com:${owner}/${repo}.git`}, logger: t.context.logger})
122+
);
114123
t.true(github.isDone());
115124
});
116125

@@ -119,7 +128,10 @@ test.serial('Throw SemanticReleaseError if "assets" option is not a String or an
119128
const assets = 42;
120129

121130
const error = await t.throws(
122-
verify({assets}, {repositoryUrl: 'https://github.com/semantic-release/github.git'}, t.context.logger)
131+
verify(
132+
{assets},
133+
{options: {repositoryUrl: 'https://github.com/semantic-release/github.git'}, logger: t.context.logger}
134+
)
123135
);
124136

125137
t.true(error instanceof SemanticReleaseError);
@@ -131,7 +143,10 @@ test.serial('Throw SemanticReleaseError if "assets" option is an Array with inva
131143
const assets = ['file.js', 42];
132144

133145
const error = await t.throws(
134-
verify({assets}, {repositoryUrl: 'https://github.com/semantic-release/github.git'}, t.context.logger)
146+
verify(
147+
{assets},
148+
{options: {repositoryUrl: 'https://github.com/semantic-release/github.git'}, logger: t.context.logger}
149+
)
135150
);
136151

137152
t.true(error instanceof SemanticReleaseError);
@@ -147,7 +162,9 @@ test.serial('Verify "assets" is a String', async t => {
147162
.get(`/repos/${owner}/${repo}`)
148163
.reply(200, {permissions: {push: true}});
149164

150-
await t.notThrows(verify({assets}, {repositoryUrl: `git@othertesturl.com:${owner}/${repo}.git`}, t.context.logger));
165+
await t.notThrows(
166+
verify({assets}, {options: {repositoryUrl: `git@othertesturl.com:${owner}/${repo}.git`}, logger: t.context.logger})
167+
);
151168

152169
t.true(github.isDone());
153170
});
@@ -161,7 +178,9 @@ test.serial('Verify "assets" is an Object with a path property', async t => {
161178
.get(`/repos/${owner}/${repo}`)
162179
.reply(200, {permissions: {push: true}});
163180

164-
await t.notThrows(verify({assets}, {repositoryUrl: `git@othertesturl.com:${owner}/${repo}.git`}, t.context.logger));
181+
await t.notThrows(
182+
verify({assets}, {options: {repositoryUrl: `git@othertesturl.com:${owner}/${repo}.git`}, logger: t.context.logger})
183+
);
165184

166185
t.true(github.isDone());
167186
});
@@ -175,7 +194,9 @@ test.serial('Verify "assets" is an Array of Object with a path property', async
175194
.get(`/repos/${owner}/${repo}`)
176195
.reply(200, {permissions: {push: true}});
177196

178-
await t.notThrows(verify({assets}, {repositoryUrl: `git@othertesturl.com:${owner}/${repo}.git`}, t.context.logger));
197+
await t.notThrows(
198+
verify({assets}, {options: {repositoryUrl: `git@othertesturl.com:${owner}/${repo}.git`}, logger: t.context.logger})
199+
);
179200

180201
t.true(github.isDone());
181202
});
@@ -189,7 +210,9 @@ test.serial('Verify "assets" is an Array of glob Arrays', async t => {
189210
.get(`/repos/${owner}/${repo}`)
190211
.reply(200, {permissions: {push: true}});
191212

192-
await t.notThrows(verify({assets}, {repositoryUrl: `git@othertesturl.com:${owner}/${repo}.git`}, t.context.logger));
213+
await t.notThrows(
214+
verify({assets}, {options: {repositoryUrl: `git@othertesturl.com:${owner}/${repo}.git`}, logger: t.context.logger})
215+
);
193216

194217
t.true(github.isDone());
195218
});
@@ -203,7 +226,9 @@ test.serial('Verify "assets" is an Array of Object with a glob Arrays in path pr
203226
.get(`/repos/${owner}/${repo}`)
204227
.reply(200, {permissions: {push: true}});
205228

206-
await t.notThrows(verify({assets}, {repositoryUrl: `git@othertesturl.com:${owner}/${repo}.git`}, t.context.logger));
229+
await t.notThrows(
230+
verify({assets}, {options: {repositoryUrl: `git@othertesturl.com:${owner}/${repo}.git`}, logger: t.context.logger})
231+
);
207232

208233
t.true(github.isDone());
209234
});
@@ -213,7 +238,10 @@ test.serial('Throw SemanticReleaseError if "assets" option is an Object missing
213238
const assets = {name: 'file.js'};
214239

215240
const error = await t.throws(
216-
verify({assets}, {repositoryUrl: 'https://github.com/semantic-release/github.git'}, t.context.logger)
241+
verify(
242+
{assets},
243+
{options: {repositoryUrl: 'https://github.com/semantic-release/github.git'}, logger: t.context.logger}
244+
)
217245
);
218246

219247
t.true(error instanceof SemanticReleaseError);
@@ -227,7 +255,10 @@ test.serial(
227255
const assets = [{path: 'lib/file.js'}, {name: 'file.js'}];
228256

229257
const error = await t.throws(
230-
verify({assets}, {repositoryUrl: 'https://github.com/semantic-release/github.git'}, t.context.logger)
258+
verify(
259+
{assets},
260+
{options: {repositoryUrl: 'https://github.com/semantic-release/github.git'}, logger: t.context.logger}
261+
)
231262
);
232263

233264
t.true(error instanceof SemanticReleaseError);
@@ -237,7 +268,7 @@ test.serial(
237268

238269
test.serial('Throw SemanticReleaseError for missing github token', async t => {
239270
const error = await t.throws(
240-
verify({}, {repositoryUrl: 'https://github.com/semantic-release/github.git'}, t.context.logger)
271+
verify({}, {options: {repositoryUrl: 'https://github.com/semantic-release/github.git'}, logger: t.context.logger})
241272
);
242273

243274
t.true(error instanceof SemanticReleaseError);
@@ -253,7 +284,7 @@ test.serial('Throw SemanticReleaseError for invalid token', async t => {
253284
.reply(401);
254285

255286
const error = await t.throws(
256-
verify({}, {repositoryUrl: `https://github.com:${owner}/${repo}.git`}, t.context.logger)
287+
verify({}, {options: {repositoryUrl: `https://github.com:${owner}/${repo}.git`}, logger: t.context.logger})
257288
);
258289

259290
t.true(error instanceof SemanticReleaseError);
@@ -264,7 +295,7 @@ test.serial('Throw SemanticReleaseError for invalid token', async t => {
264295
test.serial('Throw SemanticReleaseError for invalid repositoryUrl', async t => {
265296
process.env.GITHUB_TOKEN = 'github_token';
266297

267-
const error = await t.throws(verify({}, {repositoryUrl: 'invalid_url'}, t.context.logger));
298+
const error = await t.throws(verify({}, {options: {repositoryUrl: 'invalid_url'}, logger: t.context.logger}));
268299

269300
t.true(error instanceof SemanticReleaseError);
270301
t.is(error.code, 'EINVALIDGITURL');
@@ -279,7 +310,7 @@ test.serial("Throw SemanticReleaseError if token doesn't have the push permissio
279310
.reply(200, {permissions: {push: false}});
280311

281312
const error = await t.throws(
282-
verify({}, {repositoryUrl: `https://github.com:${owner}/${repo}.git`}, t.context.logger)
313+
verify({}, {options: {repositoryUrl: `https://github.com:${owner}/${repo}.git`}, logger: t.context.logger})
283314
);
284315

285316
t.true(error instanceof SemanticReleaseError);
@@ -296,7 +327,7 @@ test.serial("Throw SemanticReleaseError if the repository doesn't exist", async
296327
.reply(404);
297328

298329
const error = await t.throws(
299-
verify({}, {repositoryUrl: `https://github.com:${owner}/${repo}.git`}, t.context.logger)
330+
verify({}, {options: {repositoryUrl: `https://github.com:${owner}/${repo}.git`}, logger: t.context.logger})
300331
);
301332

302333
t.true(error instanceof SemanticReleaseError);
@@ -313,7 +344,7 @@ test.serial('Throw error if github return any other errors', async t => {
313344
.reply(500);
314345

315346
const error = await t.throws(
316-
verify({}, {repositoryUrl: `https://github.com:${owner}/${repo}.git`}, t.context.logger)
347+
verify({}, {options: {repositoryUrl: `https://github.com:${owner}/${repo}.git`}, logger: t.context.logger})
317348
);
318349

319350
t.is(error.code, 500);

0 commit comments

Comments
 (0)