Initial commit

This commit is contained in:
Daniel Flanagan 2024-03-18 22:46:41 -05:00
commit 870d9ea43f
11 changed files with 2189 additions and 0 deletions

1
.envrc Normal file
View File

@ -0,0 +1 @@
use flake

10
.gitignore vendored Normal file
View File

@ -0,0 +1,10 @@
/target
/.direnv
/.pre-commit-config.yaml
# Added by cargo
#
# already existing elements were commented out
#/target

1816
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

19
Cargo.toml Normal file
View File

@ -0,0 +1,19 @@
[package]
name = "tasks"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.81"
color-eyre = "0.6.3"
reqwest = { version = "0.11.26", features = ["json", "socks"] }
reqwest-middleware = "0.2.5"
reqwest-retry = "0.4.0"
reqwest-tracing = "0.4.8"
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.114"
tokio = { version = "1.36.0", features = ["full"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }

138
flake.lock Normal file
View File

@ -0,0 +1,138 @@
{
"nodes": {
"flake-compat": {
"flake": false,
"locked": {
"lastModified": 1696426674,
"narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=",
"owner": "edolstra",
"repo": "flake-compat",
"rev": "0f9255e01c2351cc7d116c072cb317785dd33b33",
"type": "github"
},
"original": {
"owner": "edolstra",
"repo": "flake-compat",
"type": "github"
}
},
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1701680307,
"narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "4022d587cbbfd70fe950c1e2083a02621806a725",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"gitignore": {
"inputs": {
"nixpkgs": [
"pre-commit-hooks",
"nixpkgs"
]
},
"locked": {
"lastModified": 1703887061,
"narHash": "sha256-gGPa9qWNc6eCXT/+Z5/zMkyYOuRZqeFZBDbopNZQkuY=",
"owner": "hercules-ci",
"repo": "gitignore.nix",
"rev": "43e1aa1308018f37118e34d3a9cb4f5e75dc11d5",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "gitignore.nix",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1710631334,
"narHash": "sha256-rL5LSYd85kplL5othxK5lmAtjyMOBg390sGBTb3LRMM=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "c75037bbf9093a2acb617804ee46320d6d1fea5a",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs-stable": {
"locked": {
"lastModified": 1704874635,
"narHash": "sha256-YWuCrtsty5vVZvu+7BchAxmcYzTMfolSPP5io8+WYCg=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "3dc440faeee9e889fe2d1b4d25ad0f430d449356",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-23.11",
"repo": "nixpkgs",
"type": "github"
}
},
"pre-commit-hooks": {
"inputs": {
"flake-compat": "flake-compat",
"flake-utils": "flake-utils",
"gitignore": "gitignore",
"nixpkgs": [
"nixpkgs"
],
"nixpkgs-stable": "nixpkgs-stable"
},
"locked": {
"lastModified": 1708018599,
"narHash": "sha256-M+Ng6+SePmA8g06CmUZWi1AjG2tFBX9WCXElBHEKnyM=",
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"rev": "5df5a70ad7575f6601d91f0efec95dd9bc619431",
"type": "github"
},
"original": {
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs",
"pre-commit-hooks": "pre-commit-hooks"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

52
flake.nix Normal file
View File

@ -0,0 +1,52 @@
{
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
inputs.pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix";
inputs.pre-commit-hooks.inputs.nixpkgs.follows = "nixpkgs";
outputs = {
self,
pre-commit-hooks,
nixpkgs,
}: let
inherit (self) outputs;
supportedSystems = ["x86_64-linux" "aarch64-darwin"];
forEachSupportedSystem = nixpkgs.lib.genAttrs supportedSystems;
in {
formatter = forEachSupportedSystem (system: nixpkgs.legacyPackages.${system}.alejandra);
checks = forEachSupportedSystem (system: {
pre-commit-check = pre-commit-hooks.lib.${system}.run {
src = ./.;
hooks = {
alejandra.enable = true;
};
};
});
devShells = forEachSupportedSystem (system: let
pkgs = import nixpkgs {inherit system;};
in {
rust-dev = pkgs.mkShell {
inherit (outputs.checks.${system}.pre-commit-check) shellHook;
buildInputs = with pkgs; [
# nix tools
nil
alejandra
# rust tools
cargo
rustc
rustfmt
rustPackages.clippy
rust-analyzer
openssl
pkg-config
];
};
default = outputs.devShells.${system}.rust-dev;
});
};
}

1
src/config.rs Normal file
View File

@ -0,0 +1 @@

100
src/gitlab.rs Normal file
View File

@ -0,0 +1,100 @@
use reqwest::{
header::{HeaderMap, HeaderValue},
Client, Url,
};
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware};
use reqwest_tracing::TracingMiddleware;
use serde::Deserialize;
use tracing::trace;
pub struct GitLab {
url: Url,
client: ClientWithMiddleware,
}
// ---
/* "avatar_url": "http://localhost:3000/uploads/user/avatar/1/index.jpg",
// "web_url": "http://localhost:3000/john_smith",
// "created_at": "2012-05-23T08:00:58Z",
// "bio": "",
// "location": null,
// "skype": "",
// "linkedin": "",
// "twitter": "",
// "discord": "",
// "website_url": "",
// "organization": "",
// "job_title": "",
// "pronouns": "he/him",
// "bot": false,
// "work_information": null,
// "followers": 0,
// "following": 0,
// "local_time": "3:38 PM",
// "last_sign_in_at": "2012-06-01T11:41:01Z",
// "confirmed_at": "2012-05-23T09:05:22Z",
// "theme_id": 1,
// "last_activity_on": "2012-05-23",
// "color_scheme_id": 2,
// "projects_limit": 100,
// "current_sign_in_at": "2012-06-02T06:36:55Z",
// "identities": [
// {"provider": "github", "extern_uid": "2435223452345"},
// {"provider": "bitbucket", "extern_uid": "john_smith"},
// {"provider": "google_oauth2", "extern_uid": "8776128412476123468721346"}
// ],
// "can_create_group": true,
// "can_create_project": true,
// "two_factor_enabled": true,
// "external": false,
// "private_profile": false,
// "commit_email": "admin@example.com",
// }
*/
#[derive(Deserialize, Debug)]
pub struct User {
pub id: u64,
pub username: String,
pub email: String,
pub name: String,
pub state: String,
pub locked: bool,
pub created_at: String,
pub public_email: String,
}
impl GitLab {
pub fn try_new(url: &str, token: &str) -> Result<Self, anyhow::Error> {
let url = Url::parse(url)?;
let mut headers = HeaderMap::new();
headers.insert("PRIVATE-TOKEN", HeaderValue::from_str(token)?);
let proxy = reqwest::Proxy::http("socks5h://localhost:9982")?;
let retry_policy = ExponentialBackoff::builder().build_with_max_retries(3);
let base_client = Client::builder()
.default_headers(headers)
.proxy(proxy)
.build()?;
let client = ClientBuilder::new(base_client)
.with(TracingMiddleware::default())
.with(RetryTransientMiddleware::new_with_policy(retry_policy))
.build();
Ok(Self { url, client })
}
pub fn url(&self, path: &str) -> Result<Url, anyhow::Error> {
Ok(self.url.join(path)?)
}
pub async fn me(&self) -> Result<User, anyhow::Error> {
let res = self.client.get(self.url("/user")?).send().await?;
trace!("{res:?}");
let body = res.text().await?;
trace!("{body:?}");
Ok(serde_json::from_str::<User>(&body)?)
// Ok(res.json::<User>().await?)
}
}

1
src/jira.rs Normal file
View File

@ -0,0 +1 @@

37
src/main.rs Normal file
View File

@ -0,0 +1,37 @@
use tasks::Tasks;
use tracing::{info, instrument};
use tracing_subscriber::{filter::LevelFilter, EnvFilter};
const ANSI_CLEAR: &'static str = "\x1b[2J\x1b[1;1H";
mod config;
mod gitlab;
mod jira;
mod tasks;
#[instrument]
pub fn init() {
color_eyre::install().expect("Failed to install color_eyre");
setup_trace_logger();
info!("Instrumentation initialized.");
}
#[instrument]
pub fn setup_trace_logger() {
let filter = EnvFilter::builder()
.with_default_directive(LevelFilter::TRACE.into())
.parse_lossy("trace,tasks=trace");
tracing_subscriber::fmt().with_env_filter(filter).init();
}
#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
init();
tracing::info!("{ANSI_CLEAR}Hallo, mate!");
let tasks = Tasks::try_new()?;
let gitlab_user = tasks.gitlab.me().await?;
info!("{gitlab_user:#?}");
Ok(())
}

14
src/tasks.rs Normal file
View File

@ -0,0 +1,14 @@
use std::env;
use crate::gitlab::GitLab;
pub struct Tasks {
pub gitlab: GitLab,
}
impl Tasks {
pub fn try_new() -> Result<Self, anyhow::Error> {
let gitlab = GitLab::try_new("https://git.hq.bill.com", &env::var("GITLAB_TOKEN")?)?;
Ok(Self { gitlab })
}
}