automatons_github/resource/repository/
mod.rs

1use std::fmt::{Display, Formatter};
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5use url::Url;
6
7use crate::resource::{Account, License, NodeId, Visibility};
8use crate::{id, name};
9
10pub use self::minimal::MinimalRepository;
11
12mod minimal;
13
14id!(
15    /// Repository id
16    ///
17    /// The [`RepositoryId`] is a unique, numerical id that is used to interact with an account
18    /// through [GitHub's REST API](https://docs.github.com/en/rest).
19    RepositoryId
20);
21
22name!(
23    /// Repository name
24    ///
25    /// Repositories on GitHub have a human-readable name that is used throughout GitHub's
26    /// website. The name is unique within the scope of its owner.
27    RepositoryName
28);
29
30name!(
31    /// Repository owner and name
32    ///
33    /// The full name of a repository is a unique combination of the repository's owner and name.
34    RepositoryFullName
35);
36
37/// Repository on GitHub
38///
39/// Repositories are a core resource on GitHub, and most other resources belong to them. They are
40/// uniquely identified by the combination of their `owner` and `name`.
41#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Deserialize, Serialize)]
42pub struct Repository {
43    #[serde(flatten)]
44    minimal: MinimalRepository,
45
46    node_id: NodeId,
47    owner: Account,
48    full_name: RepositoryFullName,
49    description: String,
50    homepage: String,
51    language: String,
52    license: License,
53    visibility: Visibility,
54    default_branch: String,
55    topics: Vec<String>,
56    size: u64,
57    stargazers_count: u64,
58    watchers_count: u64,
59    forks_count: u64,
60    open_issues_count: u64,
61    private: bool,
62    fork: bool,
63    has_issues: bool,
64    has_projects: bool,
65    has_wiki: bool,
66    has_pages: bool,
67    archived: bool,
68    disabled: bool,
69    allow_forking: bool,
70    is_template: bool,
71    web_commit_signoff_required: bool,
72    html_url: Url,
73    keys_url: Url,
74    collaborators_url: Url,
75    teams_url: Url,
76    hooks_url: Url,
77    issue_events_url: Url,
78    events_url: Url,
79    assignees_url: Url,
80    branches_url: Url,
81    tags_url: Url,
82    blobs_url: Url,
83    git_tags_url: Url,
84    git_refs_url: Url,
85    trees_url: Url,
86    statuses_url: Url,
87    languages_url: Url,
88    stargazers_url: Url,
89    contributors_url: Url,
90    subscribers_url: Url,
91    subscription_url: Url,
92    commits_url: Url,
93    git_commits_url: Url,
94    comments_url: Url,
95    issue_comment_url: Url,
96    contents_url: Url,
97    compare_url: Url,
98    merges_url: Url,
99    archive_url: Url,
100    downloads_url: Url,
101    issues_url: Url,
102    pulls_url: Url,
103    milestones_url: Url,
104    notifications_url: Url,
105    labels_url: Url,
106    releases_url: Url,
107    deployments_url: Url,
108    git_url: Url,
109    ssh_url: String,
110    clone_url: Url,
111    svn_url: Url,
112    mirror_url: Option<Url>,
113    created_at: DateTime<Utc>,
114    updated_at: DateTime<Utc>,
115    pushed_at: DateTime<Utc>,
116}
117
118impl Repository {
119    /// Returns the repository's unique id.
120    #[cfg_attr(feature = "tracing", tracing::instrument)]
121    pub fn id(&self) -> RepositoryId {
122        self.minimal.id()
123    }
124
125    /// Returns the repository's node id.
126    #[cfg_attr(feature = "tracing", tracing::instrument)]
127    pub fn node_id(&self) -> &NodeId {
128        &self.node_id
129    }
130
131    /// Returns the repository's name.
132    #[cfg_attr(feature = "tracing", tracing::instrument)]
133    pub fn name(&self) -> &RepositoryName {
134        self.minimal.name()
135    }
136
137    /// Returns the account which ows the repository.
138    #[cfg_attr(feature = "tracing", tracing::instrument)]
139    pub fn owner(&self) -> &Account {
140        &self.owner
141    }
142
143    /// Returns the repository's full name.
144    #[cfg_attr(feature = "tracing", tracing::instrument)]
145    pub fn full_name(&self) -> &RepositoryFullName {
146        &self.full_name
147    }
148
149    /// Returns the repository's description.
150    #[cfg_attr(feature = "tracing", tracing::instrument)]
151    pub fn description(&self) -> &String {
152        &self.description
153    }
154
155    /// Returns the URL to the repository's homepage.
156    #[cfg_attr(feature = "tracing", tracing::instrument)]
157    pub fn homepage(&self) -> &String {
158        &self.homepage
159    }
160
161    /// Returns the repository's primary programming language.
162    #[cfg_attr(feature = "tracing", tracing::instrument)]
163    pub fn language(&self) -> &String {
164        &self.language
165    }
166
167    /// Returns the repository's license.
168    #[cfg_attr(feature = "tracing", tracing::instrument)]
169    pub fn license(&self) -> &License {
170        &self.license
171    }
172
173    /// Returns the repository's visibility.
174    #[cfg_attr(feature = "tracing", tracing::instrument)]
175    pub fn visibility(&self) -> Visibility {
176        self.visibility
177    }
178
179    /// Returns the repository's default branch.
180    #[cfg_attr(feature = "tracing", tracing::instrument)]
181    pub fn default_branch(&self) -> &String {
182        &self.default_branch
183    }
184
185    /// Returns the repository's topics.
186    #[cfg_attr(feature = "tracing", tracing::instrument)]
187    pub fn topics(&self) -> &Vec<String> {
188        &self.topics
189    }
190
191    /// Returns the repository's size.
192    #[cfg_attr(feature = "tracing", tracing::instrument)]
193    pub fn size(&self) -> u64 {
194        self.size
195    }
196
197    /// Returns the repository's stargazers count.
198    #[cfg_attr(feature = "tracing", tracing::instrument)]
199    pub fn stargazers_count(&self) -> u64 {
200        self.stargazers_count
201    }
202
203    /// Returns the repository's watchers count.
204    #[cfg_attr(feature = "tracing", tracing::instrument)]
205    pub fn watchers_count(&self) -> u64 {
206        self.watchers_count
207    }
208
209    /// Returns the repository's forks count.
210    #[cfg_attr(feature = "tracing", tracing::instrument)]
211    pub fn forks_count(&self) -> u64 {
212        self.forks_count
213    }
214
215    /// Returns the repository's open issues count.
216    #[cfg_attr(feature = "tracing", tracing::instrument)]
217    pub fn open_issues_count(&self) -> u64 {
218        self.open_issues_count
219    }
220
221    /// Indicates whether the repository is private.
222    #[cfg_attr(feature = "tracing", tracing::instrument)]
223    pub fn private(&self) -> bool {
224        self.private
225    }
226
227    /// Indicates whether the repository is a fork.
228    #[cfg_attr(feature = "tracing", tracing::instrument)]
229    pub fn fork(&self) -> bool {
230        self.fork
231    }
232
233    /// Indicates whether the issues feature is enabled for the repository.
234    #[cfg_attr(feature = "tracing", tracing::instrument)]
235    pub fn has_issues(&self) -> bool {
236        self.has_issues
237    }
238
239    /// Indicates whether the projects feature is enabled for the repository.
240    #[cfg_attr(feature = "tracing", tracing::instrument)]
241    pub fn has_projects(&self) -> bool {
242        self.has_projects
243    }
244
245    /// Indicates whether the wiki feature is enabled for the repository.
246    #[cfg_attr(feature = "tracing", tracing::instrument)]
247    pub fn has_wiki(&self) -> bool {
248        self.has_wiki
249    }
250
251    /// Indicates whether the repository has a static website.
252    #[cfg_attr(feature = "tracing", tracing::instrument)]
253    pub fn has_pages(&self) -> bool {
254        self.has_pages
255    }
256
257    /// Indicates whether the repository has been archived.
258    #[cfg_attr(feature = "tracing", tracing::instrument)]
259    pub fn archived(&self) -> bool {
260        self.archived
261    }
262
263    /// Indicates whether the repository has been disabled.
264    #[cfg_attr(feature = "tracing", tracing::instrument)]
265    pub fn disabled(&self) -> bool {
266        self.disabled
267    }
268
269    /// Indicates whether the repository can be forked.
270    #[cfg_attr(feature = "tracing", tracing::instrument)]
271    pub fn allow_forking(&self) -> bool {
272        self.allow_forking
273    }
274
275    /// Indicates whether the repository can be used as a template.
276    #[cfg_attr(feature = "tracing", tracing::instrument)]
277    pub fn is_template(&self) -> bool {
278        self.is_template
279    }
280
281    /// Indicates whether the signoff is required for commits through GitHub's web interface.
282    #[cfg_attr(feature = "tracing", tracing::instrument)]
283    pub fn web_commit_signoff_required(&self) -> bool {
284        self.web_commit_signoff_required
285    }
286
287    /// Returns the URL to the repository.
288    #[cfg_attr(feature = "tracing", tracing::instrument)]
289    pub fn html_url(&self) -> &Url {
290        &self.html_url
291    }
292
293    /// Returns the API endpoint to query the repository.
294    #[cfg_attr(feature = "tracing", tracing::instrument)]
295    pub fn url(&self) -> &Url {
296        self.minimal.url()
297    }
298
299    /// Returns the API endpoint to query the repository's keys.
300    #[cfg_attr(feature = "tracing", tracing::instrument)]
301    pub fn keys_url(&self) -> &Url {
302        &self.keys_url
303    }
304
305    /// Returns the API endpoint to query the repository's collaborators.
306    #[cfg_attr(feature = "tracing", tracing::instrument)]
307    pub fn collaborators_url(&self) -> &Url {
308        &self.collaborators_url
309    }
310
311    /// Returns the API endpoint to query the repository's teams.
312    #[cfg_attr(feature = "tracing", tracing::instrument)]
313    pub fn teams_url(&self) -> &Url {
314        &self.teams_url
315    }
316
317    /// Returns the API endpoint to query the repository's hooks.
318    #[cfg_attr(feature = "tracing", tracing::instrument)]
319    pub fn hooks_url(&self) -> &Url {
320        &self.hooks_url
321    }
322
323    /// Returns the API endpoint to query the repository's issue events.
324    #[cfg_attr(feature = "tracing", tracing::instrument)]
325    pub fn issue_events_url(&self) -> &Url {
326        &self.issue_events_url
327    }
328
329    /// Returns the API endpoint to query the repository's events.
330    #[cfg_attr(feature = "tracing", tracing::instrument)]
331    pub fn events_url(&self) -> &Url {
332        &self.events_url
333    }
334
335    /// Returns the API endpoint to query the repository's assignees.
336    #[cfg_attr(feature = "tracing", tracing::instrument)]
337    pub fn assignees_url(&self) -> &Url {
338        &self.assignees_url
339    }
340
341    /// Returns the API endpoint to query the repository's branches.
342    #[cfg_attr(feature = "tracing", tracing::instrument)]
343    pub fn branches_url(&self) -> &Url {
344        &self.branches_url
345    }
346
347    /// Returns the API endpoint to query the repository's tags.
348    #[cfg_attr(feature = "tracing", tracing::instrument)]
349    pub fn tags_url(&self) -> &Url {
350        &self.tags_url
351    }
352
353    /// Returns the API endpoint to query the repository's blobs.
354    #[cfg_attr(feature = "tracing", tracing::instrument)]
355    pub fn blobs_url(&self) -> &Url {
356        &self.blobs_url
357    }
358
359    /// Returns the API endpoint to query the repository's git tags.
360    #[cfg_attr(feature = "tracing", tracing::instrument)]
361    pub fn git_tags_url(&self) -> &Url {
362        &self.git_tags_url
363    }
364
365    /// Returns the API endpoint to query the repository's git refs.
366    #[cfg_attr(feature = "tracing", tracing::instrument)]
367    pub fn git_refs_url(&self) -> &Url {
368        &self.git_refs_url
369    }
370
371    /// Returns the API endpoint to query the repository's git trees.
372    #[cfg_attr(feature = "tracing", tracing::instrument)]
373    pub fn trees_url(&self) -> &Url {
374        &self.trees_url
375    }
376
377    /// Returns the API endpoint to query the repository's statuses.
378    #[cfg_attr(feature = "tracing", tracing::instrument)]
379    pub fn statuses_url(&self) -> &Url {
380        &self.statuses_url
381    }
382
383    /// Returns the API endpoint to query the repository's programming languages.
384    #[cfg_attr(feature = "tracing", tracing::instrument)]
385    pub fn languages_url(&self) -> &Url {
386        &self.languages_url
387    }
388
389    /// Returns the API endpoint to query the repository's stargazers.
390    #[cfg_attr(feature = "tracing", tracing::instrument)]
391    pub fn stargazers_url(&self) -> &Url {
392        &self.stargazers_url
393    }
394
395    /// Returns the API endpoint to query the repository's contributors.
396    #[cfg_attr(feature = "tracing", tracing::instrument)]
397    pub fn contributors_url(&self) -> &Url {
398        &self.contributors_url
399    }
400
401    /// Returns the API endpoint to query the repository's subscribers.
402    #[cfg_attr(feature = "tracing", tracing::instrument)]
403    pub fn subscribers_url(&self) -> &Url {
404        &self.subscribers_url
405    }
406
407    /// Returns the API endpoint to query the repository's subscriptions.
408    #[cfg_attr(feature = "tracing", tracing::instrument)]
409    pub fn subscription_url(&self) -> &Url {
410        &self.subscription_url
411    }
412
413    /// Returns the API endpoint to query the repository's commits.
414    #[cfg_attr(feature = "tracing", tracing::instrument)]
415    pub fn commits_url(&self) -> &Url {
416        &self.commits_url
417    }
418
419    /// Returns the API endpoint to query the repository's git commits.
420    #[cfg_attr(feature = "tracing", tracing::instrument)]
421    pub fn git_commits_url(&self) -> &Url {
422        &self.git_commits_url
423    }
424
425    /// Returns the API endpoint to query the repository's comments.
426    #[cfg_attr(feature = "tracing", tracing::instrument)]
427    pub fn comments_url(&self) -> &Url {
428        &self.comments_url
429    }
430
431    /// Returns the API endpoint to query the repository's issue comments.
432    #[cfg_attr(feature = "tracing", tracing::instrument)]
433    pub fn issue_comment_url(&self) -> &Url {
434        &self.issue_comment_url
435    }
436
437    /// Returns the API endpoint to query the repository's contents.
438    #[cfg_attr(feature = "tracing", tracing::instrument)]
439    pub fn contents_url(&self) -> &Url {
440        &self.contents_url
441    }
442
443    /// Returns the API endpoint to compare refs in the repository.
444    #[cfg_attr(feature = "tracing", tracing::instrument)]
445    pub fn compare_url(&self) -> &Url {
446        &self.compare_url
447    }
448
449    /// Returns the API endpoint to query the repository's merges.
450    #[cfg_attr(feature = "tracing", tracing::instrument)]
451    pub fn merges_url(&self) -> &Url {
452        &self.merges_url
453    }
454
455    /// Returns the API endpoint to retrieve the repository's archive.
456    #[cfg_attr(feature = "tracing", tracing::instrument)]
457    pub fn archive_url(&self) -> &Url {
458        &self.archive_url
459    }
460
461    /// Returns the API endpoint to query the repository's downloads.
462    #[cfg_attr(feature = "tracing", tracing::instrument)]
463    pub fn downloads_url(&self) -> &Url {
464        &self.downloads_url
465    }
466
467    /// Returns the API endpoint to query the repository's issues.
468    #[cfg_attr(feature = "tracing", tracing::instrument)]
469    pub fn issues_url(&self) -> &Url {
470        &self.issues_url
471    }
472
473    /// Returns the API endpoint to query the repository's pull requests.
474    #[cfg_attr(feature = "tracing", tracing::instrument)]
475    pub fn pulls_url(&self) -> &Url {
476        &self.pulls_url
477    }
478
479    /// Returns the API endpoint to query the repository's milestones.
480    #[cfg_attr(feature = "tracing", tracing::instrument)]
481    pub fn milestones_url(&self) -> &Url {
482        &self.milestones_url
483    }
484
485    /// Returns the API endpoint to query the repository's notifications.
486    #[cfg_attr(feature = "tracing", tracing::instrument)]
487    pub fn notifications_url(&self) -> &Url {
488        &self.notifications_url
489    }
490
491    /// Returns the API endpoint to query the repository's labels.
492    #[cfg_attr(feature = "tracing", tracing::instrument)]
493    pub fn labels_url(&self) -> &Url {
494        &self.labels_url
495    }
496
497    /// Returns the API endpoint to query the repository's releases.
498    #[cfg_attr(feature = "tracing", tracing::instrument)]
499    pub fn releases_url(&self) -> &Url {
500        &self.releases_url
501    }
502
503    /// Returns the API endpoint to query the repository's deployments.
504    #[cfg_attr(feature = "tracing", tracing::instrument)]
505    pub fn deployments_url(&self) -> &Url {
506        &self.deployments_url
507    }
508
509    /// Returns the Git URL to clone the repository.
510    #[cfg_attr(feature = "tracing", tracing::instrument)]
511    pub fn git_url(&self) -> &Url {
512        &self.git_url
513    }
514
515    /// Returns the SSH URL to clone the repository.
516    #[cfg_attr(feature = "tracing", tracing::instrument)]
517    pub fn ssh_url(&self) -> &str {
518        &self.ssh_url
519    }
520
521    /// Returns the HTTP URL to clone the repository.
522    #[cfg_attr(feature = "tracing", tracing::instrument)]
523    pub fn clone_url(&self) -> &Url {
524        &self.clone_url
525    }
526
527    /// Returns the SVN URL to clone the repository.
528    #[cfg_attr(feature = "tracing", tracing::instrument)]
529    pub fn svn_url(&self) -> &Url {
530        &self.svn_url
531    }
532
533    /// Returns the URL to the repository's mirror.
534    #[cfg_attr(feature = "tracing", tracing::instrument)]
535    pub fn mirror_url(&self) -> &Option<Url> {
536        &self.mirror_url
537    }
538
539    /// Returns the date when the repository was created.
540    #[cfg_attr(feature = "tracing", tracing::instrument)]
541    pub fn created_at(&self) -> &DateTime<Utc> {
542        &self.created_at
543    }
544
545    /// Returns the date when the repository was last updated.
546    #[cfg_attr(feature = "tracing", tracing::instrument)]
547    pub fn updated_at(&self) -> &DateTime<Utc> {
548        &self.updated_at
549    }
550
551    /// Returns the date when the repository was last pushed.
552    #[cfg_attr(feature = "tracing", tracing::instrument)]
553    pub fn pushed_at(&self) -> &DateTime<Utc> {
554        &self.pushed_at
555    }
556}
557
558impl Display for Repository {
559    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
560        write!(f, "{}", self.full_name)
561    }
562}
563
564#[cfg(test)]
565mod tests {
566    use super::Repository;
567
568    #[test]
569    fn trait_deserialize() {
570        let repository: Repository = serde_json::from_str(include_str!(
571            "../../../tests/fixtures/resource/repository.json"
572        ))
573        .unwrap();
574
575        assert_eq!("automatons", repository.name().get());
576    }
577
578    #[test]
579    fn trait_display() {
580        let repository: Repository = serde_json::from_str(include_str!(
581            "../../../tests/fixtures/resource/repository.json"
582        ))
583        .unwrap();
584
585        assert_eq!("devxbots/automatons", repository.to_string());
586    }
587
588    #[test]
589    fn trait_send() {
590        fn assert_send<T: Send>() {}
591        assert_send::<Repository>();
592    }
593
594    #[test]
595    fn trait_sync() {
596        fn assert_sync<T: Sync>() {}
597        assert_sync::<Repository>();
598    }
599}