diff options
author | 2025-04-26 13:04:24 -0700 | |
---|---|---|
committer | 2025-04-26 13:04:24 -0700 | |
commit | b43f927efec7ab2ceaca70aa1eff32b6c8e54725 (patch) | |
tree | bd008681858eb63b3dcffa921e425664f380d967 | |
parent | 9f46ca93b86e5bcf43e11d6f533f82c68794e0b1 (diff) | |
parent | 2e6607739e4f5bbc757e43e6f5028abb3b4c0c61 (diff) | |
download | github-mirror-b43f927efec7ab2ceaca70aa1eff32b6c8e54725.tar.gz github-mirror-b43f927efec7ab2ceaca70aa1eff32b6c8e54725.tar.zst github-mirror-b43f927efec7ab2ceaca70aa1eff32b6c8e54725.zip |
Merge pull request #1 from ansg191/private-filter
-rw-r--r-- | queries/list_repos.graphql | 1 | ||||
-rw-r--r-- | src/config.c | 29 | ||||
-rw-r--r-- | src/config.h | 2 | ||||
-rw-r--r-- | src/github_types.c | 8 | ||||
-rw-r--r-- | src/github_types.h | 1 | ||||
-rw-r--r-- | src/main.c | 6 |
6 files changed, 42 insertions, 5 deletions
diff --git a/queries/list_repos.graphql b/queries/list_repos.graphql index edcab80..d7d36b3 100644 --- a/queries/list_repos.graphql +++ b/queries/list_repos.graphql @@ -5,6 +5,7 @@ query GetUserRepos($username: String!, $after: String) { name url isFork + isPrivate } pageInfo { hasNextPage diff --git a/src/config.c b/src/config.c index 56aac11..71b735e 100644 --- a/src/config.c +++ b/src/config.c @@ -152,6 +152,21 @@ token_check: return NULL; } +static int parse_bool(const char *value, int *out) +{ + if (!strcmp(value, "true") || !strcmp(value, "1") || + !strcmp(value, "yes") || !strcmp(value, "on")) { + *out = 1; + return 0; + } + if (!strcmp(value, "false") || !strcmp(value, "0") || + !strcmp(value, "no") || !strcmp(value, "off")) { + *out = 0; + return 0; + } + return -1; +} + static int parse_line_inner(struct config *cfg, enum config_section section, char *key, char *value) { @@ -171,17 +186,21 @@ static int parse_line_inner(struct config *cfg, enum config_section section, else if (!strcmp(key, "owner")) cfg->head->owner = value; else if (!strcmp(key, "skip-forks")) { - if (!strcmp(value, "true")) - cfg->head->skip_forks = 1; - else if (!strcmp(value, "false")) - cfg->head->skip_forks = 0; - else { + if (parse_bool(value, &cfg->head->skip_forks) < 0) { fprintf(stderr, "Error parsing config file: " "invalid value for skip-forks: %s\n", value); return -1; } + } else if (!strcmp(key, "skip-private")) { + if (parse_bool(value, &cfg->head->skip_private) < 0) { + fprintf(stderr, + "Error parsing config file: " + "invalid value for skip-private: %s\n", + value); + return -1; + } } else { fprintf(stderr, "Error parsing config file: unknown key: %s\n", diff --git a/src/config.h b/src/config.h index f5dfe2d..aaeec9c 100644 --- a/src/config.h +++ b/src/config.h @@ -15,6 +15,8 @@ extern const char *config_locations[]; struct github_cfg { /// Whether to skip mirroring fork repositories int skip_forks; + /// Whether to skip mirroring private repositories + int skip_private; // Borrowed /// Github graphql API endpoint diff --git a/src/github_types.c b/src/github_types.c index ea1d6c5..878758d 100644 --- a/src/github_types.c +++ b/src/github_types.c @@ -126,10 +126,18 @@ int list_repos_from_json(cJSON *root, struct list_repos_res *res) status = -1; goto end; } + cJSON *is_private = cJSON_GetObjectItemCaseSensitive( + repo, "isPrivate"); + if (!is_private || !cJSON_IsBool(is_private)) { + fprintf(stderr, "Error: isPrivate not found\n"); + status = -1; + goto end; + } res->repos[res->repos_len].name = strdup(name->valuestring); res->repos[res->repos_len].url = strdup(url->valuestring); res->repos[res->repos_len].is_fork = cJSON_IsTrue(is_fork); + res->repos[res->repos_len].is_private = cJSON_IsTrue(is_private); res->repos_len++; } diff --git a/src/github_types.h b/src/github_types.h index ab3f884..90a52a9 100644 --- a/src/github_types.h +++ b/src/github_types.h @@ -17,6 +17,7 @@ struct list_repos_res { char *name; char *url; int is_fork; + int is_private; } *repos; size_t repos_len; @@ -108,6 +108,12 @@ static int mirror_owner(const char *git_base, const struct github_cfg *cfg, res.repos[i].name); continue; } + if (cfg->skip_private && res.repos[i].is_private) { + if (!quiet) + printf("Skipping private repo: %s\n", + res.repos[i].name); + continue; + } if (!quiet) printf("Repo: %s\t%s\n", res.repos[i].name, |