diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..cd1250b --- /dev/null +++ b/.github/workflows/release.yml @@ -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 \ No newline at end of file diff --git a/scripts/consts.js b/scripts/consts.js new file mode 100644 index 0000000..d0b7163 --- /dev/null +++ b/scripts/consts.js @@ -0,0 +1,26 @@ +const REPO = '2023.2-UnB-TV-Users'; // Nome do repositório +const OWNER = 'fga-eps-mds'; +const SONAR_ID = 'fga-eps-mds_2023.2-UnB-TV-Users'; // 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, +}; diff --git a/scripts/package.json b/scripts/package.json new file mode 100644 index 0000000..9443de6 --- /dev/null +++ b/scripts/package.json @@ -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" + } +} \ No newline at end of file diff --git a/scripts/release.js b/scripts/release.js new file mode 100644 index 0000000..c373926 --- /dev/null +++ b/scripts/release.js @@ -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(); diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 0000000..fe0902d --- /dev/null +++ b/sonar-project.properties @@ -0,0 +1,13 @@ +sonar.projectKey=fga-eps-mds_2023.2-UnB-TV-Users +sonar.organization=fga-eps-mds-1 + +sonar.sources=src +sonar.tests=tests +# sonar.test.inclusions=tests/*.py +sonar.exclusions=__pycache__, tests +# sonar.testExecutionReportPaths +sonar.sourceEncoding=UTF-8 + +sonar.python.version=3.11.5 +sonar.python.xunit.reportPath=junit.xml +sonar.python.coverage.reportPaths=coverage.xml \ No newline at end of file