新增个人访问令牌、使用统计与配置校验功能

This commit is contained in:
2026-01-31 17:14:58 +08:00
parent 1cbb01ccc4
commit cb24b8ae85
10 changed files with 980 additions and 149 deletions

View File

@@ -1,5 +1,6 @@
use anyhow::{bail, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
/// Git profile containing user identity and authentication settings
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -40,6 +41,14 @@ pub struct GitProfile {
/// Company/Organization name (for work profiles)
#[serde(default)]
pub organization: Option<String>,
/// Personal Access Tokens (PATs) for different services
#[serde(default)]
pub tokens: HashMap<String, TokenConfig>,
/// Usage statistics
#[serde(default)]
pub usage: UsageStats,
}
impl GitProfile {
@@ -56,6 +65,8 @@ impl GitProfile {
description: None,
is_work: false,
organization: None,
tokens: HashMap::new(),
usage: UsageStats::default(),
}
}
@@ -84,6 +95,10 @@ impl GitProfile {
gpg.validate()?;
}
for token in self.tokens.values() {
token.validate()?;
}
Ok(())
}
@@ -97,6 +112,11 @@ impl GitProfile {
self.gpg.is_some() || self.signing_key.is_some()
}
/// Check if profile has any tokens configured
pub fn has_tokens(&self) -> bool {
!self.tokens.is_empty()
}
/// Get signing key (from GPG config or direct)
pub fn signing_key(&self) -> Option<&str> {
self.signing_key
@@ -105,15 +125,44 @@ impl GitProfile {
.or_else(|| self.gpg.as_ref().map(|g| g.key_id.as_str()))
}
/// Apply this profile to a git repository
/// Add a token to the profile
pub fn add_token(&mut self, service: String, token: TokenConfig) {
self.tokens.insert(service, token);
}
/// Get a token for a specific service
pub fn get_token(&self, service: &str) -> Option<&TokenConfig> {
self.tokens.get(service)
}
/// Remove a token from the profile
pub fn remove_token(&mut self, service: &str) {
self.tokens.remove(service);
}
/// Record usage of this profile
pub fn record_usage(&mut self, repo_path: Option<String>) {
self.usage.last_used = Some(chrono::Utc::now().to_rfc3339());
self.usage.total_uses += 1;
if let Some(repo) = repo_path {
let count = self.usage.repo_usage.entry(repo).or_insert(0);
*count += 1;
}
}
/// Get usage statistics
pub fn usage_stats(&self) -> &UsageStats {
&self.usage
}
/// Apply this profile to a git repository (local config)
pub fn apply_to_repo(&self, repo: &git2::Repository) -> Result<()> {
let mut config = repo.config()?;
// Set user info
config.set_str("user.name", &self.user_name)?;
config.set_str("user.email", &self.user_email)?;
// Set signing key if available
if let Some(key) = self.signing_key() {
config.set_str("user.signingkey", key)?;
@@ -126,7 +175,6 @@ impl GitProfile {
}
}
// Set SSH if configured
if let Some(ref ssh) = self.ssh {
if let Some(ref key_path) = ssh.private_key_path {
config.set_str("core.sshCommand",
@@ -150,6 +198,52 @@ impl GitProfile {
Ok(())
}
/// Compare with current git configuration
pub fn compare_with_git_config(&self, repo: &git2::Repository) -> Result<ProfileComparison> {
let config = repo.config()?;
let git_user_name = config.get_string("user.name").ok();
let git_user_email = config.get_string("user.email").ok();
let git_signing_key = config.get_string("user.signingkey").ok();
let mut comparison = ProfileComparison {
profile_name: self.name.clone(),
matches: true,
differences: vec![],
};
if git_user_name.as_deref() != Some(&self.user_name) {
comparison.matches = false;
comparison.differences.push(ConfigDifference {
key: "user.name".to_string(),
profile_value: self.user_name.clone(),
git_value: git_user_name.unwrap_or_else(|| "<not set>".to_string()),
});
}
if git_user_email.as_deref() != Some(&self.user_email) {
comparison.matches = false;
comparison.differences.push(ConfigDifference {
key: "user.email".to_string(),
profile_value: self.user_email.clone(),
git_value: git_user_email.unwrap_or_else(|| "<not set>".to_string()),
});
}
if let Some(profile_key) = self.signing_key() {
if git_signing_key.as_deref() != Some(profile_key) {
comparison.matches = false;
comparison.differences.push(ConfigDifference {
key: "user.signingkey".to_string(),
profile_value: profile_key.to_string(),
git_value: git_signing_key.unwrap_or_else(|| "<not set>".to_string()),
});
}
}
Ok(comparison)
}
}
/// Profile settings
@@ -285,6 +379,122 @@ impl GpgConfig {
}
}
/// Token configuration for services (GitHub, GitLab, etc.)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenConfig {
/// Token value (encrypted)
#[serde(skip_serializing_if = "Option::is_none")]
pub token: Option<String>,
/// Token type (personal, oauth, etc.)
#[serde(default)]
pub token_type: TokenType,
/// Token scopes/permissions
#[serde(default)]
pub scopes: Vec<String>,
/// Expiration date (RFC3339)
#[serde(default)]
pub expires_at: Option<String>,
/// Last used timestamp
#[serde(default)]
pub last_used: Option<String>,
/// Description
#[serde(default)]
pub description: Option<String>,
}
impl TokenConfig {
/// Create a new token config
pub fn new(token: String, token_type: TokenType) -> Self {
Self {
token: Some(token),
token_type,
scopes: vec![],
expires_at: None,
last_used: None,
description: None,
}
}
/// Validate token configuration
pub fn validate(&self) -> Result<()> {
if self.token.is_none() && self.token_type != TokenType::None {
bail!("Token value is required for {:?}", self.token_type);
}
Ok(())
}
/// Record token usage
pub fn record_usage(&mut self) {
self.last_used = Some(chrono::Utc::now().to_rfc3339());
}
}
/// Token type
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum TokenType {
None,
Personal,
OAuth,
Deploy,
App,
}
impl Default for TokenType {
fn default() -> Self {
Self::None
}
}
impl std::fmt::Display for TokenType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TokenType::None => write!(f, "none"),
TokenType::Personal => write!(f, "personal"),
TokenType::OAuth => write!(f, "oauth"),
TokenType::Deploy => write!(f, "deploy"),
TokenType::App => write!(f, "app"),
}
}
}
/// Usage statistics for a profile
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct UsageStats {
/// Total number of times this profile has been used
#[serde(default)]
pub total_uses: u64,
/// Last used timestamp (RFC3339)
#[serde(default)]
pub last_used: Option<String>,
/// Repository-specific usage counts
#[serde(default)]
pub repo_usage: HashMap<String, u64>,
}
/// Comparison result between profile and git configuration
#[derive(Debug, Clone)]
pub struct ProfileComparison {
pub profile_name: String,
pub matches: bool,
pub differences: Vec<ConfigDifference>,
}
/// Configuration difference
#[derive(Debug, Clone)]
pub struct ConfigDifference {
pub key: String,
pub profile_value: String,
pub git_value: String,
}
fn default_gpg_program() -> String {
"gpg".to_string()
}
@@ -306,6 +516,7 @@ pub struct GitProfileBuilder {
description: Option<String>,
is_work: bool,
organization: Option<String>,
tokens: HashMap<String, TokenConfig>,
}
impl GitProfileBuilder {
@@ -359,6 +570,11 @@ impl GitProfileBuilder {
self
}
pub fn token(mut self, service: impl Into<String>, token: TokenConfig) -> Self {
self.tokens.insert(service.into(), token);
self
}
pub fn build(self) -> Result<GitProfile> {
let name = self.name.ok_or_else(|| anyhow::anyhow!("Name is required"))?;
let user_name = self.user_name.ok_or_else(|| anyhow::anyhow!("User name is required"))?;
@@ -375,6 +591,8 @@ impl GitProfileBuilder {
description: self.description,
is_work: self.is_work,
organization: self.organization,
tokens: self.tokens,
usage: UsageStats::default(),
})
}
}
@@ -409,4 +627,10 @@ mod tests {
assert!(profile.validate().is_err());
}
#[test]
fn test_token_config() {
let token = TokenConfig::new("test-token".to_string(), TokenType::Personal);
assert!(token.validate().is_ok());
}
}