Skip to content

Commit 2561e68

Browse files
committed
fix(scf): update apigw trigger bug
1 parent 2e096ce commit 2561e68

File tree

3 files changed

+75
-38
lines changed

3 files changed

+75
-38
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ env.js
2424
package-lock.json
2525
yarn.lock
2626

27-
__tests__/apigw.list.test.ts
27+
__tests__/apigw.sp.test.ts
2828
__tests__/scf.image.test.ts
29+
__tests__/scf.sp.test.ts

src/modules/scf/index.ts

Lines changed: 69 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -89,37 +89,23 @@ export default class Scf {
8989
oldList: TriggerType[],
9090
) {
9191
const deleteList: (TriggerType | null)[] = deepClone(oldList);
92-
const createList: (OriginTriggerType | null)[] = deepClone(events);
9392
const deployList: (TriggerType | null)[] = [];
94-
// const noKeyTypes = ['apigw'];
95-
const updateList: (OriginTriggerType | null)[] = [];
9693

97-
for (let index = 0; index < events.length; index++) {
98-
const event = events[index];
99-
const Type = Object.keys(event)[0];
100-
const TriggerClass = TRIGGERS[Type];
101-
const triggerInstance: BaseTrigger = new TriggerClass({
102-
credentials: this.credentials,
103-
region: this.region,
104-
});
105-
const { triggerKey } = await triggerInstance.formatInputs({
106-
region: this.region,
107-
inputs: {
108-
namespace: funcInfo.Namespace,
109-
functionName: funcInfo.FunctionName,
110-
...event[Type],
111-
},
112-
});
113-
deployList[index] = {
114-
NeedCreate: true,
115-
Type,
116-
...event[Type],
117-
};
118-
119-
for (let i = 0; i < oldList.length; i++) {
120-
const oldTrigger = oldList[i];
94+
const compareTriggerKey = async ({
95+
triggerType,
96+
newIndex,
97+
newKey,
98+
oldTriggerList,
99+
}: {
100+
triggerType: string;
101+
newIndex: number;
102+
newKey: string;
103+
oldTriggerList: TriggerType[];
104+
}) => {
105+
for (let i = 0; i < oldTriggerList.length; i++) {
106+
const oldTrigger = oldTriggerList[i];
121107
// 如果类型不一致或者已经比较过(key值一致),则继续下一次循环
122-
if (oldTrigger.Type !== Type || oldTrigger.compared === true) {
108+
if (oldTrigger.Type !== triggerType || oldTrigger.compared === true) {
123109
continue;
124110
}
125111
const OldTriggerClass = TRIGGERS[oldTrigger.Type];
@@ -130,29 +116,77 @@ export default class Scf {
130116
const oldKey = await oldTriggerInstance.getKey(oldTrigger);
131117

132118
// 如果 key 不一致则继续下一次循环
133-
if (oldKey !== triggerKey) {
119+
if (oldKey !== newKey) {
134120
continue;
135121
}
136122

137123
oldList[i].compared = true;
138124

139125
deleteList[i] = null;
140-
updateList.push(createList[index]);
141-
if (CAN_UPDATE_TRIGGER.indexOf(Type) === -1) {
142-
createList[index] = null;
143-
deployList[index] = {
126+
127+
if (CAN_UPDATE_TRIGGER.indexOf(triggerType) === -1) {
128+
deployList[newIndex] = {
144129
NeedCreate: false,
145130
...oldTrigger,
146131
};
147132
}
148133
// 如果找到 key 值一样的,直接跳出循环
149134
break;
150135
}
136+
};
137+
138+
for (let index = 0; index < events.length; index++) {
139+
const event = events[index];
140+
const Type = Object.keys(event)[0];
141+
const TriggerClass = TRIGGERS[Type];
142+
const triggerInstance: BaseTrigger = new TriggerClass({
143+
credentials: this.credentials,
144+
region: this.region,
145+
});
146+
deployList[index] = {
147+
NeedCreate: true,
148+
Type,
149+
...event[Type],
150+
};
151+
152+
// 需要特殊比较 API 网关触发器,因为一个触发器配置中,可能包含多个 API 触发器
153+
if (Type === 'apigw') {
154+
const { parameters = {} } = event[Type];
155+
const { endpoints = [{ path: '/', method: 'ANY' }] } = parameters;
156+
for (const item of endpoints) {
157+
const newKey = await triggerInstance.getKey({
158+
TriggerDesc: {
159+
serviceId: parameters.serviceId,
160+
path: item.path,
161+
method: item.method,
162+
},
163+
});
164+
await compareTriggerKey({
165+
triggerType: Type,
166+
newIndex: index,
167+
newKey: newKey,
168+
oldTriggerList: oldList,
169+
});
170+
}
171+
} else {
172+
const { triggerKey } = await triggerInstance.formatInputs({
173+
region: this.region,
174+
inputs: {
175+
namespace: funcInfo.Namespace,
176+
functionName: funcInfo.FunctionName,
177+
...event[Type],
178+
},
179+
});
180+
await compareTriggerKey({
181+
triggerType: Type,
182+
newIndex: index,
183+
newKey: triggerKey,
184+
oldTriggerList: oldList,
185+
});
186+
}
151187
}
152188
return {
153-
updateList,
154189
deleteList: deleteList.filter((item) => item) as TriggerType[],
155-
createList: createList.filter((item) => item) as OriginTriggerType[],
156190
deployList: deployList.map((item) => {
157191
delete item?.compared;
158192
return item as TriggerType;

src/modules/triggers/apigw.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,15 @@ export default class ApigwTrigger extends BaseTrigger<ApigwTriggerInputsParams>
127127
try {
128128
const { api } = JSON.parse(TriggerDesc);
129129
const { path, method } = api.requestConfig;
130-
return `${serviceId}/${path.toLowerCase()}/${method}`;
130+
return `${serviceId}/${path.toLowerCase()}/${method.toLowerCase()}`;
131131
} catch (e) {
132132
return '';
133133
}
134134
}
135135

136-
return `${TriggerDesc.serviceId}/${TriggerDesc.path.toLowerCase()}/${TriggerDesc.method}`;
136+
return `${
137+
TriggerDesc.serviceId
138+
}/${TriggerDesc.path.toLowerCase()}/${TriggerDesc.method.toLowerCase()}`;
137139
}
138140

139141
/** 格式化输入 */

0 commit comments

Comments
 (0)