this github action receives a git diff (e.g., a pr diff) and uses openai to summarize and explain the changes made in that diff in a clear, concise way.
- generates concise explanations of code changes
 - customizable output length and style
 - easy to integrate into your ci/cd workflow
 - now includes comprehensive tests and error handling
 
required the diff to be explained.
required your openai api key. get one at openai platform.
optional an example summary to guide the model's output style. default: "update the code with new features: parallelisation, caching, and better error handling"
optional maximum number of tokens to generate. default: 30
optional maximum characters in the generated explanation. default: 140
the explanation and summary of the diff generated by openai.
name: Explain PR Changes
on:
  pull_request:
    types: [opened, synchronize]
jobs:
  explain-diff:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          
      - name: Get PR diff
        id: diff
        run: |
          git fetch origin ${{ github.event.pull_request.base.ref }}
          DIFF=$(git diff origin/${{ github.event.pull_request.base.ref }}..HEAD)
          echo "DIFF<<EOF" >> $GITHUB_ENV
          echo "$DIFF" >> $GITHUB_ENV
          echo "EOF" >> $GITHUB_ENV
          
      - name: Explain Diff
        id: explain
        uses: grey/openai-summarize-diff-action@main
        with:
          diff: ${{ env.DIFF }}
          apikey: ${{ secrets.OPENAI_API_KEY }}
          
      - name: Output explanation
        run: echo "${{ steps.explain.outputs.explanation }}"name: Explain PR Changes and Comment
on:
  pull_request:
    types: [opened, synchronize]
jobs:
  explain-diff:
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          
      - name: Get PR diff
        id: diff
        run: |
          git fetch origin ${{ github.event.pull_request.base.ref }}
          DIFF=$(git diff origin/${{ github.event.pull_request.base.ref }}..HEAD)
          echo "DIFF<<EOF" >> $GITHUB_ENV
          echo "$DIFF" >> $GITHUB_ENV
          echo "EOF" >> $GITHUB_ENV
          
      - name: Explain Diff
        id: explain
        uses: grey/openai-summarize-diff-action@main
        with:
          diff: ${{ env.DIFF }}
          apikey: ${{ secrets.OPENAI_API_KEY }}
          
      - name: Post comment
        uses: actions/github-script@v7
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            const explanation = process.env.EXPLANATION;
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: `## 🤖 AI Summary of Changes\n\n${explanation}`
            });
        env:
          EXPLANATION: ${{ steps.explain.outputs.explanation }}- 
clone the repository:
git clone https://github.com/your-username/openai-summarize-diff-action.git cd openai-summarize-diff-action - 
install dependencies:
npm install
 - 
create a
.envfile with your openai api key:OPENAI_API_KEY=your_api_key_here 
run tests:
npm testrun tests with coverage:
npm run test:coveragerun tests in watch mode (useful during development):
npm run test:watchrun linting:
npm run lintbuild the action:
npm run buildthis will run linting, tests, and then build the action into the dist directory.
you can test the action locally by creating a test script:
// test-local.js
require('dotenv').config();
const { generateDiffExplanation } = require('./src/openai');
async function test() {
  const diff = `diff --git a/file.js b/file.js
index 123..456 789
--- a/file.js
+++ b/file.js
@@ -1,3 +1,4 @@
 const a = 1;
+const b = 2;
 const c = 3;`;
  try {
    const explanation = await generateDiffExplanation({
      diff,
      apiKey: process.env.OPENAI_API_KEY,
      maxTokens: 50,
      maxCharacters: 200
    });
    
    console.log('Explanation:', explanation);
  } catch (error) {
    console.error('Error:', error.message);
  }
}
test();run the test script:
node test-local.js- this action uses openai's gpt-4o-mini model by default.
 - make sure to store your openai api key as a secret in your repository settings.
 - the action will ignore library folders to focus on meaningful code changes.