Skip to content

Commit d7c3741

Browse files
dag-andersenDag Andersen
authored and
Dag Andersen
committed
Fixed diff_ignore not working
1 parent 46186a7 commit d7c3741

10 files changed

+77
-66
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ target/
33
argocd/
44
secrets/
55
output/
6+
.vscode/

Cargo.lock

+12-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
tokio = {version="1.4.0",features = ["full"]}
10-
base64 = "0.22.0"
9+
tokio = {version="1.37.0",features = ["full"]}
10+
base64 = "0.22.1"
1111
serde = "1.0"
1212
serde_yaml = "0.9"
13-
serde_json = "1.0.115"
13+
serde_json = "1.0.117"
1414
walkdir = "2.5.0"
15-
schemars = "0.8.16"
15+
schemars = "0.8.19"
1616
structopt = { version = "0.3" }
1717
regex = "1.10.4"
1818
log = "0.4.21"

Dockerfile_AMD64

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ RUN rm ./target/release/deps/argocd_diff_preview-*
3434
RUN cargo build --release
3535

3636
# our final base
37-
FROM rust:1-slim-buster
37+
FROM rust:1-slim-bookworm
3838

3939
COPY --from=docker:dind /usr/local/bin/docker /usr/local/bin/
4040

Dockerfile_ARM64

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ RUN rm ./target/release/deps/argocd_diff_preview-*
3434
RUN cargo build --release
3535

3636
# our final base
37-
FROM rust:1-slim-buster
37+
FROM rust:1-slim-bookworm
3838

3939
COPY --from=docker:dind /usr/local/bin/docker /usr/local/bin/
4040

@@ -46,4 +46,4 @@ COPY --from=build /argocd-diff-preview/target/release/argocd-diff-preview .
4646
RUN apt-get update && apt-get install -y git
4747

4848
# set the startup command to run your binary
49-
CMD ["./argocd-diff-preview"]
49+
ENTRYPOINT ["./argocd-diff-preview"]

README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,9 @@ OPTIONS:
286286
--base-branch-folder <folder> Base branch folder [env: BASE_BRANCH_FOLDER=] [default: base-branch]
287287
-i, --diff-ignore <diff-ignore> Ignore lines in diff. Example: use 'v[1,9]+.[1,9]+.[1,9]+' for ignoring changes caused by version changes following semver [env: DIFF_IGNORE=]
288288
-r, --file-regex <file-regex> Regex to filter files. Example: "/apps_.*\.yaml" [env: FILE_REGEX=]
289-
-l, --line-count <line-count> Generate diffs with <n> lines above and below the highlighted changes in the diff. Default: 10 [env: LINE_COUNT=]
289+
-l, --line-count <line-count> Generate diffs with <n> lines above and below the highlighted changes in the diff. [env: LINE_COUNT=] [Default: 10]
290290
--local-cluster-tool <tool> Local cluster tool. Options: kind, minikube [env: LOCAL_CLUSTER_TOOL=] [default: auto]
291+
--max-diff-length <length> Max diff message character count. [env: MAX_DIFF_LENGTH=] [Default: 65536] (GitHub comment limit)
291292
-o, --output-folder <output-folder> Output folder where the diff will be saved [env: OUTPUT_FOLDER=] [default: ./output]
292293
--repo <repo> Git Repository. Format: OWNER/REPO [env: REPO=]
293294
-s, --secrets-folder <secrets-folder> Secrets folder where the secrets are read from [env: SECRETS_FOLDER=] [default: ./secrets]
@@ -298,9 +299,6 @@ OPTIONS:
298299
299300
## Roadmap
300301
- Make a dedicated GitHub Action that wraps the Docker container, so the tool becomes more user-friendly.
301-
- Let the user specify Argo CD version. Currently it always uses the newest version available.
302-
- Let the user specify how many lines above and below the code change they want to see. This is useful when the diff is too big to be displayed in a PR comment.
303-
- Let the user specify max characters in the diff. This is useful when the diff is too big to be displayed in a PR comment.
304302
- Delete Argo CD Applications, when they have been parsed by the tool, so Argo CD can focus on the remaining applications, which hopefully speeds up the rendering process.
305303
306304
## Questions, issues, or suggestions

makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ gitops_repo ?= argocd-diff-preview
22
github_org ?= dag-andersen
33
base_branch := main
44
docker_file := Dockerfile_ARM64
5+
timeout := 120
56

67
pull-repostory:
78
@rm -rf base-branch || true && mkdir -p base-branch
@@ -10,7 +11,7 @@ pull-repostory:
1011
cd target-branch && gh repo clone $(github_org)/$(gitops_repo) -- --depth=1 --branch "$(target_branch)" && cp -r $(gitops_repo)/. . && rm -rf .git && echo "*" > .gitignore && rm -rf $(gitops_repo) && cd -
1112

1213
local-test-cargo: pull-repostory
13-
cargo run -- -b "$(base_branch)" -t "$(target_branch)" --repo $(github_org)/$(gitops_repo) -r "$(regex)" --debug
14+
cargo run -- -b "$(base_branch)" -t "$(target_branch)" --repo $(github_org)/$(gitops_repo) -r "$(regex)" --debug --diff-ignore "$(diff-ignore)" --timeout $(timeout)
1415

1516
local-test-docker: pull-repostory
1617
docker build . -f $(docker_file) -t image
@@ -26,4 +27,6 @@ local-test-docker: pull-repostory
2627
-e TARGET_BRANCH=$(target_branch) \
2728
-e REPO=$(github_org)/$(gitops_repo) \
2829
-e FILE_REGEX="$(regex)" \
30+
-e DIFF_IGNORE="$(diff-ignore)" \
31+
-e TIMEOUT=$(timeout) \
2932
image

src/diff.rs

+38-29
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use log::info;
1+
use log::{debug, info};
22
use std::fs;
33
use std::{error::Error, process::Output};
44

@@ -20,45 +20,54 @@ pub async fn generate_diff(
2020
base_branch_name, target_branch_name
2121
);
2222

