Skip to content
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

feat: new projects #48

Merged
merged 11 commits into from
Jan 9, 2024
Merged
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
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
lint:
cargo fmt
cargo clippy --release --all-targets --all-features -- -D clippy::all

prepare:
cd doseid && cargo sqlx prepare

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

26 changes: 26 additions & 0 deletions doseid/migrations/0002_projects.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
CREATE TYPE git_source AS ENUM ('github', 'gitlab', 'bitbucket');

CREATE TABLE IF NOT EXISTS projects (
id UUID NOT NULL,
name TEXT NOT NULL,
owner_id UUID NOT NULL,
git_source git_source NOT NULL,
git_source_metadata jsonb NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (id),
UNIQUE (name, owner_id)
);

CREATE TYPE deployment_status AS ENUM ('queued', 'building', 'error', 'canceled', 'ready');

CREATE TABLE IF NOT EXISTS deployments (
id UUID NOT NULL,
value TEXT NOT NULL,
project_id UUID NOT NULL,
owner_id UUID NOT NULL,
status deployment_status NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (id)
);
4 changes: 0 additions & 4 deletions doseid/src/deployment.rs → doseid/src/deployment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,12 @@ async fn build(owner_id: Uuid, project_id: Uuid, deployment_id: String, folder_p
}

mod tests {
use crate::config::Config;
use crate::deployment::build;
use crate::git::git_clone;
use git2::Repository;
use once_cell::sync::Lazy;
use tempfile::tempdir;
use uuid::Uuid;

static CONFIG: Lazy<Config> = Lazy::new(|| Config::new().unwrap());

#[tokio::test]
async fn test_clone_and_build() {
let temp_dir = tempdir().expect("Failed to create a temp dir");
Expand Down
30 changes: 23 additions & 7 deletions doseid/src/git/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ impl GithubIntegration {
.map_err(|_| anyhow!("invalid secret"))
}

async fn new_individual_repo(
pub async fn new_individual_repository(
&self,
name: &str,
private: Option<bool>,
access_token: &str,
) -> Result<Response, CreateRepoError> {
) -> Result<Value, CreateRepoError> {
let response = Client::new()
.post("https://api.github.com/user/repos")
.bearer_auth(access_token)
Expand All @@ -90,7 +90,8 @@ impl GithubIntegration {

let status_code = response.status();
if status_code.is_success() {
return Ok(response);
let body = response.json::<Value>().await?;
return Ok(body);
}

let error_result = response.error_for_status_ref().err().unwrap(); // safe to unwrap after checking success
Expand All @@ -111,6 +112,21 @@ impl GithubIntegration {
Err(CreateRepoError::RequestError(error_result))
}

async fn delete_repository(
&self,
repo_full_name: &str,
access_token: &str,
) -> Result<Response, Error> {
Client::new()
.delete(format!("https://api.github.com/repos/{}", repo_full_name))
.bearer_auth(access_token)
.header("Accept", "application/vnd.github.v3+json")
.header("User-Agent", "Dosei")
.send()
.await?
.error_for_status()
}

async fn update_deployment_status(
&self,
status: GithubDeploymentStatus,
Expand Down Expand Up @@ -193,7 +209,7 @@ impl GithubIntegration {
}

#[derive(Debug, thiserror::Error)]
enum CreateRepoError {
pub enum CreateRepoError {
#[error("Request failed")]
RequestError(#[from] Error),

Expand Down Expand Up @@ -233,7 +249,7 @@ impl GithubDeploymentStatus {
// TODO: Support passing settings to run github tests
#[cfg(test)]
mod tests {
use crate::test_utils::CONFIG;
use crate::test::CONFIG;
use std::env;

#[test]
Expand All @@ -254,12 +270,12 @@ mod tests {
.github_integration
.as_ref()
.unwrap()
.new_individual_repo(
.new_individual_repository(
"rust-tests-create",
None,
&env::var("GITHUB_TEST_ACCESS_TOKEN").unwrap(),
)
.await;
assert!(result.is_ok(), "Failed to create repository: {:?}", result)
assert!(result.is_ok(), "Failed to create repository: {:?}", result);
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion doseid/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod schema;
mod server;

#[cfg(test)]
mod test_utils;
mod test;

use config::Config;

Expand Down
20 changes: 20 additions & 0 deletions doseid/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use uuid::Uuid;

#[derive(Serialize, Deserialize, Debug)]
Expand Down Expand Up @@ -36,3 +37,22 @@ pub struct Secret {
pub updated_at: DateTime<Utc>,
pub created_at: DateTime<Utc>,
}

#[derive(sqlx::Type, Serialize, Deserialize, Debug)]
#[sqlx(type_name = "git_source", rename_all = "lowercase")]
pub enum GitSource {
Github,
Gitlab,
Bitbucket,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Project {
pub id: Uuid,
pub name: String,
pub owner_id: Uuid,
pub git_source: GitSource,
pub git_source_metadata: Value,
pub updated_at: DateTime<Utc>,
pub created_at: DateTime<Utc>,
}
File renamed without changes.
5 changes: 5 additions & 0 deletions doseid/src/server.rs → doseid/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod cron;
mod info;
mod integration;
mod ping;
mod project;
mod secret;

use anyhow::Context;
Expand Down Expand Up @@ -45,6 +46,10 @@ pub async fn start_server(config: &'static Config) -> anyhow::Result<()> {
"/unstable/integration/github/events",
routing::post(integration::github::api_integration_github_events),
)
.route(
"/projects/:owner_id/clone",
routing::post(project::api_new_project),
)
.route("/info", routing::get(info::api_info))
.route("/ping", routing::get(ping::api_ping))
.layer(Extension(Arc::clone(&shared_pool)))
Expand Down
107 changes: 107 additions & 0 deletions doseid/src/server/project.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
use crate::config::Config;
use crate::git::github::CreateRepoError;
use crate::schema::{GitSource, Project};
use axum::http::StatusCode;
use axum::{Extension, Json};
use serde::Deserialize;
use sqlx::{Pool, Postgres};
use std::collections::HashMap;
use std::env;
use std::sync::Arc;
use tempfile::tempdir;
use tracing::{error, info};
use uuid::Uuid;

pub async fn api_new_project(
config: Extension<&'static Config>,
pool: Extension<Arc<Pool<Postgres>>>,
Json(body): Json<NewProjectFromClone>,
) -> Result<StatusCode, StatusCode> {
let github_integration = match config.github_integration.as_ref() {
Some(github) => github,
None => {
error!("Github integration not enabled");
return Err(StatusCode::SERVICE_UNAVAILABLE);
}
};
// TODO: Find on db
let access_token = &env::var("GITHUB_TEST_ACCESS_TOKEN").unwrap();
let github_repo_response = github_integration
.new_individual_repository(&body.name, None, access_token)
.await
.map_err(|e| match e {
CreateRepoError::RequestError(_) => {
// TODO: report to sentry or something
StatusCode::INTERNAL_SERVER_ERROR
}
CreateRepoError::RepoExists => StatusCode::UNPROCESSABLE_ENTITY,
})?;

let temp_dir = tempdir().map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let temp_path = temp_dir.path();
let template_path = format!("{}/{}", temp_path.display(), &body.path.unwrap());

github_integration
.github_clone(
body.source_full_name,
temp_path,
body.branch.as_deref(),
Some(access_token),
None,
)
.await
.map_err(|_| {
error!("Github Clone Failed");
StatusCode::INTERNAL_SERVER_ERROR
})?;

let project = Project {
id: Uuid::new_v4(),
name: body.name,
owner_id: body.owner_id,
git_source: GitSource::Github,
git_source_metadata: github_repo_response,
updated_at: Default::default(),
created_at: Default::default(),
};
match sqlx::query_as!(
Project,
r#"INSERT INTO projects (id, name, owner_id, git_source, git_source_metadata, updated_at, created_at)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id, name, owner_id, git_source AS "git_source!: GitSource", git_source_metadata, updated_at, created_at"#,
project.id,
project.name,
project.owner_id,
project.git_source as GitSource,
project.git_source_metadata,
project.updated_at,
project.created_at,
).fetch_one(&**pool).await {
Ok(recs) => {
info!("{:?}", recs);
},
Err(err) => {
error!("Error in creating project: {:?}", err);
return Err(StatusCode::INTERNAL_SERVER_ERROR);
}
}

// TODO: Assign domain

// TODO: Save secrets / envs

// TODO: Git push
drop(temp_dir);
Ok(StatusCode::OK)
}

#[derive(Deserialize)]
pub struct NewProjectFromClone {
source_full_name: String,
branch: Option<String>,
path: Option<String>,
private: Option<bool>,
owner_id: Uuid,
name: String,
envs: Option<HashMap<String, String>>,
}
File renamed without changes.