summaryrefslogtreecommitdiff
path: root/server/src/githook.rs
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2025-01-26 23:55:50 +0100
committerJoel Klinghed <the_jk@spawned.biz>2025-01-26 23:55:50 +0100
commitd1647b7a056f04ad5828976dd5a7e2e06b431feb (patch)
tree11a4e1ad3f40bf653b7971bd40e54568f507ffb5 /server/src/githook.rs
parent4fe3e610d5697aca4dfdc1033261130ec2b431ee (diff)
Stop using current user in git hooks
Want to support any authentication for the git server, so use git commiter as username for creating reviews instead of the local user that logged in to git. Also verify that pushed commits has a valid author in pre-receive. This is tricky as pre-receive must do this check in the hook, because pre-receive runs when before the objects are pushed so the server can't read the commits, the hook must do this.
Diffstat (limited to 'server/src/githook.rs')
-rw-r--r--server/src/githook.rs35
1 files changed, 15 insertions, 20 deletions
diff --git a/server/src/githook.rs b/server/src/githook.rs
index 057ee47..f0e872a 100644
--- a/server/src/githook.rs
+++ b/server/src/githook.rs
@@ -6,7 +6,6 @@ use std::os::unix::net::UnixStream;
use std::path::PathBuf;
use tokio::io::{self, AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::task;
-use users::get_current_username;
mod fs_utils;
mod git;
@@ -33,11 +32,6 @@ impl fmt::Display for IoError {
impl Error for IoError {}
-async fn get_socket() -> Result<String, git::Error> {
- let repo = git::Repository::new(PathBuf::from("."), true, None::<String>, None::<String>);
- repo.config_get("eyeballs.socket").await
-}
-
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let pre = match std::env::current_exe()?
@@ -49,40 +43,41 @@ async fn main() -> Result<(), Box<dyn Error>> {
_ => return Err(Box::<dyn Error>::from("Invalid hook executable name")),
};
- let user = match get_current_username() {
- Some(username) => match username.into_string() {
- Ok(valid_username) => valid_username,
- Err(_) => return Err(Box::<dyn Error>::from("Invalid username for current user")),
- },
- None => {
- return Err(Box::<dyn Error>::from(
- "Unable to get username of current user",
- ))
- }
- };
-
let input = io::stdin();
let reader = BufReader::new(input);
let mut lines = reader.lines();
let mut request = git_socket::GitHookRequest {
pre,
- user,
receive: Vec::new(),
};
+
+ let repo = git::Repository::new(PathBuf::from("."), true, None::<String>, None::<String>);
+
while let Some(line) = lines.next_line().await? {
let data: Vec<&str> = line.split(' ').collect();
if data.len() == 3 {
+ let mut commiter: Option<String> = None;
+ if pre && data[1] != git::EMPTY {
+ match repo.get_commiter(data[1]).await {
+ Ok(user) => {
+ commiter = Some(user.username);
+ }
+ Err(_) => {}
+ }
+ }
+
request.receive.push(git_socket::GitReceive {
old_value: data[0].to_string(),
new_value: data[1].to_string(),
reference: data[2].to_string(),
+ commiter: commiter,
})
}
}
- let socket = PathBuf::from(get_socket().await?);
+ let socket = PathBuf::from(repo.config_get("eyeballs.socket").await?);
let response = task::spawn_blocking(move || {
let stream = UnixStream::connect(socket).map_err(|e| IoError::new(e.to_string()))?;