Repository: aliyun/fun
Pull request #187 (2019-04-12 09:26:22)

sunfeiyu/fix fun config will change api_version
#181 and #185  fix fun config will change api_version and -vvvv 时，应该打印 trigger 的参数配置 


Patch:
diff --git a/lib/commands/config.js b/lib/commands/config.js
index 84b856a..3c1f9d9 100644
--- a/lib/commands/config.js
+++ b/lib/commands/config.js
@@ -9,6 +9,7 @@ const util = require('util');
 const mkdirp = require('mkdirp-promise');
 
 const { mark } = require('../profile');
+const { isShortDateStr } = require('../profile');
 const getProfile = require('../profile').getProfileFromFile;
 
 const writeFile = util.promisify(fs.writeFile);
@@ -96,13 +97,28 @@ async function config() {
 
   if (isExists) {
     const profContent = await readFile(profPath, 'utf8');
-    profYml = yaml.safeLoad(profContent);
+    profYml = yaml.safeLoad(profContent,{
+      schema: yaml.JSON_SCHEMA
+    });
     profYml.endpoint = `https://${newProf.accountId}.${newProf.defaultRegion}.fc.aliyuncs.com`;
     profYml.access_key_id = newProf.accessKeyId;
     profYml.access_key_secret = newProf.accessKeySecret;
     profYml.sls_endpoint = `${newProf.defaultRegion}.log.aliyuncs.com`;
     profYml.timeout = newProf.timeout;
     profYml.retries = newProf.retries;
+    if (!isShortDateStr(profYml.api_version)) { 
+      // 1. fcli 默认配置的格式为 api_version: 2016-08-15
+      // 2. js-yaml 在没有配置 schema: yaml.JSON_SCHEMA 时，会将其按照日期解析
+      // 3. js-yaml dump 后，生成的内容为： 2016-08-15T00:00:00.000Z，然后会被写入 config.yaml
+      // 4. fcli 读取到这个配置会导致请求出错
+      
+      // 这里做到了以下三种格式，最终都被格式化成 ‘2016-08-15’：
+      
+      // 1. 兼容 2016-08-15
+      // 2. 兼容 ‘2016-08-15’
+      // 3. 兼容 2016-08-15T00:00:00.000Z
+      profYml.api_version = profYml.api_version.slice(0,10);
+    }
   } else {
     profYml = {
       endpoint: `https://${newProf.accountId}.${newProf.defaultRegion}.fc.aliyuncs.com`,
@@ -115,12 +131,10 @@ async function config() {
       retries: newProf.retries,
       sls_endpoint: `${newProf.defaultRegion}.log.aliyuncs.com`
     };
-
     await mkdirp(configDir);
   }
 
   await writeFile(profPath, yaml.dump(profYml));
 }
-
 module.exports = config;
 
diff --git a/lib/deploy/deploy-support.js b/lib/deploy/deploy-support.js
index 3d86af3..ecdeeea 100644
--- a/lib/deploy/deploy-support.js
+++ b/lib/deploy/deploy-support.js
@@ -440,8 +440,8 @@ async function makeFunction(baseDir, {
     environmentVariables: addEnv(environmentVariables)
   };
 
-  for (let i in  params.environmentVariables) {
-    if(!isNaN(params.environmentVariables[i])){
+  for (let i in params.environmentVariables) {
+    if(!isNaN(params.environmentVariables[i])) {
       debug(`the value in environmentVariables:${params.environmentVariables[i]} cast String Done`);
       params.environmentVariables[i] = params.environmentVariables[i]+ '';
     }
@@ -717,6 +717,8 @@ async function makeTrigger({
     triggerConfig: getTriggerConfig(triggerType, triggerProperties)
   };
 
+  debug('serviceName is %s, functionName is %s, trigger params is %j', serviceName, functionName, params);
+
   const invocationRole = await makeInvocationRole(serviceName, functionName, triggerType);
   if (invocationRole) {
     Object.assign(params, {
diff --git a/lib/profile.js b/lib/profile.js
index 5c2f1b7..6620d51 100644
--- a/lib/profile.js
+++ b/lib/profile.js
@@ -181,4 +181,12 @@ function mark(source) {
   return `***********${subStr}`;
 }
 
-module.exports = { getProfile, getProfileFromFile , mark};
\ No newline at end of file
+function isShortDateStr(str) { //example:2008-07-22
+  var dateFormat = /^\d{4}-\d{2}-\d{2}$/;
+  if (dateFormat.test(str)) {
+    return true;
+  } 
+  return false;
+    
+}
+module.exports = { getProfile, getProfileFromFile, mark, isShortDateStr };
\ No newline at end of file


Test patch:
diff --git a/test/commands/config.test.js b/test/commands/config.test.js
index 878c280..d9a73ee 100644
--- a/test/commands/config.test.js
+++ b/test/commands/config.test.js
@@ -17,7 +17,6 @@ const { setProcess } = require('../test-utils');
 const writeFile = util.promisify(fs.writeFile);
 const readFile = util.promisify(fs.readFile);
 
-
 describe('config prompt', () => {
 
   let restoreProcess;
@@ -74,5 +73,45 @@ describe('config prompt', () => {
     expect(profYml.endpoint).to.be('https://3333555543.cn-shanghai.fc.aliyuncs.com');
     expect(profYml.access_key_id).to.be('23r2fwefw');
   });
+});
+
+describe('config api_version', () => {
+
+  let restoreProcess;
+
+  before(async () => {
+
+    restoreProcess = setProcess({
+      HOME: os.tmpdir(),
+    });
+
+    await mkdirp(`${os.homedir()}/.fcli/`);
+    await writeFile(`${os.homedir()}/.fcli/config.yaml`, yaml.dump({
+      endpoint: `https://33232.cn-hangzhou.fc.aliyuncs.com`,
+      api_version: '2016-08-15T00:00:00.000Z',       
+      access_key_id: '22222',
+      access_key_secret: '3333333',
+      security_token: '',
+      user_agent: 'fcli-0.1',
+      debug: false,
+      timeout: 60,
+      sls_endpoint: `cn-hangzhou.log.aliyuncs.com`
+    }));
+  });
+
+  after(async () => {
+    rimraf.sync(`${os.homedir()}/.fcli/`);
+    restoreProcess();
+  });
+
+  it('config dateStr', async () => {  
+    mocki({});
+    await config();
+    const profContent = await readFile(`${os.homedir()}/.fcli/config.yaml`, 'utf8');
+    const profYml = yaml.safeLoad(profContent,{
+      schema: yaml.JSON_SCHEMA
+    });
+    expect(profYml.api_version).to.be('2016-08-15');
+  });
+});
 
-});
\ No newline at end of file
diff --git a/test/profile.test.js b/test/profile.test.js
index 54b2190..3a2ace4 100644
--- a/test/profile.test.js
+++ b/test/profile.test.js
@@ -14,6 +14,8 @@ const writeFile = util.promisify(fs.writeFile);
 
 const { setProcess } = require('./test-utils');
 
+const { isShortDateStr } = require('../lib/profile');
+
 describe('without local ~/.fcli/config.yaml', () => {
   
   let restoreProcess;
@@ -105,4 +107,11 @@ describe('with local ~/.fcli/config.yaml', () => {
     expect(profile.timeout).to.be(60);
     expect(profile.retries).to.be(10);
   });
+
+  it('pattern',()=>{
+    const correctValue = isShortDateStr('2017-09-09');
+    const notCorrectValue = isShortDateStr('2017/09/09');
+    expect(correctValue).to.be(true);
+    expect(notCorrectValue).to.be(false);
+  });
 });
\ No newline at end of file