diff --git a/.github/workflows/impactifier.yml b/.github/workflows/impactifier.yml index db2b6d9..f98ae46 100644 --- a/.github/workflows/impactifier.yml +++ b/.github/workflows/impactifier.yml @@ -46,16 +46,53 @@ jobs: 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=refactor - name: Post Comment on Pull Request if: github.event_name == 'pull_request' uses: actions/github-script@v6 with: script: | + 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 + const rawData = fs.readFileSync(path, 'utf8'); + let diffData; + try { + diffData = JSON.parse(rawData); + } catch (error) { + console.error('Failed to 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) { + formattedDiff += `${delta.value}\n`; + } + }); + } else { + formattedDiff = 'No differences found.'; + } + + // Create the comment body with the diff in a Markdown code block + const commentBody = `## Impactifier Report + + ```diff ${formattedDiff} ````; + github.rest.issues.createComment({ issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo, - body: `## Impactifier Report` + body: commentBody }); + 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, }