Make gitea serv use api/internal (#4886)
* Start to move to internal/private * Add GetPublicKeyByID * Add HasDeployKey * Add private.UpdateDeployKeyUpdated * Add private.GetUserByKeyID * Add private.AccessLevel * Add private.CheckUnitUser * Fix mistakes I made * Some cleaning + moving code to separate files * Fix error handling * Remove useless error handling for setup * lint: fix comment on exported func * fix copyright header * Fix order of argstokarchuk/v1.17
parent
aefeb8c465
commit
617a2433a3
@ -0,0 +1,116 @@ |
||||
// Copyright 2018 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 ( |
||||
"encoding/json" |
||||
"fmt" |
||||
|
||||
"code.gitea.io/gitea/models" |
||||
"code.gitea.io/gitea/modules/log" |
||||
"code.gitea.io/gitea/modules/setting" |
||||
) |
||||
|
||||
// UpdateDeployKeyUpdated update deploy key updates
|
||||
func UpdateDeployKeyUpdated(keyID int64, repoID int64) error { |
||||
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/repositories/%d/keys/%d/update", repoID, keyID) |
||||
log.GitLogger.Trace("UpdateDeployKeyUpdated: %s", reqURL) |
||||
|
||||
resp, err := newInternalRequest(reqURL, "POST").Response() |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
defer resp.Body.Close() |
||||
|
||||
// All 2XX status codes are accepted and others will return an error
|
||||
if resp.StatusCode/100 != 2 { |
||||
return fmt.Errorf("Failed to update deploy key: %s", decodeJSONError(resp).Err) |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// HasDeployKey check if repo has deploy key
|
||||
func HasDeployKey(keyID, repoID int64) (bool, error) { |
||||
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/repositories/%d/has-keys/%d", repoID, keyID) |
||||
log.GitLogger.Trace("HasDeployKey: %s", reqURL) |
||||
|
||||
resp, err := newInternalRequest(reqURL, "GET").Response() |
||||
if err != nil { |
||||
return false, err |
||||
} |
||||
defer resp.Body.Close() |
||||
|
||||
if resp.StatusCode == 200 { |
||||
return true, nil |
||||
} |
||||
return false, nil |
||||
} |
||||
|
||||
// GetPublicKeyByID get public ssh key by his ID
|
||||
func GetPublicKeyByID(keyID int64) (*models.PublicKey, error) { |
||||
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/ssh/%d", keyID) |
||||
log.GitLogger.Trace("GetPublicKeyByID: %s", reqURL) |
||||
|
||||
resp, err := newInternalRequest(reqURL, "GET").Response() |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
defer resp.Body.Close() |
||||
|
||||
if resp.StatusCode != 200 { |
||||
return nil, fmt.Errorf("Failed to get repository: %s", decodeJSONError(resp).Err) |
||||
} |
||||
|
||||
var pKey models.PublicKey |
||||
if err := json.NewDecoder(resp.Body).Decode(&pKey); err != nil { |
||||
return nil, err |
||||
} |
||||
return &pKey, nil |
||||
} |
||||
|
||||
// GetUserByKeyID get user attached to key
|
||||
func GetUserByKeyID(keyID int64) (*models.User, error) { |
||||
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/ssh/%d/user", keyID) |
||||
log.GitLogger.Trace("GetUserByKeyID: %s", reqURL) |
||||
|
||||
resp, err := newInternalRequest(reqURL, "GET").Response() |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
defer resp.Body.Close() |
||||
|
||||
if resp.StatusCode != 200 { |
||||
return nil, fmt.Errorf("Failed to get user: %s", decodeJSONError(resp).Err) |
||||
} |
||||
|
||||
var user models.User |
||||
if err := json.NewDecoder(resp.Body).Decode(&user); err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return &user, nil |
||||
} |
||||
|
||||
// UpdatePublicKeyUpdated update public key updates
|
||||
func UpdatePublicKeyUpdated(keyID int64) error { |
||||
// Ask for running deliver hook and test pull request tasks.
|
||||
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/ssh/%d/update", keyID) |
||||
log.GitLogger.Trace("UpdatePublicKeyUpdated: %s", reqURL) |
||||
|
||||
resp, err := newInternalRequest(reqURL, "POST").Response() |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
defer resp.Body.Close() |
||||
|
||||
// All 2XX status codes are accepted and others will return an error
|
||||
if resp.StatusCode/100 != 2 { |
||||
return fmt.Errorf("Failed to update public key: %s", decodeJSONError(resp).Err) |
||||
} |
||||
return nil |
||||
} |
@ -0,0 +1,84 @@ |
||||
// Copyright 2018 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 includes all internal routes. The package name internal is ideal but Golang is not allowed, so we use private as package name instead.
|
||||
package private |
||||
|
||||
import ( |
||||
"code.gitea.io/gitea/models" |
||||
"code.gitea.io/gitea/modules/util" |
||||
|
||||
macaron "gopkg.in/macaron.v1" |
||||
) |
||||
|
||||
// UpdateDeployKey update deploy key updates
|
||||
func UpdateDeployKey(ctx *macaron.Context) { |
||||
repoID := ctx.ParamsInt64(":repoid") |
||||
keyID := ctx.ParamsInt64(":keyid") |
||||
deployKey, err := models.GetDeployKeyByRepo(keyID, repoID) |
||||
if err != nil { |
||||
ctx.JSON(500, map[string]interface{}{ |
||||
"err": err.Error(), |
||||
}) |
||||
return |
||||
} |
||||
deployKey.UpdatedUnix = util.TimeStampNow() |
||||
if err = models.UpdateDeployKeyCols(deployKey, "updated_unix"); err != nil { |
||||
ctx.JSON(500, map[string]interface{}{ |
||||
"err": err.Error(), |
||||
}) |
||||
return |
||||
} |
||||
ctx.PlainText(200, []byte("success")) |
||||
} |
||||
|
||||
// UpdatePublicKey update publick key updates
|
||||
func UpdatePublicKey(ctx *macaron.Context) { |
||||
keyID := ctx.ParamsInt64(":id") |
||||
if err := models.UpdatePublicKeyUpdated(keyID); err != nil { |
||||
ctx.JSON(500, map[string]interface{}{ |
||||
"err": err.Error(), |
||||
}) |
||||
return |
||||
} |
||||
|
||||
ctx.PlainText(200, []byte("success")) |
||||
} |
||||
|
||||
//GetPublicKeyByID chainload to models.GetPublicKeyByID
|
||||
func GetPublicKeyByID(ctx *macaron.Context) { |
||||
keyID := ctx.ParamsInt64(":id") |
||||
key, err := models.GetPublicKeyByID(keyID) |
||||
if err != nil { |
||||
ctx.JSON(500, map[string]interface{}{ |
||||
"err": err.Error(), |
||||
}) |
||||
return |
||||
} |
||||
ctx.JSON(200, key) |
||||
} |
||||
|
||||
//GetUserByKeyID chainload to models.GetUserByKeyID
|
||||
func GetUserByKeyID(ctx *macaron.Context) { |
||||
keyID := ctx.ParamsInt64(":id") |
||||
user, err := models.GetUserByKeyID(keyID) |
||||
if err != nil { |
||||
ctx.JSON(500, map[string]interface{}{ |
||||
"err": err.Error(), |
||||
}) |
||||
return |
||||
} |
||||
ctx.JSON(200, user) |
||||
} |
||||
|
||||
//HasDeployKey chainload to models.HasDeployKey
|
||||
func HasDeployKey(ctx *macaron.Context) { |
||||
repoID := ctx.ParamsInt64(":repoid") |
||||
keyID := ctx.ParamsInt64(":keyid") |
||||
if models.HasDeployKey(keyID, repoID) { |
||||
ctx.PlainText(200, []byte("success")) |
||||
return |
||||
} |
||||
ctx.PlainText(404, []byte("not found")) |
||||
} |
Loading…
Reference in new issue