Hello!

Today I will show how to automatically generate GitHub Readme with the latest blog posts using go. In GitHub you need to create a repository named as your GitHub profile. My GitHub profile is called mpostument, so I’ll create a repository with the same name.

I clone the repository and call go mod init in it, in order to initialize the go module which will generate readme. At the root of the repository you need to create main.go which will be executed.

In the beginning there will be imports and the structure in which posts will be stored.

package main

import (
	"log"
	"os"
	"strings"
	"text/template"

	"github.com/mmcdole/gofeed"
)

type ReadmeData struct {
	Title string
	Link  string
}

The gofeed module is needed to read the rss feed and generate a list of posts based on it. The code itself is quite simple, read the template using template.ParseFiles. After that create gofeed.NewParser() which will be used to get rss feed from my site. I loop through all posts from rss feed and add data about posts in a slice which I will transfer to a template and I will save in a file by means of template.Execute.

func main() {
	template, err := template.ParseFiles("README.tmpl")
	if err != nil {
		log.Fatalln(err)
	}

	fp := gofeed.NewParser()

	feed, err := fp.ParseURL("https://mpostument.com/index.xml")
	if err != nil {
		log.Fatalln(err)
	}

	postList := []ReadmeData{}
	for index, post := range feed.Items {
		if index == 7 {
			break
		}

		readmeData := ReadmeData{
			Title: post.Title,
			Link:  post.Link,
		}
		postList = append(postList, readmeData)
	}

	f, err := os.Create("README.MD")
	if err != nil {
		log.Fatalln(err)
	}

	err = template.Execute(f, postList)
	if err != nil {
		log.Fatalln(err)
	}
}

The next step is to create a template README.tmpl with content:

## 📝 My Latest Blog Posts
{{range .}}
- [{{.Title}}](https://mpostument.com{{.Link}}/)
{{end}}

If you run the code, it will create a file README.MD with a list of recent posts.

You can use GithubActions to generate a GitHub readme automatically. You need to create the .github/workflows folders at the root of the repository. And inside create a file generate_readme.yml. This file is a configuration of the steps that GitHubActions will call. The action will run every day at midnight. First, the repository is cloned, go is installed and all dependencies are loaded, the code is compiled, script started and Readme.md is generated. And if there are changes in the file, they will be pushed in the repository and will be available in the profile.

name: Generate readme
on:
  schedule:
    - cron: "0 0 * * *"
  workflow_dispatch:
jobs:
  update-readme-with-blog:
    name: Update this repo's README with latest blog posts
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Setup go
        uses: actions/setup-go@v2
        with:
          go-version: "1.16"
      - name: Go tidy
        run: go mod tidy
      - name: Build project
        run: go build
      - name: Generate readme
        run: ./mpostument
      - name: Commit and push if changed
        run: |-
          git diff
          git config --global user.email "actions@users.noreply.github.com"
          git config --global user.name "README-bot"
          git add -A
          git commit -m "Updated readme content" || exit 0
          git push