dep

Package manager for embedded C libraries
git clone git://git.finwo.net/app/dep
Log | Files | Refs | README | LICENSE

github-utils.c (1721B)


      1 #include "github-utils.h"
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 
      7 #include "net-utils.h"
      8 #include "tidwall/json.h"
      9 
     10 static char *github_ref(const char *full_name, const char *ref_type, const char *ref) {
     11   char url[512];
     12   snprintf(url, sizeof(url), "https://api.github.com/repos/%s/git/ref/%s/%s", full_name, ref_type, ref);
     13 
     14   size_t size;
     15   char  *response = download_url(url, &size);
     16   if (!response) return NULL;
     17 
     18   struct json root    = json_parse(response);
     19   struct json ref_obj = json_object_get(root, "ref");
     20 
     21   char *full_ref = NULL;
     22   if (json_exists(ref_obj) && json_type(ref_obj) == JSON_STRING) {
     23     size_t len = json_string_length(ref_obj);
     24     full_ref   = malloc(len + 1);
     25     if (full_ref) {
     26       json_string_copy(ref_obj, full_ref, len + 1);
     27     }
     28   }
     29 
     30   free(response);
     31   return full_ref;
     32 }
     33 
     34 char *github_default_branch(const char *full_name) {
     35   char url[512];
     36   snprintf(url, sizeof(url), "https://api.github.com/repos/%s", full_name);
     37 
     38   size_t size;
     39   char  *response = download_url(url, &size);
     40   if (!response) return NULL;
     41 
     42   struct json root           = json_parse(response);
     43   struct json default_branch = json_object_get(root, "default_branch");
     44 
     45   char *branch = NULL;
     46   if (json_exists(default_branch) && json_type(default_branch) == JSON_STRING) {
     47     size_t len = json_string_length(default_branch);
     48     branch     = malloc(len + 1);
     49     if (branch) {
     50       json_string_copy(default_branch, branch, len + 1);
     51     }
     52   }
     53 
     54   free(response);
     55   return branch;
     56 }
     57 
     58 char *github_matching_ref(const char *full_name, const char *ref) {
     59   char *full_ref = github_ref(full_name, "tags", ref);
     60   if (full_ref) return full_ref;
     61 
     62   return github_ref(full_name, "heads", ref);
     63 }