Skip to content

Commit

Permalink
feat: try to intercept output and use it in the CI
Browse files Browse the repository at this point in the history
  • Loading branch information
wzslr321 committed Oct 9, 2024
1 parent b41f885 commit 375b220
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
41 changes: 39 additions & 2 deletions .github/workflows/impactifier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
16 changes: 12 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fs::File;
use std::io::Write;
use std::path::Path;

use clap::Parser;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -127,22 +132,25 @@ 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,
&crate::git::DiffOptions::Branches {
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(())
}
Expand Down
5 changes: 3 additions & 2 deletions src/git.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::path::Path;
use serde::Serialize;
use thiserror::Error;

use git2::{Cred, RemoteCallbacks, Repository};
Expand Down Expand Up @@ -29,12 +30,12 @@ pub enum GitError {
// Unknown { err: Box<dyn std::error::Error> },
}

#[derive(Debug)]
#[derive(Debug, Serialize)]
pub struct Diff {
pub deltas: Vec<FileDelta>,
}

#[derive(Debug)]
#[derive(Debug, Serialize)]
pub struct FileDelta {
pub value: String,
}
Expand Down

0 comments on commit 375b220

Please sign in to comment.