1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use color_eyre::Result;
use open::that as open_in_browser;
use serde::{Deserialize, Serialize};
use crate::formatters::formatter::{Formatter, FormatterType};
use crate::vcs::{bitbucket::Bitbucket, github::GitHub, gitlab::GitLab};
#[derive(Debug, Default, Deserialize, Serialize)]
pub struct User {
pub id: String,
pub username: String,
}
#[derive(Debug, Deserialize, Serialize)]
pub enum PullRequestState {
Open,
Closed,
Merged,
Locked,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct PullRequest {
pub id: u32,
pub state: PullRequestState,
pub title: String,
pub description: String,
pub source: String,
pub target: String,
pub url: String,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub author: User,
pub closed_by: Option<User>,
pub reviewers: Option<Vec<User>>,
}
impl PullRequest {
pub fn print(&self, in_browser: bool, formatter_type: FormatterType) {
if in_browser && open_in_browser(&self.url).is_ok() {
return;
}
print!("{}", self.show(formatter_type));
}
}
#[derive(Debug, Deserialize, Serialize)]
pub struct CreatePullRequest {
pub title: String,
pub description: String,
pub source: String,
pub target: Option<String>,
pub close_source_branch: bool,
pub reviewers: Vec<String>,
}
#[derive(Debug, Default, Deserialize, Serialize)]
pub enum PullRequestUserFilter {
Me,
#[default]
All,
}
#[derive(Debug, Default, Deserialize, Serialize)]
pub enum PullRequestStateFilter {
#[default]
Open,
Closed,
Merged,
Locked,
All,
}
#[derive(Debug, Default, Deserialize, Serialize)]
pub struct ListPullRequestFilters {
pub author: PullRequestUserFilter,
pub state: PullRequestStateFilter,
}
#[derive(Debug, Default)]
pub struct VersionControlSettings {
pub auth: String,
pub vcs_type: Option<String>,
pub default_branch: Option<String>,
}
#[async_trait]
pub trait VersionControl {
fn init(hostname: String, repo: String, settings: VersionControlSettings) -> Self
where
Self: Sized;
fn login_url(&self) -> String;
fn validate_token(&self, token: &str) -> Result<()>;
async fn create_pr(&self, pr: CreatePullRequest) -> Result<PullRequest>;
async fn get_pr_by_id(&self, id: u32) -> Result<PullRequest>;
async fn get_pr_by_branch(&self, branch: &str) -> Result<PullRequest>;
async fn list_prs(&self, filters: ListPullRequestFilters) -> Result<Vec<PullRequest>>;
async fn approve_pr(&self, id: u32) -> Result<()>;
async fn close_pr(&self, id: u32) -> Result<PullRequest>;
async fn merge_pr(&self, id: u32, delete_source_branch: bool) -> Result<PullRequest>;
}
pub fn init_vcs(
hostname: String,
repo: String,
settings: VersionControlSettings,
) -> Box<dyn VersionControl> {
match (hostname.as_str(), &settings.vcs_type) {
("github.com", _) => Box::new(GitHub::init(hostname, repo, settings)),
("bitbucket.org", _) => Box::new(Bitbucket::init(hostname, repo, settings)),
(_, _) => Box::new(GitLab::init(hostname, repo, settings)),
}
}