Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 69970aa

Browse files
author
noah
committed
Add paths to redirect to the URL for a config file
1 parent 3c1b507 commit 69970aa

File tree

6 files changed

+153
-0
lines changed

6 files changed

+153
-0
lines changed

internal/interactor/interface.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ type (
9696

9797
ListRemoteRepos(ctx context.Context, u *ent.User) ([]*extent.RemoteRepo, error)
9898

99+
GetConfigRedirectURL(ctx context.Context, u *ent.User, r *ent.Repo) (string, error)
100+
GetNewFileRedirectURL(ctx context.Context, u *ent.User, r *ent.Repo) (string, error)
101+
99102
// SCM returns the deployment with UID and SHA.
100103
CreateRemoteDeployment(ctx context.Context, u *ent.User, r *ent.Repo, d *ent.Deployment, e *extent.Env) (*extent.RemoteDeployment, error)
101104
CancelDeployment(ctx context.Context, u *ent.User, r *ent.Repo, d *ent.Deployment, s *ent.DeploymentStatus) error

internal/interactor/mock/pkg.go

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/pkg/github/link.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"path"
7+
8+
"github.com/gitploy-io/gitploy/model/ent"
9+
"github.com/gitploy-io/gitploy/pkg/e"
10+
)
11+
12+
func (g *Github) GetConfigRedirectURL(ctx context.Context, u *ent.User, r *ent.Repo) (string, error) {
13+
remote, res, err := g.Client(ctx, u.Token).
14+
Repositories.
15+
Get(ctx, r.Namespace, r.Name)
16+
if res.StatusCode == http.StatusForbidden {
17+
return "", e.NewError(e.ErrorPermissionRequired, err)
18+
} else if res.StatusCode == http.StatusNotFound {
19+
return "", e.NewError(e.ErrorCodeEntityNotFound, err)
20+
} else if err != nil {
21+
return "", e.NewError(e.ErrorCodeInternalError, err)
22+
}
23+
24+
// The latest version file on the main branch.
25+
// https://docs.github.com/en/repositories/working-with-files/using-files/getting-permanent-links-to-files
26+
url := path.Join(*remote.HTMLURL, "blob", *remote.DefaultBranch, r.ConfigPath)
27+
return url, nil
28+
}
29+
30+
func (g *Github) GetNewFileRedirectURL(ctx context.Context, u *ent.User, r *ent.Repo) (string, error) {
31+
remote, res, err := g.Client(ctx, u.Token).
32+
Repositories.
33+
Get(ctx, r.Namespace, r.Name)
34+
if res.StatusCode == http.StatusForbidden {
35+
return "", e.NewError(e.ErrorPermissionRequired, err)
36+
} else if res.StatusCode == http.StatusNotFound {
37+
return "", e.NewError(e.ErrorCodeEntityNotFound, err)
38+
} else if err != nil {
39+
return "", e.NewError(e.ErrorCodeInternalError, err)
40+
}
41+
42+
// Redirect to the URL to create a configuration file.
43+
// https://docs.github.com/en/enterprise-server@3.0/repositories/working-with-files/managing-files/creating-new-files
44+
url := path.Join(*remote.HTMLURL, "new", *remote.DefaultBranch, r.ConfigPath)
45+
return url, nil
46+
}

internal/server/router.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ func NewRouter(c *RouterConfig) *gin.Engine {
247247
}
248248

249249
root.GET("/signin", w.Signin)
250+
root.GET("/link/:namespace/:name/config", w.RedirectToConfig)
251+
root.GET("/link/:namespace/:name/config/new", w.RedirectToNewConfig)
250252

251253
// Static files located at the 'ui/public' directory.
252254
r.StaticFile("/favicon.ico", "./favicon.ico")

internal/server/web/interface.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ type (
2020
GetRemoteUserByToken(ctx context.Context, token string) (*extent.RemoteUser, error)
2121
ListRemoteOrgsByToken(ctx context.Context, token string) ([]string, error)
2222
FindUserByHash(ctx context.Context, hash string) (*ent.User, error)
23+
FindRepoOfUserByNamespaceName(ctx context.Context, u *ent.User, namespace, name string) (*ent.Repo, error)
24+
GetConfigRedirectURL(ctx context.Context, u *ent.User, r *ent.Repo) (string, error)
25+
GetNewFileRedirectURL(ctx context.Context, u *ent.User, r *ent.Repo) (string, error)
2326
GetLicense(ctx context.Context) (*extent.License, error)
2427
}
2528
)

internal/server/web/link.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package web
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/gin-gonic/gin"
7+
"go.uber.org/zap"
8+
9+
gb "github.com/gitploy-io/gitploy/internal/server/global"
10+
"github.com/gitploy-io/gitploy/model/ent"
11+
)
12+
13+
// RedirectToConfig redirects to the URL to read the configuration file.
14+
func (w *Web) RedirectToConfig(c *gin.Context) {
15+
var (
16+
namespace = c.Param("namespace")
17+
name = c.Param("name")
18+
)
19+
20+
ctx := c.Request.Context()
21+
22+
uv, _ := c.Get(gb.KeyUser)
23+
u := uv.(*ent.User)
24+
25+
r, err := w.i.FindRepoOfUserByNamespaceName(ctx, u, namespace, name)
26+
if err != nil {
27+
w.log.Check(gb.GetZapLogLevel(err), "Failed to get the repository.").Write(zap.Error(err))
28+
gb.AbortWithError(c, err)
29+
return
30+
}
31+
32+
url, err := w.i.GetConfigRedirectURL(ctx, u, r)
33+
if err != nil {
34+
w.log.Check(gb.GetZapLogLevel(err), "Failed to get the redirect URL for the configuration file.").Write(zap.Error(err))
35+
gb.AbortWithError(c, err)
36+
return
37+
}
38+
39+
c.Redirect(http.StatusMovedPermanently, url)
40+
}
41+
42+
// RedirectToNewConfig redirect to the URL to create a new file.
43+
func (w *Web) RedirectToNewConfig(c *gin.Context) {
44+
var (
45+
namespace = c.Param("namespace")
46+
name = c.Param("name")
47+
)
48+
49+
ctx := c.Request.Context()
50+
51+
uv, _ := c.Get(gb.KeyUser)
52+
u := uv.(*ent.User)
53+
54+
r, err := w.i.FindRepoOfUserByNamespaceName(ctx, u, namespace, name)
55+
if err != nil {
56+
w.log.Check(gb.GetZapLogLevel(err), "Failed to get the repository.").Write(zap.Error(err))
57+
gb.AbortWithError(c, err)
58+
return
59+
}
60+
61+
url, err := w.i.GetNewFileRedirectURL(ctx, u, r)
62+
if err != nil {
63+
w.log.Check(gb.GetZapLogLevel(err), "Failed to get the redirect URL to create a new file.").Write(zap.Error(err))
64+
gb.AbortWithError(c, err)
65+
return
66+
}
67+
68+
c.Redirect(http.StatusMovedPermanently, url)
69+
}

0 commit comments

Comments
 (0)