diff --git a/.github/workflows/codacy-analysis.yml b/.github/workflows/codacy-analysis.yml index 7e62010..c6eb789 100644 --- a/.github/workflows/codacy-analysis.yml +++ b/.github/workflows/codacy-analysis.yml @@ -21,7 +21,7 @@ jobs: # Execute Codacy Analysis CLI and generate a SARIF output with the security issues identified during the analysis - name: Run Codacy Analysis CLI - uses: codacy/codacy-analysis-cli-action@3.0.0 + uses: codacy/codacy-analysis-cli-action@4.0.0 with: # Check https://github.com/codacy/codacy-analysis-cli#project-token to get your project token from your Codacy repository # You can also omit the token and run the tools that support default configurations diff --git a/.github/workflows/format-lint.yml b/.github/workflows/format-lint.yml index e36df51..843f0ab 100644 --- a/.github/workflows/format-lint.yml +++ b/.github/workflows/format-lint.yml @@ -8,7 +8,7 @@ jobs: - uses: actions/checkout@v2 with: ref: ${{ github.head_ref }} - - uses: actions/setup-node@v2.1.5 + - uses: actions/setup-node@v2.3.0 with: node-version: '12.x' - name: Install dependencies diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 0d71dd4..4f2d333 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,8 +1,7 @@ name: Release on: push: - branches: - - master + branches: [master, main] jobs: release: name: Release @@ -13,7 +12,7 @@ jobs: with: fetch-depth: 0 - name: Setup Node.js - uses: actions/setup-node@v2.1.5 + uses: actions/setup-node@v2.3.0 with: node-version: 12 - name: Install dependencies diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8aa22de..aa8c7d9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,12 +15,6 @@ jobs: run: npm run test - name: Run coverage run: npm run coverage - - name: Upload coverage to Codecov - uses: codecov/codecov-action@v1 - with: - token: ${{ secrets.CODECOV_TOKEN }} - flags: unittests,integrationtest - name: codecov-nyc - name: Upload coverage to Codacy run: export CODACY_PROJECT_TOKEN=${{ secrets.CODACY_PROJECT_TOKEN }} && bash <(curl -Ls https://coverage.codacy.com/get.sh) report -r coverage/* continue-on-error: true diff --git a/README.md b/README.md index ddd6288..ccea146 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ {{Texto introdutorio}} + Respositório template para CF utilizando - Deploy com terraform de (Arquivos para GCS, Tabelas BQ e CF) @@ -52,14 +53,21 @@ Respositório template para CF utilizando - Github pages com template do github.dp6.io # Preparando o repositório +Crie seu projeto baseado nesse repositório. -WIP + +(Passo a passo github)[https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/creating-a-repository-on-github/creating-a-repository-from-a-template] ## Variáveis de ambiente no github (secrets) +Primeiro é necessário conectar o repositório do Github com o (coday.com)[https://app.codacy.com/organizations/gh/DP6/repositories] (referência para conexão)[https://docs.codacy.com/organizations/managing-repositories/#adding-a-repository]. -WIP +Necessário criar a screte CODACY_PROJECT_TOKEN nas configurações do seu repositório no Github em Settings > Secret > New repository Secret -## Renomeando arquivos + + + + +## Renomeando os arquivos - Substituir o nome `template-js-cloudfunction-with-terraform` pelo nome do novo repositório criado no arquivo package.json - Substituir a chave `{{nome_projeto}}` no arquivo `_config.yml` com o nome do projeto em linguagem natural, para ser usado como titulo da página no site. @@ -68,7 +76,10 @@ WIP ## Entendendo o terraform -WIP +É recomendo a leitura da documentação oficial do (terraform para Google)[https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/getting_started] + + + ## Entendendo os running do NPM @@ -102,6 +113,7 @@ WIP 4. Variável [GOOGLE_APPLICATION_CREDENTIALS](https://cloud.google.com/docs/authentication/getting-started#setting_the_environment_variable) 5. Instalar o [Terraform](https://www.terraform.io/downloads.html) 6. Habilitar os produtos no GCP Cloud Function, BigQuery, Cloud Build API, Cloud Resource Manager API, BigQuery Data Transfer API e Cloud Storage, para uso do BigQuery é necessário ter um billing ativo +7. *Importante* o usuário que executar o script do terraform precisa ter a permissão de owner no projeto do GCP. _Observação:_ Utilizando o ambiente no [Google Cloud Shell](https://cloud.google.com/shell/docs) não é necessário fazer os **1**, **2**, **4** e **5** @@ -116,6 +128,7 @@ git clone https://github.com/DP6/{{repo}}.git Para fazer deploy no GCP usando o Terraform, o utilize o shell script terraform_deploy ```console +cd {{repo}} sh terraform_deploy.sh ``` diff --git a/index.js b/index.js index d48dce9..4b5f69c 100644 --- a/index.js +++ b/index.js @@ -42,7 +42,7 @@ const templateCf = async (req, res) => { ); trace('RESULT VALID', result); - insertRowsAsStream(result); + insertRowsAsStream(result, projectConfig.BQ_SCHEMA_RAWDATA, projectConfig.BQ_TABLE_ID_RAWDATA); res.status(200).send(debugging ? { debugging: debugging, result: result } : 'sucesso!'); } }; @@ -81,11 +81,13 @@ function addTimestamp(data) { /** * Realiza a persistências dos dados por Stream no BigQuery * @param {Array} data Dados estruturados no padrão de persistência do BQ + * @param {String} schema Schema da tabela do BQ + * @param {String} tableId Nome da tabela do BQ */ -async function insertRowsAsStream(data) { +async function insertRowsAsStream(data, schema, tableId) { const bigquery = new BigQuery(); const options = { - schema: projectConfig.BQ_SCHEMA_RAWDATA, + schema: schema, skipInvalidRows: true, ignoreUnknownValues: true, }; @@ -94,7 +96,7 @@ async function insertRowsAsStream(data) { // Insert data into a table await bigquery .dataset(projectConfig.BQ_DATASET_ID) - .table(projectConfig.BQ_TABLE_ID_RAWDATA) + .table(tableId) .insert(data, options, insertHandler); console.log(`Inserted ${data.length} rows`); diff --git a/package.json b/package.json index d2cb428..4e7fdfa 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "lint-prettier": "npx prettier --check . || exit 0", "lint": "npm run lint-md && npm run lint-prettier", "unit-test": "mocha ./test/unit -timeout 8000", - "integration-test": "mocha ./test/integration --timeout=16000 --exit", + "integration-test": "mocha ./test/integration --timeout=20000 --exit", "all-test": "npm run integration-test && npm run unit-test", "test": "npm -- run all-test", "coverage": "nyc --reporter=lcov --reporter=cobertura npm run unit-test", diff --git a/terraform/scripts/using-local-penguin-project.sh b/terraform/scripts/using-local-project.sh similarity index 100% rename from terraform/scripts/using-local-penguin-project.sh rename to terraform/scripts/using-local-project.sh diff --git a/terraform/variables.tf b/terraform/variables.tf index 58e699f..0e4605d 100644 --- a/terraform/variables.tf +++ b/terraform/variables.tf @@ -26,8 +26,8 @@ variable "project_prefix" { type = string description = "Pré-fixo que será utilizado para nomear os produtos que serão utilizados e criados no GCP, exemplo para o cliente Brasil podemos usar o pré-fixo br" validation { - condition = can(regex("[a-z0-9]", var.project_prefix)) && length(var.project_prefix) <= 4 - error_message = "The prefix value must be a [a-z0-9] and size <= 4, exemple \"br01\"." + condition = can(regex("[a-z0-9]", var.project_prefix)) && length(var.project_prefix) <= 8 + error_message = "The prefix value must be a [a-z0-9] and size <= 4, exemple \"br012020\"." } } diff --git a/test/integration/request-http.test.js b/test/integration/request-http.test.js index 83ecd61..26a632c 100644 --- a/test/integration/request-http.test.js +++ b/test/integration/request-http.test.js @@ -18,7 +18,7 @@ describe('Execução cloud function template', async () => { // exec's 'timeout' param won't kill children of "shim" /bin/sh process // Workaround: include "& sleep ; kill $!" in executed command ffProc = execPromise( - `functions-framework --target=templateCf --signature-type=http --port ${PORT} & sleep 8; kill $!`, + `functions-framework --target=templateCf --signature-type=http --port ${PORT} & sleep 12; kill $!`, { shell: true, cwd } ); });