-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathworkspace.rs
More file actions
159 lines (144 loc) · 4.34 KB
/
workspace.rs
File metadata and controls
159 lines (144 loc) · 4.34 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use chrono::{DateTime, Utc};
use serde::Serialize;
use std::fmt::Display;
use super::{
queries::user_projects::{
UserProjectsExternalWorkspaces, UserProjectsExternalWorkspacesProjects,
UserProjectsMeWorkspaces, UserProjectsMeWorkspacesProjectsEdgesNode,
},
*,
};
pub async fn workspaces() -> Result<Vec<Workspace>> {
let configs = Configs::new()?;
let vars = queries::user_projects::Variables {};
let client = GQLClient::new_authorized_with_scope(&configs, AuthScope::GlobalOnly)?;
let response =
post_graphql::<queries::UserProjects, _>(&client, configs.get_backboard(), vars).await?;
let mut workspaces: Vec<Workspace> = response
.me
.workspaces
.into_iter()
.map(Workspace::Member)
.collect();
workspaces.extend(
response
.external_workspaces
.into_iter()
.map(Workspace::External),
);
workspaces.sort_by(|a, b| b.id().cmp(a.id()));
Ok(workspaces)
}
#[derive(Debug, Clone)]
pub enum Workspace {
External(UserProjectsExternalWorkspaces),
Member(UserProjectsMeWorkspaces),
}
impl Workspace {
pub fn id(&self) -> &str {
match self {
Self::External(w) => w.id.as_str(),
Self::Member(w) => w.id.as_str(),
}
}
pub fn name(&self) -> &str {
match self {
Self::External(w) => w.name.as_str(),
Self::Member(w) => w.name.as_str(),
}
}
#[allow(deprecated)] // team field deprecated but needed for backwards compat with scripts using team IDs
pub fn team_id(&self) -> Option<&str> {
match self {
Self::External(w) => w.team_id.as_deref(),
Self::Member(w) => w.team.as_ref().map(|t| t.id.as_str()),
}
}
pub fn projects(&self) -> Vec<Project> {
let mut projects: Vec<_> = match self {
Self::External(w) => w.projects.iter().cloned().map(Project::External).collect(),
Self::Member(w) => w
.projects
.edges
.iter()
.cloned()
.map(|e| Project::Workspace(e.node))
.collect(),
};
projects.sort_by_key(|b| std::cmp::Reverse(b.updated_at()));
projects
}
pub fn projects_with_workspace(&self) -> Vec<ProjectWithWorkspace> {
let workspace_info = WorkspaceInfo {
id: self.id().to_string(),
name: self.name().to_string(),
};
self.projects()
.into_iter()
.map(|project| ProjectWithWorkspace {
workspace: workspace_info.clone(),
project,
})
.collect()
}
}
impl Display for Workspace {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let name = match self {
Self::External(w) => w.name.as_str(),
Self::Member(w) => w.name.as_str(),
};
write!(f, "{name}")
}
}
#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum Project {
External(UserProjectsExternalWorkspacesProjects),
Workspace(UserProjectsMeWorkspacesProjectsEdgesNode),
}
impl Project {
pub fn id(&self) -> &str {
match self {
Self::External(w) => &w.id,
Self::Workspace(w) => &w.id,
}
}
pub fn name(&self) -> &str {
match self {
Self::External(w) => &w.name,
Self::Workspace(w) => &w.name,
}
}
pub fn updated_at(&self) -> DateTime<Utc> {
match self {
Self::External(w) => w.updated_at,
Self::Workspace(w) => w.updated_at,
}
}
pub fn deleted_at(&self) -> Option<DateTime<Utc>> {
match self {
Self::External(w) => w.deleted_at,
Self::Workspace(w) => w.deleted_at,
}
}
}
impl Display for Project {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Workspace(project) => write!(f, "{}", project.name),
Self::External(project) => write!(f, "{}", project.name),
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct WorkspaceInfo {
pub id: String,
pub name: String,
}
#[derive(Debug, Clone, Serialize)]
pub struct ProjectWithWorkspace {
pub workspace: WorkspaceInfo,
#[serde(flatten)]
pub project: Project,
}