File: gitlab-parse.c

package info (click to toggle)
gcli 2.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,652 kB
  • sloc: ansic: 27,086; sh: 678; makefile: 545; perl: 392; yacc: 261; lex: 60
file content (377 lines) | stat: -rw-r--r-- 12,606 bytes parent folder | download
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * Copyright Nico Sonack <nsonack@herrhotzenplotz.de>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided
 * with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <templates/gitlab/forks.h>
#include <templates/gitlab/issues.h>
#include <templates/gitlab/labels.h>
#include <templates/gitlab/merge_requests.h>
#include <templates/gitlab/milestones.h>
#include <templates/gitlab/pipelines.h>
#include <templates/gitlab/releases.h>
#include <templates/gitlab/repos.h>
#include <templates/gitlab/snippets.h>

#include <string.h>

#include <gcli/ctx.h>
#include <gcli/gitlab/api.h>
#include <gcli/port/util.h>

#include "unit.h"

static gcli_forge_type
get_gitlab_forge_type(struct gcli_ctx *ctx)
{
	(void) ctx;
	return GCLI_FORGE_GITLAB;
}

static struct gcli_ctx *
test_context(UNIT_CTX)
{
	struct gcli_ctx *ctx;
	REQUIRE(gcli_init(&ctx, get_gitlab_forge_type, NULL, NULL) == NULL);
	return ctx;
}

static FILE *
open_sample(UNIT_CTX, char const *const name)
{
	FILE *r;
	char p[4096] = {0};

	snprintf(p, sizeof p, "%s/unit/samples/%s", TESTSRCDIR, name);

	REQUIRE((r = fopen(p, "r")) != NULL);

	return r;
}

DEFINE_TESTCASE(gitlab_simple_merge_request)
{
	struct json_stream stream = {0};
	struct gcli_ctx *ctx = test_context(UNIT_CTX_VAR);
	FILE *f = open_sample(UNIT_CTX_VAR, "gitlab_simple_merge_request.json");
	struct gcli_pull pull = {0};

	json_open_stream(&stream, f);
	REQUIRE(parse_gitlab_mr(ctx, &stream, &pull) == 0);

	CHECK_STREQ(pull.author, "herrhotzenplotz");
	CHECK_STREQ(pull.state, "merged");
	CHECK_STREQ(pull.title, "Fix test suite");
	CHECK_STREQ(pull.body, "This finally fixes the broken test suite");
	CHECK_EQ(pull.created_at, 1693525070);
	CHECK(pull.commits_link == NULL);
	CHECK_STREQ(pull.head_label, "fix-test-suite");
	CHECK_STREQ(pull.base_label, "trunk");
	CHECK_STREQ(pull.head_sha, "3eab596a6806434e4a34bb19de12307ab1217af3");
	CHECK(pull.milestone == NULL);
	CHECK(pull.id == 246912053);
	CHECK(pull.number == 216);
	CHECK(pull.comments == 0); // not supported
	CHECK(pull.additions == 0); // not supported
	CHECK(pull.deletions == 0); // not supported
	CHECK(pull.commits == 0); // not supported
	CHECK(pull.changed_files == 0); // not supported
	CHECK(pull.head_pipeline_id == 989409992);
	CHECK(pull.labels_size == 0);
	CHECK(pull.labels == NULL);
	CHECK(pull.merged == false);
	CHECK(pull.mergeable == true);
	CHECK(pull.draft == false);

	json_close(&stream);
	gcli_pull_free(&pull);
	gcli_destroy(&ctx);
}

