Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #5960 - Adds API Endpoint for Repo Edit #7006

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
cda61ea
Feature - #5960 - API Endpoint for Repo Editing
richmahn May 10, 2019
7dc43ee
Merge remote-tracking branch 'upstream/master' into fix-5960-edit-rep…
richmahn May 10, 2019
682a97b
Merge branch 'master' into fix-5960-edit-repo-api-endpoint
richmahn May 13, 2019
f73411a
Merge remote-tracking branch 'upstream/master' into fix-5960-edit-rep…
richmahn May 14, 2019
6af22ce
Merge remote-tracking branch 'upstream/master' into fix-5960-edit-rep…
richmahn May 17, 2019
3e88b7a
Revert from merge
richmahn May 17, 2019
cd6773c
Adds integration testing
richmahn May 17, 2019
eb1c307
Merge branch 'fix-5960-edit-repo-api-endpoint' of github.com:richmahn…
richmahn May 17, 2019
68cc8c0
Updates to integration tests
richmahn May 20, 2019
e62b38a
Revert changes
richmahn May 20, 2019
baf74b4
Update year in file header
richmahn May 20, 2019
d9cb21c
Merge remote-tracking branch 'upstream/master' into fix-5960-edit-rep…
richmahn May 20, 2019
d0934f8
Misspell fix
richmahn May 21, 2019
312f4e0
XORM = test
richmahn May 21, 2019
2aadd42
XORM = test
richmahn May 21, 2019
48d6321
revert XORM = file
richmahn May 21, 2019
8d17541
Makes RepoUnit.ID be pk and autoincr
richmahn May 21, 2019
5443da8
Fix to units
richmahn May 22, 2019
c0ea7ad
revert header
richmahn May 22, 2019
4ece10f
Merge remote-tracking branch 'upstream/master' into fix-5960-edit-rep…
richmahn May 22, 2019
666fea7
Remove print statement
richmahn May 22, 2019
f51bd8c
Adds other responses
richmahn May 22, 2019
c5889a5
Improves swagger for creating repo
richmahn May 23, 2019
7356c42
Merge branch 'master' into fix-5960-edit-repo-api-endpoint
lafriks May 24, 2019
dad4f47
Fixes import order
richmahn May 25, 2019
05c15d8
Merge branch 'fix-5960-edit-repo-api-endpoint' of github.com:richmahn…
richmahn May 25, 2019
f59a481
Merge remote-tracking branch 'upstream/master' into fix-5960-edit-rep…
richmahn May 25, 2019
ed21161
Merge remote-tracking branch 'upstream/master' into fix-5960-edit-rep…
richmahn May 25, 2019
4923135
Better Unit Type does not exist error
richmahn May 25, 2019
c4407ba
Adds editable repo properties to the response repo structure
richmahn May 25, 2019
ec39b0d
Fix to api_repo_edit_test.go
richmahn May 25, 2019
3ed8a81
Fixes repo test
richmahn May 25, 2019
a7673dc
Merge branch 'master' into fix-5960-edit-repo-api-endpoint
zeripath May 25, 2019
9605cd8
Merge remote-tracking branch 'upstream/master' into fix-5960-edit-rep…
richmahn May 26, 2019
ea12d09
Merge branch 'fix-5960-edit-repo-api-endpoint' of github.com:richmahn…
richmahn May 26, 2019
7316549
Changes per review
richmahn May 26, 2019
32be744
Fixes typo and standardizes comments in the EditRepoOption struct for…
richmahn May 28, 2019
2dc0a90
Fixes typo and standardizes comments in the EditRepoOption struct for…
richmahn May 28, 2019
e641151
Actually can unarchive through the API
richmahn May 28, 2019
e89a326
Unlike delete, user doesn't have to be the owner of the org, just adm…
richmahn May 28, 2019
f831557
Fix to swagger comments for field name change
richmahn May 28, 2019
1b7bd53
Update to swagger docs
richmahn May 28, 2019
1236fda
Update swagger
richmahn May 28, 2019
9ed95dc
Merge remote-tracking branch 'upstream/master' into fix-5960-edit-rep…
richmahn May 28, 2019
17b70f3
Merge remote-tracking branch 'upstream/master' into fix-5960-edit-rep…
richmahn May 29, 2019
ba7d5e9
Merge remote-tracking branch 'upstream/master' into fix-5960-edit-rep…
richmahn May 30, 2019
3f44c1c
Changes allow_pull_requests to has_pull_requests
richmahn May 30, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
225 changes: 225 additions & 0 deletions integrations/api_repo_edit_test.go
@@ -0,0 +1,225 @@
// 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 integrations

