Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions examples/github-actions/markdown-converter/convert_to_md.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import os
from google import genai


def convert_to_markdown(text_content,api_key):


client = genai.Client(api_key = api_key)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

According to the Google Python Style Guide, which this project follows, there should be no spaces around the = sign for keyword arguments.1

Suggested change
client = genai.Client(api_key = api_key)
client = genai.Client(api_key=api_key)

Style Guide References

Footnotes

  1. The project's style guide specifies the use of the Google Python Style Guide. (link)




MODEL_ID = "gemini-2.5-flash" # @param ["gemini-2.5-flash-lite", "gemini-2.5-flash", "gemini-2.5-pro"] {"allow-input":true, isTemplate: true}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This # @param comment is specific to Google Colab and creates a form element there. It has no effect in a regular Python script and can be confusing in the context of a GitHub Action. It's best to remove it for clarity.

Suggested change
MODEL_ID = "gemini-2.5-flash" # @param ["gemini-2.5-flash-lite", "gemini-2.5-flash", "gemini-2.5-pro"] {"allow-input":true, isTemplate: true}
MODEL_ID = "gemini-2.5-flash"



prompt = f"""
Convert the following text into well-structured Markdown format suitable for GitHub documentation.
Use appropriate headings (#, ##, ###), lists, code blocks, and bold/italic formatting.
Ensure the output is clean and directly usable in a .md code don't include (```Markdown ````).

Text to convert:
{text_content}
"""

try:

response = client.models.generate_content(
model = MODEL_ID,
contents = prompt
)


return response.text
except Exception as e:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Catching a generic Exception is too broad and can hide bugs or prevent specific error handling. It's better to catch more specific exceptions, such as those from google.api_core.exceptions, to handle potential API errors more precisely.

print(f"An error occurred during content generation: {e}")
return ""

def main():


api_key = os.getenv("GEMINI_API_KEY")
if not api_key:
print("Error: GEMINI_API_KEY environment variable is not set.")
exit(1)

input_file = "source.txt" # You can change this to your input file
output_file = "README.md" # You can change this to your desired output file
Comment on lines +60 to +61

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Hardcoding filenames makes the script less reusable and can lead to accidentally overwriting important files like the main README.md. It's better to accept file paths as command-line arguments, for example, by using Python's argparse module.



if not os.path.exists(input_file):
print(f"Error: Input file '{input_file}' not found.")
exit(1)

with open(input_file, "r") as f:
text_content = f.read()

markdown_content = convert_to_markdown(text_content, api_key)

if markdown_content:

with open(output_file, "w") as f:
f.write(markdown_content)
print(f"Successfully converted '{input_file}' to '{output_file}'.")
else:
print("Markdown conversion failed.")

if __name__ == "__main__":
main()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The file is missing a final newline, which is a standard convention for text files.

Suggested change
main()
main()

54 changes: 54 additions & 0 deletions examples/github-actions/markdown-converter/generate-markdown.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@


name: Generate Markdown from Text


on:
push:
paths:
- 'source.txt'
pull_request:
paths:
- 'source.txt'




permissions:
contents: write


jobs:
convert_to_md:
runs-on: ubuntu-latest


steps:
- name: Checkout Repository
uses: actions/checkout@v3


- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'


- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install google-genai


- name: Run Markdown Converter Script
run: python convert_to_md.py
env:

GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}


- name: Commit and Push Changes
uses: stefanzweifel/git-auto-commit-action@v4
Comment on lines +28 to +51

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Several of the GitHub Actions used in this workflow are outdated. It's recommended to update them to their latest major versions to benefit from new features, performance improvements, and security patches:

  • actions/checkout@v3 should be actions/checkout@v4
  • actions/setup-python@v4 should be actions/setup-python@v5
  • stefanzweifel/git-auto-commit-action@v4 should be stefanzweifel/git-auto-commit-action@v5

with:
commit_message: 'Auto-generate README.md from source.txt'
branch: main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Hardcoding branch: main will prevent this action from working correctly on pull requests or feature branches. The git-auto-commit-action defaults to committing to the current branch, which is the desired behavior here. Please remove this line to allow the action to work on any branch. Also, the file is missing a final newline.

62 changes: 62 additions & 0 deletions examples/github-actions/markdown-converter/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# AI Text to Markdown Converter GitHub Action

This is a GitHub Action that automatically converts plain text into a well-structured Markdown document using the Gemini API. It's designed to streamline the process of creating and maintaining documentation, like `README.md` files, directly within your repository.

## How It Works

