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

Commit

Permalink
Add new paths to redirect for the configuration file (#285)
Browse files Browse the repository at this point in the history
* Add paths to redirect to the URL for a config file

* Add the link to redirect in UI

* Fix quick-start doc
  • Loading branch information
Noah Lee authored Jan 1, 2022
1 parent 2a772ee commit 455c401
Show file tree
Hide file tree
Showing 14 changed files with 773 additions and 16 deletions.
10 changes: 8 additions & 2 deletions docs/concepts/deploy.yml.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# deploy.yml

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).
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).

*⚠️ Note that the configuration file must be at the head of the default branch.*

## Quick Start

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.
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.

```yaml
# deploy.yml
Expand All @@ -15,6 +17,10 @@ envs:
# required_context: []
```

Figure) New Configuration Link

![Quick Start](../images/quickstart.png)

## Features
### Multi Environment

Expand Down
Binary file added docs/images/quickstart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions internal/interactor/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ type (

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

GetConfigRedirectURL(ctx context.Context, u *ent.User, r *ent.Repo) (string, error)
GetNewConfigRedirectURL(ctx context.Context, u *ent.User, r *ent.Repo) (string, error)

// SCM returns the deployment with UID and SHA.
CreateRemoteDeployment(ctx context.Context, u *ent.User, r *ent.Repo, d *ent.Deployment, e *extent.Env) (*extent.RemoteDeployment, error)
CancelDeployment(ctx context.Context, u *ent.User, r *ent.Repo, d *ent.Deployment, s *ent.DeploymentStatus) error
Expand Down
30 changes: 30 additions & 0 deletions internal/interactor/mock/pkg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions internal/pkg/github/link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package github

import (
"context"
"fmt"
"net/http"

"github.com/gitploy-io/gitploy/model/ent"
"github.com/gitploy-io/gitploy/pkg/e"
)

func (g *Github) GetConfigRedirectURL(ctx context.Context, u *ent.User, r *ent.Repo) (string, error) {
remote, res, err := g.Client(ctx, u.Token).
Repositories.
Get(ctx, r.Namespace, r.Name)
if res.StatusCode == http.StatusForbidden {
return "", e.NewError(e.ErrorPermissionRequired, err)
} else if res.StatusCode == http.StatusNotFound {
return "", e.NewError(e.ErrorCodeEntityNotFound, err)
} else if err != nil {
return "", e.NewError(e.ErrorCodeInternalError, err)
}

// The latest version file on the main branch.
// https://docs.github.com/en/repositories/working-with-files/using-files/getting-permanent-links-to-files
url := fmt.Sprintf("%s/blob/%s/%s", *remote.HTMLURL, *remote.DefaultBranch, r.ConfigPath)
return url, nil
}

func (g *Github) GetNewConfigRedirectURL(ctx context.Context, u *ent.User, r *ent.Repo) (string, error) {
remote, res, err := g.Client(ctx, u.Token).
Repositories.
Get(ctx, r.Namespace, r.Name)
if res.StatusCode == http.StatusForbidden {
return "", e.NewError(e.ErrorPermissionRequired, err)
} else if res.StatusCode == http.StatusNotFound {
return "", e.NewError(e.ErrorCodeEntityNotFound, err)
} else if err != nil {
return "", e.NewError(e.ErrorCodeInternalError, err)
}

// Redirect to the URL to create a configuration file.
// https://docs.github.com/en/enterprise-server@3.0/repositories/working-with-files/managing-files/creating-new-files
url := fmt.Sprintf("%s/new/%s/%s", *remote.HTMLURL, *remote.DefaultBranch, r.ConfigPath)
return url, nil
}
70 changes: 70 additions & 0 deletions internal/pkg/github/link_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package github

import (
"context"
"reflect"
"testing"

"github.com/gitploy-io/gitploy/model/ent"
"gopkg.in/h2non/gock.v1"
)

func TestGithub_GetConfigRedirectURL(t *testing.T) {
t.Run("Return the link of the configuration file.", func(t *testing.T) {
t.Log("Mocking the GET repo API.")
gock.New("https://api.github.com").
Get("/repos/octocat/hello-world").
Reply(200).
File("./testdata/repo.get.json")

g := NewGithub(&GithubConfig{})

link, err := g.GetConfigRedirectURL(
context.Background(),
&ent.User{},
&ent.Repo{
Namespace: "octocat",
Name: "hello-world",
ConfigPath: "config.yml",
},
)
if err != nil {
t.Fatalf("GetConfigRedirectURL returns an error: %s", err)
}

want := "https://github.com/octocat/Hello-World/blob/master/config.yml"
if !reflect.DeepEqual(link, want) {
t.Fatalf("GetConfigRedirectURL = %v, wanted %v", link, want)
}
})
}

func TestGithub_GetNewConfigRedirectURL(t *testing.T) {
t.Run("Return the link of the configuration file.", func(t *testing.T) {
t.Log("Mocking the GET repo API.")
gock.New("https://api.github.com").
Get("/repos/octocat/hello-world").
Reply(200).
File("./testdata/repo.get.json")

g := NewGithub(&GithubConfig{})

link, err := g.GetNewConfigRedirectURL(
context.Background(),
&ent.User{},
&ent.Repo{
Namespace: "octocat",
Name: "hello-world",
ConfigPath: "config.yml",
},
)
if err != nil {
t.Fatalf("GetNewConfigRedirectURL returns an error: %s", err)
}

want := "https://github.com/octocat/Hello-World/new/master/config.yml"
if !reflect.DeepEqual(link, want) {
t.Fatalf("GetNewConfigRedirectURL = %v, wanted %v", link, want)
}
})
}
Loading

0 comments on commit 455c401

Please sign in to comment.