Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:fga-eps-mds/2023.2-UnB-TV-API-Ga…
Browse files Browse the repository at this point in the history
…teway into config-api-gateway
  • Loading branch information
joao15victor08 committed Dec 5, 2023
2 parents b327d65 + 64555ff commit 812f78e
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Análise de Código

on: push

jobs:
sonarcloud:
runs-on: ubuntu-latest

steps:
- name: Checkout ✅
uses: actions/checkout@v3

- name: Setup 🏗
uses: actions/setup-node@v2
with:
node-version: lts/*
cache: "npm"

- name: Install ⚙️
run: npm install

- name: Build 🛠
run: npm run build

- name: Test 📋
run: npm run test:prod

- name: Executa SonarCloud Scan
if: ${{ always() }}
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{secrets.API_TOKEN_GITHUB}}
SONAR_TOKEN: ${{secrets.SONAR_TOKEN}}
40 changes: 40 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Release

on:
pull_request:
branches:
- main
- develop
types: [closed]

jobs:
release:
if: github.event.pull_request.merged == true && contains(github.event.pull_request.labels.*.name, 'NOT RELEASE') == false
runs-on: "ubuntu-latest"

steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Cria arquivo .env
run: |
touch ./scripts/.env
echo TOKEN=${{ secrets.API_TOKEN_GITHUB }} >> ./scripts/.env
echo RELEASE_MAJOR=${{ contains(github.event.pull_request.labels.*.name, 'MAJOR RELEASE') }} >> ./scripts/.env
echo RELEASE_MINOR=${{ contains(github.event.pull_request.labels.*.name, 'MINOR RELEASE') }} >> ./scripts/.env
echo RELEASE_FIX=${{ contains(github.event.pull_request.labels.*.name, 'FIX RELEASE') }} >> ./scripts/.env
echo DEVELOP=${{ contains(github.event.pull_request.labels.*.name, 'DEVELOP') }} >> ./scripts/.env
- name: Gera release e envia métricas para repositório de DOC
run: |
cd scripts && yarn install && node release.js
git config --global user.email "${{secrets.GIT_USER_EMAIL}}"
git config --global user.name "${{secrets.GIT_USER_NAME}}"
git clone --single-branch --branch main "https://x-access-token:${{secrets.API_TOKEN_GITHUB}}@github.com/fga-eps-mds/${{secrets.GIT_DOC_REPO}}" ${{secrets.GIT_DOC_REPO}}
mkdir -p ${{secrets.GIT_DOC_REPO}}/analytics-raw-data
cp -R analytics-raw-data/*.json ${{secrets.GIT_DOC_REPO}}/analytics-raw-data
cd ${{secrets.GIT_DOC_REPO}}
git add .
git commit -m "Adicionando métricas do repositório ${{ github.event.repository.name }} ${{ github.ref_name }}"
git push
26 changes: 26 additions & 0 deletions scripts/consts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const REPO = '2023.2-UnB-TV-API-Gateway'; // Nome do repositório
const OWNER = 'fga-eps-mds';
const SONAR_ID = 'fga-eps-mds_2023.2-UnB-TV-API-Gateway'; // Id do projeto no SonarCloud
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(
','
)}`;

module.exports = {
SONAR_URL,
REPO,
OWNER,
};
13 changes: 13 additions & 0 deletions scripts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "scripts",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"@octokit/core": "^3.4.0",
"axios": "^0.21.1",
"dotenv": "^8.2.0",
"fs": "^0.0.1-security",
"gh-release-assets": "^2.0.0"
}
}
103 changes: 103 additions & 0 deletions scripts/release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
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('./consts.js');

const { TOKEN, RELEASE_MAJOR, RELEASE_MINOR, RELEASE_FIX, DEVELOP } = process.env;

const octokit = new Octokit({ auth: TOKEN });

const now = new Date();
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`;
}
if (RELEASE_MINOR === 'true') {
const minorTagNum = parseInt(oldTag[1]) + 1;
return `${oldTag[0]}.${minorTagNum}.0`;
}
if (RELEASE_FIX === 'true') {
const fixTagNum = parseInt(oldTag[2]) + 1;
return `${oldTag[0]}.${oldTag[1]}.${fixTagNum}`;
}
if (DEVELOP === 'true') {
return `develop`;
}
// Caso não tenha nenhuma flag de release, é feito um release de fix
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/';
let filePath = `${dirPath}fga-eps-mds-${REPO}-${month}-${day}-${year}-${hours}-${minutes}-${seconds}-v${tag}.json`;
fs.mkdirSync(dirPath);
if(tag === 'develop') {
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}-v${release[1]}.json`,
{
name: `fga-eps-mds-${REPO}-${month}-${day}-${year}-${hours}-${minutes}-${seconds}-v${release[1]}.json`,
path: '',
},
],
});
};

const script = async () => {
if(DEVELOP === 'true') {
await saveSonarFile('develop');
return;
}
const release = await createRelease();
await uploadSonarFile(release);
};

script();
11 changes: 11 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
sonar.projectKey=fga-eps-mds_2023.2-UnB-TV-API-Gateway
sonar.organization=fga-eps-mds-1

sonar.sources=src
sonar.tests=src
sonar.javascript.lcov.reportPaths=coverage/lcov.info
sonar.testExecutionReportPaths=report/sonar-report.xml
sonar.sourceEncoding=UTF-8
sonar.test.inclusions=src/**/*.spec.ts
sonar.coverage.exclusions=dist/, node_modules, public, tsconfig.json, src/App.tsx, src/index.tsx, src/reportWebVitals.ts, src/setupTests.ts, src/routes/*.tsx
sonar.cpd.exclusions=src/**/*.test.ts

0 comments on commit 812f78e

Please sign in to comment.