#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →

Use ChatGPT to automate common dev workflows

Pasting code into the ChatGPT window and copying the answer back is helpful, but it is not automation. Automation starts when a script calls the model for you at a fixed point in your workflow: when you commit, when a pull request opens, when a build fails. This guide shows four such recipes using the OpenAI API from Python, plus the rules that keep them safe and cheap.

Use ChatGPT to automate common dev workflows

Use ChatGPT to automate common dev workflows

The shared helper

Every recipe below uses one small function. Install the SDK with pip install openai and set OPENAI_API_KEY as an environment variable (never commit the key).

# scripts/ai.py
from openai import OpenAI

client = OpenAI()  # reads OPENAI_API_KEY from the environment
MODEL = "gpt-4o-mini"  # example; use a current, low-cost model

def ask(system: str, user: str, max_chars: int = 20000) -> str:
    """Send one request; trims very large inputs to control cost."""
    resp = client.chat.completions.create(
        model=MODEL,
        messages=[
            {"role": "system", "content": system},
            {"role": "user", "content": user[:max_chars]},
        ],
        temperature=0.2,
    )
    return resp.choices[0].message.content.strip()

A low temperature keeps answers consistent from run to run, which is what you want in tooling.

Recipe 1: draft commit messages from the staged diff

# scripts/ai_commit_msg.py
import subprocess
from ai import ask

diff = subprocess.run(["git", "diff", "--staged"],
                      capture_output=True, text=True).stdout
if diff.strip():
    print(ask(
        "Write a git commit message: a summary line under 72 characters "
        "in imperative mood, a blank line, then up to 4 bullet points. "
        "Describe only what the diff shows.",
        diff,
    ))

Hook it into Git with .git/hooks/prepare-commit-msg (make it executable with chmod +x):

#!/bin/sh
# $1 = path of the commit message file
# $2 = message source; empty for a plain "git commit"
if [ -z "$2" ]; then
  python scripts/ai_commit_msg.py > "$1.ai" && cat "$1" >> "$1.ai" && mv "$1.ai" "$1"
fi

Now git commit opens your editor with a suggested message already filled in. You edit it or replace it; nothing is committed without you. The hook skips git commit -m, merges and amends, because Git sets $2 in those cases. If you are new to commits and staging, see version control basics.

Recipe 2: summarise every pull request

A GitHub Actions workflow can post a plain-English summary on each new pull request, which helps reviewers start faster.

# .github/workflows/pr-summary.yml
name: PR summary
on:
  pull_request:
    types: [opened]

permissions:
  contents: read
  pull-requests: write

jobs:
  summary:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
      - run: pip install openai
      - name: Build summary
        env:
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
          BASE: ${{ github.base_ref }}
        run: |
          git diff "origin/$BASE...HEAD" > pr.diff
          python scripts/pr_summary.py pr.diff > summary.md
      - name: Comment on the PR
        env:
          GH_TOKEN: ${{ github.token }}
        run: gh pr comment ${{ github.event.pull_request.number }} --body-file summary.md
# scripts/pr_summary.py
import sys
from ai import ask

diff = open(sys.argv[1], encoding="utf-8").read()
print(ask(
    "Summarise this pull request for a reviewer. Sections: What changed, "
    "Why it might matter, Files to review first, Possible risks. "
    "Be specific and brief. Say 'unclear' rather than guessing.",
    diff,
))

Store the API key under the repository's Actions secrets. For pull requests from forks, secrets are not available by default, so the job will not have a key there; that is a safety feature, not a bug.

The same structure works for a first-pass review comment; this guide on ChatGPT for code review covers what to ask for, and you can go further with an AI-powered review assistant.

Recipe 3: draft unit tests for a function

# scripts/draft_tests.py  usage: python scripts/draft_tests.py app/pricing.py
import sys, pathlib
from ai import ask

src = pathlib.Path(sys.argv[1]).read_text(encoding="utf-8")
print(ask(
    "Write pytest tests for the functions in this module. Cover normal cases, "
    "edge cases (empty input, zero, negative numbers, None) and expected "
    "exceptions. Do not change the module. Output only Python code.",
    src,
))

Redirect the output to a file such as tests/test_pricing_draft.py, run it, and read every test. Generated tests sometimes assert the current (buggy) behaviour as if it were correct, so a passing test is not proof of correct code. Keep the tests that make sense, fix or delete the rest.

Recipe 4: triage a failing build log

# scripts/explain_log.py  usage: python scripts/explain_log.py build.log
import sys
from ai import ask

log = open(sys.argv[1], encoding="utf-8", errors="ignore").read()
tail = log[-15000:]  # the error is usually near the end
print(ask(
    "This is the end of a failed CI log. Give: 1) the first real error line, "
    "quoted; 2) the most likely cause; 3) two things to check. "
    "If the log is not enough to tell, say so.",
    tail,
))

Sending only the tail of the log keeps the request small, which saves money and time.

Rules that keep this safe

  • Check your company's policy first. Many employers restrict sending source code to external services. Some allow it only through an enterprise account or an approved provider.
  • Strip secrets. Make sure .env files, keys and customer data never appear in diffs or logs you send. A secret scanner in your pre-commit setup helps.
  • Keep a human gate. The model suggests; a person commits, approves and merges.
  • Control cost. Trim inputs, use a small model for routine jobs, and trigger only on events that matter (PR opened, not every push).
  • Fail softly. If the API is down, the hook or workflow should skip quietly, not block the commit or the build.

Where to go next

Once these basics work, the same ask() helper can draft release notes from merged PR titles, convert API responses into TypeScript types, or explain a stack trace from a production alert. For prompts that work well in these scripts, see ChatGPT prompts for coding and debugging, and for automation beyond code, read about AI tools for daily IT workflows.

Sources: OpenAI API reference and openai-python README; Git documentation (githooks: prepare-commit-msg); GitHub Docs (workflow syntax, GITHUB_TOKEN permissions, gh pr comment).

Placed Students

Our Clients

Partners

...