Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 46

Git Push Workflow: Parsing Webhooks & Logging Commit Summaries

Recommended resources

Introduction

In our last lesson, you mastered the crucial skill of parsing the complex JSON payload from a GitHub push event. You learned how to use both n8n expressions and Python to extract key details like the commit message, author, and repository name. This transformed the raw data into a structured, usable format.

Today, we'll build directly on that foundation. The goal of this lesson is to take those individual pieces of data and compose them into a single, human-readable commit summary. This formatted summary is the core message we'll use for notifications and logging. This step acts as a bridge, turning structured data into meaningful information, which is a fundamental pattern in building CI/CD and automation workflows.


Creating the Commit Summary

You currently have a workflow with a GitHub Trigger node followed by a node that parses the payload. The output of your parsing node is a clean JSON object containing fields like repo, branch, author, and commit_message. Our task is to combine these into a formatted string. We will explore two ways to do this, mirroring the methods you learned for parsing.

Method 1: Formatting with a Set Node and Expressions

The quickest way to combine data is with a Set node using n8n's expression engine. Expressions allow you to embed the values of your JSON fields directly into a string.

Let's add this to your existing workflow:

  1. After your parsing node (whether it's a Set or Code node), add a new Set node.
  2. We will create a single new value. In the Name field, enter commit_summary.
  3. For the Value, click the "Add Expression" icon (fx). This is where we'll construct our message. You can combine static text with dynamic data from the incoming node.

Enter the following expression. This format creates a multi-line string that is clear and readable.

New Push to {{ $json.repo }}
Branch: {{ $json.ref.replace('refs/heads/', '') }}
Author: {{ $json.author }}
Message: {{ $json.commit_message }}
View Commit: {{ $json.commit_url }}

A few things to note here:

  • We are directly referencing the fields you created in the previous parsing step (e.g., {{ $json.repo }}).
  • The expression {{ $json.ref.replace('refs/heads/', '') }} is a small but useful trick. The ref field from GitHub includes the full path refs/heads/main. The .replace() JavaScript method cleans this up to just show the branch name, main.
  • Pressing Shift+Enter inside the expression editor allows you to create new lines for better formatting.

After configuring the Set node, re-run the test from your GitHub Trigger node. When you inspect the output of this new Set node, you will see a single JSON object with the commit_summary field containing the fully formatted message.

This technique of using expressions to construct messages is very common. The Educative.io article you reviewed previously uses a similar method to prepare data for an AI agent.

How to build a GitHub PR agent with n8n in 15 minutes - Educative.io

This article, while focused on an AI use case, demonstrates the same core principle of combining data into a new field using expressions.

Review the section Feed the PR Data. Notice how the author uses an expression {{ ... }} to combine the PR's title and body into a single Input field. This is analogous to how we are combining commit details into our commit_summary.

Method 2: Formatting with a Code Node and Python

For more control and to leverage your Python expertise, the Code node is the superior choice. Using Python's f-strings for string formatting is both powerful and highly readable.

You can either add a new Code node after your parsing node or, more efficiently, integrate this formatting step directly into the Python code you wrote in the last lesson. Let's modify the previous Code node to handle both parsing and formatting in one step.

  1. Open the Code node from the previous lesson.
  2. We will adjust the code to not only parse the data but also to construct the summary string and add it as a new field.

Here is the updated Python script:




# Access the incoming JSON payload from the GitHub Trigger
incoming_data = _input.first()['json']




# --- Parsing Step (from previous lesson) ---
# Safely extract key fields using .get() to avoid errors
ref = incoming_data.get('ref', 'N/A')
repo_full_name = incoming_data.get('repository', {}).get('full_name', 'N/A')
pusher_name = incoming_data.get('pusher', {}).get('name', 'N/A')
head_commit = incoming_data.get('head_commit', {})
commit_message = head_commit.get('message', 'No commit message')
commit_url = head_commit.get('url', '#')




# --- New Formatting Step ---
# Use an f-string to create a clean, multi-line summary
# The .replace() method here is standard Python string manipulation
branch_name = ref.replace('refs/heads/', '')

commit_summary = (
    f"New Push to {repo_full_name}\n"
    f"Branch: {branch_name}\n"
    f"Author: {pusher_name}\n"
    f"Message: {commit_message}\n"
    f"View Commit: {commit_url}"
)




# Construct the final JSON object, including the new summary field
output_data = {
    "repo": repo_full_name,
    "branch": ref,
    "author": pusher_name,
    "commit_message": commit_message,
    "commit_url": commit_url,
    "commit_summary": commit_summary  # Add the formatted summary
}




# n8n expects a list of items to be returned.
# Each item must be a dictionary with a "json" key.
return [{"json": output_data}]

When you re-run the workflow with this updated Code node, the output will be a single, comprehensive JSON object that includes both the raw parsed fields and your new, nicely formatted commit_summary. This approach centralizes all your custom logic into one place, which is often preferable for maintainability.

The n8n documentation provides a good overview of the Code node's capabilities, which you can refer to if you need to recall how data structures are handled.

Using the Code node

This official documentation is a useful reference for the fundamentals of the Code node.

Quickly review the sections on Python (Native) and Key concepts. This reinforces the data structure n8n expects (item["json"]) and the variables available to you (_input or _items), which are central to how our Python script works.


Conclusion

In this lesson, you have successfully transformed the parsed data from a GitHub push into a formatted, human-readable summary. This completes a key step in our CI/CD notification pipeline.

Key Takeaways:

  • You can create formatted strings using n8n expressions in a Set node, which is suitable for simple compositions.
  • For greater flexibility and power, Python's f-strings within a Code node offer a more robust and familiar method for string formatting.
  • The process of parsing data and then formatting it into a message is a fundamental pattern in automation.
  • You can choose to separate parsing and formatting into different nodes for modularity, or combine them into a single Code node for efficiency.

You are now perfectly positioned for the final step of this module. In our next lesson, we will take the commit_summary you've just created and use a notification node to post it to a chat channel, completing your first automated CI/CD feedback loop.