monochange_gitlab 0.1.0

GitLab release payload rendering and publishing for monochange
Documentation
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
#![forbid(clippy::indexing_slicing)]

use std::env;
use std::path::Path;
use std::path::PathBuf;
use std::thread;

use monochange_core::CommitMessage;
use monochange_core::HostedSourceAdapter;
use monochange_core::HostedSourceFeatures;
use monochange_core::HostingCapabilities;
use monochange_core::HostingProviderKind;
use monochange_core::MonochangeError;
use monochange_core::MonochangeResult;
use monochange_core::PreparedChangeset;
use monochange_core::ProviderReleaseNotesSource;
use monochange_core::ReleaseManifest;
use monochange_core::SourceCapabilities;
use monochange_core::SourceChangeRequest;
use monochange_core::SourceChangeRequestOperation;
use monochange_core::SourceChangeRequestOutcome;
use monochange_core::SourceConfiguration;
use monochange_core::SourceProvider;
use monochange_core::SourceReleaseOperation;
use monochange_core::SourceReleaseOutcome;
use monochange_core::SourceReleaseRequest;
use monochange_core::git::git_head_commit;
use monochange_hosting::build_http_client;
use monochange_hosting::get_json;
use monochange_hosting::get_optional_json;
use monochange_hosting::git_checkout_branch;
use monochange_hosting::git_commit_paths;
use monochange_hosting::git_push_branch;
use monochange_hosting::git_stage_paths;
use monochange_hosting::patch_json;
use monochange_hosting::post_json;
use monochange_hosting::put_json;
use monochange_hosting::release_body;
use monochange_hosting::release_pull_request_body;
use monochange_hosting::release_pull_request_branch;
use reqwest::blocking::Client;
use reqwest::header::CONTENT_TYPE;
use reqwest::header::HeaderMap;
use reqwest::header::HeaderValue;
use serde::Deserialize;
use serde::Serialize;
use urlencoding::encode;

/// Return the hosted-source capabilities supported by the GitLab provider.
#[must_use]
pub const fn source_capabilities() -> SourceCapabilities {
	SourceCapabilities {
		draft_releases: false,
		prereleases: false,
		generated_release_notes: false,
		auto_merge_change_requests: false,
		released_issue_comments: false,
		requires_host: false,
	}
}

/// Shared GitLab hosted-source adapter instance used by the workspace.
pub static HOSTED_SOURCE_ADAPTER: GitLabHostedSourceAdapter = GitLabHostedSourceAdapter;

/// Hosted-source adapter for GitLab repositories.
pub struct GitLabHostedSourceAdapter;

impl HostedSourceAdapter for GitLabHostedSourceAdapter {
	fn provider(&self) -> SourceProvider {
		SourceProvider::GitLab
	}

	fn features(&self) -> HostedSourceFeatures {
		HostedSourceFeatures {
			batched_changeset_context_lookup: false,
			released_issue_comments: false,
			release_retarget_sync: false,
		}
	}

	fn annotate_changeset_context(
		&self,
		source: &SourceConfiguration,
		changesets: &mut [PreparedChangeset],
	) {
		annotate_changeset_context(source, changesets);
	}

	fn enrich_changeset_context(
		&self,
		source: &SourceConfiguration,
		changesets: &mut [PreparedChangeset],
	) {
		enrich_changeset_context(source, changesets);
	}
}

/// Return the hosting metadata features available from GitLab changeset context.
#[must_use]
pub const fn gitlab_hosting_capabilities() -> HostingCapabilities {
	HostingCapabilities {
		commit_web_urls: true,
		actor_profiles: false,
		review_request_lookup: false,
		related_issues: false,
		issue_comments: false,
	}
}

/// Extract the host name used for rendered GitLab links.
#[must_use]
pub fn gitlab_host_name(source: &SourceConfiguration) -> Option<String> {
	let host = gitlab_host(source)
		.trim_start_matches("https://")
		.trim_start_matches("http://")
		.split('/')
		.next()
		.unwrap_or_default()
		.trim();
	if host.is_empty() {
		None
	} else {
		Some(host.to_string())
	}
}

