Skip to content

Commit

Permalink
[Fix go-gitea#3246 1/2] Substitute calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Nodraak committed Mar 30, 2018
1 parent 45d1fc0 commit c20afe9
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 243 deletions.
18 changes: 0 additions & 18 deletions integrations/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,6 @@ import (
"github.com/stretchr/testify/assert"
)

func assertProtectedBranch(t *testing.T, repoID int64, branchName string, isErr, canPush bool) {
reqURL := fmt.Sprintf("/api/internal/branch/%d/%s", repoID, url.QueryEscape(branchName))
req := NewRequest(t, "GET", reqURL)
t.Log(reqURL)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", setting.InternalToken))

resp := MakeRequest(t, req, NoExpectedStatus)
if isErr {
assert.EqualValues(t, http.StatusInternalServerError, resp.Code)
} else {
assert.EqualValues(t, http.StatusOK, resp.Code)
var branch models.ProtectedBranch
t.Log(resp.Body.String())
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), &branch))
assert.Equal(t, canPush, !branch.IsProtected())
}
}

func TestInternal_GetProtectedBranch(t *testing.T) {
prepareTestEnv(t)

Expand Down
52 changes: 12 additions & 40 deletions modules/private/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,34 @@
package private

import (
"encoding/json"
"fmt"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
)

// GetProtectedBranchBy get protected branch information
func GetProtectedBranchBy(repoID int64, branchName string) (*models.ProtectedBranch, error) {
// Ask for running deliver hook and test pull request tasks.
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/branch/%d/%s", repoID, branchName)
log.GitLogger.Trace("GetProtectedBranchBy: %s", reqURL)

resp, err := newInternalRequest(reqURL, "GET").Response()
protectBranch, err := models.GetProtectedBranchBy(repoID, branchName)
if err != nil {
return nil, err
}

var branch models.ProtectedBranch
if err := json.NewDecoder(resp.Body).Decode(&branch); err != nil {
return nil, err
return nil, fmt.Errorf("GetProtectedBranchBy: failed to get protected branch: %s", err.Error())
}

defer resp.Body.Close()

// All 2XX status codes are accepted and others will return an error
if resp.StatusCode/100 != 2 {
return nil, fmt.Errorf("Failed to update public key: %s", decodeJSONError(resp).Err)
if protectBranch == nil {
protectBranch = &models.ProtectedBranch{ID: 0}
}

return &branch, nil
return protectBranch, nil
}

// CanUserPush returns if user can push
func CanUserPush(protectedBranchID, userID int64) (bool, error) {
// Ask for running deliver hook and test pull request tasks.
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/protectedbranch/%d/%d", protectedBranchID, userID)
log.GitLogger.Trace("CanUserPush: %s", reqURL)
pbID := protectedBranchID

resp, err := newInternalRequest(reqURL, "GET").Response()
protectBranch, err := models.GetProtectedBranchByID(pbID)
if err != nil {
return false, err
return false, fmt.Errorf("CanUserPush: failed to retrieve push user: %s", err.Error())
} else if protectBranch != nil {
return protectBranch.CanUserPush(userID), nil
} else {
return false, nil
}

var canPush = make(map[string]interface{})
if err := json.NewDecoder(resp.Body).Decode(&canPush); err != nil {
return false, err
}

defer resp.Body.Close()

// All 2XX status codes are accepted and others will return an error
if resp.StatusCode/100 != 2 {
return false, fmt.Errorf("Failed to retrieve push user: %s", decodeJSONError(resp).Err)
}

return canPush["can_push"].(bool), nil
}
17 changes: 3 additions & 14 deletions modules/private/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"net"
"net/http"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/httplib"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
)

Expand Down Expand Up @@ -51,20 +51,9 @@ func newInternalRequest(url, method string) *httplib.Request {

// UpdatePublicKeyUpdated update publick 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
if err := models.UpdatePublicKeyUpdated(keyID); err != nil {
return fmt.Errorf("Failed to update public key: %s", err.Error())
}

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
}
30 changes: 12 additions & 18 deletions modules/private/push_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,29 @@
package private

import (
"encoding/json"
"fmt"
"strings"

"code.gitea.io/git"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
)

// PushUpdate update publick key updates
func PushUpdate(opt models.PushUpdateOptions) error {
// Ask for running deliver hook and test pull request tasks.
reqURL := setting.LocalURL + "api/internal/push/update"
log.GitLogger.Trace("PushUpdate: %s", reqURL)
errMessageFormat := "PushUpdate: failed to update public key: %s"

body, err := json.Marshal(&opt)
if err != nil {
return err
branch := strings.TrimPrefix(opt.RefFullName, git.BranchPrefix)
if len(branch) == 0 || opt.PusherID <= 0 {
return fmt.Errorf(errMessageFormat, "branch or secret is empty, or pusher ID is not valid")
}

resp, err := newInternalRequest(reqURL, "POST").Body(body).Response()
err := models.PushUpdate(branch, opt)
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)
if models.IsErrUserNotExist(err) {
return fmt.Errorf(errMessageFormat, "user does not exist")
} else {
return fmt.Errorf(errMessageFormat, err.Error())
}
}

return nil
Expand Down
52 changes: 0 additions & 52 deletions routers/private/branch.go

This file was deleted.

48 changes: 0 additions & 48 deletions routers/private/internal.go

This file was deleted.

47 changes: 0 additions & 47 deletions routers/private/push_update.go

This file was deleted.

6 changes: 0 additions & 6 deletions routers/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
apiv1 "code.gitea.io/gitea/routers/api/v1"
"code.gitea.io/gitea/routers/dev"
"code.gitea.io/gitea/routers/org"
"code.gitea.io/gitea/routers/private"
"code.gitea.io/gitea/routers/repo"
"code.gitea.io/gitea/routers/user"

Expand Down Expand Up @@ -715,11 +714,6 @@ func RegisterRoutes(m *macaron.Macaron) {
apiv1.RegisterRoutes(m)
}, ignSignIn)

m.Group("/api/internal", func() {
// package name internal is ideal but Golang is not allowed, so we use private as package name.
private.RegisterRoutes(m)
})

// robots.txt
m.Get("/robots.txt", func(ctx *context.Context) {
if setting.HasRobotsTxt {
Expand Down

0 comments on commit c20afe9

Please sign in to comment.