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
645
//! GitHub Git Database API.
//!
//! See: [GitHub REST API Documentation](https://docs.github.com/en/rest/git?apiVersion=2022-11-28)
use crate::api::repos::RepoRef;
use crate::models::commits::GitCommitObject;
use crate::models::git::{CreateTreeEntry, CreatedBlob, GitBlob, GitTree};
use crate::models::repos::{CommitAuthor, GitTag, Ref};
use crate::params::repos::Reference;
use crate::{Octocrab, Result};
/// Handler for GitHub's Git database API.
///
/// Created with [`Octocrab::git`].
pub struct GitHandler<'octo> {
crab: &'octo Octocrab,
repo: RepoRef,
}
impl<'octo> GitHandler<'octo> {
pub(crate) fn new(crab: &'octo Octocrab, repo: RepoRef) -> Self {
Self { crab, repo }
}
// -----------------------------------------------------------------------
// Blobs
// -----------------------------------------------------------------------
/// Gets a Git blob from the repository.
///
/// See: [GitHub API Documentation](https://docs.github.com/en/rest/git/blobs?apiVersion=2022-11-28#get-a-blob)
///
/// # Examples
///
/// ```no_run
/// # async fn run(octocrab: &octocrab::Octocrab) -> octocrab::Result<()> {
/// let blob = octocrab
/// .git("owner", "repo")
/// .get_blob("3a0f86fb8db8eea7ccbb9a95f325ddbedfb25e15")
/// .await?;
/// # Ok(())
/// # }
/// ```
pub async fn get_blob(&self, file_sha: impl Into<String>) -> Result<GitBlob> {
let route = format!("/{}/git/blobs/{}", self.repo, file_sha.into());
self.crab.get(route, None::<&()>).await
}
/// Creates a new Git blob in the repository.
///
/// See: [GitHub API Documentation](https://docs.github.com/en/rest/git/blobs?apiVersion=2022-11-28#create-a-blob)
///
/// # Examples
///
/// ```no_run
/// # async fn run(octocrab: &octocrab::Octocrab) -> octocrab::Result<()> {
/// let blob = octocrab
/// .git("owner", "repo")
/// .create_blob("Hello World")
/// .encoding("utf-8")
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn create_blob(&self, content: impl Into<String>) -> CreateBlobBuilder<'octo> {
CreateBlobBuilder::new(self.crab, self.repo.clone(), content.into())
}
// -----------------------------------------------------------------------
// Commits
// -----------------------------------------------------------------------
/// Gets a Git commit object from the repository.
///
/// See: [GitHub API Documentation](https://docs.github.com/en/rest/git/commits?apiVersion=2022-11-28#get-a-commit-object)
///
/// # Examples
///
/// ```no_run
/// # async fn run(octocrab: &octocrab::Octocrab) -> octocrab::Result<()> {
/// let commit = octocrab
/// .git("owner", "repo")
/// .get_commit("7638417db6d59f3c431d3e1f261cc637155684cd")
/// .await?;
/// # Ok(())
/// # }
/// ```
pub async fn get_commit(&self, commit_sha: impl Into<String>) -> Result<GitCommitObject> {
let route = format!("/{}/git/commits/{}", self.repo, commit_sha.into());
self.crab.get(route, None::<&()>).await
}
/// Creates a new Git commit object in the repository.
///
/// See: [GitHub API Documentation](https://docs.github.com/en/rest/git/commits?apiVersion=2022-11-28#create-a-commit)
///
/// # Examples
///
/// ```no_run
/// # async fn run(octocrab: &octocrab::Octocrab) -> octocrab::Result<()> {
/// let commit = octocrab
/// .git("owner", "repo")
/// .create_commit("commit message", "691272480426f78a0138979dd3ce63b77f706feb")
/// .parents(vec!["1acc419d4d6a9ce985db7be48c6349a0475975b5".to_string()])
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn create_commit(
&self,
message: impl Into<String>,
tree: impl Into<String>,
) -> CreateGitCommitObjectBuilder<'octo> {
CreateGitCommitObjectBuilder::new(self.crab, self.repo.clone(), message.into(), tree.into())
}
// -----------------------------------------------------------------------
// References
// -----------------------------------------------------------------------
/// Fetches information about a Git reference.
///
/// See: [GitHub API Documentation](https://docs.github.com/en/rest/git/refs?apiVersion=2022-11-28#get-a-reference)
///
/// # Examples
///
/// ```no_run
/// # async fn run(octocrab: &octocrab::Octocrab) -> octocrab::Result<()> {
/// use octocrab::params::repos::Reference;
///
/// let master = octocrab
/// .git("owner", "repo")
/// .get_ref(&Reference::Branch("master".to_string()))
/// .await?;
/// # Ok(())
/// # }
/// ```
pub async fn get_ref(&self, reference: &Reference) -> Result<Ref> {
let route = format!(
"/{repo}/git/ref/{reference}",
repo = self.repo,
reference = reference.ref_url(),
);
self.crab.get(route, None::<&()>).await
}
/// Creates a new Git reference in the repository.
///
/// See: [GitHub API Documentation](https://docs.github.com/en/rest/git/refs?apiVersion=2022-11-28#create-a-reference)
///
/// # Examples
///
/// ```no_run
/// # async fn run(octocrab: &octocrab::Octocrab) -> octocrab::Result<()> {
/// use octocrab::params::repos::Reference;
///
/// let master = octocrab
/// .git("owner", "repo")
/// .create_ref(
/// &Reference::Tag("1.0".to_string()),
/// "c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc",
/// )
/// .await?;
/// # Ok(())
/// # }
/// ```
pub async fn create_ref(&self, reference: &Reference, sha: impl Into<String>) -> Result<Ref> {
let route = format!("/{}/git/refs", self.repo);
self.crab
.post(
route,
Some(&serde_json::json!({
"ref": reference.full_ref_url(),
"sha": sha.into(),
})),
)
.await
}
/// Updates a Git reference in the repository.
///
/// See: [GitHub API Documentation](https://docs.github.com/en/rest/git/refs?apiVersion=2022-11-28#update-a-reference)
///
/// # Examples
///
/// ```no_run
/// # async fn run(octocrab: &octocrab::Octocrab) -> octocrab::Result<()> {
/// use octocrab::params::repos::Reference;
///
/// let updated = octocrab
/// .git("owner", "repo")
/// .update_ref(
/// &Reference::Branch("feature-a".to_string()),
/// "aa218f56b14c9653891f9e74264a383fa43fefbd",
/// )
/// .force(true)
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn update_ref(
&self,
reference: &Reference,
sha: impl Into<String>,
) -> UpdateRefBuilder<'octo> {
UpdateRefBuilder::new(self.crab, self.repo.clone(), reference.clone(), sha.into())
}
/// Deletes an existing Git reference from the repository.
///
/// See: [GitHub API Documentation](https://docs.github.com/en/rest/git/refs?apiVersion=2022-11-28#delete-a-reference)
///
/// # Examples
///
/// ```no_run
/// # async fn run(octocrab: &octocrab::Octocrab) -> octocrab::Result<()> {
/// use octocrab::params::repos::Reference;
///
/// octocrab
/// .git("owner", "repo")
/// .delete_ref(&Reference::Branch("temporary-branch".to_string()))
/// .await?;
/// # Ok(())
/// # }
/// ```
pub async fn delete_ref(&self, reference: &Reference) -> Result<()> {
let route = format!(
"/{repo}/git/refs/{ref}",
repo = self.repo,
ref = reference.ref_url()
);
crate::map_github_error(self.crab._delete(route, None::<&()>).await?)
.await
.map(drop)
}
/// Lists Git references that match the supplied sub-string.
///
/// See: [GitHub API Documentation](https://docs.github.com/en/rest/git/refs?apiVersion=2022-11-28#list-matching-references)
///
/// # Examples
///
/// ```no_run
/// # async fn run(octocrab: &octocrab::Octocrab) -> octocrab::Result<()> {
/// let refs = octocrab
/// .git("owner", "repo")
/// .list_matching_refs("heads")
/// .await?;
/// # Ok(())
/// # }
/// ```
pub async fn list_matching_refs(&self, reference: impl AsRef<str>) -> Result<Vec<Ref>> {
let ref_path = reference
.as_ref()
.strip_prefix("refs/")
.unwrap_or(reference.as_ref());
let route = format!("/{}/git/matching-refs/{}", self.repo, ref_path);
self.crab.get(route, None::<&()>).await
}
// -----------------------------------------------------------------------
// Tags
// -----------------------------------------------------------------------
/// Fetches information about a Git tag with the given `tag_sha`.
///
/// See: [GitHub API Documentation](https://docs.github.com/en/rest/git/tags?apiVersion=2022-11-28#get-a-tag)
///
/// # Examples
///
/// ```no_run
/// # async fn run(octocrab: &octocrab::Octocrab) -> octocrab::Result<()> {
/// let tag = octocrab
/// .git("owner", "repo")
/// .get_tag("940bd336248efae0f9ee5bc7b2d5c985887b16ac")
/// .await?;
/// # Ok(())
/// # }
/// ```
pub async fn get_tag(&self, tag_sha: impl Into<String>) -> Result<GitTag> {
let route = format!(
"/{repo}/git/tags/{tag_sha}",
repo = self.repo,
tag_sha = tag_sha.into(),
);
self.crab.get(route, None::<&()>).await
}
/// Creates a new Git tag object in the repository.
///
/// See: [GitHub API Documentation](https://docs.github.com/en/rest/git/tags?apiVersion=2022-11-28#create-a-tag-object)
///
/// # Examples
///
/// ```no_run
/// # async fn run(octocrab: &octocrab::Octocrab) -> octocrab::Result<()> {
/// let tag = octocrab
/// .git("owner", "repo")
/// .create_tag(
/// "v0.0.1",
/// "initial version",
/// "c3d0be41ecbe669545ee3e94d31ed9a4bc91ee3c",
/// "commit",
/// )
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn create_tag(
&self,
tag: impl Into<String>,
message: impl Into<String>,
object: impl Into<String>,
object_type: impl Into<String>,
) -> CreateTagBuilder<'octo> {
CreateTagBuilder::new(
self.crab,
self.repo.clone(),
tag.into(),
message.into(),
object.into(),
object_type.into(),
)
}
// -----------------------------------------------------------------------
// Trees
// -----------------------------------------------------------------------
/// Gets a Git tree object from the repository.
///
/// See: [GitHub API Documentation](https://docs.github.com/en/rest/git/trees?apiVersion=2022-11-28#get-a-tree)
///
/// # Examples
///
/// ```no_run
/// # async fn run(octocrab: &octocrab::Octocrab) -> octocrab::Result<()> {
/// let tree = octocrab
/// .git("owner", "repo")
/// .get_tree("9fb037999f264ba9a7fc6274d15fa3ae2ab98312")
/// .recursive(true)
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn get_tree(&self, tree_sha: impl Into<String>) -> GetTreeBuilder<'octo> {
GetTreeBuilder::new(self.crab, self.repo.clone(), tree_sha.into())
}
/// Creates a new Git tree object in the repository.
///
/// See: [GitHub API Documentation](https://docs.github.com/en/rest/git/trees?apiVersion=2022-11-28#create-a-tree)
///
/// # Examples
///
/// ```no_run
/// # async fn run(octocrab: &octocrab::Octocrab) -> octocrab::Result<()> {
/// use octocrab::models::git::CreateTreeEntry;
///
/// let entry = CreateTreeEntry::new("file.rb", "100644", "blob")
/// .with_sha("44b4fc6d56897b048c772eb4087f854f46256132");
///
/// let tree = octocrab
/// .git("owner", "repo")
/// .create_tree(vec![entry])
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn create_tree(&self, tree: Vec<CreateTreeEntry>) -> CreateTreeBuilder<'octo> {
CreateTreeBuilder::new(self.crab, self.repo.clone(), tree)
}
}
// ===========================================================================
// Builders
// ===========================================================================
#[derive(serde::Serialize)]
pub struct CreateBlobBuilder<'octo> {
#[serde(skip)]
crab: &'octo Octocrab,
#[serde(skip)]
repo: RepoRef,
content: String,
#[serde(skip_serializing_if = "Option::is_none")]
encoding: Option<String>,
}
impl<'octo> CreateBlobBuilder<'octo> {
pub(crate) fn new(crab: &'octo Octocrab, repo: RepoRef, content: String) -> Self {
Self {
crab,
repo,
content,
encoding: None,
}
}
/// The encoding used for content. Currently, "utf-8" and "base64" are supported.
pub fn encoding(mut self, encoding: impl Into<String>) -> Self {
self.encoding = Some(encoding.into());
self
}
/// Sends the request.
pub async fn send(self) -> Result<CreatedBlob> {
let route = format!("/{}/git/blobs", self.repo);
self.crab.post(route, Some(&self)).await
}
}
#[derive(serde::Serialize)]
pub struct CreateGitCommitObjectBuilder<'octo> {
#[serde(skip)]
crab: &'octo Octocrab,
#[serde(skip)]
repo: RepoRef,
message: String,
tree: String,
parents: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
author: Option<CommitAuthor>,
#[serde(skip_serializing_if = "Option::is_none")]
committer: Option<CommitAuthor>,
#[serde(skip_serializing_if = "Option::is_none")]
signature: Option<String>,
}
impl<'octo> CreateGitCommitObjectBuilder<'octo> {
pub(crate) fn new(crab: &'octo Octocrab, repo: RepoRef, message: String, tree: String) -> Self {
Self {
crab,
repo,
message,
tree,
parents: Vec::new(),
author: None,
committer: None,
signature: None,
}
}
/// The author of the commit.
pub fn author(mut self, author: impl Into<CommitAuthor>) -> Self {
self.author = Some(author.into());
self
}
/// The committer of the commit.
pub fn committer(mut self, committer: impl Into<CommitAuthor>) -> Self {
self.committer = Some(committer.into());
self
}
/// The signature of the commit.
pub fn signature(mut self, signature: impl Into<String>) -> Self {
self.signature = Some(signature.into());
self
}
/// The parents of the commit.
pub fn parents(mut self, parents: Vec<String>) -> Self {
self.parents = parents;
self
}
/// Sends the request.
pub async fn send(&self) -> Result<GitCommitObject> {
let route = format!("/{}/git/commits", self.repo);
self.crab.post(route, Some(&self)).await
}
}
#[derive(serde::Serialize)]
pub struct UpdateRefBuilder<'octo> {
#[serde(skip)]
crab: &'octo Octocrab,
#[serde(skip)]
repo: RepoRef,
#[serde(skip)]
reference: Reference,
sha: String,
#[serde(skip_serializing_if = "Option::is_none")]
force: Option<bool>,
}
impl<'octo> UpdateRefBuilder<'octo> {
pub(crate) fn new(
crab: &'octo Octocrab,
repo: RepoRef,
reference: Reference,
sha: String,
) -> Self {
Self {
crab,
repo,
reference,
sha,
force: None,
}
}
/// Indicates whether to force the update or to make sure the update is a fast-forward update.
pub fn force(mut self, force: bool) -> Self {
self.force = Some(force);
self
}
/// Sends the request.
pub async fn send(self) -> Result<Ref> {
let route = format!(
"/{repo}/git/refs/{ref}",
repo = self.repo,
ref = self.reference.ref_url()
);
self.crab.patch(route, Some(&self)).await
}
}
#[derive(serde::Serialize)]
pub struct CreateTagBuilder<'octo> {
#[serde(skip)]
crab: &'octo Octocrab,
#[serde(skip)]
repo: RepoRef,
tag: String,
message: String,
object: String,
r#type: String,
#[serde(skip_serializing_if = "Option::is_none")]
tagger: Option<CommitAuthor>,
}
impl<'octo> CreateTagBuilder<'octo> {
pub(crate) fn new(
crab: &'octo Octocrab,
repo: RepoRef,
tag: String,
message: String,
object: String,
object_type: String,
) -> Self {
Self {
crab,
repo,
tag,
message,
object,
r#type: object_type,
tagger: None,
}
}
/// Information about the individual creating the tag.
pub fn tagger(mut self, tagger: impl Into<CommitAuthor>) -> Self {
self.tagger = Some(tagger.into());
self
}
/// Sends the request.
pub async fn send(self) -> Result<GitTag> {
let route = format!("/{}/git/tags", self.repo);
self.crab.post(route, Some(&self)).await
}
}
#[derive(serde::Serialize)]
struct GetTreeParams {
#[serde(skip_serializing_if = "Option::is_none")]
recursive: Option<u8>,
}
pub struct GetTreeBuilder<'octo> {
crab: &'octo Octocrab,
repo: RepoRef,
tree_sha: String,
recursive: Option<u8>,
}
impl<'octo> GetTreeBuilder<'octo> {
pub(crate) fn new(crab: &'octo Octocrab, repo: RepoRef, tree_sha: String) -> Self {
Self {
crab,
repo,
tree_sha,
recursive: None,
}
}
/// Setting this parameter to true returns the objects or subtrees referenced by the tree specified in tree_sha.
pub fn recursive(mut self, recursive: bool) -> Self {
self.recursive = if recursive { Some(1) } else { None };
self
}
/// Sends the request.
pub async fn send(self) -> Result<GitTree> {
let route = format!("/{}/git/trees/{}", self.repo, self.tree_sha);
let params = GetTreeParams {
recursive: self.recursive,
};
self.crab.get(route, Some(¶ms)).await
}
}
#[derive(serde::Serialize)]
pub struct CreateTreeBuilder<'octo> {
#[serde(skip)]
crab: &'octo Octocrab,
#[serde(skip)]
repo: RepoRef,
tree: Vec<CreateTreeEntry>,
#[serde(skip_serializing_if = "Option::is_none")]
base_tree: Option<String>,
}
impl<'octo> CreateTreeBuilder<'octo> {
pub(crate) fn new(crab: &'octo Octocrab, repo: RepoRef, tree: Vec<CreateTreeEntry>) -> Self {
Self {
crab,
repo,
tree,
base_tree: None,
}
}
/// The SHA1 of an existing Git tree object which will be used as the base for the new tree.
pub fn base_tree(mut self, base_tree: impl Into<String>) -> Self {
self.base_tree = Some(base_tree.into());
self
}
/// Sends the request.
pub async fn send(self) -> Result<GitTree> {
let route = format!("/{}/git/trees", self.repo);
self.crab.post(route, Some(&self)).await
}
}