/// Build a web URL for a commit on the configured GitLab repository.
#[must_use]
pub fn gitlab_commit_url(source: &SourceConfiguration, sha: &str) -> String {
	format!(
		"{}/{}/{}/-/commit/{sha}",
		gitlab_host(source).trim_end_matches('/'),
		source.owner,
		source.repo
	)
}

/// Apply GitLab provider metadata and commit URLs to prepared changesets.
pub fn annotate_changeset_context(
	source: &SourceConfiguration,
	changesets: &mut [PreparedChangeset],
) {
	let host = gitlab_host_name(source);
	let capabilities = gitlab_hosting_capabilities();

	for changeset in changesets {
		let Some(context) = changeset.context.as_mut() else {
			continue;
		};

		context.provider = HostingProviderKind::GitLab;
		context.host.clone_from(&host);
		context.capabilities = capabilities.clone();

		for revision in [&mut context.introduced, &mut context.last_updated] {
			let Some(revision) = revision.as_mut() else {
				continue;
			};

			if let Some(commit) = revision.commit.as_mut() {
				commit.provider = HostingProviderKind::GitLab;
				commit.host.clone_from(&host);
				commit.url = Some(gitlab_commit_url(source, &commit.sha));
			}

			if let Some(actor) = revision.actor.as_mut() {
				actor.provider = HostingProviderKind::GitLab;
				actor.host.clone_from(&host);
			}
		}
	}
}

/// Enrich changeset context for GitLab-backed workspaces.
///
/// GitLab currently exposes only local annotations, so this delegates to
/// [`annotate_changeset_context`].
#[tracing::instrument(skip_all)]
pub fn enrich_changeset_context(
	source: &SourceConfiguration,
	changesets: &mut [PreparedChangeset],
) {
	annotate_changeset_context(source, changesets);
}

/// Validate that a source configuration is compatible with the GitLab provider.
#[must_use = "the validation result must be checked"]
pub fn validate_source_configuration(source: &SourceConfiguration) -> MonochangeResult<()> {
	if source.releases.draft {
		return Err(MonochangeError::Config(
			"[source.releases].draft is not supported for `provider = \"gitlab\"`".to_string(),
		));
	}
	if source.releases.prerelease {
		return Err(MonochangeError::Config(
			"[source.releases].prerelease is not supported for `provider = \"gitlab\"`".to_string(),
		));
	}
	if source.releases.generate_notes
		|| matches!(
			source.releases.source,
			ProviderReleaseNotesSource::GitHubGenerated
		) {
		return Err(MonochangeError::Config(
			"provider-generated release notes are not supported for `provider = \"gitlab\"`; use `source = \"monochange\"`"
				.to_string(),
		));
	}
	if source.pull_requests.auto_merge {
		return Err(MonochangeError::Config(
			"[source.pull_requests].auto_merge is not supported for `provider = \"gitlab\"`"
				.to_string(),
		));
	}
	Ok(())
}

#[derive(Debug, Serialize)]
struct GitLabReleaseCreatePayload<'a> {
	name: &'a str,
	tag_name: &'a str,
	description: Option<&'a str>,
	#[serde(rename = "ref")]
	ref_: &'a str,
}

#[derive(Debug, Serialize)]
struct GitLabReleaseUpdatePayload<'a> {
	name: &'a str,
	description: Option<&'a str>,
}

#[derive(Debug, Serialize)]
struct GitLabMergeRequestPayload<'a> {
	title: &'a str,
	source_branch: &'a str,
	target_branch: &'a str,
	description: &'a str,
	labels: &'a str,
}

#[derive(Debug, Serialize)]
struct GitLabMergeRequestUpdatePayload<'a> {
	title: &'a str,
	target_branch: &'a str,
	description: &'a str,
	labels: &'a str,
}

#[derive(Debug, Deserialize)]
struct GitLabReleaseResponse {
	web_url: Option<String>,
}

#[derive(Debug, Deserialize)]
struct GitLabMergeRequestResponse {
	iid: u64,
	web_url: Option<String>,
}

