feat: implement Branch and add test

This commit also includes the implementation and tests for:

- PayloadCommit
- PayloadUser
- PayloadCommitVerification
This commit is contained in:
RobWalt 2023-10-22 13:48:55 +02:00
parent 0383b32fdd
commit ee72be4bf8
No known key found for this signature in database
GPG key ID: 333C6AC0CEF0CE68
11 changed files with 288 additions and 44 deletions

View file

@ -14,3 +14,43 @@
// user_can_merge boolean
// user_can_push boolean
// }]
//
use serde::{Deserialize, Serialize};
use crate::types::api::payload_commit::PayloadCommit;
use crate::types::misc::boolean_enums::can::merge::CanMerge;
use crate::types::misc::boolean_enums::can::push::CanPush;
use crate::types::misc::boolean_enums::enable::enable_status_check::EnableStatusCheck;
use crate::types::misc::boolean_enums::is::protected::IsProtected;
/// Represents a repository branch.
#[derive(Debug, Serialize, Deserialize)]
pub struct Branch {
/// The commit associated with the branch.
pub commit: PayloadCommit,
/// The effective branch protection name.
pub effective_branch_protection_name: String,
/// Indicates whether status checks are enabled for the branch.
pub enable_status_check: EnableStatusCheck,
/// The name of the branch.
pub name: String,
/// Indicates whether the branch is protected.
pub protected: IsProtected,
/// The number of required approvals for the branch.
pub required_approvals: usize,
/// Contexts for status checks.
pub status_check_contexts: Vec<String>,
/// Indicates whether the user can merge into the branch.
pub user_can_merge: CanMerge,
/// Indicates whether the user can push to the branch.
pub user_can_push: CanPush,
}
#[cfg(test)]
mod tests {
#[test]
fn deserialize_branches() {
let data = include_str!("../../../test_data/example_branch.json");
let _: Vec<super::Branch> = serde_json::from_str(data).unwrap();
}
}

View file

@ -1,23 +1,41 @@
// PayloadCommit{
// description:
// PayloadCommit represents a commit
//
// added [
// x-go-name: Added
// string]
// author PayloadUser{...}
// committer PayloadUser{...}
// id string
// sha1 hash of the commit
//
// message string
// modified [
// x-go-name: Modified
// string]
// removed [
// x-go-name: Removed
// string]
// timestamp string($date-time)
// url string
// verification PayloadCommitVerification{...}
// }
use std::path::PathBuf;
use crate::types::api::payload_commit_verification::PayloadCommitVerification;
use crate::types::api::payload_user::PayloadUser;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use url::Url;
/// Represents a commit payload.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PayloadCommit {
/// List of added files.
pub added: Vec<PathBuf>,
/// The author of the commit.
pub author: PayloadUser,
/// The committer of the commit.
pub committer: PayloadUser,
/// The SHA1 hash of the commit.
pub id: String,
/// The commit message.
pub message: String,
/// List of modified files.
pub modified: Vec<PathBuf>,
/// List of removed files.
pub removed: Vec<PathBuf>,
/// The timestamp of the commit.
pub timestamp: DateTime<Utc>,
/// The URL of the commit.
pub url: Url,
/// Verification information for the commit.
pub verification: PayloadCommitVerification,
}
#[cfg(test)]
mod tests {
#[test]
fn deserialize_payload_commit() {
let data = include_str!("../../../test_data/example_payload_commit.json");
let _: super::PayloadCommit = serde_json::from_str(data).unwrap();
}
}

View file

@ -1,11 +1,28 @@
// PayloadCommitVerification{
// description:
// PayloadCommitVerification represents the GPG verification of a commit
//
// payload string
// reason string
// signature string
// signer PayloadUser{...}
// verified boolean
// }
// }
use serde::{Deserialize, Serialize};
use crate::types::api::payload_user::PayloadUser;
use crate::types::misc::boolean_enums::is::verified::IsVerified;
/// Represents the GPG verification of a commit.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PayloadCommitVerification {
/// The payload to be verified.
pub payload: String,
/// The reason for verification.
pub reason: String,
/// The GPG signature.
pub signature: String,
/// The user who signed the commit.
pub signer: PayloadUser,
/// Indicates whether the verification was successful.
pub verified: IsVerified,
}
#[cfg(test)]
mod tests {
#[test]
fn deserialize_payload_commit_verification() {
let data = include_str!("../../../test_data/example_payload_commit_verification.json");
let _: super::PayloadCommitVerification = serde_json::from_str(data).unwrap();
}
}

View file

