From 09929480a77b39c9f5328a5283622fa1bd81a438 Mon Sep 17 00:00:00 2001
From: Hugo SUBTIL <ext.sopra.husubtil@grandlyon.com>
Date: Mon, 2 May 2022 14:31:44 +0200
Subject: [PATCH] update manifest + cicd

---
 .gitlab-ci.yml     | 55 ++++++++++++++++++++++++++++++++++++++++++++--
 manifest.konnector | 12 +++++++++-
 src/index.js       | 37 ++++++++++++++-----------------
 3 files changed, 81 insertions(+), 23 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index fef13ef..fc9104e 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -6,8 +6,59 @@
 # Note that environment variables can be set in several places
 # See https://docs.gitlab.com/ee/ci/variables/#cicd-variable-precedence
 stages:
-- test
+  - test
+  - build
+  - publish
+
 sast:
   stage: test
 include:
-- template: Security/SAST.gitlab-ci.yml
+  - template: Security/SAST.gitlab-ci.yml
+
+build-dev:
+  image: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/node:16.14.2-alpine3.14
+  stage: build
+  before_script:
+    - apk add git
+    - apk add bash
+  script:
+    - yarn
+    - yarn build-dev
+    - git config --global user.name build-pipeline
+    - git config --global user.email "$GIT_USER"
+    - git config --global user.password "$GIT_PWD"
+    - git config user.email "$GIT_USER"
+    - git remote set-url origin https://"$GIT_USER":"$GIT_PWD"@forge.grandlyon.com/llle_project/enedis-sge-konnector.git
+    - git config --global credential.helper store
+    - yarn deploy-dev
+  only:
+    - dev
+
+build:
+  image: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/node:16.14.2-alpine3.14
+  stage: build
+  before_script:
+    - apk add git
+    - apk add bash
+  script:
+    - yarn
+    - yarn build
+    - git config --global user.name build-pipeline
+    - git config --global user.email "$GIT_USER"
+    - git config --global user.password "$GIT_PWD"
+    - git config user.email "$GIT_USER"
+    - git remote set-url origin https://"$GIT_USER":"$GIT_PWD"@forge.grandlyon.com/web-et-numerique/llle_project/enedis-sge-konnector.git
+    - git config --global credential.helper store
+    - yarn deploy
+  only:
+    - master
+
+publish:
+  stage: publish
+  before_script:
+    - apk add git
+  script:
+    - yarn cozyPublish
+  only:
+    - tags
+  when: manual
diff --git a/manifest.konnector b/manifest.konnector
index 4196b41..29b0c77 100644
--- a/manifest.konnector
+++ b/manifest.konnector
@@ -11,6 +11,15 @@
   "categories": ["energy"],
   "frequency": "daily",
   "fields": {
+    "wso2BaseUrl": {
+      "type": "hidden"
+    },
+    "loginUtilisateur": {
+      "type": "hidden"
+    },
+    "apiToken": {
+      "type": "hidden"
+    },
     "pdl": {
       "type": "text"
     },
@@ -26,7 +35,8 @@
   "screenshots": [],
   "permissions": {
     "accounts": {
-      "type": "io.cozy.accounts"
+      "type": "io.cozy.accounts",
+      "verbs": ["GET"]
     },
      "enedis data": {
       "type": "com.grandlyon.enedis.*"
diff --git a/src/index.js b/src/index.js
index 262dae5..da6036c 100644
--- a/src/index.js
+++ b/src/index.js
@@ -134,16 +134,7 @@ async function getData(url, apiAuthKey, userLogin, pointId) {
     apikey: apiAuthKey,
   }
 
-  // If start date exceed the maximum amount of data we can get with one query
-  // get only 24 month
-  if (moment(endDate).diff(startDailyDate, 'months', true) > 24) {
-    log(
-      'info',
-      'Start date exceed 24 month, setting start date to current date minus 24 month'
-    )
-    startDailyDate = moment(endDate).subtract(24, 'month')
-    startDailyDateString = startDailyDate.format('YYYY-MM-DD')
-  }
+  setStartDate()
 
   const { response } = await soapRequest({
     url: url,
@@ -185,16 +176,7 @@ async function getMaxPowerData(url, apiAuthKey, userLogin, pointId) {
     apikey: apiAuthKey,
   }
 
-  // If start date exceed the maximum amount of data we can get with one query
-  // get only 24 month
-  if (moment(endDate).diff(startDailyDate, 'months', true) > 24) {
-    log(
-      'info',
-      'Start date exceed 24 month, setting start date to current date minus 24 month'
-    )
-    startDailyDate = moment(endDate).subtract(24, 'month')
-    startDailyDateString = startDailyDate.format('YYYY-MM-DD')
-  }
+  setStartDate()
 
   const { response } = await soapRequest({
     url: url,
@@ -217,6 +199,21 @@ async function getMaxPowerData(url, apiAuthKey, userLogin, pointId) {
   )
 }
 
+/**
+ * If start date exceed the maximum amount of data we can get with one query
+ * get only 36 month
+ */
+function setStartDate() {
+  if (moment(endDate).diff(startDailyDate, 'months', true) > 36) {
+    log(
+      'info',
+      'Start date exceed 36 month, setting start date to current date minus 36 month'
+    )
+    startDailyDate = moment(endDate).subtract(36, 'month')
+    startDailyDateString = startDailyDate.format('YYYY-MM-DD')
+  }
+}
+
 /**
  * Get half-hour data
  * @param {string} url
-- 
GitLab