The workflow is simple and automated. You write your content in a plain text file, push your changes, and the GitHub Action handles the rest.

1. **Write Content:** You write or paste your text into a designated source file (e.g., `source.txt`).
2. **Commit & Push:** You commit and push the changes to your repository.
3. **Workflow Trigger:** The GitHub Action automatically detects the change to the `source.txt` file and starts the conversion process.
4. **AI Conversion:** A Python script uses the Gemini API to intelligently convert your text into Markdown, adding headings, lists, bold text, and other formatting as needed.
5. **Auto-Commit:** The action then automatically commits the new or updated Markdown content to a destination file (e.g., `README.md`).

## Getting Started

To use this tool in your own repository, you need to set up two files and a secret.

### Step 1: Add the Tool's Files

Place the following two files in your repository:

* **`convert_to_md.py`**: This script contains the logic for the AI conversion.
* **`.github/workflows/generate-markdown.yml`**: This YAML file defines the GitHub Action workflow.

* **`structure of the repository`**:
Comment on lines +23 to +26

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The formatting of this list is a bit confusing. The last item, 'structure of the repository', seems like it should be a sub-heading or regular text rather than a bullet point in the list of files.

Suggested change
* **`convert_to_md.py`**: This script contains the logic for the AI conversion.
* **`.github/workflows/generate-markdown.yml`**: This YAML file defines the GitHub Action workflow.
* **`structure of the repository`**:
* **`convert_to_md.py`**: This script contains the logic for the AI conversion.
* **`.github/workflows/generate-markdown.yml`**: This YAML file defines the GitHub Action workflow.
**Repository Structure**:


/your-repository
├── .github
│ └── workflows
│ └── generate-markdown.yml # The GitHub Action workflow file
├── convert_to_md.py # The Python script for conversion
├── source.txt # The input text file
└── README.md # The output Markdown file (created automatically)



### Step 2: Add Your API Key

To securely use the Gemini API, you must store your API key as a GitHub Secret.

1. Go to your repository's **Settings > Secrets and variables > Actions**.
2. Click **New repository secret**.
3. Set the **Name** to `GEMINI_API_KEY`.
4. Paste your personal Gemini API key into the **Secret** field.

### Step 3: Ensure Read and Write Permissions

For the GitHub Action to be able to create and push a new file to your repository, you must grant it permission.

1. Go to your repository's **Settings > Actions > General**.
2. Scroll down to the **Workflow permissions** section.
3. Select **Read and write permissions**.
4. Click **Save**.

### Step 4: Use the Tool

1. Create a file named `source.txt` in the root of your repository.
2. Add your plain text content to `source.txt`.
3. Commit and push your changes.

The workflow will run automatically, and you will see a new `README.md` file created with the formatted Markdown content.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Please add a newline at the end of the file. It's a standard convention for text files.

Suggested change
The workflow will run automatically, and you will see a new `README.md` file created with the formatted Markdown content.
The workflow will run automatically, and you will see a new `README.md` file created with the formatted Markdown content.

17 changes: 17 additions & 0 deletions examples/github-actions/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# GitHub Actions and the Gemini API

This directory contains examples of using GitHub Actions to create automated workflows powered by the Gemini API.

## What is a GitHub Action?
A GitHub Action is a custom, automated task that you can run in your repository. You can use actions to automate a wide range of tasks, from running tests and building applications to creating documentation and performing content-related tasks. They are a core part of a developer's CI/CD (Continuous Integration/Continuous Delivery) workflow.

## How They Integrate with the Gemini API
GitHub Actions are perfect for automating AI-powered tasks. By creating a workflow that calls the Gemini API, you can:
- **Generate content:** Automatically create blog posts, documentation, or code snippets.
- **Analyze text:** Review new code, issues, or pull requests for summaries or key information.
- **Format data:** Convert unstructured data into a clean, structured format, such as the text-to-Markdown example in this directory.

Because the workflows run on GitHub's servers, they have direct access to your repository's files and can use secrets to securely handle your API keys. This creates a powerful, hands-off automation system.

## Examples
* [AI Text to Markdown Converter](./ai-markdown-converter/README.md)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This link appears to be broken. The directory for the example is markdown-converter, not ai-markdown-converter.

Suggested change
* [AI Text to Markdown Converter](./ai-markdown-converter/README.md)
* [AI Text to Markdown Converter](./markdown-converter/readme.md)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Please add a newline at the end of the file. It's a standard convention for text files.

Suggested change
* [AI Text to Markdown Converter](./ai-markdown-converter/README.md)
* [AI Text to Markdown Converter](./ai-markdown-converter/README.md)