DEFINE_TESTCASE(gitlab_simple_issue)
{
	struct json_stream stream = {0};
	struct gcli_ctx *ctx = test_context(UNIT_CTX_VAR);
	FILE *f = open_sample(UNIT_CTX_VAR, "gitlab_simple_issue.json");
	struct gcli_issue issue = {0};

	json_open_stream(&stream, f);
	REQUIRE(parse_gitlab_issue(ctx, &stream, &issue) == 0);

	CHECK(issue.number == 193);
	CHECK_STREQ(issue.title, "Make notifications API use a list struct containing both the ptr and size");
	CHECK_EQ(issue.created_at, 1691952185);
	CHECK_STREQ(issue.author, "herrhotzenplotz");
	CHECK_STREQ(issue.state, "closed");
	CHECK(issue.comments == 2);
	CHECK(issue.locked == false);
	CHECK_STREQ(issue.body, "That would make some of the code much cleaner");
	CHECK(issue.labels_size == 1);
	CHECK_STREQ(issue.labels[0], "good-first-issue");
	CHECK(issue.assignees == NULL);
	CHECK(issue.assignees_size == 0);
	CHECK(issue.is_pr == 0);
	CHECK(issue.milestone == NULL);

	json_close(&stream);
	gcli_issue_free(&issue);
	gcli_destroy(&ctx);
}

DEFINE_TESTCASE(gitlab_simple_label)
{
	struct json_stream stream = {0};
	struct gcli_ctx *ctx = test_context(UNIT_CTX_VAR);
	FILE *f = open_sample(UNIT_CTX_VAR, "gitlab_simple_label.json");
	struct gcli_label label = {0};

	json_open_stream(&stream, f);
	REQUIRE(parse_gitlab_label(ctx, &stream, &label) == 0);

	CHECK(label.id == 24376073);
	CHECK_STREQ(label.name, "bug");
	CHECK_STREQ(label.description, "Something isn't working as expected");
	CHECK(label.colour == 0xD73A4A00);

	json_close(&stream);
	gcli_free_label(&label);
	gcli_destroy(&ctx);
}

DEFINE_TESTCASE(gitlab_simple_release)
{
	struct json_stream stream = {0};
	struct gcli_ctx *ctx = test_context(UNIT_CTX_VAR);
	FILE *f = open_sample(UNIT_CTX_VAR, "gitlab_simple_release.json");
	struct gcli_release release = {0};

	json_open_stream(&stream, f);
	REQUIRE(parse_gitlab_release(ctx, &stream, &release) == 0);

	/* NOTE(Nico): this silly hack is needed as the fixup is only
	 * applied internally when you fetch the release list using the
	 * public library API. */
	gitlab_fixup_release_assets(ctx, &release);

	/* NOTE(Nico): on gitlab this is the tag name */
	CHECK_STREQ(release.id, "1.2.0");
	CHECK(release.assets_size == 4);
	{
		CHECK_STREQ(release.assets[0].name, "gcli-1.2.0.zip");
		CHECK_STREQ(release.assets[0].url,
		            "https://gitlab.com/herrhotzenplotz/gcli/-/archive/1.2.0/gcli-1.2.0.zip");
	}
	{
		CHECK_STREQ(release.assets[1].name, "gcli-1.2.0.tar.gz");
		CHECK_STREQ(release.assets[1].url,
		            "https://gitlab.com/herrhotzenplotz/gcli/-/archive/1.2.0/gcli-1.2.0.tar.gz");
	}
	{
		CHECK_STREQ(release.assets[2].name, "gcli-1.2.0.tar.bz2");
		CHECK_STREQ(release.assets[2].url,
		            "https://gitlab.com/herrhotzenplotz/gcli/-/archive/1.2.0/gcli-1.2.0.tar.bz2");
	}
	{
		CHECK_STREQ(release.assets[3].name, "gcli-1.2.0.tar");
		CHECK_STREQ(release.assets[3].url,
		            "https://gitlab.com/herrhotzenplotz/gcli/-/archive/1.2.0/gcli-1.2.0.tar");
	}

	CHECK_STREQ(release.name, "1.2.0");
	CHECK_STREQ(release.body, "# Version 1.2.0\n\nThis is version 1.2.0 of gcli.\n\n## Notes\n\nPlease test and report bugs.\n\nYou can download autotoolized tarballs at: https://herrhotzenplotz.de/gcli/releases/gcli-1.2.0/\n\n## Bug Fixes\n\n- Fix compile error when providing --with-libcurl without any arguments\n- Fix memory leaks in string processing functions\n- Fix missing nul termination in read-file function\n- Fix segmentation fault when clearing the milestone of a PR on Gitea\n- Fix missing documentation for milestone action in issues and pulls\n- Set the 'merged' flag properly when showing Gitlab merge requests\n\n## New features\n\n- Add a config subcommand for managing ssh keys (see gcli-config(1))\n- Show number of comments/notes in list of issues and PRs\n- Add support for milestone management in pull requests\n");
	CHECK_STREQ(release.author, "herrhotzenplotz");
	CHECK_EQ(release.date, 1691740566);
	CHECK(release.upload_url == NULL);
	CHECK(release.draft == false);
	CHECK(release.prerelease == false);

	json_close(&stream);
	gcli_release_free(&release);
	gcli_destroy(&ctx);
}