#[derive(Debug, Deserialize)]
struct GitLabExistingMergeRequest {
	iid: u64,
	web_url: Option<String>,
	title: String,
	description: Option<String>,
	target_branch: String,
	#[serde(default)]
	labels: Vec<String>,
	sha: Option<String>,
}

fn gitlab_host(source: &SourceConfiguration) -> &str {
	source
		.host
		.as_deref()
		.unwrap_or("https://gitlab.com")
		.trim_end_matches('/')
}

/// Build the public release URL for a tag on the configured GitLab repository.
#[must_use]
pub fn tag_url(source: &SourceConfiguration, tag_name: &str) -> String {
	let host = gitlab_host(source);
	format!(
		"{host}/{}/{}/-/releases/{tag_name}",
		source.owner, source.repo
	)
}

/// Build the comparison URL between two tags on the configured GitLab repository.
#[must_use]
pub fn compare_url(source: &SourceConfiguration, previous_tag: &str, current_tag: &str) -> String {
	let host = gitlab_host(source);
	format!(
		"{host}/{}/{}/-/compare/{previous_tag}...{current_tag}",
		source.owner, source.repo
	)
}

/// Convert releasable targets into provider-specific GitLab release requests.
#[must_use]
pub fn build_release_requests(
	source: &SourceConfiguration,
	manifest: &ReleaseManifest,
) -> Vec<SourceReleaseRequest> {
	manifest
		.release_targets
		.iter()
		.filter(|target| target.release)
		.map(|target| {
			SourceReleaseRequest {
				provider: SourceProvider::GitLab,
				repository: format!("{}/{}", source.owner, source.repo),
				owner: source.owner.clone(),
				repo: source.repo.clone(),
				target_id: target.id.clone(),
				target_kind: target.kind,
				tag_name: target.tag_name.clone(),
				name: target.rendered_title.clone(),
				body: release_body(source, manifest, target),
				draft: source.releases.draft,
				prerelease: source.releases.prerelease,
				generate_release_notes: source.releases.generate_notes,
			}
		})
		.collect()
}

/// Build the release merge request request for the configured GitLab repository.
#[must_use]
pub fn build_release_pull_request_request(
	source: &SourceConfiguration,
	manifest: &ReleaseManifest,
) -> SourceChangeRequest {
	let repository = format!("{}/{}", source.owner, source.repo);
	let title = source.pull_requests.title.clone();
	SourceChangeRequest {
		provider: SourceProvider::GitLab,
		repository: repository.clone(),
		owner: source.owner.clone(),
		repo: source.repo.clone(),
		base_branch: source.pull_requests.base.clone(),
		head_branch: release_pull_request_branch(
			&source.pull_requests.branch_prefix,
			&manifest.command,
		),
		title: title.clone(),
		body: release_pull_request_body(manifest),
		labels: source.pull_requests.labels.clone(),
		auto_merge: source.pull_requests.auto_merge,
		commit_message: CommitMessage {
			subject: title,
			body: None,
		},
	}
}

/// Publish or update all planned GitLab releases for a manifest.
#[tracing::instrument(skip_all)]
#[must_use = "the publish result must be checked"]
pub fn publish_release_requests(
	source: &SourceConfiguration,
	requests: &[SourceReleaseRequest],
) -> MonochangeResult<Vec<SourceReleaseOutcome>> {
	let client = build_http_client("GitLab")?;
	let token = gitlab_token()?;
	let headers = auth_headers(&token)?;
	let api_base = gitlab_api_base(source)?;
	requests
		.iter()
		.map(|request| publish_release_request(&client, &headers, &api_base, source, request))
		.collect()
}

