-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dawid Ciepiela
committed
Aug 30, 2024
1 parent
edc0bbc
commit 40a694d
Showing
15 changed files
with
1,778 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,5 @@ dictionaries: | |
words: [] | ||
ignoreWords: [] | ||
import: [] | ||
enableFiletypes: | ||
- "!json" |
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
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
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,168 @@ | ||
package restclient | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/sarumaj/gh-gr/v2/pkg/configfile" | ||
) | ||
|
||
func setupTestServer(tb testing.TB) *httptest.Server { | ||
tb.Helper() | ||
|
||
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
http.ServeFile(w, r, filepath.Join("test", r.URL.Path+".json")) | ||
})) | ||
server.TLS = &tls.Config{} | ||
|
||
server.StartTLS() | ||
tb.Cleanup(server.Close) | ||
|
||
return server | ||
} | ||
|
||
func setupTestClient(tb testing.TB, server *httptest.Server) *RESTClient { | ||
tb.Helper() | ||
|
||
parsed, err := url.Parse(server.URL) | ||
if err != nil { | ||
tb.Fatalf("Failed to parse server URL: %v", err) | ||
} | ||
|
||
client, err := NewRESTClient( | ||
&configfile.Configuration{Concurrency: 16, Timeout: time.Hour}, | ||
ClientOptions{ | ||
AuthToken: "1234", | ||
Host: parsed.Host, | ||
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}, | ||
}, | ||
true, | ||
) | ||
if err != nil { | ||
tb.Fatalf("Failed to create REST client: %v", err) | ||
} | ||
|
||
tb.Cleanup(func() { _ = client.Close() }) | ||
return client | ||
} | ||
|
||
func TestRESTClient(t *testing.T) { | ||
server := setupTestServer(t) | ||
client := setupTestClient(t, server) | ||
|
||
t.Run("ClosePullRequest", func(t *testing.T) { | ||
// spellchecker:ignore octocat | ||
if err := client.ClosePullRequest(context.TODO(), "octocat", "Hello-World", 1347); err != nil { | ||
t.Fatalf("Failed to close pull request: %v", err) | ||
} | ||
}) | ||
|
||
t.Run("GetAllUserRepos", func(t *testing.T) { | ||
if repos, err := client.GetAllUserRepos(context.TODO(), nil, nil); err != nil { | ||
t.Fatalf("Failed to get user repositories: %v", err) | ||
} else if len(repos) == 0 { | ||
t.Fatalf("Failed to get user repositories: no repositories found") | ||
} | ||
}) | ||
|
||
t.Run("GetOrgRepos", func(t *testing.T) { | ||
if repos, err := client.GetOrgRepos(context.TODO(), "github"); err != nil { | ||
t.Fatalf("Failed to get org repos: %v", err) | ||
} else if len(repos) == 0 { | ||
t.Fatalf("Failed to get org repos: no repositories found") | ||
} | ||
}) | ||
|
||
t.Run("GetUserOrgs", func(t *testing.T) { | ||
if orgs, err := client.GetUserOrgs(context.TODO()); err != nil { | ||
t.Fatalf("Failed to get user orgs: %v", err) | ||
} else if len(orgs) == 0 { | ||
t.Fatalf("Failed to get user orgs: no organizations found") | ||
} | ||
}) | ||
|
||
t.Run("GetUserRepos", func(t *testing.T) { | ||
if repos, err := client.GetUserRepos(context.TODO()); err != nil { | ||
t.Fatalf("Failed to get user repos: %v", err) | ||
} else if len(repos) == 0 { | ||
t.Fatalf("Failed to get user repos: no repositories found") | ||
} | ||
}) | ||
|
||
t.Run("GetOrg", func(t *testing.T) { | ||
if org, err := client.GetOrg(context.TODO(), "github"); err != nil { | ||
t.Fatalf("Failed to get org: %v", err) | ||
} else if org == nil { | ||
t.Fatalf("Failed to get org: no organization found") | ||
} | ||
}) | ||
|
||
t.Run("GetOrgs", func(t *testing.T) { | ||
if orgs, err := client.GetOrgs(context.TODO()); err != nil { | ||
t.Fatalf("Failed to get orgs: %v", err) | ||
} else if len(orgs) == 0 { | ||
t.Fatalf("Failed to get orgs: no organizations found") | ||
} | ||
}) | ||
|
||
t.Run("GetRateLimit", func(t *testing.T) { | ||
if rate, _, err := client.GetRateLimit(context.TODO()); err != nil { | ||
t.Fatalf("Failed to get rate limit: %v", err) | ||
} else if rate == nil { | ||
t.Fatalf("Failed to get rate limit: no rate limit found") | ||
} | ||
}) | ||
|
||
t.Run("GetOrgRepoPulls", func(t *testing.T) { | ||
if pulls, err := client.GetOrgRepoPulls(context.TODO(), "octocat", "Hello-World", map[string]string{}); err != nil { | ||
t.Fatalf("Failed to get org repo pulls: %v", err) | ||
} else if len(pulls) == 0 { | ||
t.Fatalf("Failed to get org repo pulls: no pull requests found") | ||
} | ||
}) | ||
|
||
t.Run("GetUser", func(t *testing.T) { | ||
if user, err := client.GetUser(context.TODO()); err != nil { | ||
t.Fatalf("Failed to get user: %v", err) | ||
} else if user == nil { | ||
t.Fatalf("Failed to get user: no user found") | ||
} | ||
}) | ||
|
||
t.Run("GetUserRepos", func(t *testing.T) { | ||
if repos, err := client.GetUserRepos(context.TODO()); err != nil { | ||
t.Fatalf("Failed to get user repos: %v", err) | ||
} else if len(repos) == 0 { | ||
t.Fatalf("Failed to get user repos: no repositories found") | ||
} | ||
}) | ||
|
||
t.Run("GetUserOrgs", func(t *testing.T) { | ||
if orgs, err := client.GetUserOrgs(context.TODO()); err != nil { | ||
t.Fatalf("Failed to get user orgs: %v", err) | ||
} else if len(orgs) == 0 { | ||
t.Fatalf("Failed to get user orgs: no organizations found") | ||
} | ||
}) | ||
|
||
t.Run("ReopenPullRequest", func(t *testing.T) { | ||
// spellchecker:ignore octocat | ||
if err := client.ReopenPullRequest(context.TODO(), "octocat", "Hello-World", 1347); err != nil { | ||
t.Fatalf("Failed to reopen pull request: %v", err) | ||
} | ||
}) | ||
|
||
t.Run("SearchOrgRepoPulls", func(t *testing.T) { | ||
if pulls, err := client.SearchOrgRepoPulls(context.TODO(), "octocat", "Hello-World", ""); err != nil { | ||
t.Fatalf("Failed to search org repo pulls: %v", err) | ||
} else if len(pulls) == 0 { | ||
t.Fatalf("Failed to search org repo pulls: no pull requests found") | ||
} | ||
}) | ||
} |
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,16 @@ | ||
[ | ||
{ | ||
"login": "github", | ||
"id": 1, | ||
"node_id": "MDEyOk9yZ2FuaXphdGlvbjE=", | ||
"url": "https://api.github.com/orgs/github", | ||
"repos_url": "https://api.github.com/orgs/github/repos", | ||
"events_url": "https://api.github.com/orgs/github/events", | ||
"hooks_url": "https://api.github.com/orgs/github/hooks", | ||
"issues_url": "https://api.github.com/orgs/github/issues", | ||
"members_url": "https://api.github.com/orgs/github/members{/member}", | ||
"public_members_url": "https://api.github.com/orgs/github/public_members{/member}", | ||
"avatar_url": "https://github.com/images/error/octocat_happy.gif", | ||
"description": "A great organization" | ||
} | ||
] |
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,64 @@ | ||
{ | ||
"login": "github", | ||
"id": 1, | ||
"node_id": "MDEyOk9yZ2FuaXphdGlvbjE=", | ||
"url": "https://api.github.com/orgs/github", | ||
"repos_url": "https://api.github.com/orgs/github/repos", | ||
"events_url": "https://api.github.com/orgs/github/events", | ||
"hooks_url": "https://api.github.com/orgs/github/hooks", | ||
"issues_url": "https://api.github.com/orgs/github/issues", | ||
"members_url": "https://api.github.com/orgs/github/members{/member}", | ||
"public_members_url": "https://api.github.com/orgs/github/public_members{/member}", | ||
"avatar_url": "https://github.com/images/error/octocat_happy.gif", | ||
"description": "A great organization", | ||
"name": "github", | ||
"company": "GitHub", | ||
"blog": "https://github.com/blog", | ||
"location": "San Francisco", | ||
"email": "octocat@github.com", | ||
"twitter_username": "github", | ||
"is_verified": true, | ||
"has_organization_projects": true, | ||
"has_repository_projects": true, | ||
"public_repos": 2, | ||
"public_gists": 1, | ||
"followers": 20, | ||
"following": 0, | ||
"html_url": "https://github.com/octocat", | ||
"created_at": "2008-01-14T04:33:35Z", | ||
"type": "Organization", | ||
"total_private_repos": 100, | ||
"owned_private_repos": 100, | ||
"private_gists": 81, | ||
"disk_usage": 10000, | ||
"collaborators": 8, | ||
"billing_email": "mona@github.com", | ||
"plan": { | ||
"name": "Medium", | ||
"space": 400, | ||
"private_repos": 20, | ||
"filled_seats": 4, | ||
"seats": 5 | ||
}, | ||
"default_repository_permission": "read", | ||
"members_can_create_repositories": true, | ||
"two_factor_requirement_enabled": true, | ||
"members_allowed_repository_creation_type": "all", | ||
"members_can_create_public_repositories": false, | ||
"members_can_create_private_repositories": false, | ||
"members_can_create_internal_repositories": false, | ||
"members_can_create_pages": true, | ||
"members_can_create_public_pages": true, | ||
"members_can_create_private_pages": true, | ||
"members_can_fork_private_repositories": false, | ||
"web_commit_signoff_required": false, | ||
"updated_at": "2014-03-03T18:58:10Z", | ||
"dependency_graph_enabled_for_new_repositories": false, | ||
"dependabot_alerts_enabled_for_new_repositories": false, | ||
"dependabot_security_updates_enabled_for_new_repositories": false, | ||
"advanced_security_enabled_for_new_repositories": false, | ||
"secret_scanning_enabled_for_new_repositories": false, | ||
"secret_scanning_push_protection_enabled_for_new_repositories": false, | ||
"secret_scanning_push_protection_custom_link": "https://github.com/octo-org/octo-repo/blob/main/im-blocked.md", | ||
"secret_scanning_push_protection_custom_link_enabled": false | ||
} |
Oops, something went wrong.