-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add initial api to create pipeline with jobs
- Loading branch information
Showing
9 changed files
with
190 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,7 @@ pnpm-debug.log* | |
*.njsproj | ||
*.sln | ||
*.sw? | ||
|
||
# auto-generated via `yarn dev` | ||
components.d.ts | ||
typed-router.d.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use crate::{ | ||
models::{NewJob, NewPipeline, Pipeline}, | ||
ALL_ARCH, ARGS, | ||
}; | ||
use anyhow::Context; | ||
use buildit_utils::github::update_abbs; | ||
use diesel::{ | ||
r2d2::{ConnectionManager, Pool}, | ||
PgConnection, RunQueryDsl, SelectableHelper, | ||
}; | ||
|
||
pub async fn pipeline_new( | ||
pool: Pool<ConnectionManager<PgConnection>>, | ||
git_branch: &str, | ||
packages: &str, | ||
archs: &str, | ||
) -> anyhow::Result<i32> { | ||
// resolve branch name to commit hash | ||
update_abbs(git_branch, &ARGS.abbs_path) | ||
.await | ||
.context("Failed to update ABBS tree")?; | ||
|
||
let output = tokio::process::Command::new("git") | ||
.arg("rev-parse") | ||
.arg("HEAD") | ||
.current_dir(&ARGS.abbs_path) | ||
.output() | ||
.await | ||
.context("Failed to resolve branch to git commit")?; | ||
let git_sha = String::from_utf8_lossy(&output.stdout).trim().to_string(); | ||
|
||
// sanitize archs arg | ||
let mut archs: Vec<&str> = archs.split(",").collect(); | ||
if archs.contains(&"mainline") { | ||
// archs | ||
archs.extend(ALL_ARCH.iter()); | ||
archs.retain(|arch| *arch != "mainline"); | ||
} | ||
archs.sort(); | ||
archs.dedup(); | ||
|
||
// create a new pipeline | ||
let mut conn = pool | ||
.get() | ||
.context("Failed to get db connection from pool")?; | ||
|
||
use crate::schema::pipelines; | ||
let new_pipeline = NewPipeline { | ||
packages: packages.to_string(), | ||
archs: archs.join(","), | ||
git_branch: git_branch.to_string(), | ||
git_sha: git_sha.clone(), | ||
creation_time: chrono::Utc::now(), | ||
}; | ||
let pipeline = diesel::insert_into(pipelines::table) | ||
.values(&new_pipeline) | ||
.returning(Pipeline::as_returning()) | ||
.get_result(&mut conn) | ||
.context("Failed to create pipeline")?; | ||
|
||
// for each arch, create a new job | ||
for arch in &archs { | ||
use crate::schema::jobs; | ||
let new_job = NewJob { | ||
pipeline_id: pipeline.id, | ||
packages: packages.to_string(), | ||
arch: arch.to_string(), | ||
creation_time: chrono::Utc::now(), | ||
status: "created".to_string(), | ||
}; | ||
diesel::insert_into(jobs::table) | ||
.values(&new_job) | ||
.execute(&mut conn) | ||
.context("Failed to create job")?; | ||
} | ||
|
||
Ok(pipeline.id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use axum::{ | ||
extract::{Json, State}, | ||
http::StatusCode, | ||
response::{IntoResponse, Response}, | ||
}; | ||
use diesel::{ | ||
r2d2::{ConnectionManager, Pool}, | ||
PgConnection, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::api; | ||
|
||
pub async fn ping() -> &'static str { | ||
"PONG" | ||
} | ||
|
||
// learned from https://github.com/tokio-rs/axum/blob/main/examples/anyhow-error-response/src/main.rs | ||
pub struct AnyhowError(anyhow::Error); | ||
|
||
impl IntoResponse for AnyhowError { | ||
fn into_response(self) -> Response { | ||
(StatusCode::INTERNAL_SERVER_ERROR, format!("{}", self.0)).into_response() | ||
} | ||
} | ||
|
||
impl<E> From<E> for AnyhowError | ||
where | ||
E: Into<anyhow::Error>, | ||
{ | ||
fn from(err: E) -> Self { | ||
Self(err.into()) | ||
} | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub struct PipelineNewRequest { | ||
git_branch: String, | ||
packages: String, | ||
archs: String, | ||
} | ||
|
||
#[derive(Serialize)] | ||
pub struct PipelineNewResponse { | ||
id: i32, | ||
} | ||
|
||
pub async fn pipeline_new( | ||
State(pool): State<Pool<ConnectionManager<PgConnection>>>, | ||
Json(payload): Json<PipelineNewRequest>, | ||
) -> Result<Json<PipelineNewResponse>, AnyhowError> { | ||
let pipeline_id = | ||
api::pipeline_new(pool, &payload.git_branch, &payload.packages, &payload.archs).await?; | ||
Ok(Json(PipelineNewResponse { id: pipeline_id })) | ||
} |