summaryrefslogtreecommitdiff
path: root/src/github.c
blob: 38c28a0e6b47e606a442b392564fe6a051484343 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//
// Created by Anshul Gupta on 4/4/25.
//

#include "github.h"

#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#include <cjson/cJSON.h>
#include <curl/curl.h>

#include "queries/identity.h"
#include "queries/list_repos.h"

#include "buffer.h"
#include "github_types.h"

struct gh_impl {
	struct github_ctx ctx;
	CURL *curl;
};

static CURLcode gh_impl_send(const struct gh_impl *client, const char *query,
			     cJSON *args, buffer_t *buf);

static int gh_handle_error(const cJSON *root);

github_client *github_client_new(const struct github_ctx ctx)
{
	struct gh_impl *c = malloc(sizeof(*c));
	if (!c)
		return NULL;

	c->ctx.endpoint = strdup(ctx.endpoint);
	c->ctx.token = strdup(ctx.token);
	c->ctx.user_agent = strdup(ctx.user_agent);
	c->curl = curl_easy_init();
	return c;
}

github_client *github_client_dup(github_client *client)
{
	struct gh_impl *c = client;
	if (!c)
		return NULL;

	struct gh_impl *dup = malloc(sizeof(*dup));
	if (!dup)
		return NULL;

	dup->ctx.endpoint = strdup(c->ctx.endpoint);
	dup->ctx.token = strdup(c->ctx.token);
	dup->ctx.user_agent = strdup(c->ctx.user_agent);
	dup->curl = curl_easy_duphandle(c->curl);

	return dup;
}

void github_client_free(github_client *client)
{
	struct gh_impl *c = client;
	if (!c)
		return;
	curl_easy_cleanup(c->curl);
	free((char *) c->ctx.endpoint);
	free((char *) c->ctx.token);
	free((char *) c->ctx.user_agent);
	free(c);
}

char *github_client_identity(const github_client *client)
{
	char *login = NULL;
	const struct gh_impl *c = client;
	buffer_t buf = buffer_new(4096);

	const CURLcode ret = gh_impl_send(c, identity, NULL, &buf);
	if (ret != CURLE_OK) {
		fprintf(stderr, "Failed to send request: %s\n",
			curl_easy_strerror(ret));
		goto fail;
	}

	// Parse the response
	cJSON *root = cJSON_Parse((const char *) buf.data);
	if (!root) {
		const char *err = cJSON_GetErrorPtr();
		if (err)
			fprintf(stderr, "Error parsing response: %s\n", err);
		goto fail;
	}

	// Check for errors
	if (gh_handle_error(root) < 0)
		goto fail1;

	// Get login from json
	login = identity_from_json(root);

fail1:
	cJSON_Delete(root);
fail:
	buffer_free(buf);
	return login;
}

int github_client_list_user_repos(const github_client *client,
				  const char *username, const char *after,
				  struct list_repos_res *res)
{
	int status = 0;
	const struct gh_impl *c = client;
	buffer_t buf = buffer_new(4096);

	cJSON *args = cJSON_CreateObject();
	if (!args) {
		status = -1;
		goto end;
	}
	cJSON_AddItemToObject(args, "username", cJSON_CreateString(username));
	cJSON_AddItemToObject(args, "after", cJSON_CreateString(after));

	const CURLcode ret = gh_impl_send(c, list_repos, args, &buf);
	if (ret != CURLE_OK) {
		fprintf(stderr, "Failed to send request: %s\n",
			curl_easy_strerror(ret));
		status = -1;
		goto end;
	}

	// Parse the response
	cJSON *root = cJSON_Parse((const char *) buf.data);
	if (!root) {
		const char *err = cJSON_GetErrorPtr();
		if (err)
			fprintf(stderr, "Error parsing response: %s\n", err);
		status = -1;
		goto end;
	}

	// Check for errors
	if (gh_handle_error(root) < 0) {
		status = -1;
		goto end;
	}

