From 26f7abf633bf662287308fa68ae597845f087366 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Mon, 13 Dec 2021 01:38:05 -0800 Subject: [PATCH] zig/db- add Remote repo details parsing --- src/db/Remote.zig | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/db/Remote.zig b/src/db/Remote.zig index 9f6d34b..1871bee 100644 --- a/src/db/Remote.zig +++ b/src/db/Remote.zig @@ -36,6 +36,15 @@ pub const Remote = struct { added: bool, }; + pub const RepoDetails = struct { + id: string, + name: string, + clone_url: string, + description: string, + default_branch: string, + star_count: u32, + }; + pub const findUserBy = _internal.FindByGen(Remote, User, .provider, .id).first; pub fn byKey(alloc: *std.mem.Allocator, comptime key: std.meta.FieldEnum(Remote), value: extras.FieldType(Remote, key)) !?Remote { @@ -120,6 +129,32 @@ pub const Remote = struct { .github => "https://api.github.com", }; } + + pub fn getRepo(self: Remote, alloc: *std.mem.Allocator, repo: string) !RepoDetails { + return self.parseDetails( + alloc, + (try self.apiRequest( + alloc, + null, + try std.mem.join(alloc, "/", switch (self.type) { + .github => &.{ "", "repos", repo }, + }), + )) orelse return error.ApiRequestFail, + ); + } + + fn parseDetails(self: Remote, alloc: *std.mem.Allocator, raw: json.Value) !RepoDetails { + return switch (self.type) { + .github => .{ + .id = try std.fmt.allocPrint(alloc, "{}", .{raw.getT("id", .Int).?}), + .name = raw.getT("name", .String).?, + .clone_url = raw.getT("clone_url", .String).?, + .description = raw.getT("description", .String) orelse "", + .default_branch = raw.getT("default_branch", .String).?, + .star_count = @intCast(u32, raw.getT("stargazers_count", .Int).?), + }, + }; + } }; fn containsPackage(haystack: []const Package, id: string, name: string) bool {