6

I have a codecommit repo.

I have a push trigger setup with a "Send to" = "Amazon SNS".

At SNS, I have some email subscribers hooked up to the notification event.

As a result, the project developers receive an email each time any developer executes a git push against the repo.

The email looks similar to:

enter image description here


Is there a way to add the git push or commit message in that notification?

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
  • There is a setting option to execute an AWS Lambda script - I wonder if there is some way to pass the message through to Lambda, if not SNS. – Brian Webster May 19 '17 at 15:03
  • Git Message is an important field. I need it to detect WIP commits. What a shame amazon :(. Github, Bitbucket and Gitlab support it. – JRichardsz Jun 23 '21 at 21:10

2 Answers2

6

While that information is not provided directly from the trigger payload, it does provide a list of reference updates. Each new/updated reference (often a branch) contains the commit ID. If you were to configure an AWS Lambda trigger, you could get these commit IDs from the trigger payload, then use them with CodeCommit's 'GetCommit' api to retrieve the commit message. Then you could send your new payload to SNS for emailing.

Information on GetCommit: http://docs.aws.amazon.com/codecommit/latest/APIReference/API_GetCommit.html

Example Lambda trigger setup with AWS CodeCommit: http://docs.aws.amazon.com/codecommit/latest/userguide/how-to-notify-lambda.html

David Jackson
  • 591
  • 2
  • 5
  • 1
    Has anyone tried this? I seem to only be getting one reference in the record given to the Lambda function, even when a push contains multiple commits, so my notification only has the newest commit. Using the "Test Trigger" button in the CodeCommit console sends 3 references and I'm able to list all 3 commits. Am I missing something? – Michael Rush Apr 30 '18 at 22:34
  • 1
    I found the answer..... "if you make 5 commits to branch 'master' and push, your trigger payload should contain the commit ID of the new tip of master, and the name of the reference updated (in this case, the branch 'master'). Despite there being 5 new commits, there will be only a single trigger event notification, because only 1 reference (branch master) was updated. The IDs of the 4 non-tip commits pushed are not included in the payload." See https://forums.aws.amazon.com/thread.jspa?messageID=756611򸮃 – Michael Rush Apr 30 '18 at 22:45
0

If you are receiving the aws payload in your continuous integration server (jenkins, travis, etc) and you are able to clone the repository (if you are going to buid your app) you will be able to get the message using git tool which is already installed if you are going to clone your repository

This is the notification payload from aws code commit trigger to my ci server:

{
  "Type": "Notification",
  "MessageId": "296892a1a77b",
  "TopicArn": "arn:aws:sns:us-bar-1:123456:TopicFoo",
  "Subject": "UPDATE: AWS CodeCommit us-bar-1 push: my-awesome-repo",
  "Message": ".....",
  "Timestamp": "2021-06-23T20:57:15.040Z",
  "SignatureVersion": "1",
  "SigningCertURL": "https://sns.us-bar-1.amazonaws.com/SimpleNotificationService-foo-.pem",
  "UnsubscribeURL": "https://foo.bar"
}

Message field is stringified :s

{\"Records\":[{\"awsRegion\":\"us-bar-1\",\"codecommit\":{\"references\":[{\"commit\":\"fb28ebbec522cc403\",\"ref\":\"refs/heads/mybranch\"}]},\"customData\":null,\"eventId\":\"d1dab883\",\"eventName\":\"ReferenceChanges\",\"eventPartNumber\":1,\"eventSource\":\"aws:codecommit\",\"eventSourceARN\":\"arn:aws:codecommit:us-bar-1:123456:my-awesome-repo\",\"eventTime\":\"2021-06-23T20:57:15.005+0000\",\"eventTotalParts\":1,\"eventTriggerConfigId\":\"e4ea5f3bec6c\",\"eventTriggerName\":\"my_ci_server_notification\",\"eventVersion\":\"1.0\",\"userIdentityARN\":\"arn:aws:iam::123456:user/jane_doe\"}]}

But replacing \" by ", We will get a readable json

{
  "Records": [{
    "awsRegion": "us-bar-1",
    "codecommit": {
      "references": [{
        "commit": "fb28ebbec522cc403",
        "ref": "refs/heads/mybranch"
      }]
    },
    "customData": null,
    "eventId": "d1dab883",
    "eventName": "ReferenceChanges",
    "eventPartNumber": 1,
    "eventSource": "aws:codecommit",
    "eventSourceARN": "arn:aws:codecommit:us-bar-1:123456:my-awesome-repo",
    "eventTime": "2021-06-23T20:57:15.005+0000",
    "eventTotalParts": 1,
    "eventTriggerConfigId": "e4ea5f3bec6c",
    "eventTriggerName": "my_ci_server_notification",
    "eventVersion": "1.0",
    "userIdentityARN": "arn:aws:iam::123456:user/jane_doe"
  }]
}

In which we can extract the commit id in the section: references.commit

"references": [{
  "commit": "fb28ebbec522cc403",
  "ref": "refs/heads/mybranch"
}]

And finally get the message using git shell tool :S

git log --format=%B -n 1 fb28ebbec522cc403
JRichardsz
  • 14,356
  • 6
  • 59
  • 94