From c7f7cc2d230245ff2e31756f79fc69edb7a4d420 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 19 Sep 2023 13:55:39 +0000 Subject: [PATCH 1/3] Commit message here --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index abe91ee..e12b423 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "docker-container-action", - "version": "1.5.0", + "version": "1.6.0", "description": "This action prints \"Hello World\" or \"Hello\" + the name of a person to greet to the log.", "main": "index.js", "scripts": { From 1171d06e7008d079fcf8e814dbe860d05b69ceb9 Mon Sep 17 00:00:00 2001 From: Shakar Bakr <5h4k4r.b4kr@gmail.com> Date: Tue, 19 Sep 2023 16:57:05 +0300 Subject: [PATCH 2/3] Build index.js with @vercel/ncc Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com> --- action.yml | 2 +- dist/index.js | 280 ++++++++++++++++++++++++++++++++++++++++++++++ dist/licenses.txt | 9 ++ 3 files changed, 290 insertions(+), 1 deletion(-) create mode 100644 dist/index.js create mode 100644 dist/licenses.txt diff --git a/action.yml b/action.yml index 1c0992f..3ad0cb9 100644 --- a/action.yml +++ b/action.yml @@ -17,4 +17,4 @@ outputs: runs: using: "node20" - main: "index.js" + main: "dist/index.js" diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..4fc54c1 --- /dev/null +++ b/dist/index.js @@ -0,0 +1,280 @@ +/******/ (() => { // webpackBootstrap +/******/ var __webpack_modules__ = ({ + +/***/ 958: +/***/ ((module) => { + +module.exports = eval("require")("@actions/core"); + + +/***/ }), + +/***/ 257: +/***/ ((module) => { + +module.exports = eval("require")("@actions/github"); + + +/***/ }), + +/***/ 462: +/***/ ((module) => { + +module.exports = eval("require")("axios"); + + +/***/ }), + +/***/ 147: +/***/ ((module) => { + +"use strict"; +module.exports = require("fs"); + +/***/ }) + +/******/ }); +/************************************************************************/ +/******/ // The module cache +/******/ var __webpack_module_cache__ = {}; +/******/ +/******/ // The require function +/******/ function __nccwpck_require__(moduleId) { +/******/ // Check if module is in cache +/******/ var cachedModule = __webpack_module_cache__[moduleId]; +/******/ if (cachedModule !== undefined) { +/******/ return cachedModule.exports; +/******/ } +/******/ // Create a new module (and put it into the cache) +/******/ var module = __webpack_module_cache__[moduleId] = { +/******/ // no module.id needed +/******/ // no module.loaded needed +/******/ exports: {} +/******/ }; +/******/ +/******/ // Execute the module function +/******/ var threw = true; +/******/ try { +/******/ __webpack_modules__[moduleId](module, module.exports, __nccwpck_require__); +/******/ threw = false; +/******/ } finally { +/******/ if(threw) delete __webpack_module_cache__[moduleId]; +/******/ } +/******/ +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } +/******/ +/************************************************************************/ +/******/ /* webpack/runtime/compat */ +/******/ +/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/"; +/******/ +/************************************************************************/ +var __webpack_exports__ = {}; +// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk. +(() => { +const core = __nccwpck_require__(958) +const github = __nccwpck_require__(257) +const fs = __nccwpck_require__(147) +const axios = __nccwpck_require__(462); + +run(); +async function run() { + try { + console.log(`CWD: ${process.cwd()}`) + + const filePathInput = core.getInput('filePath'); + const labelInput = core.getInput('label'); + + const filePath = getProjectInfoFilePath(filePathInput); + const file = require(filePath); + + + console.log(`Label: ${labelInput}`) + console.log(`File path: ${file}`) + + core.setOutput("label", labelInput); + + const version = getProjectVersion(filePath); + + // the version is in semantic format, so we can split it by dot + const versionParts = version.split('.'); + // 1.2.3 => [1, 2, 3] + if (labelInput === 'major') { + + versionParts[0] = parseInt(versionParts[0]) + 1; + versionParts[1] = 0; + versionParts[2] = 0; + + } else if (labelInput == 'minor') { + + versionParts[1] = parseInt(versionParts[1]) + 1; + versionParts[2] = 0; + + } + else if (labelInput == 'patch') + versionParts[2] = parseInt(versionParts[2]) + 1; + + + // join the parts back together + const newVersion = versionParts.join('.'); + + updateProjectVersion(filePath, newVersion); + + console.log(`Old version: ${version}. New version: ${newVersion}`) + + const filePathRelatedToRoot = getProjectInfoFilePath(filePathInput, true); + await commitChanges(file, filePathRelatedToRoot); + + // console.log(`The event payload: ${payload}`); + } catch (error) { + core.setFailed(error.message); + } +} + + +// Region functions +async function commitChanges(file, filePath) { + const commitMessage = 'Commit message here'; + + let newContent = JSON.stringify(file, null, 2); + // Append a newline character to the end of the new content + newContent += '\n'; + + const githubToken = core.getInput('githubToken'); + + // Get the repository owner and name + const repoFullName = process.env.GITHUB_REPOSITORY; + const [owner, repo] = repoFullName.split('/'); + + // Get the current branch + const branch = process.env.GITHUB_REF.replace('refs/heads/', ''); + + try { + // Get the current commit SHA for the branch + const branchResponse = await axios.get( + `https://api.github.com/repos/${owner}/${repo}/branches/${branch}` + ); + + const baseTreeSha = branchResponse.data.commit.sha; + console.log(typeof file, file) + // Create a new blob with the updated content + const blobResponse = await axios.post( + `https://api.github.com/repos/${owner}/${repo}/git/blobs`, + { + content: newContent, + encoding: 'utf-8', + }, + { + headers: { + 'Accept': 'application/vnd.github.v3+json', + 'Authorization': `Bearer ${githubToken}`, + }, + } + ); + + console.log('Blob Created') + console.log('File path: ', filePath) + const newBlobSha = blobResponse.data.sha; + // Create a new tree with the updated blob + const treeResponse = await axios.post( + `https://api.github.com/repos/${owner}/${repo}/git/trees`, + { + base_tree: baseTreeSha, + tree: [ + { + path: filePath, + mode: '100644', + type: 'blob', + sha: newBlobSha, + }, + ], + }, + { + headers: { + 'Accept': 'application/vnd.github+json', + 'Authorization': `Bearer ${githubToken}` + }, + } + ); + + console.log('Tree Created') + const newTreeSha = treeResponse.data.sha; + + // Create a new commit + const commitResponse = await axios.post( + `https://api.github.com/repos/${owner}/${repo}/git/commits`, + { + message: commitMessage, + tree: newTreeSha, + parents: [baseTreeSha], + }, + { + headers: { + 'Accept': 'application/vnd.github+json', + 'Authorization': `Bearer ${githubToken}`, + }, + } + ); + + console.log('Commit Created') + const newCommitSha = commitResponse.data.sha; + + // Update the branch reference + await axios.patch( + `https://api.github.com/repos/${owner}/${repo}/git/refs/heads/${branch}`, + { + sha: newCommitSha, + }, + { + headers: { + 'Accept': 'application/vnd.github+json', + 'Authorization': `Bearer ${githubToken}`, + }, + } + ); + console.log('Branch Updated') + } catch (error) { + core.setFailed(error); + + } +} + +function getProjectInfoFilePath(filePath, relativeToRoot = false) { + if (filePath == null || filePath == undefined || filePath == '') { + // List files inside the root directory of the repository + const files = fs.readdirSync(process.cwd()); + // Return the first file that matches .csproj or package.json + const projectInfoFile = files.find(file => file.match(/\.csproj|package\.json/)); + return relativeToRoot ? projectInfoFile : `${process.cwd()}/${projectInfoFile}`; + } + else + return relativeToRoot ? filePath : `${process.cwd()}/${filePath}`; +} +function getProjectVersion(filePath) { + const projectInfoFile = require(filePath); + + // Update the version if the file is .csproj + if (filePath.match(/\.csproj/)) + return projectInfoFile.Project.PropertyGroup[0].Version; + else if (filePath.match(/package\.json/)) + return projectInfoFile.version; + +} +function updateProjectVersion(filePath, newVersion) { + + const projectInfoFile = require(filePath); + + // Update the version if the file is .csproj + if (filePath.match(/\.csproj/)) + projectInfoFile.Project.PropertyGroup[0].Version = newVersion; + else if (filePath.match(/package\.json/)) + projectInfoFile.version = newVersion; +} + +})(); + +module.exports = __webpack_exports__; +/******/ })() +; \ No newline at end of file diff --git a/dist/licenses.txt b/dist/licenses.txt new file mode 100644 index 0000000..a5ac3e7 --- /dev/null +++ b/dist/licenses.txt @@ -0,0 +1,9 @@ +@vercel/ncc +MIT +Copyright 2018 ZEIT, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. From 63a5267ec34bf1b6d91351d87e97d12ddd8f7f75 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 19 Sep 2023 13:57:34 +0000 Subject: [PATCH 3/3] Commit message here --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e12b423..85c9a1f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "docker-container-action", - "version": "1.6.0", + "version": "1.7.0", "description": "This action prints \"Hello World\" or \"Hello\" + the name of a person to greet to the log.", "main": "index.js", "scripts": {