This is the local branch I've been running, and tweaking as I encounter
bugs. Sent in one thread to avoid spamming the mailing list.
Also, thanks for the cool program!
remph (2):
Misc. small patches:
Fix github API error 422 on `gcli forks create'
configure | 2 ++
docs/gcli-forks.1.in | 2 +-
include/gcli/github/repos.h | 2 ++
src/cmd/cmdconfig.c | 8 +++++---
src/cmd/repos.c | 2 +-
src/github/forks.c | 11 ++++++++++-
src/github/repos.c | 38 ++++++++++++++++++++-----------------
7 files changed, 42 insertions(+), 23 deletions(-)
--
2.47.1
The github distinction between user and organisation, noted in
github_get_repos(), is relevant also for gcli forks. This separates into
github_user_is_org() the code from github_get_repos() used to determine if
the user is an organisation, and uses that in github_fork_create().
Also match manpage treatment of -i option with --help output
---
docs/gcli-forks.1.in | 2 +-include/gcli/github/repos.h | 2 ++src/github/forks.c | 11 ++++++++++-src/github/repos.c | 38 ++++++++++++++++++++-----------------
4 files changed, 34 insertions(+), 19 deletions(-)
diff --git a/docs/gcli-forks.1.in b/docs/gcli-forks.1.in
index 4f68de54..3b2a3c59 100644
--- a/docs/gcli-forks.1.in+++ b/docs/gcli-forks.1.in
@@ -14,7 +14,7 @@
.Nm
.Cm create
.Op Fl o Ar owner Fl r Ar repo
-.Fl i Ar target-owner+.Op Fl i Ar target-owner.Sh DESCRIPTION
Use
.Nm
diff --git a/include/gcli/github/repos.h b/include/gcli/github/repos.h
index 4acd7798..f6b192d4 100644
--- a/include/gcli/github/repos.h+++ b/include/gcli/github/repos.h
@@ -39,6 +39,8 @@
int github_repo_make_url(struct gcli_ctx *ctx, struct gcli_path const *path,
char **url, char const *suffix_fmt, ...);
+int github_user_is_org(struct gcli_ctx *ctx, char const *e_owner);+int github_get_repos(struct gcli_ctx *ctx, char const *owner, int max,
struct gcli_repo_list *out);
diff --git a/src/github/forks.c b/src/github/forks.c
index c84cc1ca..2fedd154 100644
--- a/src/github/forks.c+++ b/src/github/forks.c
@@ -67,13 +67,22 @@ github_fork_create(struct gcli_ctx *ctx,
char *url = NULL;
char *post_data = NULL;
int rc = 0;
+ bool is_org = false;+ if (in) {+ char *const e_in = gcli_urlencode(in);+ rc = github_user_is_org(ctx, e_in);+ free(e_in);+ if (rc < 0)+ return rc;+ is_org = rc;+ } rc = github_repo_make_url(ctx, repo_path, &url, "/forks");
if (rc < 0)
return rc;
- if (in) {+ if (is_org) { struct gcli_jsongen gen = {0};
gcli_jsongen_init(&gen);
diff --git a/src/github/repos.c b/src/github/repos.c
index 17d0b9f5..b63548a4 100644
--- a/src/github/repos.c+++ b/src/github/repos.c
@@ -75,13 +75,28 @@ github_repo_make_url(struct gcli_ctx *ctx, struct gcli_path const *const path,
return rc;
}
+int+github_user_is_org(struct gcli_ctx *ctx, char const *e_owner)+{+ /* Github is a little stupid in that it distinguishes+ * organizations and users. Thus, we have to find out, whether the+ * <org> param is a user or an actual organization. */+ char *url = sn_asprintf("%s/users/%s", gcli_get_apibase(ctx), e_owner);+ int const rc = gcli_curl_test_success(ctx, url);+ free(url);++ /* 0 = failed, 1 = success, -1 = error (just like a BOOL in Win32+ * /sarc). But to make the name of the function make sense, reverse+ * non-negative return values (failure means user *is* an org);+ * negative return to indiciate error is preserved */+ return rc < 0 ? rc : !rc;+}+int
github_get_repos(struct gcli_ctx *ctx, char const *owner, int const max,
struct gcli_repo_list *const list)
{
char *url = NULL;
- char *e_owner = NULL;- int rc = 0; struct gcli_fetch_list_ctx lf = {
.listp = &list->repos,
@@ -90,30 +105,19 @@ github_get_repos(struct gcli_ctx *ctx, char const *owner, int const max,
.parse = (parsefn)(parse_github_repos),
};
- e_owner = gcli_urlencode(owner);+ char *e_owner = gcli_urlencode(owner);+ int rc = github_user_is_org(ctx, e_owner);- /* Github is a little stupid in that it distinguishes- * organizations and users. Thus, we have to find out, whether the- * <org> param is a user or an actual organization. */- url = sn_asprintf("%s/users/%s", gcli_get_apibase(ctx), e_owner);-- /* 0 = failed, 1 = success, -1 = error (just like a BOOL in Win32- * /sarc) */- rc = gcli_curl_test_success(ctx, url);- if (rc < 0) {- free(url);+ if (rc < 0) return rc;
- }- if (rc) {+ if (!rc) { /* it is a user */
- free(url); url = sn_asprintf("%s/users/%s/repos",
gcli_get_apibase(ctx),
e_owner);
} else {
/* this is an actual organization */
- free(url); url = sn_asprintf("%s/orgs/%s/repos",
gcli_get_apibase(ctx),
e_owner);
--
2.47.1