Python script: RSS feed from Git commit log

The Python script below creates an RSS file from a git commit log. I'm using an expanded version of this script to generate the RSS feed for my notes.group42.ca site. It can be used as a starting template or demo of the GitPython and feedgen modules. Although the script is runnable the resulting RSS file is only useful as sample output.

To use the script the following modules must be installed:

  • GitPython
  • feedgen

Links

Notes

  • The code used in the feed creation function is taken directly from the Python feedgeen Create a Feed example.
  • RSS and Atom feed items are intended to point to individual web pages which this script doesn't create. If you want useful item links you'll need to generate the target pages or create logic that maps commits to your desired target pages.
  • This code has only been tested in one feed reader. If the description doesn't look correct in your feed reader you may need to place the description text in a CDATA block.
  • To leverage the automation check out Conventional Commits. It's a convention for wording commit messages that, among many things, makes it easier writing automation tools for processing the messages.

The Script

#!/usr/bin/env python3

from feedgen.feed import FeedGenerator
from git import Repo

repo_directory = '.'
repo_branch = 'main'
num_feed_items = 20


def create_feed():
    feed = FeedGenerator()
    feed.id('http://lernfunk.de/media/654321')
    feed.title('Some Testfeed')
    feed.author({'name': 'John Doe', 'email': 'john@example.de'})
    feed.link(href='http://example.com', rel='alternate')
    feed.logo('http://ex.com/logo.jpg')
    feed.subtitle('This is a cool feed!')
    feed.link(href='http://larskiesow.de/feed.xml', rel='self')
    feed.language('en')
    return feed


def main():
    repo = Repo(repo_directory)
    rss_feed = create_feed()
    for commit in list(repo.iter_commits(repo_branch, max_count=num_feed_items)):
        feed_item = rss_feed.add_entry()
        feed_item.pubDate(str(commit.committed_datetime.isoformat()))
        feed_item.title(commit.summary)
        feed_item.description(commit.message.replace('\n', '<br>\n'))
        feed_item.link({'href': 'http://example.com/' + commit.hexsha})
        feed_item.guid(commit.hexsha)
    rss_feed.rss_file('feed.xml')
    return


if __name__ == "__main__":
    main()