import (
"fmt"
"net/http"
"net/url"
"testing"

"code.gitea.io/gitea/models"
api "code.gitea.io/gitea/modules/structs"

"github.com/stretchr/testify/assert"
)

// getRepoEditOptionFromRepo gets the options for an existing repo exactly as is
func getRepoEditOptionFromRepo(repo *models.Repository) *api.EditRepoOption {
name := repo.Name
description := repo.Description
website := repo.Website
private := repo.IsPrivate
hasIssues := false
if _, err := repo.GetUnit(models.UnitTypeIssues); err == nil {
hasIssues = true
}
hasWiki := false
if _, err := repo.GetUnit(models.UnitTypeWiki); err == nil {
hasWiki = true
}
defaultBranch := repo.DefaultBranch
hasPullRequests := false
ignoreWhitespaceConflicts := false
allowMerge := false
allowRebase := false
allowRebaseMerge := false
allowSquash := false
if unit, err := repo.GetUnit(models.UnitTypePullRequests); err == nil {
config := unit.PullRequestsConfig()
hasPullRequests = true
ignoreWhitespaceConflicts = config.IgnoreWhitespaceConflicts
allowMerge = config.AllowMerge
allowRebase = config.AllowRebase
allowRebaseMerge = config.AllowRebaseMerge
allowSquash = config.AllowSquash
}
archived := repo.IsArchived
return &api.EditRepoOption{
Name: &name,
Description: &description,
Website: &website,
Private: &private,
HasIssues: &hasIssues,
HasWiki: &hasWiki,
DefaultBranch: &defaultBranch,
HasPullRequests: &hasPullRequests,
IgnoreWhitespaceConflicts: &ignoreWhitespaceConflicts,
AllowMerge: &allowMerge,
AllowRebase: &allowRebase,
AllowRebaseMerge: &allowRebaseMerge,
AllowSquash: &allowSquash,
Archived: &archived,
}
}

// getNewRepoEditOption Gets the options to change everything about an existing repo by adding to strings or changing
// the boolean
func getNewRepoEditOption(opts *api.EditRepoOption) *api.EditRepoOption {
// Gives a new property to everything
name := *opts.Name + "renamed"
description := "new description"
website := "http://wwww.newwebsite.com"
private := !*opts.Private
hasIssues := !*opts.HasIssues
hasWiki := !*opts.HasWiki
defaultBranch := "master"
hasPullRequests := !*opts.HasPullRequests
ignoreWhitespaceConflicts := !*opts.IgnoreWhitespaceConflicts
allowMerge := !*opts.AllowMerge
allowRebase := !*opts.AllowRebase
allowRebaseMerge := !*opts.AllowRebaseMerge
allowSquash := !*opts.AllowSquash
archived := !*opts.Archived

return &api.EditRepoOption{
Name: &name,
Description: &description,
Website: &website,
Private: &private,
DefaultBranch: &defaultBranch,
HasIssues: &hasIssues,
HasWiki: &hasWiki,
HasPullRequests: &hasPullRequests,
IgnoreWhitespaceConflicts: &ignoreWhitespaceConflicts,
AllowMerge: &allowMerge,
AllowRebase: &allowRebase,
AllowRebaseMerge: &allowRebaseMerge,
AllowSquash: &allowSquash,
Archived: &archived,
}
}

