Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Utilities #18

Merged
merged 2 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 77 additions & 33 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6185,6 +6185,75 @@ module.exports = timer;
module.exports["default"] = timer;


/***/ }),

/***/ 4478:
/***/ ((__unused_webpack_module, exports) => {

"use strict";

exports.__esModule = true;
exports.chunkArray = void 0;
function chunkArray(arr, chunkSize) {
if (!Array.isArray(arr))
throw new Error('arr must be an array');
if (!Number.isInteger(chunkSize) && chunkSize >= 0)
throw new Error('chunkSize must be a positive integer');
var res = [];
for (var i = 0; i < arr.length; i += chunkSize) {
var chunk = arr.slice(i, i + chunkSize);
res.push(chunk);
}
return res;
}
exports.chunkArray = chunkArray;
//# sourceMappingURL=index.js.map

/***/ }),

/***/ 9497:
/***/ ((__unused_webpack_module, exports) => {

"use strict";

exports.__esModule = true;
exports.isDifferent = void 0;
function isDifferent(firstValue, secondValue) {
return JSON.stringify(firstValue) !== JSON.stringify(secondValue);
}
exports.isDifferent = isDifferent;
//# sourceMappingURL=index.js.map

/***/ }),

/***/ 7348:
/***/ ((__unused_webpack_module, exports) => {

"use strict";

exports.__esModule = true;
exports.softAssign = void 0;
var isObject = function (obj) {
return typeof obj === 'object' && !Array.isArray(obj) && obj !== null;
};
function softAssign(obj, keys, value) {
if (!isObject(obj))
throw new Error('obj must be an object');
if (!Array.isArray(keys) || (keys.length === 0))
throw new Error('keys must be an Array with at least one element');
var lastKeyIndex = keys.length - 1;
for (var i = 0; i < lastKeyIndex; ++i) {
var key = keys[i];
if (!(key in obj)) {
obj[key] = {};
}
obj = obj[key];
}
obj[keys[lastKeyIndex]] = obj[keys[lastKeyIndex]] !== undefined ? obj[keys[lastKeyIndex]] : value;
}
exports.softAssign = softAssign;
//# sourceMappingURL=index.js.map

/***/ }),