23-
let list_of_patterns_to_ignore = match diff_ignore {
24-
Some(s) => format!("--ignore-matching-lines='{}'", s),
23+
let patterns_to_ignore = match diff_ignore {
24+
Some(s) => format!("--ignore-matching-lines {}", s),
2525
None => "".to_string(),
2626
};
2727

2828
let parse_diff_output = |output: Result<Output, Output>| -> String {
29-
match output {
30-
Ok(_) => "No changes found".to_string(),
31-
Err(e) => String::from_utf8_lossy(&e.stdout).trim().to_string(),
29+
let o = match output {
30+
Err(e) if !e.stderr.is_empty() => panic!(
31+
"Error running diff command with error: {}",
32+
String::from_utf8_lossy(&e.stderr)
33+
),
34+
Ok(e) => String::from_utf8_lossy(&e.stdout).trim_end().to_string(),
35+
Err(e) => String::from_utf8_lossy(&e.stdout).trim_end().to_string(),
36+
};
37+
if o.trim().is_empty() {
38+
return "No changes found".to_string();
39+
} else {
40+
o
3241
}
3342
};
3443

35-
let summary_as_string = parse_diff_output(
36-
run_command(
37-
&format!(
38-
"git --no-pager diff --compact-summary --no-index {} {} {}",
39-
list_of_patterns_to_ignore,
40-
Branch::Base,
41-
Branch::Target
42-
),
43-
Some(output_folder),
44-
)
45-
.await,
44+
let summary_diff_command = format!(
45+
"git --no-pager diff --compact-summary --no-index {} {} {}",
46+
patterns_to_ignore,
47+
Branch::Base,
48+
Branch::Target
4649
);
4750

48-
let diff_as_string = parse_diff_output(
49-
run_command(
50-
&format!(
51-
"git --no-pager diff --no-prefix -U{} --no-index {} {} {}",
52-
line_count.unwrap_or(10),
53-
list_of_patterns_to_ignore,
54-
Branch::Base,
55-
Branch::Target
56-
),
57-
Some(output_folder),
58-
)
59-
.await,
51+
debug!(
52+
"Getting summary diff with command: {}",
53+
summary_diff_command
54+
);
55+
56+
let summary_as_string =
57+
parse_diff_output(run_command(&summary_diff_command, Some(output_folder)).await);
58+
59+
let diff_command = &format!(
60+
"git --no-pager diff --no-prefix -U{} --no-index {} {} {}",
61+
line_count.unwrap_or(10),
62+
patterns_to_ignore,
63+
Branch::Base,
64+
Branch::Target
6065
);
6166

67+
debug!("Getting diff with command: {}", diff_command);
68+
69+
let diff_as_string = parse_diff_output(run_command(diff_command, Some(output_folder)).await);
70+
6271
let remaining_max_chars =
6372
max_diff_message_char_count - markdown_template_length() - summary_as_string.len();
6473

src/extract.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static ERROR_MESSAGES: [&str; 7] = [
1515
"path does not exist",
1616
"error converting YAML to JSON",
1717
"Unknown desc = `helm template .",
18-
"Unknown desc = Unable to resolve"
18+
"Unknown desc = Unable to resolve",
1919
];
2020

2121
static TIMEOUT_MESSAGES: [&str; 4] = [
@@ -146,10 +146,7 @@ pub async fn get_resources(
146146
if !other_errors.is_empty() {
147147
error!("❌ Applications with 'ComparisonError' errors:");
148148
for (name, msg) in &other_errors {
149-
error!(
150-
"❌ {}, {}",
151-
name, msg
152-
);
149+
error!("❌ {}, {}", name, msg);
153150
}
154151
}
155152
return Err("Timed out".into());
@@ -231,7 +228,8 @@ pub async fn delete_applications() {
231228
.await
232229
.map(|o| String::from_utf8_lossy(&o.stdout).to_string())
233230
.map(|e| e.trim().is_empty())
234-
.unwrap_or_default() {
231+
.unwrap_or_default()
232+
{
235233
let _ = child.kill();
236234
break;
237235
}
@@ -241,7 +239,8 @@ pub async fn delete_applications() {
241239
.await
242240
.map(|o| String::from_utf8_lossy(&o.stdout).to_string())
243241
.map(|e| e.trim().is_empty())
244-
.unwrap_or_default() {
242+
.unwrap_or_default()
243+
{
245244
let _ = child.kill();
246245
break;
247246
}

src/main.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ use log::{debug, error, info};
1010
use std::path::PathBuf;
1111
use structopt::StructOpt;
1212

13-
use crate::utils::{
14-
check_if_folder_exists, create_folder_if_not_exists, run_command,
15-
};
13+
use crate::utils::{check_if_folder_exists, create_folder_if_not_exists, run_command};
1614
mod argocd;
1715
mod diff;
1816
mod extract;
@@ -45,7 +43,7 @@ struct Opt {
4543
diff_ignore: Option<String>,
4644

4745
/// Generate diffs with <n> lines above and below the highlighted changes in the diff. Default: 10
48-
#[structopt(short, long, env)]
46+
#[structopt(short, long, env)]
4947
line_count: Option<usize>,
5048

5149
/// Argo CD version. Default: stable
@@ -138,12 +136,15 @@ async fn main() -> Result<(), Box<dyn Error>> {
138136
let base_branch_name = opt.base_branch;
139137
let target_branch_name = opt.target_branch;
140138
let repo = opt.repo;
141-
let diff_ignore = opt.diff_ignore;
139+
let diff_ignore = opt.diff_ignore.filter(|f| !f.trim().is_empty());
142140
let timeout = opt.timeout;
143141
let output_folder = opt.output_folder.as_str();
144142
let secrets_folder = opt.secrets_folder.as_str();
145143
let line_count = opt.line_count;
146-
let argocd_version = opt.argocd_version.as_deref();
144+
let argocd_version = opt
145+
.argocd_version
146+
.as_deref()
147+
.filter(|f| !f.trim().is_empty());
147148
let max_diff_length = opt.max_diff_length;
148149

149150
// select local cluster tool

0 commit comments

Comments
 (0)