Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: semantic-release/github
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v5.2.12
Choose a base ref
...
head repository: semantic-release/github
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v5.3.0
Choose a head ref
  • 2 commits
  • 3 files changed
  • 1 contributor

Commits on Mar 25, 2019

  1. feat: Append assets before release

    pmrcunha authored and gr2m committed Mar 25, 2019

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    c9c5ebf View commit details
  2. Verified

    This commit was signed with the committer’s verified signature.
    crazy-max CrazyMax
    Copy the full SHA
    06f1d91 View commit details
Showing with 119 additions and 60 deletions.
  1. +84 −50 lib/publish.js
  2. +14 −4 test/integration.test.js
  3. +21 −6 test/publish.test.js
134 changes: 84 additions & 50 deletions lib/publish.js
Original file line number Diff line number Diff line change
@@ -18,64 +18,98 @@ module.exports = async (pluginConfig, context) => {
const {githubToken, githubUrl, githubApiPathPrefix, proxy, assets} = resolveConfig(pluginConfig, context);
const {name: repo, owner} = parseGithubUrl(repositoryUrl);
const github = getClient({githubToken, githubUrl, githubApiPathPrefix, proxy});
const release = {owner, repo, tag_name: gitTag, name: gitTag, target_commitish: branch, body: notes}; // eslint-disable-line camelcase

/* eslint-disable camelcase */
const releaseData = {
owner,
repo,
tag_name: gitTag,
name: gitTag,
target_commitish: branch,
body: notes,
};
/* eslint-enable */

debug('release owner: %o', owner);
debug('release repo: %o', repo);
debug('release name: %o', gitTag);
debug('release branch: %o', branch);

const {
data: {html_url: url, upload_url: uploadUrl},
} = await github.repos.createRelease(release);
logger.log('Published GitHub release: %s', url);
// When there are no assets, we publish a release directly
if (!assets || assets.length === 0) {
const {
data: {html_url: url},
} = await github.repos.createRelease(releaseData);

if (assets && assets.length > 0) {
const globbedAssets = await globAssets(context, assets);
debug('globed assets: %o', globbedAssets);

await Promise.all(
globbedAssets.map(async asset => {
const filePath = isPlainObject(asset) ? asset.path : asset;
let file;

try {
file = await stat(resolve(cwd, filePath));
} catch (error) {
logger.error('The asset %s cannot be read, and will be ignored.', filePath);
return;
}

if (!file || !file.isFile()) {
logger.error('The asset %s is not a file, and will be ignored.', filePath);
return;
}

const fileName = asset.name || basename(filePath);
const upload = {
url: uploadUrl,
file: await readFile(resolve(cwd, filePath)),
name: fileName,
headers: {
'content-type': mime.getType(extname(fileName)) || 'text/plain',
'content-length': file.size,
},
};

debug('file path: %o', filePath);
debug('file name: %o', fileName);

if (isPlainObject(asset) && asset.label) {
upload.label = asset.label;
}

const {
data: {browser_download_url: downloadUrl},
} = await github.repos.uploadReleaseAsset(upload);
logger.log('Published file %s', downloadUrl);
})
);
logger.log('Published GitHub release: %s', url);
return {url, name: 'GitHub release'};
}

// We'll create a draft release, append the assets to it, and then publish it.
// This is so that the assets are available when we get a Github release event.
const draftRelease = {...releaseData, draft: true};

const {
data: {html_url: url, upload_url: uploadUrl, id: releaseId},
} = await github.repos.createRelease(draftRelease);

// Append assets to the release
const globbedAssets = await globAssets(context, assets);
debug('globed assets: %o', globbedAssets);

await Promise.all(
globbedAssets.map(async asset => {
const filePath = isPlainObject(asset) ? asset.path : asset;
let file;

try {
file = await stat(resolve(cwd, filePath));
} catch (error) {
logger.error('The asset %s cannot be read, and will be ignored.', filePath);
return;
}

if (!file || !file.isFile()) {
logger.error('The asset %s is not a file, and will be ignored.', filePath);
return;
}

const fileName = asset.name || basename(filePath);
const upload = {
url: uploadUrl,
file: await readFile(resolve(cwd, filePath)),
name: fileName,
headers: {
'content-type': mime.getType(extname(fileName)) || 'text/plain',
'content-length': file.size,
},
};

debug('file path: %o', filePath);
debug('file name: %o', fileName);

if (isPlainObject(asset) && asset.label) {
upload.label = asset.label;
}

const {
data: {browser_download_url: downloadUrl},
} = await github.repos.uploadReleaseAsset(upload);
logger.log('Published file %s', downloadUrl);
})
);

/* eslint-disable camelcase */
const release = {
owner,
repo,
release_id: releaseId,
draft: false,
};
/* eslint-enable */

await github.repos.updateRelease(release);

logger.log('Published GitHub release: %s', url);
return {url, name: 'GitHub release'};
};
18 changes: 14 additions & 4 deletions test/integration.test.js
Original file line number Diff line number Diff line change
@@ -152,8 +152,13 @@ test.serial('Publish a release with an array of assets', async t => {
target_commitish: options.branch,
name: nextRelease.gitTag,
body: nextRelease.notes,
draft: true,
})
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl});
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl, id: releaseId})
.patch(`/repos/${owner}/${repo}/releases/${releaseId}`, {
draft: false,
})
.reply(200, {html_url: releaseUrl});
const githubUpload1 = upload(env, {
uploadUrl: 'https://github.com',
contentLength: (await stat(path.resolve(cwd, 'upload.txt'))).size,
@@ -171,9 +176,9 @@ test.serial('Publish a release with an array of assets', async t => {

t.is(result.url, releaseUrl);
t.deepEqual(t.context.log.args[0], ['Verify GitHub authentication']);
t.deepEqual(t.context.log.args[1], ['Published GitHub release: %s', releaseUrl]);
t.true(t.context.log.calledWith('Published file %s', otherAssetUrl));
t.true(t.context.log.calledWith('Published file %s', assetUrl));
t.true(t.context.log.calledWith('Published GitHub release: %s', releaseUrl));
t.true(github.isDone());
t.true(githubUpload1.isDone());
t.true(githubUpload2.isDone());
@@ -285,8 +290,13 @@ test.serial('Verify, release and notify success', async t => {
target_commitish: options.branch,
name: nextRelease.gitTag,
body: nextRelease.notes,
draft: true,
})
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl, id: releaseId})
.patch(`/repos/${owner}/${repo}/releases/${releaseId}`, {
draft: false,
})
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl})
.reply(200, {html_url: releaseUrl})
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${owner}/${repo}`})
.get(
@@ -328,9 +338,9 @@ test.serial('Verify, release and notify success', async t => {
);

t.deepEqual(t.context.log.args[0], ['Verify GitHub authentication']);
t.deepEqual(t.context.log.args[1], ['Published GitHub release: %s', releaseUrl]);
t.true(t.context.log.calledWith('Published file %s', otherAssetUrl));
t.true(t.context.log.calledWith('Published file %s', assetUrl));
t.true(t.context.log.calledWith('Published GitHub release: %s', releaseUrl));
t.true(github.isDone());
t.true(githubUpload1.isDone());
t.true(githubUpload2.isDone());
27 changes: 21 additions & 6 deletions test/publish.test.js
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ test.serial('Publish a release', async t => {
name: nextRelease.gitTag,
body: nextRelease.notes,
})
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl});
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl, id: releaseId});

const result = await publish(pluginConfig, {cwd, env, options, nextRelease, logger: t.context.logger});

@@ -83,7 +83,7 @@ test.serial('Publish a release, retrying 4 times', async t => {
name: nextRelease.gitTag,
body: nextRelease.notes,
})
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl});
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl, id: releaseId});

const result = await publish(pluginConfig, {cwd, env, options, nextRelease, logger: t.context.logger});

@@ -113,6 +113,11 @@ test.serial('Publish a release with one asset', async t => {
target_commitish: options.branch,
name: nextRelease.gitTag,
body: nextRelease.notes,
draft: true,
})
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl, id: releaseId})
.patch(`/repos/${owner}/${repo}/releases/${releaseId}`, {
draft: false,
})
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl});

@@ -126,7 +131,7 @@ test.serial('Publish a release with one asset', async t => {
const result = await publish(pluginConfig, {cwd, env, options, nextRelease, logger: t.context.logger});

t.is(result.url, releaseUrl);
t.deepEqual(t.context.log.args[0], ['Published GitHub release: %s', releaseUrl]);
t.true(t.context.log.calledWith('Published GitHub release: %s', releaseUrl));
t.true(t.context.log.calledWith('Published file %s', assetUrl));
t.true(github.isDone());
t.true(githubUpload.isDone());
@@ -153,6 +158,11 @@ test.serial('Publish a release with one asset and custom github url', async t =>
target_commitish: options.branch,
name: nextRelease.gitTag,
body: nextRelease.notes,
draft: true,
})
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl, id: releaseId})
.patch(`/repos/${owner}/${repo}/releases/${releaseId}`, {
draft: false,
})
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl});

@@ -166,7 +176,7 @@ test.serial('Publish a release with one asset and custom github url', async t =>
const result = await publish(pluginConfig, {cwd, env, options, nextRelease, logger: t.context.logger});

t.is(result.url, releaseUrl);
t.deepEqual(t.context.log.args[0], ['Published GitHub release: %s', releaseUrl]);
t.true(t.context.log.calledWith('Published GitHub release: %s', releaseUrl));
t.true(t.context.log.calledWith('Published file %s', assetUrl));
t.true(github.isDone());
t.true(githubUpload.isDone());
@@ -191,13 +201,18 @@ test.serial('Publish a release with an array of missing assets', async t => {
target_commitish: options.branch,
name: nextRelease.gitTag,
body: nextRelease.notes,
draft: true,
})
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl});
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl, id: releaseId})
.patch(`/repos/${owner}/${repo}/releases/${releaseId}`, {
draft: false,
})
.reply(200, {html_url: releaseUrl});

const result = await publish(pluginConfig, {cwd, env, options, nextRelease, logger: t.context.logger});

t.is(result.url, releaseUrl);
t.deepEqual(t.context.log.args[0], ['Published GitHub release: %s', releaseUrl]);
t.true(t.context.log.calledWith('Published GitHub release: %s', releaseUrl));
t.true(t.context.error.calledWith('The asset %s cannot be read, and will be ignored.', 'missing.txt'));
t.true(t.context.error.calledWith('The asset %s is not a file, and will be ignored.', emptyDirectory));
t.true(github.isDone());