DEFINE_TESTCASE(gitlab_simple_fork)
{
	struct json_stream stream = {0};
	struct gcli_ctx *ctx = test_context(UNIT_CTX_VAR);
	FILE *f = open_sample(UNIT_CTX_VAR, "gitlab_simple_fork.json");
	struct gcli_fork fork = {0};

	json_open_stream(&stream, f);
	REQUIRE(parse_gitlab_fork(ctx, &stream, &fork) == 0);

	CHECK_STREQ(fork.full_name, "gjnoonan/gcli");
	CHECK_STREQ(fork.owner, "gjnoonan");
	CHECK_EQ(fork.date, 1664718860);
	CHECK(fork.forks == 0);

	json_close(&stream);
	gcli_fork_free(&fork);
	gcli_destroy(&ctx);
}

DEFINE_TESTCASE(gitlab_simple_milestone)
{
	struct json_stream stream = {0};
	struct gcli_ctx *ctx = test_context(UNIT_CTX_VAR);
	FILE *f = open_sample(UNIT_CTX_VAR, "gitlab_simple_milestone.json");
	struct gcli_milestone milestone = {0};

	json_open_stream(&stream, f);
	REQUIRE(parse_gitlab_milestone(ctx, &stream, &milestone) == 0);

	CHECK(milestone.id == 2975318);
	CHECK_STREQ(milestone.title, "Version 2");
	CHECK_STREQ(milestone.state, "active");
	CHECK_STREQ(milestone.description,
	                "Things that need to be done for version 2");
	CHECK_EQ(milestone.created_at, 1675624100);
	CHECK_EQ(milestone.due_date, 0);
	CHECK_EQ(milestone.updated_at, 1675624100);
	CHECK(milestone.expired == false);

	json_close(&stream);
	gcli_free_milestone(&milestone);
	gcli_destroy(&ctx);

	/* Ignore open issues and closed issues as they are
	 * github/gitea-specific */
}

DEFINE_TESTCASE(gitlab_simple_pipeline)
{
	struct json_stream stream = {0};
	struct gcli_ctx *ctx = test_context(UNIT_CTX_VAR);
	FILE *f = open_sample(UNIT_CTX_VAR, "gitlab_simple_pipeline.json");
	struct gitlab_pipeline pipeline = {0};

	json_open_stream(&stream, f);
	REQUIRE(parse_gitlab_pipeline(ctx, &stream, &pipeline) == 0);

	CHECK(pipeline.id == 989897020);
	CHECK_STREQ(pipeline.status, "failed");
	CHECK_EQ(pipeline.created_at, 1693665020);
	CHECK_EQ(pipeline.updated_at, 1693665100);
	CHECK_STREQ(pipeline.ref, "refs/merge-requests/219/head");
	CHECK_STREQ(pipeline.sha, "742affb88a297a6b34201ad61c8b5b72ec6eb679");
	CHECK_STREQ(pipeline.source, "merge_request_event");

	json_close(&stream);
	gitlab_pipeline_free(&pipeline);
	gcli_destroy(&ctx);
}