/// Commit, push, and publish the release merge request against GitLab.
#[must_use = "the pull request result must be checked"]
pub fn publish_release_pull_request(
	source: &SourceConfiguration,
	root: &Path,
	request: &SourceChangeRequest,
	tracked_paths: &[PathBuf],
) -> MonochangeResult<SourceChangeRequestOutcome> {
	let lookup_source = source.clone();
	let lookup_request = request.clone();
	let existing_merge_request = thread::spawn(move || {
		let client = build_http_client("GitLab")?;
		let token = gitlab_token()?;
		let headers = auth_headers(&token)?;
		let api_base = gitlab_api_base(&lookup_source)?;
		lookup_existing_merge_request(&client, &headers, &api_base, &lookup_request)
	});
	git_checkout_branch(
		root,
		&request.head_branch,
		"prepare release merge request branch",
	)?;
	git_stage_paths(root, tracked_paths, "stage release merge request files")?;
	git_commit_paths(
		root,
		&request.commit_message,
		"commit release merge request changes",
	)?;
	let head_commit = git_head_commit(root)?;
	let existing = join_existing_merge_request_lookup(existing_merge_request)?;
	let head_matches_existing =
		existing.as_ref().and_then(|mr| mr.sha.as_deref()) == Some(head_commit.as_str());
	if !head_matches_existing {
		git_push_branch(
			root,
			&request.head_branch,
			"push release merge request branch",
		)?;
	}

	let client = build_http_client("GitLab")?;
	let token = gitlab_token()?;
	let headers = auth_headers(&token)?;
	let api_base = gitlab_api_base(source)?;
	publish_merge_request_with_existing(
		&client,
		&headers,
		&api_base,
		request,
		existing.as_ref(),
		&head_commit,
	)
}

fn publish_release_request(
	client: &Client,
	headers: &HeaderMap,
	api_base: &str,
	source: &SourceConfiguration,
	request: &SourceReleaseRequest,
) -> MonochangeResult<SourceReleaseOutcome> {
	let project_id = encode(&format!("{}/{}", request.owner, request.repo)).into_owned();
	let lookup_url = format!(
		"{api_base}/projects/{project_id}/releases/{}",
		encode(&request.tag_name)
	);
	let create_url = format!("{api_base}/projects/{project_id}/releases");
	let update_url = format!(
		"{api_base}/projects/{project_id}/releases/{}",
		encode(&request.tag_name)
	);
	let existing =
		get_optional_json::<GitLabReleaseResponse>(client, headers, &lookup_url, "GitLab")?;
	let response: GitLabReleaseResponse = if existing.is_some() {
		patch_json(
			client,
			headers,
			&update_url,
			&GitLabReleaseUpdatePayload {
				name: &request.name,
				description: request.body.as_deref(),
			},
			"GitLab",
		)?
	} else {
		post_json(
			client,
			headers,
			&create_url,
			&GitLabReleaseCreatePayload {
				name: &request.name,
				tag_name: &request.tag_name,
				description: request.body.as_deref(),
				ref_: &source.pull_requests.base,
			},
			"GitLab",
		)?
	};
	Ok(SourceReleaseOutcome {
		provider: SourceProvider::GitLab,
		repository: request.repository.clone(),
		tag_name: request.tag_name.clone(),
		operation: if existing.is_some() {
			SourceReleaseOperation::Updated
		} else {
			SourceReleaseOperation::Created
		},
		url: response.web_url,
	})
}

#[cfg_attr(not(test), allow(dead_code))]
fn publish_merge_request(
	client: &Client,
	headers: &HeaderMap,
	api_base: &str,
	request: &SourceChangeRequest,
) -> MonochangeResult<SourceChangeRequestOutcome> {
	let existing = lookup_existing_merge_request(client, headers, api_base, request)?;
	publish_merge_request_with_existing(client, headers, api_base, request, existing.as_ref(), "")
}