func TestAPIRepoEdit(t *testing.T) {
onGiteaRun(t, func(t *testing.T, u *url.URL) {
user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16
user3 := models.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org
user4 := models.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos
repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo
repo3 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo
repo16 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo

// Get user2's token
session := loginUser(t, user2.Name)
token2 := getTokenForLoggedInUser(t, session)
session = emptyTestSession(t)
// Get user4's token
session = loginUser(t, user4.Name)
token4 := getTokenForLoggedInUser(t, session)
session = emptyTestSession(t)

// Test editing a repo1 which user2 owns, changing name and many properties
origRepoEditOption := getRepoEditOptionFromRepo(repo1)
repoEditOption := getNewRepoEditOption(origRepoEditOption)
url := fmt.Sprintf("/api/v1/repos/%s/%s?token=%s", user2.Name, repo1.Name, token2)
req := NewRequestWithJSON(t, "PATCH", url, &repoEditOption)
resp := session.MakeRequest(t, req, http.StatusOK)
var repo api.Repository
DecodeJSON(t, resp, &repo)
assert.NotNil(t, repo)
// check response
assert.Equal(t, *repoEditOption.Name, repo.Name)
assert.Equal(t, *repoEditOption.Description, repo.Description)
assert.Equal(t, *repoEditOption.Website, repo.Website)
assert.Equal(t, *repoEditOption.Archived, repo.Archived)
// check repo1 from database
repo1edited := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
repo1editedOption := getRepoEditOptionFromRepo(repo1edited)
assert.Equal(t, *repoEditOption.Name, *repo1editedOption.Name)
assert.Equal(t, *repoEditOption.Description, *repo1editedOption.Description)
assert.Equal(t, *repoEditOption.Website, *repo1editedOption.Website)
assert.Equal(t, *repoEditOption.Archived, *repo1editedOption.Archived)
assert.Equal(t, *repoEditOption.Private, *repo1editedOption.Private)
assert.Equal(t, *repoEditOption.HasWiki, *repo1editedOption.HasWiki)
// reset repo in db
url = fmt.Sprintf("/api/v1/repos/%s/%s?token=%s", user2.Name, *repoEditOption.Name, token2)
req = NewRequestWithJSON(t, "PATCH", url, &origRepoEditOption)
resp = session.MakeRequest(t, req, http.StatusOK)

// Test editing a non-existing repo
name := "repodoesnotexist"
url = fmt.Sprintf("/api/v1/repos/%s/%s?token=%s", user2.Name, name, token2)
req = NewRequestWithJSON(t, "PATCH", url, &api.EditRepoOption{Name: &name})
resp = session.MakeRequest(t, req, http.StatusNotFound)

// Test editing repo16 by user4 who does not have write access
origRepoEditOption = getRepoEditOptionFromRepo(repo16)
repoEditOption = getNewRepoEditOption(origRepoEditOption)
url = fmt.Sprintf("/api/v1/repos/%s/%s?token=%s", user2.Name, repo16.Name, token4)
req = NewRequestWithJSON(t, "PATCH", url, &repoEditOption)
session.MakeRequest(t, req, http.StatusNotFound)

// Tests a repo with no token given so will fail
origRepoEditOption = getRepoEditOptionFromRepo(repo16)
repoEditOption = getNewRepoEditOption(origRepoEditOption)
url = fmt.Sprintf("/api/v1/repos/%s/%s", user2.Name, repo16.Name)
req = NewRequestWithJSON(t, "PATCH", url, &repoEditOption)
resp = session.MakeRequest(t, req, http.StatusNotFound)

// Test using access token for a private repo that the user of the token owns
origRepoEditOption = getRepoEditOptionFromRepo(repo16)
repoEditOption = getNewRepoEditOption(origRepoEditOption)
url = fmt.Sprintf("/api/v1/repos/%s/%s?token=%s", user2.Name, repo16.Name, token2)
req = NewRequestWithJSON(t, "PATCH", url, &repoEditOption)
resp = session.MakeRequest(t, req, http.StatusOK)
// reset repo in db
url = fmt.Sprintf("/api/v1/repos/%s/%s?token=%s", user2.Name, *repoEditOption.Name, token2)
req = NewRequestWithJSON(t, "PATCH", url, &origRepoEditOption)
resp = session.MakeRequest(t, req, http.StatusOK)

// Test making a repo public that is private
repo16 = models.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository)
assert.True(t, repo16.IsPrivate)
private := false
repoEditOption = &api.EditRepoOption{
Private: &private,
}
url = fmt.Sprintf("/api/v1/repos/%s/%s?token=%s", user2.Name, repo16.Name, token2)
req = NewRequestWithJSON(t, "PATCH", url, &repoEditOption)
resp = session.MakeRequest(t, req, http.StatusOK)
repo16 = models.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository)
assert.False(t, repo16.IsPrivate)
// Make it private again
private = true
repoEditOption.Private = &private
req = NewRequestWithJSON(t, "PATCH", url, &repoEditOption)
resp = session.MakeRequest(t, req, http.StatusOK)

// Test using org repo "user3/repo3" where user2 is a collaborator
origRepoEditOption = getRepoEditOptionFromRepo(repo3)
repoEditOption = getNewRepoEditOption(origRepoEditOption)
url = fmt.Sprintf("/api/v1/repos/%s/%s?token=%s", user3.Name, repo3.Name, token2)
req = NewRequestWithJSON(t, "PATCH", url, &repoEditOption)
session.MakeRequest(t, req, http.StatusOK)
// reset repo in db
url = fmt.Sprintf("/api/v1/repos/%s/%s?token=%s", user3.Name, *repoEditOption.Name, token2)
req = NewRequestWithJSON(t, "PATCH", url, &origRepoEditOption)
resp = session.MakeRequest(t, req, http.StatusOK)

// Test using org repo "user3/repo3" with no user token
origRepoEditOption = getRepoEditOptionFromRepo(repo3)
repoEditOption = getNewRepoEditOption(origRepoEditOption)
url = fmt.Sprintf("/api/v1/repos/%s/%s", user3.Name, repo3.Name)
req = NewRequestWithJSON(t, "PATCH", url, &repoEditOption)
session.MakeRequest(t, req, http.StatusNotFound)

// Test using repo "user2/repo1" where user4 is a NOT collaborator
origRepoEditOption = getRepoEditOptionFromRepo(repo1)
repoEditOption = getNewRepoEditOption(origRepoEditOption)
url = fmt.Sprintf("/api/v1/repos/%s/%s?token=%s", user2.Name, repo1.Name, token4)
req = NewRequestWithJSON(t, "PATCH", url, &repoEditOption)
session.MakeRequest(t, req, http.StatusForbidden)
})
}
2 changes: 1 addition & 1 deletion integrations/api_repo_file_delete_test.go
Expand Up @@ -108,7 +108,7 @@ func TestAPIDeleteFile(t *testing.T) {
DecodeJSON(t, resp, &apiError)
assert.Equal(t, expectedAPIError, apiError)

// Test creating a file in repo1 by user4 who does not have write access
// Test creating a file in repo16 by user4 who does not have write access
fileID++
treePath = fmt.Sprintf("delete/file%d.txt", fileID)
createFile(user2, repo16, treePath)
Expand Down
4 changes: 2 additions & 2 deletions models/org.go
Expand Up @@ -162,8 +162,8 @@ func CreateOrganization(org, owner *User) (err error) {
}

// insert units for team
var units = make([]TeamUnit, 0, len(allRepUnitTypes))
for _, tp := range allRepUnitTypes {
var units = make([]TeamUnit, 0, len(AllRepoUnitTypes))
for _, tp := range AllRepoUnitTypes {
units = append(units, TeamUnit{
OrgID: org.ID,
TeamID: t.ID,
Expand Down
111 changes: 79 additions & 32 deletions models/repo.go
Expand Up @@ -274,32 +274,64 @@ func (repo *Repository) innerAPIFormat(e Engine, mode AccessMode, isParent bool)
parent = repo.BaseRepo.innerAPIFormat(e, mode, true)
}
}
hasIssues := false
if _, err := repo.getUnit(e, UnitTypeIssues); err == nil {
hasIssues = true
}
hasWiki := false
if _, err := repo.getUnit(e, UnitTypeWiki); err == nil {
hasWiki = true
}
hasPullRequests := false
ignoreWhitespaceConflicts := false
allowMerge := false
allowRebase := false
allowRebaseMerge := false
allowSquash := false
if unit, err := repo.getUnit(e, UnitTypePullRequests); err == nil {
config := unit.PullRequestsConfig()
hasPullRequests = true
ignoreWhitespaceConflicts = config.IgnoreWhitespaceConflicts
allowMerge = config.AllowMerge
allowRebase = config.AllowRebase
allowRebaseMerge = config.AllowRebaseMerge
allowSquash = config.AllowSquash
}

return &api.Repository{
ID: repo.ID,
Owner: repo.Owner.APIFormat(),
Name: repo.Name,
FullName: repo.FullName(),
Description: repo.Description,
Private: repo.IsPrivate,
Empty: repo.IsEmpty,
Archived: repo.IsArchived,
Size: int(repo.Size / 1024),
Fork: repo.IsFork,
Parent: parent,
Mirror: repo.IsMirror,
HTMLURL: repo.HTMLURL(),
SSHURL: cloneLink.SSH,
CloneURL: cloneLink.HTTPS,
Website: repo.Website,
Stars: repo.NumStars,
Forks: repo.NumForks,
Watchers: repo.NumWatches,
OpenIssues: repo.NumOpenIssues,
DefaultBranch: repo.DefaultBranch,
Created: repo.CreatedUnix.AsTime(),
Updated: repo.UpdatedUnix.AsTime(),
Permissions: permission,
AvatarURL: repo.AvatarLink(),
ID: repo.ID,
Owner: repo.Owner.APIFormat(),
Name: repo.Name,
FullName: repo.FullName(),
Description: repo.Description,
Private: repo.IsPrivate,
Empty: repo.IsEmpty,
Archived: repo.IsArchived,
Size: int(repo.Size / 1024),
Fork: repo.IsFork,
Parent: parent,
Mirror: repo.IsMirror,
HTMLURL: repo.HTMLURL(),
SSHURL: cloneLink.SSH,
CloneURL: cloneLink.HTTPS,
Website: repo.Website,
Stars: repo.NumStars,
Forks: repo.NumForks,
Watchers: repo.NumWatches,
OpenIssues: repo.NumOpenIssues,
DefaultBranch: repo.DefaultBranch,
Created: repo.CreatedUnix.AsTime(),
Updated: repo.UpdatedUnix.AsTime(),
Permissions: permission,
HasIssues: hasIssues,
HasWiki: hasWiki,
HasPullRequests: hasPullRequests,
IgnoreWhitespaceConflicts: ignoreWhitespaceConflicts,
AllowMerge: allowMerge,
AllowRebase: allowRebase,
AllowRebaseMerge: allowRebaseMerge,
AllowSquash: allowSquash,
AvatarURL: repo.AvatarLink(),
}
}

Expand Down Expand Up @@ -346,10 +378,20 @@ func (repo *Repository) UnitEnabled(tp UnitType) bool {
return false
}

var (
// ErrUnitNotExist organization does not exist
ErrUnitNotExist = errors.New("Unit does not exist")
)
// ErrUnitTypeNotExist represents a "UnitTypeNotExist" kind of error.
type ErrUnitTypeNotExist struct {
UT UnitType
}

// IsErrUnitTypeNotExist checks if an error is a ErrUnitNotExist.
func IsErrUnitTypeNotExist(err error) bool {
_, ok := err.(ErrUnitTypeNotExist)
return ok
}

func (err ErrUnitTypeNotExist) Error() string {
return fmt.Sprintf("Unit type does not exist: %s", err.UT.String())
}

// MustGetUnit always returns a RepoUnit object
func (repo *Repository) MustGetUnit(tp UnitType) *RepoUnit {
Expand All @@ -373,6 +415,11 @@ func (repo *Repository) MustGetUnit(tp UnitType) *RepoUnit {
Type: tp,
Config: new(PullRequestsConfig),
}
} else if tp == UnitTypeIssues {
return &RepoUnit{
Type: tp,
Config: new(IssuesConfig),
}
}
return &RepoUnit{
Type: tp,
Expand All @@ -394,7 +441,7 @@ func (repo *Repository) getUnit(e Engine, tp UnitType) (*RepoUnit, error) {
return unit, nil
}
}
return nil, ErrUnitNotExist
return nil, ErrUnitTypeNotExist{tp}
}

func (repo *Repository) getOwner(e Engine) (err error) {
Expand Down Expand Up @@ -1232,8 +1279,8 @@ func createRepository(e *xorm.Session, doer, u *User, repo *Repository) (err err
}

// insert units for repo
var units = make([]RepoUnit, 0, len(defaultRepoUnits))
for _, tp := range defaultRepoUnits {
var units = make([]RepoUnit, 0, len(DefaultRepoUnits))
for _, tp := range DefaultRepoUnits {
if tp == UnitTypeIssues {
units = append(units, RepoUnit{
RepoID: repo.ID,
Expand Down