80 lines
1.8 KiB
Rust
80 lines
1.8 KiB
Rust
|
use reqwest::Client;
|
||
|
use serde_json::{json, Value};
|
||
|
|
||
|
#[derive(serde::Deserialize, serde::Serialize)]
|
||
|
pub struct GitCommit {
|
||
|
commit: GitCommitDetails,
|
||
|
}
|
||
|
|
||
|
#[derive(serde::Deserialize, serde::Serialize)]
|
||
|
pub struct GitCommitDetails {
|
||
|
message: String,
|
||
|
}
|
||
|
|
||
|
pub async fn get_pull_request_commits(
|
||
|
base_url: &str,
|
||
|
owner: &str,
|
||
|
repo: &str,
|
||
|
pull_request_id: u32,
|
||
|
access_token: &str,
|
||
|
) -> eyre::Result<Vec<String>> {
|
||
|
let client = Client::new();
|
||
|
|
||
|
let api_url = format!(
|
||
|
"{}/api/v1/repos/{}/{}/pulls/{}/commits",
|
||
|
base_url, owner, repo, pull_request_id
|
||
|
);
|
||
|
|
||
|
let response = client
|
||
|
.get(&api_url)
|
||
|
.bearer_auth(access_token)
|
||
|
.send()
|
||
|
.await?;
|
||
|
|
||
|
let commits: Vec<GitCommit> = response.json().await?;
|
||
|
|
||
|
let mut commit_titles = Vec::new();
|
||
|
|
||
|
tracing::debug!(
|
||
|
commits = serde_json::to_string(&commits)?,
|
||
|
"fetched commits"
|
||
|
);
|
||
|
|
||
|
for entry in commits {
|
||
|
commit_titles.push(entry.commit.message);
|
||
|
}
|
||
|
|
||
|
Ok(commit_titles)
|
||
|
}
|
||
|
|
||
|
#[cfg(test)]
|
||
|
mod tests {
|
||
|
use tracing_test::traced_test;
|
||
|
|
||
|
use super::*;
|
||
|
|
||
|
#[tokio::test]
|
||
|
#[traced_test]
|
||
|
async fn test_get_pull_request_commits() {
|
||
|
dotenv::dotenv().unwrap();
|
||
|
|
||
|
let base_url = "https://git.front.kjuulh.io";
|
||
|
let owner = "kjuulh";
|
||
|
let repo = "test-repo";
|
||
|
let pull_request_id = 3;
|
||
|
let access_token = &std::env::var("GITEA_ACCESS_TOKEN").unwrap();
|
||
|
|
||
|
let commits =
|
||
|
get_pull_request_commits(base_url, owner, repo, pull_request_id, access_token)
|
||
|
.await
|
||
|
.unwrap();
|
||
|
|
||
|
assert!(
|
||
|
!commits.is_empty(),
|
||
|
"Expected at least one commit in the pull request"
|
||
|
);
|
||
|
|
||
|
// You can add more assertions here to check the contents of the commits vector
|
||
|
}
|
||
|
}
|