Skip to content

Commit

Permalink
feat: output log.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jul 28, 2024
1 parent fa106ae commit d2fc89c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
13 changes: 5 additions & 8 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44973,15 +44973,12 @@ tagName=github.context.ref;myToken=(0,core.getInput)('token');// This removes th
currentTag=tag||tagName.replace("refs/tags/","");octokit=(0,github.getOctokit)(myToken);(0,core.info)("Getting release for tag: ".concat(currentTag));// Get a release from the tag name
// API Documentation: https://developer.github.com/v3/repos/releases/#create-a-release
// Octokit Documentation: https://octokit.github.io/rest.js/#octokit-routes-repos-create-release
_context2.next=8;return octokit.rest.repos.getReleaseByTag({owner:owner,repo:repo,tag:currentTag});case 8:getReleaseResponse=_context2.sent;return _context2.abrupt("return",getReleaseResponse.data);case 10:case"end":return _context2.stop();}},_callee2);}));return _getReleaseURL.apply(this,arguments);}function getAssetName(assetPath){try{return JSON.parse(assetPath);}catch(error){if(assetPath){return assetPath;}}return undefined;};(0,asyncToGenerator/* default */.A)(/*#__PURE__*/(0,regeneratorRuntime/* default */.A)().mark(function _callee(){var assetPath,tagName,myToken,files,octokit,release,downloadURLs,i,assetFile,contentLength,contentType,headers,assetName,data,response;return (0,regeneratorRuntime/* default */.A)().wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:/**
_context2.next=8;return octokit.rest.repos.getReleaseByTag({owner:owner,repo:repo,tag:currentTag});case 8:getReleaseResponse=_context2.sent;return _context2.abrupt("return",getReleaseResponse.data);case 10:case"end":return _context2.stop();}},_callee2);}));return _getReleaseURL.apply(this,arguments);}function getAssetName(assetPath){try{return JSON.parse(assetPath);}catch(error){if(assetPath){return assetPath;}}return undefined;}// Function to upload the file
function uploadFile(_x2,_x3,_x4,_x5){return _uploadFile.apply(this,arguments);}function _uploadFile(){_uploadFile=(0,asyncToGenerator/* default */.A)(/*#__PURE__*/(0,regeneratorRuntime/* default */.A)().mark(function _callee3(owner,repo,releaseId,filePath){var myToken,octokit,fileContent,fileStat,fileName,headers,response;return (0,regeneratorRuntime/* default */.A)().wrap(function _callee3$(_context3){while(1)switch(_context3.prev=_context3.next){case 0:myToken=(0,core.getInput)('token');octokit=(0,github.getOctokit)(myToken);fileContent=external_fs_default().readFileSync(filePath);// Read file content as Buffer
fileStat=external_fs_default().statSync(filePath);fileName=external_path_default().basename(filePath);headers={'content-type':'application/octet-stream','content-length':fileStat.size};_context3.next=8;return octokit.rest.repos.uploadReleaseAsset({owner:owner,repo:repo,release_id:releaseId,headers:headers,name:fileName,data:fileContent.toString()// Use file content directly
});case 8:response=_context3.sent;return _context3.abrupt("return",response);case 10:case"end":return _context3.stop();}},_callee3);}));return _uploadFile.apply(this,arguments);};(0,asyncToGenerator/* default */.A)(/*#__PURE__*/(0,regeneratorRuntime/* default */.A)().mark(function _callee(){var assetPath,tagName,myToken,files,release,downloadURLs,i,assetFile,response;return (0,regeneratorRuntime/* default */.A)().wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:/**
* Get the path to the file to upload
*/assetPath=getAssetName((0,core.getInput)('asset-path',{required:true}));tagName=(0,core.getInput)('tag')||undefined;myToken=(0,core.getInput)('token');_context.prev=3;if(!(!assetPath||assetPath.length===0)){_context.next=6;break;}throw new Error('asset-path must contain a JSON array of quoted paths');case 6:_context.next=8;return glob(assetPath,{});case 8:files=_context.sent;if(!(files.length===0)){_context.next=11;break;}throw new Error('No files found');case 11:octokit=(0,github.getOctokit)(myToken);_context.next=14;return getReleaseURL(tagName);case 14:release=_context.sent;downloadURLs=[];i=0;case 17:if(!(i<files.length)){_context.next=32;break;}assetFile=files[i];// Determine content-length for header to upload asset
contentLength=function contentLength(filePath){return external_fs_default().statSync(filePath).size;};contentType='application/octet-stream';//const contentType = "binary/octet-stream"
// Setup headers for API call, see Octokit Documentation: https://octokit.github.io/rest.js/v20#repos-update-release-asset for more information
headers={'content-type':contentType,'content-length':contentLength(assetFile)};assetName=external_path_default().basename(assetFile);data=external_fs_default().readFileSync(assetFile);// Upload a release asset
// API Documentation: https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#update-a-release
// Octokit Documentation: https://octokit.github.io/rest.js/v20#repos-update-release-asset
_context.next=26;return octokit.rest.repos.uploadReleaseAsset({owner:github.context.repo.owner,repo:github.context.repo.repo,release_id:release.id,headers:headers,name:assetName,data:data.toString()});case 26:response=_context.sent;downloadURLs.push(response.data.browser_download_url);if(response.status<200||response.status>299){new Error("Asset upload failed \"".concat(assetPath,". Response:\" ").concat(response));}case 29:i++;_context.next=17;break;case 32:(0,core.setOutput)('browser_download_urls',JSON.stringify(downloadURLs));_context.next=38;break;case 35:_context.prev=35;_context.t0=_context["catch"](3);(0,core.setFailed)(_context.t0);case 38:case"end":return _context.stop();}},_callee,null,[[3,35]]);}))();
*/assetPath=getAssetName((0,core.getInput)('asset-path',{required:true}));tagName=(0,core.getInput)('tag')||undefined;myToken=(0,core.getInput)('token');_context.prev=3;if(!(!assetPath||assetPath.length===0)){_context.next=6;break;}throw new Error('asset-path must contain a JSON array of quoted paths');case 6:_context.next=8;return glob(assetPath,{});case 8:files=_context.sent;if(!(files.length===0)){_context.next=11;break;}throw new Error('No files found');case 11:_context.next=13;return getReleaseURL(tagName);case 13:release=_context.sent;downloadURLs=[];i=0;case 16:if(!(i<files.length)){_context.next=27;break;}assetFile=files[i];(0,core.info)("Uploading asset(".concat(github.context.repo.owner,"/").concat(github.context.repo.repo,"): ").concat(assetFile));_context.next=21;return uploadFile(github.context.repo.owner,github.context.repo.repo,release.id,assetFile);case 21:response=_context.sent;downloadURLs.push(response.data.browser_download_url);if(response.status<200||response.status>299){new Error("Asset upload failed \"".concat(assetPath,". Response:\" ").concat(response));}case 24:i++;_context.next=16;break;case 27:(0,core.setOutput)('browser_download_urls',JSON.stringify(downloadURLs));_context.next=33;break;case 30:_context.prev=30;_context.t0=_context["catch"](3);(0,core.setFailed)(_context.t0);case 33:case"end":return _context.stop();}},_callee,null,[[3,30]]);}))();
})();

