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
use super::RepoHandler;
use crate::models::{repos::Autolink, AutolinkId};
use crate::Result;
/// A client to GitHub's repository autolinks API.
///
/// Created with [`RepoHandler::autolinks`].
pub struct RepoAutolinksHandler<'octo, 'r> {
handler: &'r RepoHandler<'octo>,
}
impl<'octo, 'r> RepoAutolinksHandler<'octo, 'r> {
pub(crate) fn new(handler: &'r RepoHandler<'octo>) -> Self {
Self { handler }
}
/// Gets all autolink references configured for a repository.
///
/// Information about autolinks is only available to repository administrators.
///
/// See: [GitHub API Documentation](https://docs.github.com/en/rest/repos/autolinks#get-all-autolinks-of-a-repository)
///
/// # Examples
///
/// ```no_run
/// # async fn run(octocrab: &octocrab::Octocrab) -> octocrab::Result<()> {
/// let autolinks = octocrab.repos("owner", "repo")
/// .autolinks()
/// .list()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub async fn list(&self) -> Result<Vec<Autolink>> {
let route = format!("/{}/autolinks", self.handler.repo);
self.handler.crab.get(route, None::<&()>).await
}
/// Gets a single autolink reference by its ID.
///
/// Information about autolinks is only available to repository administrators.
///
/// See: [GitHub API Documentation](https://docs.github.com/en/rest/repos/autolinks#get-an-autolink-reference-of-a-repository)
///
/// # Examples
///
/// ```no_run
/// # async fn run(octocrab: &octocrab::Octocrab) -> octocrab::Result<()> {
/// let autolink = octocrab.repos("owner", "repo")
/// .autolinks()
/// .get(42)
/// .await?;
/// # Ok(())
/// # }
/// ```
pub async fn get(&self, autolink_id: impl Into<AutolinkId>) -> Result<Autolink> {
let autolink_id = autolink_id.into();
let route = format!("/{}/autolinks/{autolink_id}", self.handler.repo);
self.handler.crab.get(route, None::<&()>).await
}
/// Creates a [`CreateAutolinkBuilder`] to configure a new autolink reference for a repository.
///
/// Users with admin access to the repository can create an autolink.
///
/// See: [GitHub API Documentation](https://docs.github.com/en/rest/repos/autolinks#create-an-autolink-reference-for-a-repository)
///
/// # Examples
///
/// ```no_run
/// # async fn run(octocrab: &octocrab::Octocrab) -> octocrab::Result<()> {
/// let autolink = octocrab.repos("owner", "repo")
/// .autolinks()
/// .create("TICKET-", "https://example.com/TICKET?query=<num>")
/// .is_alphanumeric(true)
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn create(
&self,
key_prefix: impl Into<String>,
url_template: impl Into<String>,
) -> CreateAutolinkBuilder<'octo, 'r> {
CreateAutolinkBuilder::new(self.handler, key_prefix.into(), url_template.into())
}
/// Deletes an autolink reference from a repository.
///
/// Information about autolinks is only available to repository administrators.
///
/// See: [GitHub API Documentation](https://docs.github.com/en/rest/repos/autolinks#delete-an-autolink-reference-from-a-repository)
///
/// # Examples
///
/// ```no_run
/// # async fn run(octocrab: &octocrab::Octocrab) -> octocrab::Result<()> {
/// octocrab.repos("owner", "repo")
/// .autolinks()
/// .delete(42)
/// .await?;
/// # Ok(())
/// # }
/// ```
pub async fn delete(&self, autolink_id: impl Into<AutolinkId>) -> Result<()> {
let autolink_id = autolink_id.into();
let route = format!("/{}/autolinks/{autolink_id}", self.handler.repo);
let response = self.handler.crab._delete(route, None::<&()>).await?;
if response.status() != http::StatusCode::NO_CONTENT {
return Err(crate::map_github_error(response).await.unwrap_err());
}
Ok(())
}
}
/// A builder pattern struct for creating an autolink reference for a repository.
///
/// Created by [`RepoAutolinksHandler::create`].
#[derive(serde::Serialize)]
pub struct CreateAutolinkBuilder<'octo, 'r> {
#[serde(skip)]
handler: &'r RepoHandler<'octo>,
key_prefix: String,
url_template: String,
#[serde(skip_serializing_if = "Option::is_none")]
is_alphanumeric: Option<bool>,
}
impl<'octo, 'r> CreateAutolinkBuilder<'octo, 'r> {
pub(crate) fn new(
handler: &'r RepoHandler<'octo>,
key_prefix: String,
url_template: String,
) -> Self {
Self {
handler,
key_prefix,
url_template,
is_alphanumeric: None,
}
}
/// Whether this autolink reference matches alphanumeric characters.
/// If true, `<num>` matches `A-Z` (case insensitive), `0-9`, and `-`.
/// If false, this autolink reference only matches numeric characters.
pub fn is_alphanumeric(mut self, is_alphanumeric: impl Into<bool>) -> Self {
self.is_alphanumeric = Some(is_alphanumeric.into());
self
}
/// Sends the actual request.
pub async fn send(self) -> Result<Autolink> {
let route = format!("/{}/autolinks", self.handler.repo);
self.handler.crab.post(route, Some(&self)).await
}
}