fn publish_merge_request_with_existing(
	client: &Client,
	headers: &HeaderMap,
	api_base: &str,
	request: &SourceChangeRequest,
	existing: Option<&GitLabExistingMergeRequest>,
	head_commit: &str,
) -> MonochangeResult<SourceChangeRequestOutcome> {
	let labels = request.labels.join(",");
	let labels_match = existing.is_some_and(|merge_request| {
		let mut existing_labels = merge_request.labels.clone();
		existing_labels.sort();
		existing_labels.dedup();
		let mut requested_labels = request.labels.clone();
		requested_labels.sort();
		requested_labels.dedup();
		existing_labels == requested_labels
	});
	let content_matches = existing.is_some_and(|merge_request| {
		merge_request.title == request.title
			&& merge_request.description.as_deref().unwrap_or_default() == request.body
			&& merge_request.target_branch == request.base_branch
	});
	let head_matches_existing =
		existing.and_then(|merge_request| merge_request.sha.as_deref()) == Some(head_commit);
	let project_id = encode(&format!("{}/{}", request.owner, request.repo)).into_owned();
	let create_url = format!("{api_base}/projects/{project_id}/merge_requests");
	let response: GitLabMergeRequestResponse = if let Some(existing_mr) = &existing {
		if content_matches && labels_match {
			GitLabMergeRequestResponse {
				iid: existing_mr.iid,
				web_url: existing_mr.web_url.clone(),
			}
		} else {
			let update_url = format!(
				"{api_base}/projects/{project_id}/merge_requests/{}",
				existing_mr.iid
			);
			let update_payload = GitLabMergeRequestUpdatePayload {
				title: &request.title,
				target_branch: &request.base_branch,
				description: &request.body,
				labels: &labels,
			};

			put_json(client, headers, &update_url, &update_payload, "GitLab")?
		}
	} else {
		post_json(
			client,
			headers,
			&create_url,
			&GitLabMergeRequestPayload {
				title: &request.title,
				source_branch: &request.head_branch,
				target_branch: &request.base_branch,
				description: &request.body,
				labels: &labels,
			},
			"GitLab",
		)?
	};
	Ok(SourceChangeRequestOutcome {
		provider: SourceProvider::GitLab,
		repository: request.repository.clone(),
		number: response.iid,
		head_branch: request.head_branch.clone(),
		operation: if existing.is_none() {
			SourceChangeRequestOperation::Created
		} else if content_matches && labels_match && head_matches_existing {
			SourceChangeRequestOperation::Skipped
		} else {
			SourceChangeRequestOperation::Updated
		},
		url: response.web_url,
	})
}

fn lookup_existing_merge_request(
	client: &Client,
	headers: &HeaderMap,
	api_base: &str,
	request: &SourceChangeRequest,
) -> MonochangeResult<Option<GitLabExistingMergeRequest>> {
	let project_id = encode(&format!("{}/{}", request.owner, request.repo)).into_owned();
	let list_url = format!(
		"{api_base}/projects/{project_id}/merge_requests?state=opened&source_branch={}&target_branch={}",
		encode(&request.head_branch),
		encode(&request.base_branch),
	);
	Ok(
		get_json::<Vec<GitLabExistingMergeRequest>>(client, headers, &list_url, "GitLab")?
			.into_iter()
			.next(),
	)
}

fn join_existing_merge_request_lookup(
	handle: thread::JoinHandle<MonochangeResult<Option<GitLabExistingMergeRequest>>>,
) -> MonochangeResult<Option<GitLabExistingMergeRequest>> {
	handle.join().map_err(|_| {
		MonochangeError::Config("failed to join GitLab merge request lookup thread".to_string())
	})?
}

#[cfg(test)]
fn gitlab_client() -> MonochangeResult<Client> {
	build_http_client("GitLab")
}

fn gitlab_token() -> MonochangeResult<String> {
	env::var("GITLAB_TOKEN")
		.or_else(|_| env::var("GL_TOKEN"))
		.map_err(|_| {
			MonochangeError::Config(
				"set `GITLAB_TOKEN` (or `GL_TOKEN`) before running GitLab automation".to_string(),
			)
		})
}

#[allow(clippy::unnecessary_wraps)]
fn gitlab_api_base(source: &SourceConfiguration) -> MonochangeResult<String> {
	if let Some(api_url) = &source.api_url {
		return Ok(api_url.trim_end_matches('/').to_string());
	}
	let host = source
		.host
		.as_deref()
		.unwrap_or("https://gitlab.com")
		.trim_end_matches('/');
	Ok(format!("{host}/api/v4"))
}

fn auth_headers(token: &str) -> MonochangeResult<HeaderMap> {
	let mut headers = HeaderMap::new();
	headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
	headers.insert(
		"PRIVATE-TOKEN",
		HeaderValue::from_str(token).map_err(|error| {
			MonochangeError::Config(format!("invalid GitLab token header value: {error}"))
		})?,
	);
	Ok(headers)
}

#[cfg(test)]
mod __tests;