@ -1,10 +1,23 @@
// PayloadUser{
// description:
// PayloadUser represents the author or committer of a commit
//
// email string($email)
// name string
// Full name of the commit author
//
// username string
// }
use serde::{Deserialize, Serialize};
use serde_email::Email;
/// Represents the author or committer of a commit.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PayloadUser {
/// The email address of the user.
pub email: Email,
/// The full name of the user.
pub name: String,
/// The username of the user.
pub username: String,
}
#[cfg(test)]
mod tests {
#[test]
fn deserialize_payload_user() {
let data = include_str!("../../../test_data/example_payload_user.json");
let _: super::PayloadUser = serde_json::from_str(data).unwrap();
}
}

View file

@ -6,4 +6,5 @@ implement_boolean_enum!(read, CanRead);
implement_boolean_enum!(write, CanWrite);
implement_boolean_enum!(pull, CanPull);
implement_boolean_enum!(push, CanPush);
implement_boolean_enum!(merge, CanMerge);
implement_boolean_enum!(create_repo, CanCreateRepo);

View file

@ -2,3 +2,4 @@ use crate::implement_boolean_enum;
implement_boolean_enum!(issue_dependencies, IssueDependencies { On, Off });
implement_boolean_enum!(time_tracker, TimeTracker { On, Off });
implement_boolean_enum!(enable_status_check, EnableStatusCheck);

View file

@ -16,3 +16,5 @@ implement_boolean_enum!(ok, IsOk);
implement_boolean_enum!(locked, IsLocked);
implement_boolean_enum!(mergeable, IsMergeable);
implement_boolean_enum!(merged, IsMerged);
implement_boolean_enum!(verified, IsVerified);
implement_boolean_enum!(protected, IsProtected);

View file

@ -0,0 +1,99 @@
[
{
"commit": {
"added": [
"file1.txt",
"file2.txt"
],
"author": {
"email": "user@example.com",
"name": "John Doe",
"username": "johndoe"
},
"committer": {
"email": "user@example.com",
"name": "John Doe",
"username": "johndoe"
},
"id": "abcdef123456",
"message": "Commit message",
"modified": [
"file3.txt"
],
"removed": [
"file4.txt"
],
"timestamp": "2023-10-21T14:30:00Z",
"url": "https://example.com/commits/abcdef123456",
"verification": {
"payload": "payload_data",
"reason": "Verification reason",
"signature": "gpg_signature",
"signer": {
"email": "user@example.com",
"name": "John Doe",
"username": "johndoe"
},
"verified": true
}
},
"effective_branch_protection_name": "master",
"enable_status_check": true,
"name": "main",
"protected": true,
"required_approvals": 2,
"status_check_contexts": [
"status-check-1",
"status-check-2"
],
"user_can_merge": true,
"user_can_push": true
},
{
"commit": {
"added": [
"file1.txt",
"file2.txt"
],
"author": {
"email": "user@example.com",
"name": "John Doe",
"username": "johndoe"
},
"committer": {
"email": "user@example.com",
"name": "John Doe",
"username": "johndoe"
},
"id": "abcdef123456",
"message": "Commit message",
"modified": [
"file3.txt"
],
"removed": [
"file4.txt"
],
"timestamp": "2023-10-21T14:30:00Z",
"url": "https://example.com/commits/abcdef123456",
"verification": {
"payload": "payload_data",
"reason": "Verification reason",
"signature": "gpg_signature",
"signer": {
"email": "user@example.com",
"name": "John Doe",
"username": "johndoe"
},
"verified": true
}
},
"effective_branch_protection_name": "develop",
"enable_status_check": false,
"name": "develop",
"protected": false,
"required_approvals": 0,
"status_check_contexts": [],
"user_can_merge": true,
"user_can_push": true
}
]

View file

@ -0,0 +1,37 @@
{
"added": [
"file1.txt",
"file2.txt"
],
"author": {
"email": "user@example.com",
"name": "John Doe",
"username": "johndoe"
},
"committer": {
"email": "user@example.com",
"name": "John Doe",
"username": "johndoe"
},
"id": "abcdef123456",
"message": "Commit message",
"modified": [
"file3.txt"
],
"removed": [
"file4.txt"
],
"timestamp": "2023-10-21T14:30:00Z",
"url": "https://example.com/commits/abcdef123456",
"verification": {
"payload": "payload_data",
"reason": "Verification reason",
"signature": "gpg_signature",
"signer": {
"email": "user@example.com",
"name": "John Doe",
"username": "johndoe"
},
"verified": true
}
}

View file

@ -0,0 +1,11 @@
{
"payload": "payload_data",
"reason": "Verification reason",
"signature": "gpg_signature",
"signer": {
"email": "user@example.com",
"name": "John Doe",
"username": "johndoe"
},
"verified": true
}

View file

@ -0,0 +1,5 @@
{
"email": "user@example.com",
"name": "John Doe",
"username": "johndoe"
}