mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-12 08:23:48 +02:00
52d3652f2e
Use IDs of the forge to fetch repositories instead of their names and owner names. This improves handling of renamed and transferred repos. TODO - [ ] try to support as many forges as possible - [x] Gogs (no API) - [ ] Bitbucket Server - [x] Coding (no API?) - [x] update repo every time it is fetched or received from the forge - [x] if repo remote IDs are not available, use owner / name to get it - [x] handle redirections (redirect a renamed repo to its new path) - [x] ~~pull all repos once during migration to update ID (?)~~ issue fixed by on-demand loading of remote IDs - [x] handle redirections in web UI - [ ] improve handling of hooks after a repo was renamed (currently it checks for a redirection to the repo) - [x] tests - [x] `UNIQUE` constraint for remote IDs after migration shouldn't work (all repos have an empty string as remote ID) close #854 close #648 partial close https://codeberg.org/Codeberg-CI/feedback/issues/46 Possible follow-up PRs - apply the same scheme on everything fetched from the remote (currently only users) Co-authored-by: 6543 <6543@obermui.de>
206 lines
6.1 KiB
Go
206 lines
6.1 KiB
Go
// Copyright 2018 Drone.IO Inc.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package gogs
|
|
|
|
import (
|
|
"context"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/franela/goblin"
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
|
"github.com/woodpecker-ci/woodpecker/server/remote/gogs/fixtures"
|
|
)
|
|
|
|
func Test_gogs(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
s := httptest.NewServer(fixtures.Handler())
|
|
c, _ := New(Opts{
|
|
URL: s.URL,
|
|
SkipVerify: true,
|
|
})
|
|
|
|
ctx := context.Background()
|
|
g := goblin.Goblin(t)
|
|
g.Describe("Gogs", func() {
|
|
g.After(func() {
|
|
s.Close()
|
|
})
|
|
|
|
g.Describe("Creating a remote", func() {
|
|
g.It("Should return client with specified options", func() {
|
|
remote, _ := New(Opts{
|
|
URL: "http://localhost:8080",
|
|
Username: "someuser",
|
|
Password: "password",
|
|
SkipVerify: true,
|
|
PrivateMode: true,
|
|
})
|
|
g.Assert(remote.(*client).URL).Equal("http://localhost:8080")
|
|
g.Assert(remote.(*client).Username).Equal("someuser")
|
|
g.Assert(remote.(*client).Password).Equal("password")
|
|
g.Assert(remote.(*client).SkipVerify).Equal(true)
|
|
g.Assert(remote.(*client).PrivateMode).Equal(true)
|
|
})
|
|
g.It("Should handle malformed url", func() {
|
|
_, err := New(Opts{URL: "%gh&%ij"})
|
|
g.Assert(err).IsNotNil()
|
|
})
|
|
})
|
|
|
|
g.Describe("Generating a netrc file", func() {
|
|
g.It("Should return a netrc with the user token", func() {
|
|
remote, _ := New(Opts{})
|
|
netrc, _ := remote.Netrc(fakeUser, fakeRepo)
|
|
g.Assert(netrc.Machine).Equal("gogs.com")
|
|
g.Assert(netrc.Login).Equal(fakeUser.Token)
|
|
g.Assert(netrc.Password).Equal("x-oauth-basic")
|
|
})
|
|
g.It("Should return a netrc with the machine account", func() {
|
|
remote, _ := New(Opts{
|
|
Username: "someuser",
|
|
Password: "password",
|
|
})
|
|
netrc, _ := remote.Netrc(nil, fakeRepo)
|
|
g.Assert(netrc.Machine).Equal("gogs.com")
|
|
g.Assert(netrc.Login).Equal("someuser")
|
|
g.Assert(netrc.Password).Equal("password")
|
|
})
|
|
})
|
|
|
|
g.Describe("Requesting a repository", func() {
|
|
g.It("Should return the repository details", func() {
|
|
repo, err := c.Repo(ctx, fakeUser, fakeRepo.RemoteID, fakeRepo.Owner, fakeRepo.Name)
|
|
g.Assert(err).IsNil()
|
|
g.Assert(repo.Owner).Equal(fakeRepo.Owner)
|
|
g.Assert(repo.Name).Equal(fakeRepo.Name)
|
|
g.Assert(repo.FullName).Equal(fakeRepo.Owner + "/" + fakeRepo.Name)
|
|
g.Assert(repo.IsSCMPrivate).IsTrue()
|
|
g.Assert(repo.Clone).Equal("http://localhost/test_name/repo_name.git")
|
|
g.Assert(repo.Link).Equal("http://localhost/test_name/repo_name")
|
|
})
|
|
g.It("Should handle a not found error", func() {
|
|
_, err := c.Repo(ctx, fakeUser, "0", fakeRepoNotFound.Owner, fakeRepoNotFound.Name)
|
|
g.Assert(err).IsNotNil()
|
|
})
|
|
})
|
|
|
|
g.Describe("Requesting repository permissions", func() {
|
|
g.It("Should return the permission details", func() {
|
|
perm, err := c.Perm(ctx, fakeUser, fakeRepo)
|
|
g.Assert(err).IsNil()
|
|
g.Assert(perm.Admin).IsTrue()
|
|
g.Assert(perm.Push).IsTrue()
|
|
g.Assert(perm.Pull).IsTrue()
|
|
})
|
|
g.It("Should handle a not found error", func() {
|
|
_, err := c.Perm(ctx, fakeUser, fakeRepoNotFound)
|
|
g.Assert(err).IsNotNil()
|
|
})
|
|
})
|
|
|
|
g.Describe("Requesting a repository list", func() {
|
|
g.It("Should return the repository list", func() {
|
|
repos, err := c.Repos(ctx, fakeUser)
|
|
g.Assert(err).IsNil()
|
|
g.Assert(repos[0].RemoteID).Equal(fakeRepo.RemoteID)
|
|
g.Assert(repos[0].Owner).Equal(fakeRepo.Owner)
|
|
g.Assert(repos[0].Name).Equal(fakeRepo.Name)
|
|
g.Assert(repos[0].FullName).Equal(fakeRepo.Owner + "/" + fakeRepo.Name)
|
|
})
|
|
g.It("Should handle a not found error", func() {
|
|
_, err := c.Repos(ctx, fakeUserNoRepos)
|
|
g.Assert(err).IsNotNil()
|
|
})
|
|
})
|
|
|
|
g.It("Should register repositroy hooks", func() {
|
|
err := c.Activate(ctx, fakeUser, fakeRepo, "http://localhost")
|
|
g.Assert(err).IsNil()
|
|
})
|
|
|
|
g.It("Should return a repository file", func() {
|
|
raw, err := c.File(ctx, fakeUser, fakeRepo, fakeBuild, ".woodpecker.yml")
|
|
g.Assert(err).IsNil()
|
|
g.Assert(string(raw)).Equal("{ platform: linux/amd64 }")
|
|
})
|
|
|
|
g.It("Should return a repository file from a ref", func() {
|
|
raw, err := c.File(ctx, fakeUser, fakeRepo, fakeBuildWithRef, ".woodpecker.yml")
|
|
g.Assert(err).IsNil()
|
|
g.Assert(string(raw)).Equal("{ platform: linux/amd64 }")
|
|
})
|
|
|
|
g.Describe("Given an authentication request", func() {
|
|
g.It("Should redirect to login form")
|
|
g.It("Should create an access token")
|
|
g.It("Should handle an access token error")
|
|
g.It("Should return the authenticated user")
|
|
})
|
|
|
|
g.Describe("Given a repository hook", func() {
|
|
g.It("Should skip non-push events")
|
|
g.It("Should return push details")
|
|
g.It("Should handle a parsing error")
|
|
})
|
|
|
|
g.It("Should return no-op for usupporeted features", func() {
|
|
_, err1 := c.Auth(ctx, "octocat", "4vyW6b49Z")
|
|
err2 := c.Status(ctx, nil, nil, nil, nil)
|
|
err3 := c.Deactivate(ctx, nil, nil, "")
|
|
g.Assert(err1).IsNotNil()
|
|
g.Assert(err2).IsNil()
|
|
g.Assert(err3).IsNil()
|
|
})
|
|
})
|
|
}
|
|
|
|
var (
|
|
fakeUser = &model.User{
|
|
Login: "someuser",
|
|
Token: "cfcd2084",
|
|
}
|
|
|
|
fakeUserNoRepos = &model.User{
|
|
Login: "someuser",
|
|
Token: "repos_not_found",
|
|
}
|
|
|
|
fakeRepo = &model.Repo{
|
|
RemoteID: "5",
|
|
Clone: "http://gogs.com/test_name/repo_name.git",
|
|
Owner: "test_name",
|
|
Name: "repo_name",
|
|
FullName: "test_name/repo_name",
|
|
}
|
|
|
|
fakeRepoNotFound = &model.Repo{
|
|
Owner: "test_name",
|
|
Name: "repo_not_found",
|
|
FullName: "test_name/repo_not_found",
|
|
}
|
|
|
|
fakeBuild = &model.Build{
|
|
Commit: "9ecad50",
|
|
}
|
|
|
|
fakeBuildWithRef = &model.Build{
|
|
Ref: "refs/tags/v1.0.0",
|
|
}
|
|
)
|