gitea_sdk/api/repos/contents/
update_file.rs

1use build_it::Builder;
2use serde::{Deserialize, Serialize};
3
4use crate::{
5    model::repos::{CommitDateOptions, EntryMutation, Identity},
6    Result,
7};
8
9#[derive(Debug, Serialize, Deserialize, Builder)]
10#[build_it(into)]
11pub struct UpdateFileRepoBuilder {
12    /// The owner of the repository.
13    #[skip]
14    #[serde(skip)]
15    owner: String,
16    /// The name of the repository.
17    #[skip]
18    #[serde(skip)]
19    repo: String,
20    /// Path of the file to update.
21    #[skip]
22    #[serde(skip)]
23    filepath: String,
24    /// Identity for a person's identity like an author or committer.
25    #[serde(skip_serializing_if = "Option::is_none")]
26    author: Option<Identity>,
27    /// Branch (optional) to base this file from. if not given, the default branch is used.
28    #[serde(skip_serializing_if = "Option::is_none")]
29    branch: Option<String>,
30    // Identity for a person's identity like an author or committer
31    #[serde(skip_serializing_if = "Option::is_none")]
32    committer: Option<Identity>,
33    /// File content must be base64 encoded
34    #[skip]
35    content: String,
36    /// CommitDateOptions store dates for GIT_AUTHOR_DATE and GIT_COMMITTER_DATE
37    dates: Option<CommitDateOptions>,
38    /// from_path (optional) is the path of the original file which will be moved/renamed to the path in the URL
39    from_path: Option<String>,
40    /// message (optional) for the commit of this file. if not supplied, a default message will be used
41    message: Option<String>,
42    /// new_branch (optional) will make a new branch from branch before creating the file
43    new_branch: Option<String>,
44    /// sha is the SHA for the file that already exists
45    #[skip]
46    sha: String,
47    /// Add a Signed-off-by trailer by the committer at the end of the commit log message.
48    signoff: Option<bool>,
49}
50
51impl UpdateFileRepoBuilder {
52    pub fn new(
53        owner: impl ToString,
54        repo: impl ToString,
55        filepath: impl ToString,
56        content: impl ToString,
57        sha: impl ToString,
58    ) -> Self {
59        Self {
60            owner: owner.to_string(),
61            repo: repo.to_string(),
62            filepath: filepath.to_string(),
63            author: None,
64            branch: None,
65            committer: None,
66            content: content.to_string(),
67            dates: None,
68            from_path: None,
69            message: None,
70            new_branch: None,
71            sha: sha.to_string(),
72            signoff: None,
73        }
74    }
75
76    /// Send the request to update file in given repository.
77    pub async fn send(&self, client: &crate::Client) -> Result<EntryMutation> {
78        let owner = &self.owner;
79        let repo = &self.repo;
80        let filepath = &self.filepath;
81
82        let req = client
83            .put(format!("repos/{owner}/{repo}/contents/{filepath}"))
84            .json(self)
85            .build()?;
86
87        let res = client.make_request(req).await?;
88
89        client.parse_response(res).await
90    }
91}