<p>你可以试试下面的方法</p>
<pre><code>data = {
"_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
"actions": [
{
"date": "xyz",
"lastBuiltRevision": {
"branch": [
{
"SHA1": "5213affe970c86cd6e13b9d0e52515ac53f46aae",
"name": "feature/demo"
}
]
}
},
{
},
{
},
{
},
{
"date": "abc",
"lastBuiltRevision": {
"branch": [
{
"SHA1": "ca7972a32cc28304c22c98ceabf8e349fbf1a100",
"name": "refs/remotes/xyz/feature/demo_xyz"
}
]
}
},
{
"_class": "org.jenkinsci.plugins.displayurlapi.actions.RunDisplayAction"
},
{
"_class": "org.jenkinsci.plugins.pipeline.modeldefinition.actions.RestartDeclarativePipelineAction"
},
{
},
{
"_class": "org.jenkinsci.plugins.workflow.job.views.FlowGraphAction"
},
{
},
{
},
{
},
{
},
{
"_class": "org.marvelution.jji.export.ParentAction"
}
]
}
branch_name="refs/remotes/xyz/feature/demo_xyz"
data['actions'] = [x for x in data['actions'] if x and 'lastBuiltRevision' in x and x['lastBuiltRevision']['branch'][0]['name'] == branch_name]
for x in data.get('actions'):
entry = x['lastBuiltRevision']['branch'][0]
print(f'Name: {entry["name"]}, SHA1: {entry["SHA1"]}')
</code></pre>
<p>输出</p>
<pre><code>Name: refs/remotes/xyz/feature/demo_xyz, SHA1: ca7972a32cc28304c22c98ceabf8e349fbf1a100
</code></pre>