This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Noah Lee
authored
Jan 1, 2022
1 parent
2a772ee
commit 455c401
Showing
14 changed files
with
773 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} |
Oops, something went wrong.