laurigates / gha-issue-resolution

Analyzes GitHub issues and provides AI-generated suggestions using Gemini
1 stars 0 forks source link

Add functionality to check for errors in actions logs #7

Open laurigates opened 2 days ago

laurigates commented 2 days ago

The workflow could use Gemini to create issues based on errors that happen in the actions workflows. It could check the annotations in the actions logs.

laurigates commented 7 hours ago
## AI-generated suggestion

Here's a potential solution to this issue, generated by an AI assistant:

## Detailed Solution to Add Error Checking and Issue Creation in GitHub Actions

This solution details the modifications needed to implement automated GitHub issue creation based on errors within GitHub Actions workflows. It assumes familiarity with GitHub Actions YAML and Python.

1. Modify .github/workflows/issue_resolution.yml:

This workflow will be responsible for retrieving logs, parsing annotations, and creating GitHub issues.

name: Issue Resolution from Action Logs

on:
  workflow_run:
    workflows: ["your_main_workflow"] # Replace with your main workflow name
    types:
      - completed
    branches:
      - main # Specify branches to monitor

jobs:
  resolve_issues:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Get workflow run logs
        id: get_logs
        uses: actions/github-script@v6
        with:
          script: |
            const runId = context.payload.workflow_run.id;
            const logs = await github.rest.actions.getWorkflowJobLogs({
              owner: context.repo.owner,
              repo: context.repo.repo,
              job_id: context.payload.workflow_run.jobs[0].id, # Assumes only one job
              run_id: runId
            });
            return logs.data.log;

      - name: Parse logs and create issues
        uses: ./actions/gha-issue-resolution # Assumes a reusable action (see below)
        with:
          logs: ${{ steps.get_logs.outputs.log }}
          github_token: ${{ secrets.GITHUB_TOKEN }}

2. Create a Reusable Action (actions/gha-issue-resolution):

This action encapsulates the logic for parsing logs and creating issues. Create a new directory actions/gha-issue-resolution and add the following files:

name: "GitHub Issue Resolution from Action Logs"
description: "Creates GitHub issues based on error annotations in workflow logs."
inputs:
  logs:
    description: "Workflow logs to parse."
    required: true
  github_token:
    description: "GitHub personal access token."
    required: true

runs:
  using: "node16"
  main: "index.js"
const core = require('@actions/core');
const github = require('@actions/github');
const { parseLogs } = require('./log_parser'); // See below

async function run() {
  try {
    const logs = core.getInput('logs');
    const token = core.getInput('github_token');
    const octokit = github.getOctokit(token);
    const annotations = parseLogs(logs);

    for (const annotation of annotations) {
      if (annotation.level === 'error') {
        await octokit.rest.issues.create({
          owner: github.context.repo.owner,
          repo: github.context.repo.repo,
          title: `Error: ${annotation.title}`,
          body: `Error message: ${annotation.message}\nFile: ${annotation.file}\nLine: ${annotation.line}\nWorkflow Run: ${github.context.runId}`,
          labels: ['bug', 'error']
        });
      }
    }
  } catch (error) {
    core.setFailed(error.message);
  }
}

run();
// This function needs to be adapted to your specific annotation format.
// This is a simple example, assuming annotations are in JSON format.
exports.parseLogs = (logs) => {
  try {
    const annotations = JSON.parse(logs).annotations; // Adapt this to your log format.  It might be a string that needs to be parsed differently.
    return annotations;
  } catch (error) {
    console.error("Error parsing logs:", error);
    return [];
  }
};

3. Update pyproject.toml (if necessary): The Python code is no longer needed as we're using a Node.js action.

4. Testing: Thorough testing is crucial. Create test cases for various scenarios:

5. Explanation:

This solution uses a reusable GitHub Action for modularity. The main workflow retrieves logs and passes them to the reusable action. The reusable action parses the logs, identifies error annotations (based on your specific log format and needs), and creates GitHub issues using the GitHub API. The log parsing logic is separated into a dedicated function for better maintainability and testability.

6. Potential Side Effects and Considerations:

This comprehensive solution provides a robust framework for automatically creating GitHub issues from GitHub Actions workflow errors. Remember to adapt the code to your specific workflow's log format and error annotation style. Thorough testing is crucial to ensure reliability.

Please review this suggestion and let me know if you need any clarification or have any questions.
This is an AI-generated response and may require human validation and testing before implementation.