From ce8a1a51ad80134b7f954d7ee373cbaba4ef74dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Corr=C3=AAa=20da=20Silva?= <78980992+dartmol203@users.noreply.github.com> Date: Mon, 18 Nov 2024 21:10:53 -0300 Subject: [PATCH] FIX(fga-eps-mds/2024.2-ARANDU-DOC#23): fixing script to generate metrics Co-authored-by: gabrielm2q --- scripts/.gitignore | 161 +++++++++++++++++++++++++++++++++++++++++++ scripts/metrics.js | 26 +++++++ scripts/package.json | 13 ++++ scripts/release.js | 87 +++++++++++++++++++++++ 4 files changed, 287 insertions(+) create mode 100644 scripts/.gitignore create mode 100644 scripts/metrics.js create mode 100644 scripts/package.json create mode 100644 scripts/release.js diff --git a/scripts/.gitignore b/scripts/.gitignore new file mode 100644 index 0000000..3da4592 --- /dev/null +++ b/scripts/.gitignore @@ -0,0 +1,161 @@ +# Created by https://www.toptal.com/developers/gitignore/api/yarn,node +# Edit at https://www.toptal.com/developers/gitignore?templates=yarn,node + +### Node ### +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) +web_modules/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional stylelint cache +.stylelintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variable files +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) +.cache +.parcel-cache + +# Next.js build output +.next +out + +# Nuxt.js build / generate output +.nuxt +dist + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and not Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# vuepress v2.x temp and cache directory +.temp + +# Docusaurus cache and generated files +.docusaurus + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port + +# Stores VSCode versions used for testing VSCode extensions +.vscode-test + +# yarn v2 +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.* + +### Node Patch ### +# Serverless Webpack directories +.webpack/ + +# Optional stylelint cache + +# SvelteKit build / generate output +.svelte-kit + +### yarn ### +# https://yarnpkg.com/getting-started/qa#which-files-should-be-gitignored + +.yarn/* +!.yarn/releases +!.yarn/patches +!.yarn/plugins +!.yarn/sdks +!.yarn/versions + +# if you are NOT using Zero-installs, then: +# comment the following lines +!.yarn/cache + +# and uncomment the following lines +# .pnp.* + +# End of https://www.toptal.com/developers/gitignore/api/yarn,node \ No newline at end of file diff --git a/scripts/metrics.js b/scripts/metrics.js new file mode 100644 index 0000000..01caac6 --- /dev/null +++ b/scripts/metrics.js @@ -0,0 +1,26 @@ +const REPO = '2024.2-ARANDU-APP'; +const OWNER = 'fga-eps-mds'; +const SONAR_ID = 'fga-eps-mds_2024.2-ARANDU-APP'; + +const METRIC_LIST = [ + 'files', + 'functions', + 'complexity', + 'comment_lines_density', + 'duplicated_lines_density', + 'coverage', + 'ncloc', + 'tests', + 'test_errors', + 'test_failures', + 'test_execution_time', + 'security_rating' +]; + +const SONAR_URL = `https://sonarcloud.io/api/measures/component_tree?component=${SONAR_ID}&metricKeys=${METRIC_LIST.join(',')}&ps=500`; + +module.exports = { + SONAR_URL, + REPO, + OWNER +}; \ No newline at end of file diff --git a/scripts/package.json b/scripts/package.json new file mode 100644 index 0000000..1e17830 --- /dev/null +++ b/scripts/package.json @@ -0,0 +1,13 @@ +{ + "name": "scripts", + "version": "1.0.0", + "main": "index.js", + "license": "GPL-3.0 license", + "dependencies": { + "@octokit/core": "^5.0.1", + "axios": "^1.5.1", + "dotenv": "^16.3.1", + "fs": "0.0.1-security", + "gh-release-assets": "^2.0.1" + } + } \ No newline at end of file diff --git a/scripts/release.js b/scripts/release.js new file mode 100644 index 0000000..8a3e97d --- /dev/null +++ b/scripts/release.js @@ -0,0 +1,87 @@ +const { Octokit } = require('@octokit/core') +const ghReleaseAssets = require('gh-release-assets') +const axios = require('axios') +const fs = require('fs') +require('dotenv').config() + +const { SONAR_URL, REPO, OWNER } = require('./metrics.js') + +const { TOKEN, RELEASE_MAJOR, RELEASE_MINOR } = process.env + +const octokit = new Octokit({ auth: TOKEN }) + +const now = new Date() // create a new Date object with the current date and time +const year = now.getFullYear().toString() +const month = (now.getMonth() + 1).toString().padStart(2, '0') +const day = now.getDate().toString().padStart(2, '0') +const hours = now.getHours().toString().padStart(2, '0') +const minutes = now.getMinutes().toString().padStart(2, '0') +const seconds = now.getSeconds().toString().padStart(2, '0') + +const getLatestRelease = async () => { + const releases = await octokit.request('GET /repos/{owner}/{repo}/releases', { + owner: OWNER, + repo: REPO + }) + if (releases?.data.length > 0) { + return releases?.data?.[0]?.tag_name + } + return '0.0.0' +} + +const newTagName = async () => { + let oldTag = await getLatestRelease() + oldTag = oldTag.split('.') + + if (RELEASE_MAJOR === 'true') { + const majorTagNum = parseInt(oldTag[0]) + 1 + return majorTagNum + '.0.0' + } else if (RELEASE_MINOR === 'true') { + const minorTagNum = parseInt(oldTag[1]) + 1 + return oldTag[0] + '.' + minorTagNum + '.0' + } + const fixTagNum = parseInt(oldTag[2]) + 1 + return oldTag[0] + '.' + oldTag[1] + '.' + fixTagNum +} + +const createRelease = async () => { + const tag = await newTagName() + const res = await octokit.request('POST /repos/{owner}/{repo}/releases', { + owner: OWNER, + repo: REPO, + tag_name: tag, + name: tag + }) + return [res?.data?.upload_url, tag] +} + +const saveSonarFile = async (tag) => { + const dirPath = './analytics-raw-data/' + fs.mkdirSync(dirPath) + const filePath = `${dirPath}fga-eps-mds-${REPO}-${month}-${day}-${year}-${hours}-${minutes}-${seconds}-${tag}.json` + await axios.get(SONAR_URL).then((res) => { + fs.writeFileSync(filePath, JSON.stringify(res?.data)) + }) +} + +const uploadSonarFile = async (release) => { + await saveSonarFile(release[1]) + ghReleaseAssets({ + url: release[0], + token: [TOKEN], + assets: [ + `./analytics-raw-data/fga-eps-mds-${REPO}-${month}-${day}-${year}-${hours}-${minutes}-${seconds}-${release[1]}.json`, + { + name: `fga-eps-mds-${REPO}-${month}-${day}-${year}-${hours}-${minutes}-${seconds}-${release[1]}.json`, + path: '' + } + ] + }) +} + +const script = async () => { + const release = await createRelease() + await uploadSonarFile(release) +} + +script() \ No newline at end of file