DEV Community

Cover image for README Generator Using AI And Github Action
Yunesh Shrestha
Yunesh Shrestha

Posted on

README Generator Using AI And Github Action

I’m really lazy when it comes to adding a README file to my project. I don’t know why, but I feel like someone could contribute to my repo. However, no one ever did. 😞

So, why not use AI to create my README file based on my repo’s name and the files I’ve added to it?

I’m going to demonstrate the same process in this blog (one of my first).

Steps:
1) Go to GitHub and create a new repo.
You can do this by pressing the green button as shown in the image:

New Repo Button

2) Create a Dockerfile.
A Dockerfile defines the environment and the steps to set up your application inside a container. To install Git, curl, and jq in a Docker image, add the appropriate commands to update the package list and install these tools. This ensures your container has the necessary utilities to manage repositories (Git), handle HTTP requests (curl), and parse JSON (jq). You also need to copy the entrypoint script from your computer to the Docker image, make the file executable, and set it as the entrypoint.

# Use a base image that has bash installed
FROM ubuntu:latest

# Install dependencies, including Git, curl, and jq
RUN apt-get update && \
    apt-get install -y git curl jq && \
    rm -rf /var/lib/apt/lists/*

# Set the entrypoint to your script
COPY entrypoint.sh /entrypoint.sh

# Make the entrypoint script executable
RUN chmod +x /entrypoint.sh

# Set the entrypoint
ENTRYPOINT ["/entrypoint.sh"]

Enter fullscreen mode Exit fullscreen mode

3) Create entrypoint.sh.
Here’s the algorithm for the script:

  • Configure Git to trust the workspace directory.
  • If README.md doesn’t exist, create it and extract the repo name.
  • Build a prompt for the Gemini API, including a list of files if present.
  • Use the Gemini API to generate content and append it to README.md.
  • Set Git credentials, stage, commit, and push the changes.
#!/bin/bash

# Configure Git to trust the /github/workspace directory
git config --global --add safe.directory /github/workspace

# Check if README.md file exists, create it if not
if [ ! -f "README.md" ]; then
  echo "Creating README.md file..."

  # Get the repository name from the environment variable
  REPO_NAME=${GITHUB_REPOSITORY##*/} # Extract the repo name
  echo "# $REPO_NAME" > README.md

  # Add a prompt for OpenAI API to generate content
  PROMPT="Generate a brief description and usage instructions for the repository named '$REPO_NAME'"

  # List all files in the repository
  FILE_LIST=$(ls -A | grep -v README.md)

  if [ -z "$FILE_LIST" ]; then
    echo "No files found in the repository."
  else
    PROMPT="$PROMPT and with file list as: $FILE_LIST"
    echo "Files found: $FILE_LIST"
  fi

  echo "Prompt for GEMINI API: $PROMPT"

  # Call Gemini API to get content
  API_KEY=$G_API_KEY 
  echo "Using API key: $API_KEY"

  RESPONSE=$(curl -H 'Content-Type: application/json' -d '{"contents":[{"parts":[{"text":  "'"$PROMPT"'"}]}]}' -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=$API_KEY")

  echo "Response from Open AI: $RESPONSE"

  # Extract the generated text from the API response
  GENERATED_CONTENT=$(echo $RESPONSE | jq -r '.candidates[0].content.parts[0].text')

  # Append the generated content to the README
  echo -e "\n## Description" >> README.md
  echo "$GENERATED_CONTENT" >> README.md

  echo "README.md file created successfully!"
else
  echo "README.md already exists."
fi

# Set up Git config (adjust the email and name if needed)
git config --global user.email "youremail@gmail.com"
git config --global user.name "GitHub Action"

# Add README.md to the staging area
git add README.md

# Commit the changes (add a message to describe the change)
git commit -m "Add/Update README.md via custom GitHub Action"

# Push the changes back to the repository
git push

Enter fullscreen mode Exit fullscreen mode

4) This will generate the README file.
5) Now, how to run this on the repo where we need to generate the README file?
Go to the repo where you want to create the README file and create a folder structure like this: .github/workflows/custom.yml in the root directory. You can give any name to the .yml file, but .github/workflows/ should remain the same.

6) Create a YAML file.
This YAML file defines a GitHub Action workflow that:

  • Triggers on every push event.
  • Grants write permissions to the repository contents.
  • Runs on the latest Ubuntu environment.
  • Includes steps to:

    • Check out the repository.
    • Install curl and jq for handling HTTP requests and JSON processing.
    • Configure Git to trust the workspace directory.
    • Set Git credentials using the provided email and GitHub token.
      • Run the custom action using the repository YOUR-GITHUB-USER-NAME/REPO-NAME@repo-branch, with the Gemini API key passed as an environment variable.
name: Custom

on: push

permissions:
  contents: write

jobs:
  main:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Install curl and jq
        run: |
          sudo apt-get update
          sudo apt-get install -y curl jq

      - name: Set up Git safe directory
        run: git config --global --add safe.directory /github/workspace

      - name: Set up Git credentials
        run: |
          git config --global user.email "your-email-id@gmail.com"
          git config --global user.name "GitHub Action"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Run custom action
        uses: YOUR-GITHUB-USER-NAME/REPO-NAME@repo-branch
        env:
          G_API_KEY : ${{ secrets.G_API_KEY }}
Enter fullscreen mode Exit fullscreen mode

You need to add the secret key to the repo that contains the .yaml file (i.e., the repo where you need to generate the README file). To add secrets, go to Settings on the repo, then go to Security > Secrets and variables, and add keys like OPEN_API_KEY in my case or it might be G_API_KEY in your case.

Secret Button

All set! You can now use this on whichever repo you'd like.

Top comments (4)

Collapse
 
adhikareeprayush profile image
Prayush Adhikari

A really great use of gemini free api I have seen so far.

Collapse
 
yunesh_shrestha_92d55c598 profile image
Yunesh Shrestha

Thank you.

Collapse
 
ngtduc693 profile image
Duc Nguyen Thanh

great, thank you

Collapse
 
yunesh_shrestha_92d55c598 profile image
Yunesh Shrestha

You are welcome.