DEFINE_TESTCASE(gitlab_simple_repo)
{
	struct json_stream stream = {0};
	struct gcli_ctx *ctx = test_context(UNIT_CTX_VAR);
	FILE *f = open_sample(UNIT_CTX_VAR, "gitlab_simple_repo.json");
	struct gcli_repo repo = {0};

	json_open_stream(&stream, f);
	REQUIRE(parse_gitlab_repo(ctx, &stream, &repo) == 0);

	CHECK(repo.id == 34707535);
	CHECK_STREQ(repo.full_name, "herrhotzenplotz/gcli");
	CHECK_STREQ(repo.name, "gcli");
	CHECK_STREQ(repo.owner, "herrhotzenplotz");
	CHECK_EQ(repo.date, 1647968279);
	CHECK_STREQ(repo.visibility, "public");
	CHECK(repo.is_fork == false);

	json_close(&stream);
	gcli_repo_free(&repo);
	gcli_destroy(&ctx);
}

DEFINE_TESTCASE(gitlab_simple_snippet)
{
	struct json_stream stream = {0};
	struct gcli_ctx *ctx = test_context(UNIT_CTX_VAR);
	FILE *f = open_sample(UNIT_CTX_VAR, "gitlab_simple_snippet.json");
	struct gcli_gitlab_snippet snippet = {0};

	json_open_stream(&stream, f);
	REQUIRE(parse_gitlab_snippet(ctx, &stream, &snippet) == 0);

	CHECK(snippet.id == 2141655);
	CHECK_STREQ(snippet.title, "darcy-weisbach SPARC64");
	CHECK_STREQ(snippet.filename, "darcy-weisbach SPARC64");
	CHECK_STREQ(snippet.date, "2021-06-28T15:47:36.214Z");
	CHECK_STREQ(snippet.author, "herrhotzenplotz");
	CHECK_STREQ(snippet.visibility, "public");
	CHECK_STREQ(snippet.raw_url, "https://gitlab.com/-/snippets/2141655/raw");

	json_close(&stream);
	gcli_gitlab_snippet_free(&snippet);
	gcli_destroy(&ctx);
}

DEFINE_TESTCASE(gitlab_error_token_expired)
{
	struct gcli_ctx *ctx = test_context(UNIT_CTX_VAR);
	struct gcli_fetch_buffer buffer = {0};
	char const *errmsg = NULL;

	buffer.length = gcli_read_file(TESTSRCDIR"unit/samples/gitlab_token_expired.json",
	                               &buffer.data);

	REQUIRE(buffer.length);
	errmsg = gitlab_api_error_string(ctx, &buffer);

	CHECK_STREQ(errmsg, "Token has expired.");
}

DEFINE_TESTCASE(gitlab_error_unauthorised)
{
	struct gcli_ctx *ctx = test_context(UNIT_CTX_VAR);
	struct gcli_fetch_buffer buffer = {0};
	char const *errmsg = NULL;

	buffer.length = gcli_read_file(TESTSRCDIR"/unit/samples/gitlab_error_unauthorised.json",
	                               &buffer.data);

	REQUIRE(buffer.length);
	errmsg = gitlab_api_error_string(ctx, &buffer);

	CHECK_STREQ(errmsg, "401 Unauthorized");
}

TESTSUITE
{
	TESTCASE(gitlab_simple_fork);
	TESTCASE(gitlab_simple_issue);
	TESTCASE(gitlab_simple_label);
	TESTCASE(gitlab_simple_merge_request);
	TESTCASE(gitlab_simple_milestone);
	TESTCASE(gitlab_simple_pipeline);
	TESTCASE(gitlab_simple_release);
	TESTCASE(gitlab_simple_repo);
	TESTCASE(gitlab_simple_snippet);
	TESTCASE(gitlab_error_token_expired);
	TESTCASE(gitlab_error_unauthorised);
}