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

Commit 455c401

Browse files
author
Noah Lee
authored
Add new paths to redirect for the configuration file (#285)
* Add paths to redirect to the URL for a config file * Add the link to redirect in UI * Fix quick-start doc
1 parent 2a772ee commit 455c401

File tree

14 files changed

+773
-16
lines changed

14 files changed

+773
-16
lines changed

docs/concepts/deploy.yml.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# deploy.yml
22

3-
Gitploy configures a pipeline with a simple, easy‑to‑read file that you commit to your git repository. *The configuration file must be at the head of the default branch.* The default path is `deploy.yml` at the root directory, but you can replace the file path in the settings tab of Gitploy. You can check the [documentation](../references/deploy.yml.md) for the specification of the configuration file and also references [Use Cases](../tasks/usecases.md).
3+
Gitploy configures a pipeline with a simple, easy‑to‑read file that you commit to your git repository. The default path is `deploy.yml` at the root directory, but you can replace the file path in the settings tab of Gitploy. You can check the [documentation](../references/deploy.yml.md) for the specification of the configuration file and also references [Use Cases](../tasks/usecases.md).
4+
5+
*⚠️ Note that the configuration file must be at the head of the default branch.*
46

57
## Quick Start
68

7-
If you want to get started quickly, you should copy the `deploy.yml` file and push it into your git repository. Then you can find the environment in the Gitploy.
9+
If you want to get started quickly, you should click the *New Configuration* link, copy the `deploy.yml` file, and push it into your git repository. Then you can find the `production` environment in Gitploy.
810

911
```yaml
1012
# deploy.yml
@@ -15,6 +17,10 @@ envs:
1517
# required_context: []
1618
```
1719

20+
Figure) New Configuration Link
21+
22+
![Quick Start](../images/quickstart.png)
23+
1824
## Features
1925
### Multi Environment
2026

docs/images/quickstart.png

18 KB
Loading

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+
GetNewConfigRedirectURL(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+
"fmt"
6+
"net/http"
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 := fmt.Sprintf("%s/blob/%s/%s", *remote.HTMLURL, *remote.DefaultBranch, r.ConfigPath)
27+
return url, nil
28+
}
29+
30+
func (g *Github) GetNewConfigRedirectURL(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/[email protected]/repositories/working-with-files/managing-files/creating-new-files
44+
url := fmt.Sprintf("%s/new/%s/%s", *remote.HTMLURL, *remote.DefaultBranch, r.ConfigPath)
45+
return url, nil
46+
}

internal/pkg/github/link_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"reflect"
6+
"testing"
7+
8+
"github.com/gitploy-io/gitploy/model/ent"
9+
"gopkg.in/h2non/gock.v1"
10+
)
11+
12+
func TestGithub_GetConfigRedirectURL(t *testing.T) {
13+
t.Run("Return the link of the configuration file.", func(t *testing.T) {
14+
t.Log("Mocking the GET repo API.")
15+
gock.New("https://api.github.com").
16+
Get("/repos/octocat/hello-world").
17+
Reply(200).
18+
File("./testdata/repo.get.json")
19+
20+
g := NewGithub(&GithubConfig{})
21+
22+
link, err := g.GetConfigRedirectURL(
23+
context.Background(),
24+
&ent.User{},
25+
&ent.Repo{
26+
Namespace: "octocat",
27+
Name: "hello-world",
28+
ConfigPath: "config.yml",
29+
},
30+
)
31+
if err != nil {
32+
t.Fatalf("GetConfigRedirectURL returns an error: %s", err)
33+
}
34+
35+
want := "https://github.com/octocat/Hello-World/blob/master/config.yml"
36+
if !reflect.DeepEqual(link, want) {
37+
t.Fatalf("GetConfigRedirectURL = %v, wanted %v", link, want)
38+
}
39+
})
40+
}
41+
42+
func TestGithub_GetNewConfigRedirectURL(t *testing.T) {
43+
t.Run("Return the link of the configuration file.", func(t *testing.T) {
44+
t.Log("Mocking the GET repo API.")
45+
gock.New("https://api.github.com").
46+
Get("/repos/octocat/hello-world").
47+
Reply(200).
48+
File("./testdata/repo.get.json")
49+
50+
g := NewGithub(&GithubConfig{})
51+
52+
link, err := g.GetNewConfigRedirectURL(
53+
context.Background(),
54+
&ent.User{},
55+
&ent.Repo{
56+
Namespace: "octocat",
57+
Name: "hello-world",
58+
ConfigPath: "config.yml",
59+
},
60+
)
61+
if err != nil {
62+
t.Fatalf("GetNewConfigRedirectURL returns an error: %s", err)
63+
}
64+
65+
want := "https://github.com/octocat/Hello-World/new/master/config.yml"
66+
if !reflect.DeepEqual(link, want) {
67+
t.Fatalf("GetNewConfigRedirectURL = %v, wanted %v", link, want)
68+
}
69+
})
70+
}

0 commit comments

Comments
 (0)