2019-06-01 16:00:21 +01:00
// Copyright 2019 The Gitea Authors. All rights reserved.
2022-11-27 13:20:29 -05:00
// SPDX-License-Identifier: MIT
2019-06-01 16:00:21 +01:00
package private
import (
2021-07-14 15:43:13 +01:00
"context"
2019-06-01 16:00:21 +01:00
"fmt"
"net/url"
2021-12-10 16:14:24 +08:00
asymkey_model "code.gitea.io/gitea/models/asymkey"
2021-11-28 19:58:28 +08:00
"code.gitea.io/gitea/models/perm"
2021-11-24 17:49:20 +08:00
user_model "code.gitea.io/gitea/models/user"
2019-06-01 16:00:21 +01:00
"code.gitea.io/gitea/modules/setting"
)
// KeyAndOwner is the response from ServNoCommand
type KeyAndOwner struct {
2021-12-10 16:14:24 +08:00
Key * asymkey_model . PublicKey ` json:"key" `
Owner * user_model . User ` json:"user" `
2019-06-01 16:00:21 +01:00
}
// ServNoCommand returns information about the provided key
2021-12-10 16:14:24 +08:00
func ServNoCommand ( ctx context . Context , keyID int64 ) ( * asymkey_model . PublicKey , * user_model . User , error ) {
2023-03-29 14:32:26 +08:00
reqURL := setting . LocalURL + fmt . Sprintf ( "api/internal/serv/none/%d" , keyID )
2025-01-31 19:05:48 +08:00
req := newInternalRequestAPI ( ctx , reqURL , "GET" )
2023-03-29 14:32:26 +08:00
keyAndOwner , extra := requestJSONResp ( req , & KeyAndOwner { } )
if extra . HasError ( ) {
return nil , nil , extra . Error
2019-06-01 16:00:21 +01:00
}
return keyAndOwner . Key , keyAndOwner . Owner , nil
}
// ServCommandResults are the results of a call to the private route serv
type ServCommandResults struct {
IsWiki bool
2022-03-22 17:29:07 +08:00
DeployKeyID int64
KeyID int64 // public key
KeyName string // this field is ambiguous, it can be the name of DeployKey, or the name of the PublicKey
2019-06-01 16:00:21 +01:00
UserName string
2020-08-30 08:24:39 +01:00
UserEmail string
2019-06-01 16:00:21 +01:00
UserID int64
OwnerName string
RepoName string
RepoID int64
}
// ServCommand preps for a serv call
2025-05-09 09:17:08 -07:00
func ServCommand ( ctx context . Context , keyID int64 , ownerName , repoName string , mode perm . AccessMode , verb , lfsVerb string ) ( * ServCommandResults , ResponseExtra ) {
2019-06-01 16:00:21 +01:00
reqURL := setting . LocalURL + fmt . Sprintf ( "api/internal/serv/command/%d/%s/%s?mode=%d" ,
keyID ,
url . PathEscape ( ownerName ) ,
url . PathEscape ( repoName ) ,
2023-03-29 14:32:26 +08:00
mode ,
)
2025-05-09 09:17:08 -07:00
reqURL += "&verb=" + url . QueryEscape ( verb )
// reqURL += "&lfs_verb=" + url.QueryEscape(lfsVerb) // TODO: actually there is no use of this parameter. In the future, the URL construction should be more flexible
_ = lfsVerb
2025-01-31 19:05:48 +08:00
req := newInternalRequestAPI ( ctx , reqURL , "GET" )
2023-03-29 14:32:26 +08:00
return requestJSONResp ( req , & ServCommandResults { } )
2019-06-01 16:00:21 +01:00
}