Skip to content

Commit

Permalink
feat: init a Git repo for default use_case template (#97)
Browse files Browse the repository at this point in the history
* feat: init a repo for default use_case template

* fix: pr suggestions

* feat: create repo after cloning template & commit after tests

* chore: remove x86 support (rust-lang/cargo#13546)

* ci: uselibgit2-dev only

* fix: remove commit

* chore: cleanup & test ci without ssl feature

* fix: remove openssl and warm user if we can't set a git repo
  • Loading branch information
Angel-Dijoux authored Dec 7, 2024
1 parent e2203c8 commit 0d7408d
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 23 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/cd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
branches:
- main
tags:
- '*'
- "*"
pull_request:
merge_group:
workflow_dispatch:
Expand All @@ -26,18 +26,18 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
python-version: "3.10"
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.target }}
args: --release --out dist
sccache: 'true'
sccache: "true"
manylinux: auto
docker-options: -e SENTRY_DSN
env:
# Workaround ring 0.17 build issue
CFLAGS_aarch64_unknown_linux_gnu: '-D__ARM_ARCH=8'
CFLAGS_aarch64_unknown_linux_gnu: "-D__ARM_ARCH=8"
- name: Upload wheels
uses: actions/upload-artifact@v3
with:
Expand All @@ -53,14 +53,14 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
python-version: "3.10"
architecture: ${{ matrix.target }}
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.target }}
args: --release --out dist
sccache: 'true'
sccache: "true"
- name: Upload wheels
uses: actions/upload-artifact@v3
with:
Expand All @@ -76,13 +76,13 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
python-version: "3.10"
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.target }}
args: --release --out dist
sccache: 'true'
sccache: "true"
- name: Upload wheels
uses: actions/upload-artifact@v3
with:
Expand Down Expand Up @@ -158,4 +158,4 @@ jobs:
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
cache-to: type=gha,mode=max
44 changes: 44 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ tracing-subscriber = { version = "0.3", features = ["json", "env-filter"] }
url = { version = "2.5", features = ["serde"] }
uuid = "1.7"
which = "6.0"
git2 = { version = "0.19.0" , default-features = false }

[build-dependencies]
toml_edit = "0.22"
Expand Down
19 changes: 10 additions & 9 deletions src/commands/new/use_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use graphql_client::GraphQLQuery;
use indicatif::ProgressBar;
use serde::Serialize;

use crate::error::{self, Result};
use crate::error::{self, format_permission_error, Result};
use crate::git::init_repository;
use crate::graphql_client::{custom_scalars::*, GraphQLClient};

use super::GlobalArgs;
Expand Down Expand Up @@ -53,15 +54,15 @@ pub async fn use_case(args: UseCase, global: GlobalArgs) -> Result<()> {
.competition(args.competition)
.title(competition.title)
.render(&dest)
.map_err(|e| {
error::user(
&format!("Failed to create use case at '{}': {}", dest.display(), e),
&format!(
"Make sure you have the correct permissions for '{}'",
dest.display()
),
.map_err(|e| format_permission_error("create use case", &dest, &e))?;
init_repository(&pb, &dest, Some(competition.short_description))
.inspect_err(|e| {
tracing::warn!(
"Failed to create a Git repository: {}. Skipping git init.",
e
)
})?;
})
.ok();
pb.finish_with_message(format!(
"Created use case in directory '{}'",
dest.display()
Expand Down
19 changes: 15 additions & 4 deletions src/commands/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
},
download::download_archive,
error::{self, Result},
git::init_repository,
graphql_client::GraphQLClient,
};
use clap::Args;
Expand Down Expand Up @@ -99,10 +100,20 @@ pub async fn template(args: Template, global: GlobalArgs) -> Result<()> {

pb.set_message("Downloading competition template...");
match download_archive(download_url, &destination, &pb).await {
Ok(_) => pb.finish_with_message(format!(
"Competition template downloaded to {}",
destination.display()
)),
Ok(_) => {
init_repository(&pb, &destination, None)
.inspect_err(|e| {
tracing::warn!(
"Failed to create a Git repository: {}. Skipping git init.",
e
)
})
.ok();
pb.finish_with_message(format!(
"Competition template downloaded to {}",
destination.display()
))
}
Err(error) => {
pb.finish_with_message("Failed to download competition template");
return Err(error);
Expand Down
1 change: 0 additions & 1 deletion src/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,6 @@ pub async fn run_submission_tests(
),
));
}

result.map(|_| ())
}

Expand Down
12 changes: 12 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::Path;

use axum::{
body::Body,
response::{IntoResponse, Response},
Expand Down Expand Up @@ -84,6 +86,16 @@ impl IntoResponse for Error {
}
}

pub fn format_permission_error(action: &str, dest: &Path, error: &impl std::fmt::Display) -> Error {
user(
&format!("Failed to {} at '{}': {}", action, dest.display(), error),
&format!(
"Make sure you have the correct permissions for '{}'",
dest.display()
),
)
}

// macro_rules! bail_system {
// ($message:expr, $advice:expr) => {
// return Err(system($message, $advice).into());
Expand Down
31 changes: 31 additions & 0 deletions src/git.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use git2::{Repository, RepositoryInitOptions};
use indicatif::ProgressBar;
use std::path::Path;

use crate::error::{format_permission_error, Result};

pub fn init_repository(
pb: &ProgressBar,
dest: impl AsRef<Path>,
description: Option<String>,
) -> Result<()> {
pb.set_message("Initializing local Git repository...");
let mut opts = RepositoryInitOptions::new();
opts.description(
description
.unwrap_or("Aqora competition".to_string())
.as_str(),
)
.no_reinit(true);
match Repository::init_opts(&dest, &opts) {
Ok(_) => {
pb.set_message("Repository initialized successfully.");
Ok(())
}
Err(error) => Err(format_permission_error(
"init a local Git repository",
dest.as_ref(),
&error,
)),
}
}
1 change: 1 addition & 0 deletions src/graphql/use_case_template_info.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ query UseCaseTemplateInfo($slug: String!) {
competitionBySlug(slug: $slug) {
id
title
shortDescription
useCase {
latest {
version
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod dirs;
mod download;
mod error;
mod evaluate;
mod git;
mod graphql_client;
mod id;
mod ipynb;
Expand Down

0 comments on commit 0d7408d

Please sign in to comment.