Skip to content

Commit

Permalink
Rename forge-id to forge-remote-id (#1418)
Browse files Browse the repository at this point in the history
  • Loading branch information
anbraten authored Nov 15, 2022
1 parent d785d05 commit de8ea95
Show file tree
Hide file tree
Showing 31 changed files with 317 additions and 284 deletions.
2 changes: 1 addition & 1 deletion server/api/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func PostHook(c *gin.Context) {
return
}

repo, err := _store.GetRepoNameFallback(tmpRepo.ForgeID, tmpRepo.FullName)
repo, err := _store.GetRepoNameFallback(tmpRepo.ForgeRemoteID, tmpRepo.FullName)
if err != nil {
msg := fmt.Sprintf("failure to get repo %s from store", tmpRepo.FullName)
log.Error().Err(err).Msg(msg)
Expand Down
4 changes: 2 additions & 2 deletions server/api/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func PostRepo(c *gin.Context) {
sig,
)

from, err := forge.Repo(c, user, repo.ForgeID, repo.Owner, repo.Name)
from, err := forge.Repo(c, user, repo.ForgeRemoteID, repo.Owner, repo.Name)
if err == nil {
if repo.FullName != from.FullName {
// create a redirection
Expand Down Expand Up @@ -260,7 +260,7 @@ func RepairRepo(c *gin.Context) {
sig,
)

from, err := forge.Repo(c, user, repo.ForgeID, repo.Owner, repo.Name)
from, err := forge.Repo(c, user, repo.ForgeRemoteID, repo.Owner, repo.Name)
if err != nil {
log.Error().Err(err).Msgf("get repo '%s/%s' from forge", repo.Owner, repo.Name)
c.AbortWithStatus(http.StatusInternalServerError)
Expand Down
6 changes: 3 additions & 3 deletions server/forge/bitbucket/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ func (c *config) Teams(ctx context.Context, u *model.User) ([]*model.Team, error
}

// Repo returns the named Bitbucket repository.
func (c *config) Repo(ctx context.Context, u *model.User, id model.ForgeID, owner, name string) (*model.Repo, error) {
if id.IsValid() {
name = string(id)
func (c *config) Repo(ctx context.Context, u *model.User, remoteID model.ForgeRemoteID, owner, name string) (*model.Repo, error) {
if remoteID.IsValid() {
name = string(remoteID)
}
repo, err := c.newClient(ctx, u).FindRepo(owner, name)
if err != nil {
Expand Down
20 changes: 10 additions & 10 deletions server/forge/bitbucket/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ func convertStatus(status model.StatusValue) string {
// structure to the common Woodpecker repository structure.
func convertRepo(from *internal.Repo) *model.Repo {
repo := model.Repo{
ForgeID: model.ForgeID(from.UUID),
Clone: cloneLink(from),
Owner: strings.Split(from.FullName, "/")[0],
Name: strings.Split(from.FullName, "/")[1],
FullName: from.FullName,
Link: from.Links.HTML.Href,
IsSCMPrivate: from.IsPrivate,
Avatar: from.Owner.Links.Avatar.Href,
SCMKind: model.SCMKind(from.Scm),
Branch: "master",
ForgeRemoteID: model.ForgeRemoteID(from.UUID),
Clone: cloneLink(from),
Owner: strings.Split(from.FullName, "/")[0],
Name: strings.Split(from.FullName, "/")[1],
FullName: from.FullName,
Link: from.Links.HTML.Href,
IsSCMPrivate: from.IsPrivate,
Avatar: from.Owner.Links.Avatar.Href,
SCMKind: model.SCMKind(from.Scm),
Branch: "master",
}
if repo.SCMKind == model.RepoHg {
repo.Branch = "default"
Expand Down
2 changes: 1 addition & 1 deletion server/forge/bitbucketserver/bitbucketserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (*Config) TeamPerm(u *model.User, org string) (*model.Perm, error) {
return nil, nil
}

func (c *Config) Repo(ctx context.Context, u *model.User, _ model.ForgeID, owner, name string) (*model.Repo, error) {
func (c *Config) Repo(ctx context.Context, u *model.User, _ model.ForgeRemoteID, owner, name string) (*model.Repo, error) {
repo, err := internal.NewClientWithToken(ctx, c.URL, c.Consumer, u.Token).FindRepo(owner, name)
if err != nil {
return nil, err
Expand Down
14 changes: 7 additions & 7 deletions server/forge/bitbucketserver/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ func convertStatus(status model.StatusValue) string {
// structure to the common Woodpecker repository structure.
func convertRepo(from *internal.Repo) *model.Repo {
repo := model.Repo{
ForgeID: model.ForgeID(fmt.Sprint(from.ID)),
Name: from.Slug,
Owner: from.Project.Key,
Branch: "master",
SCMKind: model.RepoGit,
IsSCMPrivate: true, // Since we have to use Netrc it has to always be private :/
FullName: fmt.Sprintf("%s/%s", from.Project.Key, from.Slug),
ForgeRemoteID: model.ForgeRemoteID(fmt.Sprint(from.ID)),
Name: from.Slug,
Owner: from.Project.Key,
Branch: "master",
SCMKind: model.RepoGit,
IsSCMPrivate: true, // Since we have to use Netrc it has to always be private :/
FullName: fmt.Sprintf("%s/%s", from.Project.Key, from.Slug),
}

for _, item := range from.Links.Clone {
Expand Down
2 changes: 1 addition & 1 deletion server/forge/coding/coding.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (c *Coding) TeamPerm(u *model.User, org string) (*model.Perm, error) {
}

// Repo fetches the repository from the forge.
func (c *Coding) Repo(ctx context.Context, u *model.User, _ model.ForgeID, owner, name string) (*model.Repo, error) {
func (c *Coding) Repo(ctx context.Context, u *model.User, _ model.ForgeRemoteID, owner, name string) (*model.Repo, error) {
client := c.newClient(ctx, u)
project, err := client.GetProject(owner, name)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion server/forge/remote.go → server/forge/forge.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type Forge interface {
Teams(ctx context.Context, u *model.User) ([]*model.Team, error)

// Repo fetches the repository from the forge, preferred is using the ID, fallback is owner/name.
Repo(ctx context.Context, u *model.User, id model.ForgeID, owner, name string) (*model.Repo, error)
Repo(ctx context.Context, u *model.User, remoteID model.ForgeRemoteID, owner, name string) (*model.Repo, error)

// Repos fetches a list of repos from the forge.
Repos(ctx context.Context, u *model.User) ([]*model.Repo, error)
Expand Down
8 changes: 4 additions & 4 deletions server/forge/gitea/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,14 @@ func (c *Gitea) TeamPerm(u *model.User, org string) (*model.Perm, error) {
}

// Repo returns the Gitea repository.
func (c *Gitea) Repo(ctx context.Context, u *model.User, id model.ForgeID, owner, name string) (*model.Repo, error) {
func (c *Gitea) Repo(ctx context.Context, u *model.User, remoteID model.ForgeRemoteID, owner, name string) (*model.Repo, error) {
client, err := c.newClientToken(ctx, u.Token)
if err != nil {
return nil, err
}

if id.IsValid() {
intID, err := strconv.ParseInt(string(id), 10, 64)
if remoteID.IsValid() {
intID, err := strconv.ParseInt(string(remoteID), 10, 64)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -566,7 +566,7 @@ func (c *Gitea) getChangedFilesForPR(ctx context.Context, repo *model.Repo, inde
return []string{}, nil
}

repo, err := _store.GetRepoNameFallback(repo.ForgeID, repo.FullName)
repo, err := _store.GetRepoNameFallback(repo.ForgeRemoteID, repo.FullName)
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions server/forge/gitea/gitea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func Test_gitea(t *testing.T) {

g.Describe("Requesting a repository", func() {
g.It("Should return the repository details", func() {
repo, err := c.Repo(ctx, fakeUser, fakeRepo.ForgeID, fakeRepo.Owner, fakeRepo.Name)
repo, err := c.Repo(ctx, fakeUser, fakeRepo.ForgeRemoteID, fakeRepo.Owner, fakeRepo.Name)
g.Assert(err).IsNil()
g.Assert(repo.Owner).Equal(fakeRepo.Owner)
g.Assert(repo.Name).Equal(fakeRepo.Name)
Expand Down Expand Up @@ -118,7 +118,7 @@ func Test_gitea(t *testing.T) {
g.It("Should return the repository list", func() {
repos, err := c.Repos(ctx, fakeUser)
g.Assert(err).IsNil()
g.Assert(repos[0].ForgeID).Equal(fakeRepo.ForgeID)
g.Assert(repos[0].ForgeRemoteID).Equal(fakeRepo.ForgeRemoteID)
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)
Expand Down Expand Up @@ -192,11 +192,11 @@ var (
}

fakeRepo = &model.Repo{
Clone: "http://gitea.com/test_name/repo_name.git",
ForgeID: "5",
Owner: "test_name",
Name: "repo_name",
FullName: "test_name/repo_name",
Clone: "http://gitea.com/test_name/repo_name.git",
ForgeRemoteID: "5",
Owner: "test_name",
Name: "repo_name",
FullName: "test_name/repo_name",
}

fakeRepoNotFound = &model.Repo{
Expand Down
20 changes: 10 additions & 10 deletions server/forge/gitea/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ func toRepo(from *gitea.Repository) *model.Repo {
from.Owner.AvatarURL,
)
return &model.Repo{
ForgeID: model.ForgeID(fmt.Sprint(from.ID)),
SCMKind: model.RepoGit,
Name: name,
Owner: from.Owner.UserName,
FullName: from.FullName,
Avatar: avatar,
Link: from.HTMLURL,
IsSCMPrivate: from.Private || from.Owner.Visibility != gitea.VisibleTypePublic,
Clone: from.CloneURL,
Branch: from.DefaultBranch,
ForgeRemoteID: model.ForgeRemoteID(fmt.Sprint(from.ID)),
SCMKind: model.RepoGit,
Name: name,
Owner: from.Owner.UserName,
FullName: from.FullName,
Avatar: avatar,
Link: from.HTMLURL,
IsSCMPrivate: from.Private || from.Owner.Visibility != gitea.VisibleTypePublic,
Clone: from.CloneURL,
Branch: from.DefaultBranch,
}
}

Expand Down
40 changes: 20 additions & 20 deletions server/forge/github/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,17 @@ func convertDesc(status model.StatusValue) string {
// structure to the common Woodpecker repository structure.
func convertRepo(from *github.Repository) *model.Repo {
repo := &model.Repo{
ForgeID: model.ForgeID(fmt.Sprint(from.GetID())),
Name: from.GetName(),
FullName: from.GetFullName(),
Link: from.GetHTMLURL(),
IsSCMPrivate: from.GetPrivate(),
Clone: from.GetCloneURL(),
Branch: from.GetDefaultBranch(),
Owner: from.GetOwner().GetLogin(),
Avatar: from.GetOwner().GetAvatarURL(),
Perm: convertPerm(from.GetPermissions()),
SCMKind: model.RepoGit,
ForgeRemoteID: model.ForgeRemoteID(fmt.Sprint(from.GetID())),
Name: from.GetName(),
FullName: from.GetFullName(),
Link: from.GetHTMLURL(),
IsSCMPrivate: from.GetPrivate(),
Clone: from.GetCloneURL(),
Branch: from.GetDefaultBranch(),
Owner: from.GetOwner().GetLogin(),
Avatar: from.GetOwner().GetAvatarURL(),
Perm: convertPerm(from.GetPermissions()),
SCMKind: model.RepoGit,
}
if len(repo.Branch) == 0 {
repo.Branch = defaultBranch
Expand Down Expand Up @@ -146,15 +146,15 @@ func convertTeam(from *github.Organization) *model.Team {
// from a webhook and convert to the common Woodpecker repository structure.
func convertRepoHook(eventRepo *github.PushEventRepository) *model.Repo {
repo := &model.Repo{
ForgeID: model.ForgeID(fmt.Sprint(eventRepo.GetID())),
Owner: eventRepo.GetOwner().GetLogin(),
Name: eventRepo.GetName(),
FullName: eventRepo.GetFullName(),
Link: eventRepo.GetHTMLURL(),
IsSCMPrivate: eventRepo.GetPrivate(),
Clone: eventRepo.GetCloneURL(),
Branch: eventRepo.GetDefaultBranch(),
SCMKind: model.RepoGit,
ForgeRemoteID: model.ForgeRemoteID(fmt.Sprint(eventRepo.GetID())),
Owner: eventRepo.GetOwner().GetLogin(),
Name: eventRepo.GetName(),
FullName: eventRepo.GetFullName(),
Link: eventRepo.GetHTMLURL(),
IsSCMPrivate: eventRepo.GetPrivate(),
Clone: eventRepo.GetCloneURL(),
Branch: eventRepo.GetDefaultBranch(),
SCMKind: model.RepoGit,
}
if repo.Branch == "" {
repo.Branch = defaultBranch
Expand Down
4 changes: 2 additions & 2 deletions server/forge/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (c *client) Teams(ctx context.Context, u *model.User) ([]*model.Team, error
}

// Repo returns the GitHub repository.
func (c *client) Repo(ctx context.Context, u *model.User, id model.ForgeID, owner, name string) (*model.Repo, error) {
func (c *client) Repo(ctx context.Context, u *model.User, id model.ForgeRemoteID, owner, name string) (*model.Repo, error) {
client := c.newClientToken(ctx, u.Token)

if id.IsValid() {
Expand Down Expand Up @@ -546,7 +546,7 @@ func (c *client) loadChangedFilesFromPullRequest(ctx context.Context, pull *gith
return pipeline, nil
}

repo, err := _store.GetRepoNameFallback(tmpRepo.ForgeID, tmpRepo.FullName)
repo, err := _store.GetRepoNameFallback(tmpRepo.ForgeRemoteID, tmpRepo.FullName)
if err != nil {
return nil, err
}
Expand Down
20 changes: 10 additions & 10 deletions server/forge/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ func Test_github(t *testing.T) {

g.Describe("Requesting a repository", func() {
g.It("Should return the repository details", func() {
repo, err := c.Repo(ctx, fakeUser, fakeRepo.ForgeID, fakeRepo.Owner, fakeRepo.Name)
repo, err := c.Repo(ctx, fakeUser, fakeRepo.ForgeRemoteID, fakeRepo.Owner, fakeRepo.Name)
g.Assert(err).IsNil()
g.Assert(repo.ForgeID).Equal(fakeRepo.ForgeID)
g.Assert(repo.ForgeRemoteID).Equal(fakeRepo.ForgeRemoteID)
g.Assert(repo.Owner).Equal(fakeRepo.Owner)
g.Assert(repo.Name).Equal(fakeRepo.Name)
g.Assert(repo.FullName).Equal(fakeRepo.FullName)
Expand Down Expand Up @@ -133,14 +133,14 @@ var (
}

fakeRepo = &model.Repo{
ForgeID: "5",
Owner: "octocat",
Name: "Hello-World",
FullName: "octocat/Hello-World",
Avatar: "/~https://github.com/images/error/octocat_happy.gif",
Link: "/~https://github.com/octocat/Hello-World",
Clone: "/~https://github.com/octocat/Hello-World.git",
IsSCMPrivate: true,
ForgeRemoteID: "5",
Owner: "octocat",
Name: "Hello-World",
FullName: "octocat/Hello-World",
Avatar: "/~https://github.com/images/error/octocat_happy.gif",
Link: "/~https://github.com/octocat/Hello-World",
Clone: "/~https://github.com/octocat/Hello-World.git",
IsSCMPrivate: true,
}

fakeRepoNotFound = &model.Repo{
Expand Down
26 changes: 13 additions & 13 deletions server/forge/gitlab/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ func (g *GitLab) convertGitLabRepo(_repo *gitlab.Project) (*model.Repo, error) {
owner := strings.Join(parts[:len(parts)-1], "/")
name := parts[len(parts)-1]
repo := &model.Repo{
ForgeID: model.ForgeID(fmt.Sprint(_repo.ID)),
Owner: owner,
Name: name,
FullName: _repo.PathWithNamespace,
Avatar: _repo.AvatarURL,
Link: _repo.WebURL,
Clone: _repo.HTTPURLToRepo,
Branch: _repo.DefaultBranch,
Visibility: model.RepoVisibly(_repo.Visibility),
IsSCMPrivate: !_repo.Public,
ForgeRemoteID: model.ForgeRemoteID(fmt.Sprint(_repo.ID)),
Owner: owner,
Name: name,
FullName: _repo.PathWithNamespace,
Avatar: _repo.AvatarURL,
Link: _repo.WebURL,
Clone: _repo.HTTPURLToRepo,
Branch: _repo.DefaultBranch,
Visibility: model.RepoVisibly(_repo.Visibility),
IsSCMPrivate: !_repo.Public,
}

if len(repo.Branch) == 0 { // TODO: do we need that?
Expand Down Expand Up @@ -88,7 +88,7 @@ func convertMergeRequestHook(hook *gitlab.MergeEvent, req *http.Request) (int, *
repo.FullName = fmt.Sprintf("%s/%s", repo.Owner, repo.Name)
}

repo.ForgeID = model.ForgeID(fmt.Sprint(obj.TargetProjectID))
repo.ForgeRemoteID = model.ForgeRemoteID(fmt.Sprint(obj.TargetProjectID))
repo.Link = target.WebURL

if target.GitHTTPURL != "" {
Expand Down Expand Up @@ -142,7 +142,7 @@ func convertPushHook(hook *gitlab.PushEvent) (*model.Repo, *model.Pipeline, erro
return nil, nil, err
}

repo.ForgeID = model.ForgeID(fmt.Sprint(hook.ProjectID))
repo.ForgeRemoteID = model.ForgeRemoteID(fmt.Sprint(hook.ProjectID))
repo.Avatar = hook.Project.AvatarURL
repo.Link = hook.Project.WebURL
repo.Clone = hook.Project.GitHTTPURL
Expand Down Expand Up @@ -194,7 +194,7 @@ func convertTagHook(hook *gitlab.TagEvent) (*model.Repo, *model.Pipeline, error)
return nil, nil, err
}

repo.ForgeID = model.ForgeID(fmt.Sprint(hook.ProjectID))
repo.ForgeRemoteID = model.ForgeRemoteID(fmt.Sprint(hook.ProjectID))
repo.Avatar = hook.Project.AvatarURL
repo.Link = hook.Project.WebURL
repo.Clone = hook.Project.GitHTTPURL
Expand Down
8 changes: 4 additions & 4 deletions server/forge/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,14 @@ func (g *GitLab) getProject(ctx context.Context, client *gitlab.Client, owner, n
}

// Repo fetches the repository from the forge.
func (g *GitLab) Repo(ctx context.Context, user *model.User, id model.ForgeID, owner, name string) (*model.Repo, error) {
func (g *GitLab) Repo(ctx context.Context, user *model.User, remoteID model.ForgeRemoteID, owner, name string) (*model.Repo, error) {
client, err := newClient(g.URL, user.Token, g.SkipVerify)
if err != nil {
return nil, err
}

if id.IsValid() {
intID, err := strconv.ParseInt(string(id), 10, 64)
if remoteID.IsValid() {
intID, err := strconv.ParseInt(string(remoteID), 10, 64)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -682,7 +682,7 @@ func (g *GitLab) loadChangedFilesFromMergeRequest(ctx context.Context, tmpRepo *
return pipeline, nil
}

repo, err := _store.GetRepoNameFallback(tmpRepo.ForgeID, tmpRepo.FullName)
repo, err := _store.GetRepoNameFallback(tmpRepo.ForgeRemoteID, tmpRepo.FullName)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit de8ea95

Please sign in to comment.