	// Convert json to struct
	if (list_repos_from_json(root, res) < 0) {
		fprintf(stderr, "Failed to parse response\n");
		status = -1;
	}

end:
	buffer_free(buf);
	return status;
}

/**
 * Wraps the query in a JSON object with a "query" key and an optional
 * "variables" key
 * @param query GraphQL query
 * @param args GraphQL arguments
 * @return JSON string
 */
static char *wrap_query(const char *query, cJSON *args)
{
	cJSON *root = NULL, *query_str = NULL;
	char *str = NULL;

	root = cJSON_CreateObject();
	if (!root)
		return NULL;

	query_str = cJSON_CreateString(query);
	if (!query_str)
		goto end;

	// Transfer ownership of the string to root
	cJSON_AddItemToObject(root, "query", query_str);

	// Add the args object if it exists
	if (args)
		cJSON_AddItemToObject(root, "variables", args);

	str = cJSON_Print(root);
end:
	cJSON_Delete(root);
	return str;
}

static size_t write_data(const void *ptr, const size_t size, size_t nmemb,
			 void *stream)
{
	(void) size; // unused

	buffer_t *buf = stream;
	buffer_append(buf, ptr, nmemb);
	return nmemb;
}

/**
 * Send a GraphQL query to the GitHub API
 * @param client Github Client
 * @param query GraphQL query
 * @return CURLcode
 */
static CURLcode gh_impl_send(const struct gh_impl *client, const char *query,
			     cJSON *args, buffer_t *buf)
{
	struct curl_slist *headers = NULL;
	char auth[1024];

	// Set the URL
	curl_easy_setopt(client->curl, CURLOPT_URL, client->ctx.endpoint);

	// Set the authorization header
	snprintf(auth, sizeof(auth), "Authorization: bearer %s",
		 client->ctx.token);
	headers = curl_slist_append(headers, auth);
	// Set the content type to JSON
	headers = curl_slist_append(headers, "Content-Type: application/json");
	curl_easy_setopt(client->curl, CURLOPT_HTTPHEADER, headers);

	// Set user agent
	curl_easy_setopt(client->curl, CURLOPT_USERAGENT,
			 client->ctx.user_agent);

	// Set the request type to POST
	curl_easy_setopt(client->curl, CURLOPT_CUSTOMREQUEST, "POST");

	// Prepare request body
	char *wrapped_query = wrap_query(query, args);

	// Set the request body
	curl_easy_setopt(client->curl, CURLOPT_POSTFIELDS, wrapped_query);
	curl_easy_setopt(client->curl, CURLOPT_POSTFIELDSIZE,
			 strlen(wrapped_query));

	// Set the write function to capture the response
	curl_easy_setopt(client->curl, CURLOPT_WRITEFUNCTION, write_data);
	curl_easy_setopt(client->curl, CURLOPT_WRITEDATA, (void *) buf);

	// Perform the request
	const CURLcode ret = curl_easy_perform(client->curl);

	// Append null terminator to the buffer
	if (ret == CURLE_OK)
		buffer_append(buf, "\0", 1);

	// Cleanup
	free(wrapped_query);
	curl_slist_free_all(headers);

	return ret;
}

/**
 * Handle errors in the response.
 * Will check for the "errors" key in the response and print the error messages.
 * Returns failure if any errors are found.
 * @param root Parsed JSON response
 * @return 0 on success (no errors), -1 on failure
 */
static int gh_handle_error(const cJSON *root)
{
	// Check for errors
	cJSON *errors = cJSON_GetObjectItemCaseSensitive(root, "errors");
	if (!errors || !cJSON_IsArray(errors)) {
		// No errors
		return 0;
	}

	cJSON *err;
	cJSON_ArrayForEach(err, errors)
	{
		// Get the error message
		cJSON *message = cJSON_GetObjectItemCaseSensitive(err,
								  "message");
		if (message && cJSON_IsString(message)) {
			fprintf(stderr, "Github Error: %s\n",
				message->valuestring);
		} else {
			fprintf(stderr, "Github Error: Unknown error\n");
		}
	}

	return -1;
}