github.com/whamcloud/lemur@v0.0.0-20190827193804-4655df8a52af/packaging/ci/lambda/NotifyGithub/lambda_function.py (about)

     1  #!/usr/bin/env python
     2  
     3  import json
     4  import boto3
     5  import traceback
     6  import re
     7  import os
     8  
     9  from base64 import b64decode
    10  
    11  from lemur_ci import commit_status
    12  from github import Github
    13  from github.GithubObject import NotSet
    14  
    15  
    16  def lambda_handler(event, context):
    17      try:
    18          # for debugging
    19          print json.dumps(event)
    20  
    21          message = commit_status.Message(**json.loads(event['Records'][0]['Sns']['Message']))
    22  
    23          authToken = boto3.client('kms').decrypt(CiphertextBlob=b64decode(os.environ['GITHUB_TOKEN']))['Plaintext']
    24          g = Github(authToken, base_url=message.api_url)
    25          u = g.get_user()
    26          repo = None
    27          if u.login == message.repoOrgOrUser:
    28              repo = u.get_repo(message.repo)
    29          else:
    30              o = g.get_organization(message.repoOrgOrUser)
    31              repo = o.get_repo(message.repo)
    32  
    33          commit = repo.get_commit(message.sha)
    34          print commit.create_status(**message.as_status())
    35  
    36      except Exception as e:
    37          # If any other exceptions which we didn't expect are raised
    38          # then fail the job and log the exception message.
    39          print('Function failed due to exception.')
    40          print(e)
    41          traceback.print_exc()
    42  
    43      print('Function complete.')
    44      return "Complete."