Skip to content

Autodetect repo and branch names #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: Generate Diff
name: Generate Diff Example Change

on:
pull_request:
branches:
- "main"
paths:
- ".github/workflows/generate-diff.yml"
- ".github/workflows/generate-diff-exmaple-change.yml"
- "examples/**"

jobs:
Expand Down
49 changes: 49 additions & 0 deletions .github/workflows/generate-diff-tool-change.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Generate Diff Tool Change

on:
pull_request:
branches:
- "main"
paths:
- "src/**"
- Cargo.toml

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write

steps:
- uses: actions/checkout@v4
with:
path: pull-request

- uses: actions/checkout@v4
with:
ref: main
path: main

- name: Print Dirs
run: |
ls -la pull-request
ls -la main

- name: Generate Diff
run: |
docker build pull-request -f pull-request/Dockerfile_AMD64 -t image
docker run \
--network=host \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $(pwd)/main:/base-branch \
-v $(pwd)/pull-request:/target-branch \
-v $(pwd)/output:/output \
image --debug

- name: Post diff as comment
run: |
gh pr comment ${{ github.event.number }} --repo ${{ github.repository }} --body-file output/diff.md --edit-last || \
gh pr comment ${{ github.event.number }} --repo ${{ github.repository }} --body-file output/diff.md
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3 changes: 3 additions & 0 deletions Dockerfile_AMD64
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,8 @@ RUN apt-get update && apt-get install -y git
# copy argocd helm chart values
COPY ./argocd-config ./argocd-config

RUN git config --global --add safe.directory /base-branch
RUN git config --global --add safe.directory /target-branch

# set the startup command to run your binary
ENTRYPOINT ["./argocd-diff-preview"]
3 changes: 3 additions & 0 deletions Dockerfile_ARM64
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,8 @@ RUN apt-get update && apt-get install -y git
# copy argocd helm chart values
COPY ./argocd-config ./argocd-config

RUN git config --global --add safe.directory /base-branch
RUN git config --global --add safe.directory /target-branch

# set the startup command to run your binary
ENTRYPOINT ["./argocd-diff-preview"]
9 changes: 3 additions & 6 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ timeout := 120
pull-repostory:
@rm -rf base-branch || true && mkdir -p base-branch
@rm -rf target-branch || true && mkdir -p target-branch
cd base-branch && gh repo clone $(github_org)/$(gitops_repo) -- --depth=1 --branch "$(base_branch)" && cp -r $(gitops_repo)/. . && rm -rf .git && echo "*" > .gitignore && rm -rf $(gitops_repo) && cd -
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 -
cd base-branch && gh repo clone $(github_org)/$(gitops_repo) -- --depth=1 --branch "$(base_branch)" && cp -r $(gitops_repo)/. . && echo "*" > .gitignore && rm -rf $(gitops_repo) && cd -
cd target-branch && gh repo clone $(github_org)/$(gitops_repo) -- --depth=1 --branch "$(target_branch)" && cp -r $(gitops_repo)/. . && echo "*" > .gitignore && rm -rf $(gitops_repo) && cd -

local-test-cargo: pull-repostory
cargo run -- -b "$(base_branch)" -t "$(target_branch)" --repo $(github_org)/$(gitops_repo) -r "$(regex)" --debug --diff-ignore "$(diff-ignore)" --timeout $(timeout) -l "$(selector)"
cargo run -- -r "$(regex)" --debug --diff-ignore "$(diff-ignore)" --timeout $(timeout) -l "$(selector)"

local-test-docker: pull-repostory
docker build . -f $(docker_file) -t image
Expand All @@ -23,9 +23,6 @@ local-test-docker: pull-repostory
-v $(PWD)/target-branch:/target-branch \
-v $(PWD)/output:/output \
-v $(PWD)/secrets:/secrets \
-e BASE_BRANCH=$(base_branch) \
-e TARGET_BRANCH=$(target_branch) \
-e REPO=$(github_org)/$(gitops_repo) \
-e FILE_REGEX="$(regex)" \
-e DIFF_IGNORE="$(diff-ignore)" \
-e TIMEOUT=$(timeout) \
Expand Down
6 changes: 2 additions & 4 deletions src/argocd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub async fn install_argo_cd(options: ArgoCDOptions<'_>) -> Result<(), Box<dyn E
let mut password_encoded: Option<Output> = None;
let mut counter = 0;
while password_encoded.is_none() {
password_encoded = match run_command(&command, None).await {
password_encoded = match run_command(command, None).await {
Ok(a) => Some(a),
Err(e) => {
if counter == 5 {
Expand All @@ -121,10 +121,8 @@ pub async fn install_argo_cd(options: ArgoCDOptions<'_>) -> Result<(), Box<dyn E
let password_decoded = BASE64_STANDARD
.decode(password_encoded)
.expect("failed to decode password");
let password =
String::from_utf8(password_decoded).expect("failed to convert password to string");

password
String::from_utf8(password_decoded).expect("failed to convert password to string")
};

// sleep for 5 seconds
Expand Down
63 changes: 63 additions & 0 deletions src/diff.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::utils::check_if_folder_exists;
use crate::utils::run_command;
use crate::Branch;
use log::{debug, info};
use regex::Regex;
use std::fs;
use std::{error::Error, process::Output};

Expand Down Expand Up @@ -131,3 +133,64 @@ fn print_diff(summary: &str, diff: &str) -> String {
.trim_start()
.to_string()
}

fn folder_is_git_repo(branch: &Branch) -> bool {
check_if_folder_exists(&format!("{}/.git", branch.folder()))
}

pub async fn get_repo_name(branch: Branch) -> Option<String> {
if !folder_is_git_repo(&branch) {
debug!("Folder {} does not contain a .git folder", branch.folder());
return None;
}

let repo_name = match run_command("git remote get-url origin", Some(branch.folder())).await {
Ok(o) => o.stdout,
Err(e) => {
debug!(
"Error getting repo name for {}: {}",
branch,
String::from_utf8_lossy(&e.stderr)
);
return None;
}
};

let repo_name = String::from_utf8_lossy(&repo_name);

debug!("Repo name: {} for branch: {}", repo_name, branch);

let repo_url_regex: Regex = Regex::new(r"[:/](?P<repo>[^/]+/[^/]+)(\.git)?$").unwrap();

match repo_url_regex.captures(repo_name.as_ref()) {
Some(capture) => {
let repo_name = capture.name("repo").unwrap().as_str();
Some(repo_name.trim().to_string())
}
None => {
debug!("Failed to capture repo name from: {}", repo_name);
None
}
}
}

pub async fn get_branch_name(branch: Branch) -> Option<String> {
if !folder_is_git_repo(&branch) {
return None;
}

let branch_name =
match run_command("git rev-parse --abbrev-ref HEAD", Some(branch.folder())).await {
Ok(o) => o.stdout,
Err(e) => {
debug!(
"Error getting branch name for {}: {}",
branch,
String::from_utf8_lossy(&e.stderr)
);
return None;
}
};

Some(String::from_utf8_lossy(&branch_name).trim().to_string())
}
Loading