2019-06-01 10:00:21 -05:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package private
|
|
|
|
|
|
|
|
import (
|
2021-07-14 09:43:13 -05:00
|
|
|
"context"
|
2019-06-01 10:00:21 -05:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2021-12-10 02:14:24 -06:00
|
|
|
asymkey_model "code.gitea.io/gitea/models/asymkey"
|
2021-11-28 05:58:28 -06:00
|
|
|
"code.gitea.io/gitea/models/perm"
|
2021-11-24 03:49:20 -06:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-07-24 11:03:58 -05:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
2019-06-01 10:00:21 -05:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
)
|
|
|
|
|
|
|
|
// KeyAndOwner is the response from ServNoCommand
|
|
|
|
type KeyAndOwner struct {
|
2021-12-10 02:14:24 -06:00
|
|
|
Key *asymkey_model.PublicKey `json:"key"`
|
|
|
|
Owner *user_model.User `json:"user"`
|
2019-06-01 10:00:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServNoCommand returns information about the provided key
|
2021-12-10 02:14:24 -06:00
|
|
|
func ServNoCommand(ctx context.Context, keyID int64) (*asymkey_model.PublicKey, *user_model.User, error) {
|
2019-06-01 10:00:21 -05:00
|
|
|
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/serv/none/%d",
|
|
|
|
keyID)
|
2021-07-14 09:43:13 -05:00
|
|
|
resp, err := newInternalRequest(ctx, reqURL, "GET").Response()
|
2019-06-01 10:00:21 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, nil, fmt.Errorf("%s", decodeJSONError(resp).Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var keyAndOwner KeyAndOwner
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&keyAndOwner); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
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 04:29:07 -05: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 10:00:21 -05:00
|
|
|
UserName string
|
2020-08-30 02:24:39 -05:00
|
|
|
UserEmail string
|
2019-06-01 10:00:21 -05:00
|
|
|
UserID int64
|
|
|
|
OwnerName string
|
|
|
|
RepoName string
|
|
|
|
RepoID int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrServCommand is an error returned from ServCommmand.
|
|
|
|
type ErrServCommand struct {
|
|
|
|
Results ServCommandResults
|
|
|
|
Err string
|
|
|
|
StatusCode int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ErrServCommand) Error() string {
|
|
|
|
return err.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrServCommand checks if an error is a ErrServCommand.
|
|
|
|
func IsErrServCommand(err error) bool {
|
|
|
|
_, ok := err.(ErrServCommand)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServCommand preps for a serv call
|
2021-11-28 05:58:28 -06:00
|
|
|
func ServCommand(ctx context.Context, keyID int64, ownerName, repoName string, mode perm.AccessMode, verbs ...string) (*ServCommandResults, error) {
|
2019-06-01 10:00:21 -05:00
|
|
|
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/serv/command/%d/%s/%s?mode=%d",
|
|
|
|
keyID,
|
|
|
|
url.PathEscape(ownerName),
|
|
|
|
url.PathEscape(repoName),
|
|
|
|
mode)
|
|
|
|
for _, verb := range verbs {
|
|
|
|
if verb != "" {
|
|
|
|
reqURL += fmt.Sprintf("&verb=%s", url.QueryEscape(verb))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-14 09:43:13 -05:00
|
|
|
resp, err := newInternalRequest(ctx, reqURL, "GET").Response()
|
2019-06-01 10:00:21 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2021-07-24 11:03:58 -05:00
|
|
|
|
2019-06-01 10:00:21 -05:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
var errServCommand ErrServCommand
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&errServCommand); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
errServCommand.StatusCode = resp.StatusCode
|
|
|
|
return nil, errServCommand
|
|
|
|
}
|
|
|
|
var results ServCommandResults
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&results); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &results, nil
|
|
|
|
}
|