From 3b70925c2064d40a1a1bfbef5d07b4778757b54f Mon Sep 17 00:00:00 2001 From: wzslr321 Date: Wed, 9 Oct 2024 22:44:57 +0200 Subject: [PATCH] feat: update CI; try to intercept the git diff and comment it --- .github/workflows/impactifier.yml | 94 ++++++++++++++++++++++++++++--- src/cli.rs | 16 ++++-- src/git.rs | 5 +- 3 files changed, 100 insertions(+), 15 deletions(-) diff --git a/.github/workflows/impactifier.yml b/.github/workflows/impactifier.yml index db2b6d9..b7efc64 100644 --- a/.github/workflows/impactifier.yml +++ b/.github/workflows/impactifier.yml @@ -11,17 +11,18 @@ permissions: issues: write pull-requests: write - jobs: impactifier: runs-on: ubuntu-latest steps: + # 1. Checkout the repository with full history - name: Checkout Repository uses: actions/checkout@v4 with: - fetch-depth: 0 + fetch-depth: 0 # Fetch all history for all branches + # 2. Cache Cargo Registry - name: Cache Cargo Registry uses: actions/cache@v3 with: @@ -30,6 +31,7 @@ jobs: restore-keys: | ${{ runner.os }}-cargo-registry- + # 3. Cache Cargo Build - name: Cache Cargo Build uses: actions/cache@v3 with: @@ -38,24 +40,98 @@ jobs: restore-keys: | ${{ runner.os }}-cargo-build- + # 4. Build Impactifier - name: Build Impactifier run: | cargo build --release --manifest-path Cargo.toml + # 5. Run Impactifier and generate diff.json - name: Run Impactifier + id: run_impactifier env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | - ./target/release/impactifier --config impactifier-config.yaml + ./target/release/impactifier --tracing-level=0 --from-branch=main --to-branch=origin/refactor + + # 6. (Optional) Output diff.json for debugging + - name: Output diff.json (Debug) + if: ${{ github.event_name == 'pull_request' }} + run: | + cat diff.json + # 7. Post Comment on Pull Request with the diff - name: Post Comment on Pull Request if: github.event_name == 'pull_request' uses: actions/github-script@v6 with: script: | - github.rest.issues.createComment({ - issue_number: context.issue.number, - owner: context.repo.owner, - repo: context.repo.repo, - body: `## Impactifier Report` - }); + const fs = require('fs'); + const path = 'diff.json'; // Path to the diff JSON file + + // Check if the diff file exists + if (!fs.existsSync(path)) { + console.log('No diff.json file found.'); + return; + } + + // Read and parse the diff JSON + let diffData; + try { + const rawData = fs.readFileSync(path, 'utf8'); + diffData = JSON.parse(rawData); + } catch (error) { + console.error('Failed to read or parse diff.json:', error); + return; + } + + // Format the diff for the comment + let formattedDiff = ''; + if (diffData.deltas && Array.isArray(diffData.deltas)) { + diffData.deltas.forEach(delta => { + if (delta.value) { + // Escape backticks in the delta.value to prevent breaking the Markdown + const safeValue = delta.value.replace(/`/g, '\\`'); + formattedDiff += `${safeValue}\n`; + } + }); + } else { + formattedDiff = 'No differences found.'; + } + + // Handle large diffs by truncating (optional) + const maxLength = 60000; // GitHub comment limit + let truncatedDiff = formattedDiff; + if (formattedDiff.length > maxLength) { + truncatedDiff = formattedDiff.substring(0, maxLength) + '\n... (diff truncated)'; + } + + // Create a summary based on the number of deltas + let summary = ''; + if (diffData.deltas && diffData.deltas.length > 0) { + summary = `**Total Changes:** ${diffData.deltas.length} file(s) changed.\n\n`; + } else { + summary = 'No changes detected between the specified branches.\n\n'; + } + + // Create the comment body with summary and diff + const commentBody = `## Impactifier Report + + ${summary} + + \`\`\`diff + ${truncatedDiff} + \`\`\``; + + // Post the comment to the pull request + try { + await github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: commentBody + }); + console.log('Impactifier report posted successfully.'); + } catch (error) { + console.error('Failed to post Impactifier report:', error); + } + diff --git a/src/cli.rs b/src/cli.rs index 88cb418..2bd422d 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,3 +1,5 @@ +use std::fs::File; +use std::io::Write; use std::path::Path; use clap::Parser; @@ -67,6 +69,9 @@ struct Args { /// 4 = Error #[arg(long, default_value_t = 2)] tracing_level: u8, + + #[arg(long, default_value_t=String::from("origin"))] + origin: String, } // TODO add more credentials variants @@ -127,12 +132,12 @@ pub fn run() -> Result<(), CliError> { }, }; - let credentials = Credentials::UsernamePassword { + let mock_credentials = Credentials::UsernamePassword { username: "wzslr321", password: "TEST", }; - crate::git::fetch_remote(&repository, "origin", &credentials).unwrap(); + crate::git::fetch_remote(&repository, &args.origin, &mock_credentials).unwrap(); let diff = extract_difference( &repository, @@ -140,9 +145,12 @@ pub fn run() -> Result<(), CliError> { from: &args.from_branch.unwrap(), to: &args.to_branch.unwrap_or_else(|| "main".to_string()), }, - ); + ) + .unwrap(); + let serialized = serde_yaml::to_string(&diff); - println!("{:?}", diff); + let mut file = File::create("./diff.txt").unwrap(); + file.write_all(serialized.unwrap().as_bytes()).unwrap(); Ok(()) } diff --git a/src/git.rs b/src/git.rs index 2b98704..499e20f 100644 --- a/src/git.rs +++ b/src/git.rs @@ -1,4 +1,5 @@ use std::path::Path; +use serde::Serialize; use thiserror::Error; use git2::{Cred, RemoteCallbacks, Repository}; @@ -29,12 +30,12 @@ pub enum GitError { // Unknown { err: Box }, } -#[derive(Debug)] +#[derive(Debug, Serialize)] pub struct Diff { pub deltas: Vec, } -#[derive(Debug)] +#[derive(Debug, Serialize)] pub struct FileDelta { pub value: String, }