use std::borrow::Cow; use regex::Regex; use serde::Deserialize; use serde_json::Value; #[allow(dead_code)] #[derive(Debug, Deserialize)] #[serde(deny_unknown_fields)] pub(crate) struct HookPush { #[serde(rename = "ref")] pub(crate) ref_field: String, before: String, after: String, compare_url: String, commits: Vec, total_commits: u64, head_commit: HookCommit, repository: HookRepository, pusher: HookUser, sender: HookUser, } #[allow(dead_code)] #[derive(Debug, Deserialize)] #[serde(deny_unknown_fields)] pub(crate) struct HookUser { id: u64, login: String, login_name: String, full_name: String, email: String, avatar_url: String, language: String, is_admin: bool, last_login: String, // TODO: parse to datetime created: String, // TODO: parse to datetime restricted: bool, active: bool, prohibit_login: bool, location: String, website: String, description: String, visibility: String, followers_count: u64, following_count: u64, starred_repos_count: u64, username: String, } #[allow(dead_code)] #[derive(Debug, Deserialize)] #[serde(deny_unknown_fields)] pub(crate) struct HookRepository { id: u64, owner: HookUser, name: String, full_name: String, description: String, empty: bool, private: bool, fork: bool, template: bool, parent: Value, // Was null in test hook mirror: bool, size: u64, language: String, languages_url: String, html_url: String, url: String, link: String, ssh_url: String, clone_url: String, original_url: String, website: String, stars_count: u64, forks_count: u64, watchers_count: u64, open_issues_count: u64, open_pr_counter: u64, release_counter: u64, default_branch: String, archived: bool, created_at: String, // TODO: parse to datetime updated_at: String, // TODO: parse to datetime archived_at: String, // TODO: parse to datetime permissions: HookRepositoryPermissions, has_issues: bool, internal_tracker: HookRepositoryInternalTracker, has_wiki: bool, has_pull_requests: bool, has_projects: bool, has_releases: bool, has_packages: bool, has_actions: bool, ignore_whitespace_conflicts: bool, allow_merge_commits: bool, allow_rebase: bool, allow_rebase_explicit: bool, allow_squash_merge: bool, allow_rebase_update: bool, default_delete_branch_after_merge: bool, default_merge_style: String, default_allow_maintainer_edit: bool, avatar_url: String, internal: bool, mirror_interval: String, mirror_updated: String, // TODO: parse to datetime repo_transfer: Value, // Was null in test hook } #[allow(dead_code)] #[derive(Debug, Deserialize)] #[serde(deny_unknown_fields)] pub(crate) struct HookRepositoryPermissions { admin: bool, push: bool, pull: bool, } #[allow(dead_code)] #[derive(Debug, Deserialize)] #[serde(deny_unknown_fields)] pub(crate) struct HookRepositoryInternalTracker { enable_time_tracker: bool, allow_only_contributors_to_track_time: bool, enable_issue_dependencies: bool, } #[allow(dead_code)] #[derive(Debug, Deserialize)] #[serde(deny_unknown_fields)] pub(crate) struct HookCommit { id: String, message: String, url: String, author: HookGitUser, committer: HookGitUser, verification: Value, // Was null in test hook timestamp: String, // TODO: parse to datetime added: Vec, removed: Vec, modified: Vec, } #[allow(dead_code)] #[derive(Debug, Deserialize)] #[serde(deny_unknown_fields)] pub(crate) struct HookGitUser { name: String, email: String, username: String, } pub(crate) trait PipelineParamters { fn get_pull_base_ref(&self) -> Result, Box>; fn get_pull_base_sha(&self) -> Result, Box>; fn get_repo_url(&self) -> Result, Box>; fn get_repo_name(&self) -> Result, Box>; fn get_repo_owner(&self) -> Result, Box>; } impl PipelineParamters for HookPush { fn get_pull_base_ref(&self) -> Result, Box> { let ref_to_branch_regex = Regex::new(r"refs/(heads|tags)/(?P.+)")?; let captures = ref_to_branch_regex .captures(self.ref_field.as_str()) .ok_or("Could not find branch name.")?; let branch = &captures["branch"]; Ok(Cow::Owned(branch.to_owned())) } fn get_pull_base_sha(&self) -> Result, Box> { Ok(Cow::Borrowed(self.after.as_str())) } fn get_repo_url(&self) -> Result, Box> { Ok(Cow::Borrowed(self.repository.clone_url.as_str())) } fn get_repo_name(&self) -> Result, Box> { Ok(Cow::Borrowed(self.repository.name.as_str())) } fn get_repo_owner(&self) -> Result, Box> { Ok(Cow::Borrowed(self.repository.owner.username.as_str())) } }