From f947e4d7e72c45e9a4199ed191a4396db2ac3c60 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Thu, 20 Feb 2025 11:32:52 +0800 Subject: hooks, ssh: Indicate URL of newly-created MRs --- git_hooks_handle.go | 9 ++++++--- ssh_handle_receive_pack.go | 8 +++++++- ssh_handle_upload_pack.go | 2 +- ssh_utils.go | 18 ++++++++++-------- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/git_hooks_handle.go b/git_hooks_handle.go index 9d037b7..3a8a031 100644 --- a/git_hooks_handle.go +++ b/git_hooks_handle.go @@ -10,6 +10,7 @@ import ( "net" "os" "path/filepath" + "strconv" "strings" "syscall" @@ -131,14 +132,16 @@ func hooks_handle_connection(conn net.Conn) { if strings.HasPrefix(ref_name, "refs/heads/contrib/") { if all_zero_num_string(old_oid) { // New branch fmt.Fprintln(ssh_stderr, ansiec.Blue + "POK" + ansiec.Reset, ref_name) - _, err = database.Exec(ctx, - "INSERT INTO merge_requests (repo_id, creator, source_ref, status) VALUES ($1, $2, $3, 'open')", + var new_mr_id int + err = database.QueryRow(ctx, + "INSERT INTO merge_requests (repo_id, creator, source_ref, status) VALUES ($1, $2, $3, 'open') RETURNING id", pack_to_hook.repo_id, pack_to_hook.user_id, strings.TrimPrefix(ref_name, "refs/heads/"), - ) + ).Scan(&new_mr_id) if err != nil { wf_error(ssh_stderr, "Error creating merge request: %v", err) return 1 } + fmt.Fprintln(ssh_stderr, ansiec.Blue + "Created merge request at", generate_http_remote_url(pack_to_hook.group_name, pack_to_hook.repo_name) + "/contrib/" + strconv.FormatUint(uint64(new_mr_id), 10) + "/" + ansiec.Reset) } else { // Existing contrib branch var existing_merge_request_user_id int err = database.QueryRow(ctx, diff --git a/ssh_handle_receive_pack.go b/ssh_handle_receive_pack.go index 8803151..af576bc 100644 --- a/ssh_handle_receive_pack.go +++ b/ssh_handle_receive_pack.go @@ -17,13 +17,15 @@ type pack_to_hook_t struct { repo_path string user_id int repo_id int + group_name string + repo_name string } var pack_to_hook_by_cookie = cmap.Map[string, pack_to_hook_t]{} // ssh_handle_receive_pack handles attempts to push to repos. func ssh_handle_receive_pack(session glider_ssh.Session, pubkey string, repo_identifier string) (err error) { - repo_id, repo_path, direct_access, contrib_requirements, user_type, user_id, err := get_repo_path_perms_from_ssh_path_pubkey(session.Context(), repo_identifier, pubkey) + group_name, repo_name, repo_id, repo_path, direct_access, contrib_requirements, user_type, user_id, err := get_repo_path_perms_from_ssh_path_pubkey(session.Context(), repo_identifier, pubkey) if err != nil { return err } @@ -60,6 +62,8 @@ func ssh_handle_receive_pack(session glider_ssh.Session, pubkey string, repo_ide fmt.Fprintln(session.Stderr(), "Error while generating cookie:", err) } + fmt.Println(group_name, repo_name) + pack_to_hook_by_cookie.Store(cookie, pack_to_hook_t{ session: session, pubkey: pubkey, @@ -67,6 +71,8 @@ func ssh_handle_receive_pack(session glider_ssh.Session, pubkey string, repo_ide repo_path: repo_path, user_id: user_id, repo_id: repo_id, + group_name: group_name, + repo_name: repo_name, }) defer pack_to_hook_by_cookie.Delete(cookie) // The Delete won't execute until proc.Wait returns unless something diff --git a/ssh_handle_upload_pack.go b/ssh_handle_upload_pack.go index 8efcd28..a62f918 100644 --- a/ssh_handle_upload_pack.go +++ b/ssh_handle_upload_pack.go @@ -11,7 +11,7 @@ import ( // ssh_handle_upload_pack handles clones/fetches. It just uses git-upload-pack // and has no ACL checks. func ssh_handle_upload_pack(session glider_ssh.Session, pubkey string, repo_identifier string) (err error) { - _, repo_path, _, _, _, _, err := get_repo_path_perms_from_ssh_path_pubkey(session.Context(), repo_identifier, pubkey) + _, _, _, repo_path, _, _, _, _, err := get_repo_path_perms_from_ssh_path_pubkey(session.Context(), repo_identifier, pubkey) if err != nil { return err } diff --git a/ssh_utils.go b/ssh_utils.go index 8ad53ab..48b25b9 100644 --- a/ssh_utils.go +++ b/ssh_utils.go @@ -13,19 +13,19 @@ import ( var err_ssh_illegal_endpoint = errors.New("illegal endpoint during SSH access") -func get_repo_path_perms_from_ssh_path_pubkey(ctx context.Context, ssh_path string, ssh_pubkey string) (repo_id int, repo_path string, direct_access bool, contrib_requirements string, user_type string, user_id int, err error) { +func get_repo_path_perms_from_ssh_path_pubkey(ctx context.Context, ssh_path string, ssh_pubkey string) (group_name string, repo_name string, repo_id int, repo_path string, direct_access bool, contrib_requirements string, user_type string, user_id int, err error) { segments := strings.Split(strings.TrimPrefix(ssh_path, "/"), "/") for i, segment := range segments { var err error segments[i], err = url.PathUnescape(segment) if err != nil { - return 0, "", false, "", "", 0, err + return "", "", 0, "", false, "", "", 0, err } } if segments[0] == ":" { - return 0, "", false, "", "", 0, err_ssh_illegal_endpoint + return "", "", 0, "", false, "", "", 0, err_ssh_illegal_endpoint } separator_index := -1 @@ -41,19 +41,21 @@ func get_repo_path_perms_from_ssh_path_pubkey(ctx context.Context, ssh_path stri switch { case separator_index == -1: - return 0, "", false, "", "", 0, err_ssh_illegal_endpoint + return "", "", 0, "", false, "", "", 0, err_ssh_illegal_endpoint case len(segments) <= separator_index+2: - return 0, "", false, "", "", 0, err_ssh_illegal_endpoint + return "", "", 0, "", false, "", "", 0, err_ssh_illegal_endpoint } - group_name := segments[0] + group_name = segments[0] module_type := segments[separator_index+1] module_name := segments[separator_index+2] + repo_name = module_name switch module_type { case "repos": - return get_path_perm_by_group_repo_key(ctx, group_name, module_name, ssh_pubkey) + _1, _2, _3, _4, _5, _6, _7 := get_path_perm_by_group_repo_key(ctx, group_name, module_name, ssh_pubkey) + return group_name, repo_name, _1, _2, _3, _4, _5, _6, _7 default: - return 0, "", false, "", "", 0, err_ssh_illegal_endpoint + return "", "", 0, "", false, "", "", 0, err_ssh_illegal_endpoint } } -- cgit v1.2.3