code.gitea.io/gitea@v1.22.3/modules/private/serv.go (about)

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package private
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"net/url"
    10  
    11  	asymkey_model "code.gitea.io/gitea/models/asymkey"
    12  	"code.gitea.io/gitea/models/perm"
    13  	user_model "code.gitea.io/gitea/models/user"
    14  	"code.gitea.io/gitea/modules/setting"
    15  )
    16  
    17  // KeyAndOwner is the response from ServNoCommand
    18  type KeyAndOwner struct {
    19  	Key   *asymkey_model.PublicKey `json:"key"`
    20  	Owner *user_model.User         `json:"user"`
    21  }
    22  
    23  // ServNoCommand returns information about the provided key
    24  func ServNoCommand(ctx context.Context, keyID int64) (*asymkey_model.PublicKey, *user_model.User, error) {
    25  	reqURL := setting.LocalURL + fmt.Sprintf("api/internal/serv/none/%d", keyID)
    26  	req := newInternalRequest(ctx, reqURL, "GET")
    27  	keyAndOwner, extra := requestJSONResp(req, &KeyAndOwner{})
    28  	if extra.HasError() {
    29  		return nil, nil, extra.Error
    30  	}
    31  	return keyAndOwner.Key, keyAndOwner.Owner, nil
    32  }
    33  
    34  // ServCommandResults are the results of a call to the private route serv
    35  type ServCommandResults struct {
    36  	IsWiki      bool
    37  	DeployKeyID int64
    38  	KeyID       int64  // public key
    39  	KeyName     string // this field is ambiguous, it can be the name of DeployKey, or the name of the PublicKey
    40  	UserName    string
    41  	UserEmail   string
    42  	UserID      int64
    43  	OwnerName   string
    44  	RepoName    string
    45  	RepoID      int64
    46  }
    47  
    48  // ServCommand preps for a serv call
    49  func ServCommand(ctx context.Context, keyID int64, ownerName, repoName string, mode perm.AccessMode, verbs ...string) (*ServCommandResults, ResponseExtra) {
    50  	reqURL := setting.LocalURL + fmt.Sprintf("api/internal/serv/command/%d/%s/%s?mode=%d",
    51  		keyID,
    52  		url.PathEscape(ownerName),
    53  		url.PathEscape(repoName),
    54  		mode,
    55  	)
    56  	for _, verb := range verbs {
    57  		if verb != "" {
    58  			reqURL += fmt.Sprintf("&verb=%s", url.QueryEscape(verb))
    59  		}
    60  	}
    61  	req := newInternalRequest(ctx, reqURL, "GET")
    62  	return requestJSONResp(req, &ServCommandResults{})
    63  }