Skip to content

Commit

Permalink
fix: broken asset release upload (#45)
Browse files Browse the repository at this point in the history
* toString method on upload seems to bork

* format

* build

* change file

* add link to this PR
  • Loading branch information
jbolda authored Aug 23, 2020
1 parent 0646fc9 commit cf98c66
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 100 deletions.
5 changes: 5 additions & 0 deletions .changes/upload-toString-breaks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"action": patch
---

Uploaded assets break when `data` receives `fs.readFileSync(assetPath).toString()` even though types suggest it. Giving it a Buffer fixes the issue.
62 changes: 45 additions & 17 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9759,15 +9759,21 @@ function uploadAssets(releaseId, assets) {
// Determine content-length for header to upload asset
const contentLength = (filePath) => fs_1.default.statSync(filePath).size;
for (const assetPath of assets) {
const headers = { 'content-type': 'application/zip', 'content-length': contentLength(assetPath) };
const headers = {
'content-type': 'application/zip',
'content-length': contentLength(assetPath)
};
const ext = path_1.default.extname(assetPath);
const filename = path_1.default.basename(assetPath).replace(ext, '');
const assetName = path_1.default.dirname(assetPath).includes(`target${path_1.default.sep}debug`) ? `${filename}-debug${ext}` : `${filename}${ext}`;
const assetName = path_1.default.dirname(assetPath).includes(`target${path_1.default.sep}debug`)
? `${filename}-debug${ext}`
: `${filename}${ext}`;
console.log(`Uploading ${assetName}...`);
yield github.repos.uploadReleaseAsset({
headers,
name: assetName,
data: fs_1.default.readFileSync(assetPath).toString(),
// @ts-ignore error TS2322: Type 'Buffer' is not assignable to type 'string'.
data: fs_1.default.readFileSync(assetPath),
owner: github_1.context.repo.owner,
repo: github_1.context.repo.repo,
release_id: releaseId
Expand Down Expand Up @@ -10659,7 +10665,9 @@ function getPackageJson(root) {
}
function hasTauriDependency(root) {
const packageJson = getPackageJson(root);
return packageJson && ((packageJson.dependencies && packageJson.dependencies.tauri) || ((packageJson.devDependencies && packageJson.devDependencies.tauri)));
return (packageJson &&
((packageJson.dependencies && packageJson.dependencies.tauri) ||
(packageJson.devDependencies && packageJson.devDependencies.tauri)));
}
function usesYarn(root) {
return fs_1.existsSync(path_1.join(root, 'yarn.lock'));
Expand All @@ -10672,12 +10680,12 @@ function execCommand(command, { cwd }) {
shell: process.env.shell || true,
windowsHide: true,
stdio: 'inherit',
env: { FORCE_COLOR: '0' },
env: { FORCE_COLOR: '0' }
}).then();
}
function buildProject(root, debug, { configPath, distPath, iconPath, npmScript }) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve) => {
return new Promise(resolve => {
if (hasTauriDependency(root)) {
if (npmScript) {
resolve(usesYarn(root) ? `yarn ${npmScript}` : `npm run ${npmScript}`);
Expand All @@ -10702,8 +10710,12 @@ function buildProject(root, debug, { configPath, distPath, iconPath, npmScript }
}
else {
const packageJson = getPackageJson(root);
const appName = packageJson ? (packageJson.displayName || packageJson.name).replace(/ /g, '-') : 'app';
return execCommand(`${runner} init --ci --app-name ${appName}`, { cwd: root }).then(() => {
const appName = packageJson
? (packageJson.displayName || packageJson.name).replace(/ /g, '-')
: 'app';
return execCommand(`${runner} init --ci --app-name ${appName}`, {
cwd: root
}).then(() => {
const cargoManifest = toml_1.default.parse(fs_1.readFileSync(manifestPath).toString());
const version = packageJson ? packageJson.version : '0.1.0';
console.log(`Replacing cargo manifest options - package.version=${version}`);
Expand All @@ -10715,7 +10727,9 @@ function buildProject(root, debug, { configPath, distPath, iconPath, npmScript }
version
};
if (iconPath) {
return execCommand(`${runner} icon --i ${path_1.join(root, iconPath)}`, { cwd: root }).then(() => app);
return execCommand(`${runner} icon --i ${path_1.join(root, iconPath)}`, {
cwd: root
}).then(() => app);
}
return app;
});
Expand All @@ -10732,7 +10746,8 @@ function buildProject(root, debug, { configPath, distPath, iconPath, npmScript }
fs_1.writeFileSync(tauriConfPath, JSON.stringify(tauriConf));
}
const args = debug ? ['--debug'] : [];
return execCommand(`${app.runner} build` + (args.length ? ` ${args.join(' ')}` : ''), { cwd: root }).then(() => {
return execCommand(`${app.runner} build` + (args.length ? ` ${args.join(' ')}` : ''), { cwd: root })
.then(() => {
const appName = app.name;
const artifactsPath = path_1.join(root, `src-tauri/target/${debug ? 'debug' : 'release'}`);
switch (os_1.platform()) {
Expand All @@ -10743,18 +10758,21 @@ function buildProject(root, debug, { configPath, distPath, iconPath, npmScript }
];
case 'win32':
return [
path_1.join(artifactsPath, `bundle/msi/${appName}_${app.version}_${process.arch}.msi`),
path_1.join(artifactsPath, `bundle/msi/${appName}_${app.version}_${process.arch}.msi`)
];
default:
const arch = process.arch === 'x64'
? 'amd64'
: (process.arch === 'x32' ? 'i386' : process.arch);
: process.arch === 'x32'
? 'i386'
: process.arch;
return [
path_1.join(artifactsPath, `bundle/deb/${appName}_${app.version}_${arch}.deb`),
path_1.join(artifactsPath, `bundle/appimage/${appName}_${app.version}_${arch}.AppImage`)
];
}
}).then(paths => paths.filter(p => fs_1.existsSync(p)));
})
.then(paths => paths.filter(p => fs_1.existsSync(p)));
});
});
}
Expand All @@ -10776,7 +10794,12 @@ function run() {
if (Boolean(tagName) !== Boolean(releaseName)) {
throw new Error('`tag` is required along with `releaseName` when creating a release.');
}
const options = { configPath: fs_1.existsSync(configPath) ? configPath : null, distPath, iconPath, npmScript };
const options = {
configPath: fs_1.existsSync(configPath) ? configPath : null,
distPath,
iconPath,
npmScript
};
const artifacts = yield buildProject(projectPath, false, options);
if (includeDebug) {
const debugArtifacts = yield buildProject(projectPath, true, options);
Expand All @@ -10789,10 +10812,12 @@ function run() {
let releaseId;
if (tagName) {
const packageJson = getPackageJson(projectPath);
const templates = [{
const templates = [
{
key: '__VERSION__',
value: packageJson === null || packageJson === void 0 ? void 0 : packageJson.version
}];
}
];
templates.forEach(template => {
const regex = new RegExp(template.key, 'g');
tagName = tagName.replace(regex, template.value);
Expand All @@ -10813,7 +10838,9 @@ function run() {
let i = 0;
for (const artifact of artifacts) {
if (artifact.endsWith('.app')) {
yield execCommand(`tar -czf ${artifact}.tgz ${artifact}`, { cwd: undefined });
yield execCommand(`tar -czf ${artifact}.tgz ${artifact}`, {
cwd: undefined
});
artifacts[i] += '.tgz';
}
i++;
Expand All @@ -10823,6 +10850,7 @@ function run() {
}
}
catch (error) {
console.log('this');
core.setFailed(error.message);
}
});
Expand Down
50 changes: 36 additions & 14 deletions dist/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ function getPackageJson(root) {
}
function hasTauriDependency(root) {
const packageJson = getPackageJson(root);
return packageJson && ((packageJson.dependencies && packageJson.dependencies.tauri) || ((packageJson.devDependencies && packageJson.devDependencies.tauri)));
return (packageJson &&
((packageJson.dependencies && packageJson.dependencies.tauri) ||
(packageJson.devDependencies && packageJson.devDependencies.tauri)));
}
function usesYarn(root) {
return fs_1.existsSync(path_1.join(root, 'yarn.lock'));
Expand All @@ -63,12 +65,12 @@ function execCommand(command, { cwd }) {
shell: process.env.shell || true,
windowsHide: true,
stdio: 'inherit',
env: { FORCE_COLOR: '0' },
env: { FORCE_COLOR: '0' }
}).then();
}
function buildProject(root, debug, { configPath, distPath, iconPath, npmScript }) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve) => {
return new Promise(resolve => {
if (hasTauriDependency(root)) {
if (npmScript) {
resolve(usesYarn(root) ? `yarn ${npmScript}` : `npm run ${npmScript}`);
Expand All @@ -93,8 +95,12 @@ function buildProject(root, debug, { configPath, distPath, iconPath, npmScript }
}
else {
const packageJson = getPackageJson(root);
const appName = packageJson ? (packageJson.displayName || packageJson.name).replace(/ /g, '-') : 'app';
return execCommand(`${runner} init --ci --app-name ${appName}`, { cwd: root }).then(() => {
const appName = packageJson
? (packageJson.displayName || packageJson.name).replace(/ /g, '-')
: 'app';
return execCommand(`${runner} init --ci --app-name ${appName}`, {
cwd: root
}).then(() => {
const cargoManifest = toml_1.default.parse(fs_1.readFileSync(manifestPath).toString());
const version = packageJson ? packageJson.version : '0.1.0';
console.log(`Replacing cargo manifest options - package.version=${version}`);
Expand All @@ -106,7 +112,9 @@ function buildProject(root, debug, { configPath, distPath, iconPath, npmScript }
version
};
if (iconPath) {
return execCommand(`${runner} icon --i ${path_1.join(root, iconPath)}`, { cwd: root }).then(() => app);
return execCommand(`${runner} icon --i ${path_1.join(root, iconPath)}`, {
cwd: root
}).then(() => app);
}
return app;
});
Expand All @@ -123,7 +131,8 @@ function buildProject(root, debug, { configPath, distPath, iconPath, npmScript }
fs_1.writeFileSync(tauriConfPath, JSON.stringify(tauriConf));
}
const args = debug ? ['--debug'] : [];
return execCommand(`${app.runner} build` + (args.length ? ` ${args.join(' ')}` : ''), { cwd: root }).then(() => {
return execCommand(`${app.runner} build` + (args.length ? ` ${args.join(' ')}` : ''), { cwd: root })
.then(() => {
const appName = app.name;
const artifactsPath = path_1.join(root, `src-tauri/target/${debug ? 'debug' : 'release'}`);
switch (os_1.platform()) {
Expand All @@ -134,18 +143,21 @@ function buildProject(root, debug, { configPath, distPath, iconPath, npmScript }
];
case 'win32':
return [
path_1.join(artifactsPath, `bundle/msi/${appName}_${app.version}_${process.arch}.msi`),
path_1.join(artifactsPath, `bundle/msi/${appName}_${app.version}_${process.arch}.msi`)
];
default:
const arch = process.arch === 'x64'
? 'amd64'
: (process.arch === 'x32' ? 'i386' : process.arch);
: process.arch === 'x32'
? 'i386'
: process.arch;
return [
path_1.join(artifactsPath, `bundle/deb/${appName}_${app.version}_${arch}.deb`),
path_1.join(artifactsPath, `bundle/appimage/${appName}_${app.version}_${arch}.AppImage`)
];
}
}).then(paths => paths.filter(p => fs_1.existsSync(p)));
})
.then(paths => paths.filter(p => fs_1.existsSync(p)));
});
});
}
Expand All @@ -167,7 +179,12 @@ function run() {
if (Boolean(tagName) !== Boolean(releaseName)) {
throw new Error('`tag` is required along with `releaseName` when creating a release.');
}
const options = { configPath: fs_1.existsSync(configPath) ? configPath : null, distPath, iconPath, npmScript };
const options = {
configPath: fs_1.existsSync(configPath) ? configPath : null,
distPath,
iconPath,
npmScript
};
const artifacts = yield buildProject(projectPath, false, options);
if (includeDebug) {
const debugArtifacts = yield buildProject(projectPath, true, options);
Expand All @@ -180,10 +197,12 @@ function run() {
let releaseId;
if (tagName) {
const packageJson = getPackageJson(projectPath);
const templates = [{
const templates = [
{
key: '__VERSION__',
value: packageJson === null || packageJson === void 0 ? void 0 : packageJson.version
}];
}
];
templates.forEach(template => {
const regex = new RegExp(template.key, 'g');
tagName = tagName.replace(regex, template.value);
Expand All @@ -204,7 +223,9 @@ function run() {
let i = 0;
for (const artifact of artifacts) {
if (artifact.endsWith('.app')) {
yield execCommand(`tar -czf ${artifact}.tgz ${artifact}`, { cwd: undefined });
yield execCommand(`tar -czf ${artifact}.tgz ${artifact}`, {
cwd: undefined
});
artifacts[i] += '.tgz';
}
i++;
Expand All @@ -214,6 +235,7 @@ function run() {
}
}
catch (error) {
console.log('this');
core.setFailed(error.message);
}
});
Expand Down
12 changes: 9 additions & 3 deletions dist/upload-release-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,21 @@ function uploadAssets(releaseId, assets) {
// Determine content-length for header to upload asset
const contentLength = (filePath) => fs_1.default.statSync(filePath).size;
for (const assetPath of assets) {
const headers = { 'content-type': 'application/zip', 'content-length': contentLength(assetPath) };
const headers = {
'content-type': 'application/zip',
'content-length': contentLength(assetPath)
};
const ext = path_1.default.extname(assetPath);
const filename = path_1.default.basename(assetPath).replace(ext, '');
const assetName = path_1.default.dirname(assetPath).includes(`target${path_1.default.sep}debug`) ? `${filename}-debug${ext}` : `${filename}${ext}`;
const assetName = path_1.default.dirname(assetPath).includes(`target${path_1.default.sep}debug`)
? `${filename}-debug${ext}`
: `${filename}${ext}`;
console.log(`Uploading ${assetName}...`);
yield github.repos.uploadReleaseAsset({
headers,
name: assetName,
data: fs_1.default.readFileSync(assetPath).toString(),
// @ts-ignore error TS2322: Type 'Buffer' is not assignable to type 'string'.
data: fs_1.default.readFileSync(assetPath),
owner: github_1.context.repo.owner,
repo: github_1.context.repo.repo,
release_id: releaseId
Expand Down
35 changes: 24 additions & 11 deletions src/create-release.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as core from '@actions/core'
import { getOctokit, context } from '@actions/github'
import { GitHub } from '@actions/github/lib/utils'
import {getOctokit, context} from '@actions/github'
import {GitHub} from '@actions/github/lib/utils'
import fs from 'fs'

interface Release {
Expand All @@ -18,14 +18,23 @@ interface GitHubRelease {
target_commitish: string
}

function allReleases(github: InstanceType<typeof GitHub>): AsyncIterableIterator<{ data: GitHubRelease[] }> {
const params = { per_page: 100, ...context.repo }
function allReleases(
github: InstanceType<typeof GitHub>
): AsyncIterableIterator<{data: GitHubRelease[]}> {
const params = {per_page: 100, ...context.repo}
return github.paginate.iterator(
github.repos.listReleases.endpoint.merge(params)
);
)
}

export default async function createRelease(tagName: string, releaseName: string, body?: string, commitish?: string, draft = true, prerelease = true): Promise<Release> {
export default async function createRelease(
tagName: string,
releaseName: string,
body?: string,
commitish?: string,
draft = true,
prerelease = true
): Promise<Release> {
if (process.env.GITHUB_TOKEN === undefined) {
throw new Error('GITHUB_TOKEN is required')
}
Expand All @@ -34,13 +43,13 @@ export default async function createRelease(tagName: string, releaseName: string
const github = getOctokit(process.env.GITHUB_TOKEN)

// Get owner and repo from context of payload that triggered the action
const { owner, repo } = context.repo
const {owner, repo} = context.repo

const bodyPath = core.getInput('body_path', { required: false })
const bodyPath = core.getInput('body_path', {required: false})
let bodyFileContent = null
if (bodyPath !== '' && !!bodyPath) {
try {
bodyFileContent = fs.readFileSync(bodyPath, { encoding: 'utf8' })
bodyFileContent = fs.readFileSync(bodyPath, {encoding: 'utf8'})
} catch (error) {
core.setFailed(error.message)
}
Expand All @@ -53,10 +62,14 @@ export default async function createRelease(tagName: string, releaseName: string
if (draft) {
console.log(`Looking for a draft release with tag ${tagName}...`)
for await (const response of allReleases(github)) {
let releaseWithTag = response.data.find(release => release.tag_name === tagName)
let releaseWithTag = response.data.find(
release => release.tag_name === tagName
)
if (releaseWithTag) {
release = releaseWithTag
console.log(`Found draft release with tag ${tagName} on the release list.`)
console.log(
`Found draft release with tag ${tagName} on the release list.`
)
break
}
}
Expand Down
Loading

0 comments on commit cf98c66

Please sign in to comment.