Skip to content

Commit

Permalink
Merge pull request #7 from fga-eps-mds/release
Browse files Browse the repository at this point in the history
config release
  • Loading branch information
nYCSTs authored Nov 22, 2023
2 parents 88c564d + 945976f commit 5a5a0d3
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 0 deletions.
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-Admin'; // Nome do repositório
const OWNER = 'fga-eps-mds';
const SONAR_ID = 'fga-eps-mds_2023.2-UnB-TV-Admin'; // 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();

0 comments on commit 5a5a0d3

Please sign in to comment.