/***/ 3682:
Expand Down Expand Up @@ -19915,15 +19984,17 @@ function wrappy (fn, cb) {
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {

const core = __nccwpck_require__(2186)
const { spliceIntoChunks, getProjectScore, generateIssueContent, generateReportContent, getScore, saveScore } = __nccwpck_require__(1608)
const { getProjectScore, generateIssueContent, generateReportContent, getScore, saveScore } = __nccwpck_require__(1608)
const { chunkArray } = __nccwpck_require__(4478)

const generateScores = async ({ scope, database: currentDatabase, maxRequestInParallel }) => {
// @TODO: Improve deep clone logic
const database = JSON.parse(JSON.stringify(currentDatabase))
const platform = 'github.com'
const projects = scope[platform]
core.debug(`Total projects in scope: ${projects.length}`)

const chunks = spliceIntoChunks(projects, maxRequestInParallel)
const chunks = chunkArray(projects, maxRequestInParallel)
core.debug(`Total chunks: ${chunks.length}`)

const scores = []
Expand Down Expand Up @@ -19983,33 +20054,7 @@ const core = __nccwpck_require__(2186)
const ejs = __nccwpck_require__(8431)
const { readFile } = (__nccwpck_require__(7147).promises)
const { join } = __nccwpck_require__(1017)

const isDifferentContent = (oldContent, newContent) => {
return JSON.stringify(oldContent) !== JSON.stringify(newContent)
}

function spliceIntoChunks (arr, chunkSize) {
// @see: https://stackabuse.com/how-to-split-an-array-into-even-chunks-in-javascript/
const res = []
while (arr.length > 0) {
const chunk = arr.splice(0, chunkSize)
res.push(chunk)
}
return res
}

const softAssign = (obj, keyPath, value) => {
// @see: https://stackoverflow.com/a/5484764
const lastKeyIndex = keyPath.length - 1
for (let i = 0; i < lastKeyIndex; ++i) {
const key = keyPath[i]
if (!(key in obj)) {
obj[key] = {}
}
obj = obj[key]
}
obj[keyPath[lastKeyIndex]] = obj[keyPath[lastKeyIndex]] || value
}
const { softAssign } = __nccwpck_require__(7348)

const getProjectScore = async ({ platform, org, repo }) => {
core.debug(`Getting project score for ${platform}/${org}/${repo}`)
Expand Down Expand Up @@ -20052,10 +20097,8 @@ const generateIssueContent = async (scores) => {

module.exports = {
getProjectScore,
isDifferentContent,
saveScore,
getScore,
spliceIntoChunks,
generateReportContent,
generateIssueContent
}
Expand Down Expand Up @@ -20300,9 +20343,10 @@ const core = __nccwpck_require__(2186)
const github = __nccwpck_require__(5438)
const exec = __nccwpck_require__(1514)
const { normalizeBoolean } = __nccwpck_require__(6446)

const { readFile, writeFile, stat } = (__nccwpck_require__(7147).promises)

const { isDifferentContent } = __nccwpck_require__(1608)
const { isDifferent } = __nccwpck_require__(9497)
const { generateScores } = __nccwpck_require__(4351)

async function run () {
Expand Down Expand Up @@ -20349,7 +20393,7 @@ async function run () {
const { reportContent, issueContent, database: newDatabaseState } = await generateScores({ scope, database, maxRequestInParallel })

core.info('Checking database changes...')
const hasChanges = isDifferentContent(database, newDatabaseState)
const hasChanges = isDifferent(database, newDatabaseState)

if (!hasChanges) {
core.info('No changes to database, skipping the rest of the process')
Expand Down
44 changes: 43 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@
"@actions/core": "1.10.0",
"@actions/exec": "1.1.1",
"@actions/github": "5.1.1",
"@ulisesgascon/array-to-chunks": "1.0.1",
"@ulisesgascon/is-different": "1.0.0",
"@ulisesgascon/soft-assign-deep-property": "1.0.0",
"@vercel/ncc": "0.36.1",
"ejs": "3.1.8",
"got": "11.8.6",
"normalize-boolean": "^2.0.1"
"normalize-boolean": "2.0.1"
},
"engines": {
"node": ">=16.0.0"
Expand Down
5 changes: 3 additions & 2 deletions src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ const core = require('@actions/core')
const github = require('@actions/github')
const exec = require('@actions/exec')
const { normalizeBoolean } = require('normalize-boolean')

const { readFile, writeFile, stat } = require('fs').promises

const { isDifferentContent } = require('./utils')
const { isDifferent } = require('@ulisesgascon/is-different')
const { generateScores } = require('./')

async function run () {
Expand Down Expand Up @@ -51,7 +52,7 @@ async function run () {
const { reportContent, issueContent, database: newDatabaseState } = await generateScores({ scope, database, maxRequestInParallel })

core.info('Checking database changes...')
const hasChanges = isDifferentContent(database, newDatabaseState)
const hasChanges = isDifferent(database, newDatabaseState)

if (!hasChanges) {
core.info('No changes to database, skipping the rest of the process')
Expand Down
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const core = require('@actions/core')
const { spliceIntoChunks, getProjectScore, generateIssueContent, generateReportContent, getScore, saveScore } = require('./utils')
const { getProjectScore, generateIssueContent, generateReportContent, getScore, saveScore } = require('./utils')
const { chunkArray } = require('@ulisesgascon/array-to-chunks')

const generateScores = async ({ scope, database: currentDatabase, maxRequestInParallel }) => {
// @TODO: Improve deep clone logic
const database = JSON.parse(JSON.stringify(currentDatabase))
const platform = 'github.com'
const projects = scope[platform]
core.debug(`Total projects in scope: ${projects.length}`)

const chunks = spliceIntoChunks(projects, maxRequestInParallel)
const chunks = chunkArray(projects, maxRequestInParallel)
core.debug(`Total chunks: ${chunks.length}`)

const scores = []
Expand Down
30 changes: 1 addition & 29 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,7 @@ const core = require('@actions/core')
const ejs = require('ejs')
const { readFile } = require('fs').promises
const { join } = require('path')

const isDifferentContent = (oldContent, newContent) => {
return JSON.stringify(oldContent) !== JSON.stringify(newContent)
}

function spliceIntoChunks (arr, chunkSize) {
// @see: https://stackabuse.com/how-to-split-an-array-into-even-chunks-in-javascript/
const res = []
while (arr.length > 0) {
const chunk = arr.splice(0, chunkSize)
res.push(chunk)
}
return res
}

const softAssign = (obj, keyPath, value) => {
// @see: https://stackoverflow.com/a/5484764
const lastKeyIndex = keyPath.length - 1
for (let i = 0; i < lastKeyIndex; ++i) {
const key = keyPath[i]
if (!(key in obj)) {
obj[key] = {}
}
obj = obj[key]
}
obj[keyPath[lastKeyIndex]] = obj[keyPath[lastKeyIndex]] || value
}
const { softAssign } = require('@ulisesgascon/soft-assign-deep-property')

const getProjectScore = async ({ platform, org, repo }) => {
core.debug(`Getting project score for ${platform}/${org}/${repo}`)
Expand Down Expand Up @@ -72,10 +46,8 @@ const generateIssueContent = async (scores) => {

module.exports = {
getProjectScore,
isDifferentContent,
saveScore,
getScore,
spliceIntoChunks,
generateReportContent,
generateIssueContent
}