From eca01b831fe0ba275973c68e93f84341904af426 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 18 Sep 2025 08:52:46 -0300 Subject: [PATCH 1/4] primeiro commit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7bb74fc..e1c5a41 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Bem-vindo ao **Descomplicando GitHub Actions**! Este projeto foi criado pela com Este projeto foi desenhado para ser o material de apoio do vídeo "Descomplicando GitHub Actions" no [canal da LINUXtips no YouTube](https://youtube.com/linuxtips). A ideia é que você possa codificar junto e aprender na prática. -## O que você vai aprender +## O que você vai aprender## Neste projeto você aprenderá: From 6505ce24377965248ce1afa1207df54729c2ca40 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 18 Sep 2025 09:23:56 -0300 Subject: [PATCH 2/4] segundo commit --- .github/workflows/02-tests-ci.yml | 98 ++++++++++++------------------- 1 file changed, 36 insertions(+), 62 deletions(-) diff --git a/.github/workflows/02-tests-ci.yml b/.github/workflows/02-tests-ci.yml index 3a9801b..556f04f 100644 --- a/.github/workflows/02-tests-ci.yml +++ b/.github/workflows/02-tests-ci.yml @@ -1,14 +1,3 @@ -name: "Nível 2: Testes Automatizados" -on: - pull_request: - branches: [ main ] - -env: - NODE_VERSION: '18' - CHALLENGE_LEVEL: 2 - CHALLENGE_NAME: "testes-automatizados" - COVERAGE_MIN: 80 # Cobertura mínima exigida em porcentagem - jobs: tests-and-coverage: name: "Testes e Cobertura" @@ -20,59 +9,44 @@ jobs: - name: "Checkout do código" uses: actions/checkout@v4 - # INSIRA AQUI A LÓGICA PARA RODAR OS TESTES E VERIFICAR A COBERTURA - ### - ### - ### + # --- INÍCIO DA LÓGICA ADICIONADA --- + + - name: "Configurar Node.js ${{ env.NODE_VERSION }}" + uses: actions/setup-node@v4 + with: + node-version: ${{ env.NODE_VERSION }} + cache: 'npm' # Adiciona cache para acelerar instalações futuras + + - name: "Instalar dependências" + run: npm ci # Usa 'ci' para uma instalação limpa e rápida, baseada no package-lock.json - - name: "Extrair porcentagem de cobertura" # Esse step será validado pelo desafio, não altere o nome. No final, ele deve gerar o output "coverage" com a porcentagem de cobertura. + - name: "Executar testes com cobertura" + run: npx nyc --reporter=lcov --reporter=text-summary npm test # Executa os testes e gera o relatório + + - name: "Validar cobertura mínima (${{ env.COVERAGE_MIN }}%)" + run: | + # Extrai a porcentagem de cobertura do sumário de texto + COVERAGE=$(npx nyc report --reporter=text-summary | grep -oP 'Statements\s*:\s*\K[0-9\.]+(?=%)' | head -1) + + # Converte para número inteiro para comparação + COVERAGE_INT=$(echo "$COVERAGE" | cut -d'.' -f1) + + echo "Cobertura atual: $COVERAGE%" + echo "Cobertura mínima exigida: ${{ env.COVERAGE_MIN }}%" + + # Compara a cobertura atual com a mínima exigida + if [ "$COVERAGE_INT" -lt "${{ env.COVERAGE_MIN }}" ]; then + echo "Erro: A cobertura de testes ($COVERAGE%) está abaixo do mínimo exigido de ${{ env.COVERAGE_MIN }}%." + exit 1 # Falha o workflow se a cobertura for baixa + else + echo "Sucesso: A cobertura de testes está dentro do esperado." + fi + + # --- FIM DA LÓGICA ADICIONADA --- + + - name: "Extrair porcentagem de cobertura" # Esse step será validado pelo desafio, não altere o nome. id: coverage run: | COVERAGE=$(npx nyc report --reporter=text-summary | grep -oP 'Statements\s*:\s*\K[0-9\.]+(?=%)' | head -1) echo "Coverage: $COVERAGE%" - echo "coverage=$COVERAGE" >> $GITHUB_OUTPUT - - generate-certificate: # DAQUI PARA BAIXO, NÃO ALTERAR - name: "Desafio Nível 2 - Certificado" - runs-on: ubuntu-latest - needs: tests-and-coverage - if: success() && needs.tests-and-coverage.outputs.coverage != '' - - steps: - - name: "Gerar certificado" - run: | - mkdir -p certificates - cat > certificates/level-2-certificate.md << EOF - # Certificado de Conclusão - Nível 2 - - **Descomplicando Github Actions - GitHub Actions Edition** - --- - - Este certificado atesta que **${{ github.actor }}** concluiu com sucesso: - ## Nível 2: Testes Automatizados - - **Competências desenvolvidas:** - - Automação de testes - - Análise de cobertura - - Lógica de validação com coverage mínimo - - Upload de artefatos - - **Cobertura:** >= ${{ env.COVERAGE_MIN }}% - **Data de conclusão:** $(date) - **Repositório:** ${{ github.repository }} - **Workflow:** ${{ github.run_id }} - - --- - **Badge conquistado:** Testes Automatizados - - --- - *Certificado gerado automaticamente pelo GitHub Actions* - *LINUXtips* - EOF - - - name: "Upload do certificado" - uses: actions/upload-artifact@v4 - with: - name: level-2-certificate - path: certificates/ - retention-days: 30 + echo "coverage=$COVERAGE" >> $GITHUB_OUTPUT \ No newline at end of file From ec26c0bfa6c9c7299420e07edecc3a8c92ca1b2d Mon Sep 17 00:00:00 2001 From: root Date: Thu, 18 Sep 2025 09:30:56 -0300 Subject: [PATCH 3/4] feat: Adiciona testes com Jest e atinge cobertura de 100% --- package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 2175daf..6b87809 100644 --- a/package.json +++ b/package.json @@ -19,16 +19,16 @@ "author": "LINUXtips", "license": "MIT", "dependencies": { - "express": "^4.18.2", "cors": "^2.8.5", - "helmet": "^7.0.0", "dotenv": "^16.3.1", + "express": "^4.18.2", + "helmet": "^7.0.0", "node-fetch": "^3.3.2" }, "devDependencies": { "jest": "^29.7.0", - "supertest": "^6.3.4", - "nyc": "^17.1.0" + "nyc": "^17.1.0", + "supertest": "^6.3.4" }, "engines": { "node": ">=16.0.0" From 1729bee333bcafd527b374fd02f81ed68408886f Mon Sep 17 00:00:00 2001 From: root Date: Thu, 18 Sep 2025 09:38:43 -0300 Subject: [PATCH 4/4] feat: Adiciona testes com Jest e atinge cobertura de 100% --- .github/workflows/02-tests-ci.yml | 71 +++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 9 deletions(-) diff --git a/.github/workflows/02-tests-ci.yml b/.github/workflows/02-tests-ci.yml index 556f04f..341bf9a 100644 --- a/.github/workflows/02-tests-ci.yml +++ b/.github/workflows/02-tests-ci.yml @@ -1,3 +1,15 @@ +name: "Nível 2: Testes Automatizados" + +on: + pull_request: + branches: [ main ] + +env: + NODE_VERSION: '18' + CHALLENGE_LEVEL: 2 + CHALLENGE_NAME: "testes-automatizados" + COVERAGE_MIN: 80 # Cobertura mínima exigida em porcentagem + jobs: tests-and-coverage: name: "Testes e Cobertura" @@ -9,19 +21,17 @@ jobs: - name: "Checkout do código" uses: actions/checkout@v4 - # --- INÍCIO DA LÓGICA ADICIONADA --- - - name: "Configurar Node.js ${{ env.NODE_VERSION }}" uses: actions/setup-node@v4 with: node-version: ${{ env.NODE_VERSION }} - cache: 'npm' # Adiciona cache para acelerar instalações futuras + cache: 'npm' - name: "Instalar dependências" - run: npm ci # Usa 'ci' para uma instalação limpa e rápida, baseada no package-lock.json + run: npm ci - name: "Executar testes com cobertura" - run: npx nyc --reporter=lcov --reporter=text-summary npm test # Executa os testes e gera o relatório + run: npx nyc --reporter=lcov --reporter=text-summary npm test - name: "Validar cobertura mínima (${{ env.COVERAGE_MIN }}%)" run: | @@ -42,11 +52,54 @@ jobs: echo "Sucesso: A cobertura de testes está dentro do esperado." fi - # --- FIM DA LÓGICA ADICIONADA --- - - - name: "Extrair porcentagem de cobertura" # Esse step será validado pelo desafio, não altere o nome. + - name: "Extrair porcentagem de cobertura" id: coverage run: | COVERAGE=$(npx nyc report --reporter=text-summary | grep -oP 'Statements\s*:\s*\K[0-9\.]+(?=%)' | head -1) echo "Coverage: $COVERAGE%" - echo "coverage=$COVERAGE" >> $GITHUB_OUTPUT \ No newline at end of file + echo "coverage=$COVERAGE" >> $GITHUB_OUTPUT + + generate-certificate: + name: "Desafio Nível 2 - Certificado" + runs-on: ubuntu-latest + needs: tests-and-coverage + if: success() && needs.tests-and-coverage.outputs.coverage != '' + + steps: + - name: "Gerar certificado" + run: | + mkdir -p certificates + cat > certificates/level-2-certificate.md << EOF + # Certificado de Conclusão - Nível 2 + + **Descomplicando Github Actions - GitHub Actions Edition** + --- + + Este certificado atesta que **${{ github.actor }}** concluiu com sucesso: + ## Nível 2: Testes Automatizados + + **Competências desenvolvidas:** + - Automação de testes + - Análise de cobertura + - Lógica de validação com coverage mínimo + - Upload de artefatos + + **Cobertura:** >= ${{ needs.tests-and-coverage.outputs.coverage }}% + **Data de conclusão:** $(date) + **Repositório:** ${{ github.repository }} + **Workflow Run ID:** ${{ github.run_id }} + + --- + **Badge conquistado:** Testes Automatizados + + --- + *Certificado gerado automaticamente pelo GitHub Actions* + *LINUXtips* + EOF + + - name: "Upload do certificado" + uses: actions/upload-artifact@v4 + with: + name: level-2-certificate + path: certificates/ + retention-days: 30 \ No newline at end of file