Lambda - Python - "errorMessage" : "list indices must be integers, not str"



试图找出这个损坏的存储库的功能版本,并得到有关数据类型的错误。 它正在检测一个整数,它应该是一个字符串。你可以在 github 存储库中看到其他模块,但基本上这应该会根据 cloudwatch 事件触发一些发送到 Slack 的消息。

堆栈跟踪如下。


from __future__ import print_function
import json
import boto3
import time
# from test_events import TEST_EVENTS, TEST_ERROR_EVENTS
from build_info import BuildInfo, CodeBuildInfo
from slack_helper import post_build_msg, find_message_for_build
from message_builder import MessageBuilder
import re
import sys
client = boto3.client('codepipeline')
def findRevisionInfo(info):
r = client.get_pipeline_execution(
pipelineName=info.pipeline,
pipelineExecutionId=info.executionId
)['pipelineExecution']
revs = r.get('artifactRevisions',[])
if len(revs) > 0:
return revs[0]
return None

def pipelineFromBuild(codeBuildInfo):
r = client.get_pipeline_state(name=codeBuildInfo.pipeline)
for s in r['stageStates']:
for a in s['actionStates']:
executionId = a.get('latestExecution', {}).get('externalExecutionId')
if executionId and codeBuildInfo.buildId.endswith(executionId):
pe = s['latestExecution']['pipelineExecutionId']
return (s['stageName'], pe, a)
return (None, None, None)

def processCodePipeline(event):
buildInfo = BuildInfo.fromEvent(event)
existing_msg = find_message_for_build(buildInfo)
builder = MessageBuilder(buildInfo, existing_msg)
builder.updatePipelineEvent(event)
if builder.needsRevisionInfo():
revision = findRevisionInfo(buildInfo)
builder.attachRevisionInfo(revision)
post_build_msg(builder)
def processCodeBuild(event):
cbi = CodeBuildInfo.fromEvent(event)
(stage, pid, actionStates) = pipelineFromBuild(cbi)
if not pid:
return
buildInfo = BuildInfo(pid, cbi.pipeline)
existing_msg = find_message_for_build(buildInfo)
builder = MessageBuilder(buildInfo, existing_msg)
if 'phases' in event['detail']['additional-information']:
phases = event['detail']['additional-information']['phases']
builder.updateBuildStageInfo(stage, phases, actionStates)
logs = event['detail'].get('additional-information', {}).get('logs')
if logs:
builder.attachLogs(event['detail']['additional-information']['logs'])
post_build_msg(builder)

def process(event):
if event['source'] == "aws.codepipeline":
processCodePipeline(event)
if event['source'] == "aws.codebuild":
processCodeBuild(event)
def run(event, context):
#print(json.dumps(event, indent=2, default=str))
m = process(event)
if __name__ == "__main__":
with open ('test-event.json') as f:
events = json.load(f)
for e in events:
run(e, {})
time.sleep(1)

返回错误:

File "/var/task/notifier.py", line 86, in run
m = process(event)
File "/var/task/notifier.py", line 79, in process
if event['source'] == "aws.codepipeline":
TypeError: list indices must be integers, not str

正在引入的数据如下所示:

{
"account": "164943972409",
"region": "us-west-2",
"detail": {
"execution-id": "c776b515-1810-465f-a0ab-3a30a1d4341b",
"pipeline": "buildfish-web-ng-code-pipeline-dev",
"version": 1,
"state": "STARTED"
},
"detail-type": "CodePipeline Pipeline Execution State Change",
"source": "aws.codepipeline",
"version": "0",
"time": "2018-05-20T04:11:41Z",
"id": "ae75c080-2f81-dd60-e6cc-76ec00489305",
"resources": [
"arn:aws:codepipeline:us-west-2:164943972409:buildfish-web-ng-code-pipeline-dev"
]
}

谢谢

在主函数中,打开并加载 json 事件。

当循环遍历此字典时,即for e in events:您循环遍历字典中的键值,而不是一次加载的整个事件。

只需将从json.load(f)读入的整个事件传递到run(event, context)函数即可。

if __name__ == "__main__":
with open ('test-event.json') as f:
event = json.load(f)
run(event, {})

相关内容

最新更新