2024-01-16 10:41:11 -05:00
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
2019-01-18 00:02:57 -08:00
# Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# This file is licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
2024-01-16 10:41:11 -05:00
2019-01-18 00:02:57 -08:00
# snippet-sourcedescription:[MyCodeCommitFunction.py demonstrates how to use an AWS Lambda function to return the URLs used for cloning an AWS CodeCommit repository to a CloudWatch log.]
# snippet-service:[codecommit]
2023-10-18 10:35:05 -07:00
# snippet-keyword:[Python]
2019-08-27 12:07:08 -07:00
# snippet-sourcesyntax:[python]
2023-10-18 10:35:05 -07:00
# snippet-sourcesyntax:[python]
2019-01-18 00:02:57 -08:00
# snippet-keyword:[AWS CodeCommit]
# snippet-keyword:[Code Sample]
# snippet-keyword:[GetRepository]
# snippet-sourcetype:[full-example]
# snippet-sourceauthor:[AWS]
# snippet-sourcedate:[2016-03-07]
# snippet-start:[codecommit.python.MyCodeCommitFunction.complete]
2024-01-16 10:41:11 -05:00
2019-01-18 00:02:57 -08:00
import json
import boto3
2024-01-16 10:41:11 -05:00
2023-10-18 10:35:05 -07:00
codecommit = boto3 . client ( " codecommit " )
2024-01-16 10:41:11 -05:00
2019-01-18 00:02:57 -08:00
def lambda_handler ( event , context ) :
2023-10-18 10:35:05 -07:00
# Log the updated references from the event
references = {
reference [ " ref " ]
for reference in event [ " Records " ] [ 0 ] [ " codecommit " ] [ " references " ]
}
print ( " References: " + str ( references ) )
2024-01-16 10:41:11 -05:00
2023-10-18 10:35:05 -07:00
# Get the repository from the event and show its git clone URL
repository = event [ " Records " ] [ 0 ] [ " eventSourceARN " ] . split ( " : " ) [ 5 ]
2019-01-18 00:02:57 -08:00
try :
response = codecommit . get_repository ( repositoryName = repository )
2023-10-18 10:35:05 -07:00
print ( " Clone URL: " + response [ " repositoryMetadata " ] [ " cloneUrlHttp " ] )
return response [ " repositoryMetadata " ] [ " cloneUrlHttp " ]
2019-01-18 00:02:57 -08:00
except Exception as e :
print ( e )
2023-10-18 10:35:05 -07:00
print (
" Error getting repository {} . Make sure it exists and that your repository is in the same region as this function. " . format (
repository
)
)
2019-01-18 00:02:57 -08:00
raise e
2024-01-16 10:41:11 -05:00
2019-01-18 00:02:57 -08:00
# snippet-end:[codecommit.python.MyCodeCommitFunction.complete]