module.exports = __webpack_exports__;
Expand Down
51 changes: 28 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,32 @@ function getAssetName(assetPath: string): string | string[] | undefined {
return undefined;
}

// Function to upload the file
async function uploadFile(owner: string, repo: string, releaseId: number, filePath: string) {
const myToken = getInput('token')
const octokit = getOctokit(myToken);

const fileContent = fs.readFileSync(filePath); // Read file content as Buffer
const fileStat = fs.statSync(filePath);
const fileName = path.basename(filePath);

const headers = {
'content-type': 'application/octet-stream',
'content-length': fileStat.size
};

const response = await octokit.rest.repos.uploadReleaseAsset({
owner: owner,
repo: repo,
release_id: releaseId,
headers,
name: fileName,
data: fileContent.toString() // Use file content directly
});

return response;
}

;(async () => {
/**
* Get the path to the file to upload
Expand All @@ -56,33 +82,12 @@ function getAssetName(assetPath: string): string | string[] | undefined {
if (files.length === 0) {
throw new Error('No files found');
}
const octokit = getOctokit(myToken);
const release = await getReleaseURL(tagName)
const downloadURLs = []
for(let i = 0; i < files.length; i++) {
const assetFile = files[i];
// Determine content-length for header to upload asset
const contentLength = (filePath: string) => fs.statSync(filePath).size;
const contentType = 'application/octet-stream';
//const contentType = "binary/octet-stream"
// Setup headers for API call, see Octokit Documentation: https://octokit.github.io/rest.js/v20#repos-update-release-asset for more information
const headers = {
'content-type': contentType,
'content-length': contentLength(assetFile)
};
const assetName = path.basename(assetFile);
const data = fs.readFileSync(assetFile);
// Upload a release asset
// API Documentation: https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#update-a-release
// Octokit Documentation: https://octokit.github.io/rest.js/v20#repos-update-release-asset
const response = await octokit.rest.repos.uploadReleaseAsset({
owner: context.repo.owner,
repo: context.repo.repo,
release_id: release.id,
headers,
name: assetName,
data: data.toString()
});
info(`Uploading asset(${context.repo.owner}/${context.repo.repo}): ${assetFile}`);
const response = await uploadFile(context.repo.owner, context.repo.repo, release.id, assetFile);
downloadURLs.push(response.data.browser_download_url)
if (response.status < 200 || response.status > 299) {
new Error(`Asset upload failed "${assetPath}. Response:" ${response}`)
Expand Down

0 comments on commit d2fc89c

Please sign in to comment.