clickupc-core 1.2.0

Unofficial ClickUp API client library powering clickupc
Documentation
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! Config file (`~/.config/clickupc/config.toml`) with named profiles and
//! `CLICKUP_*` env overrides. Priority: env > selected profile > defaults.

use std::{collections::BTreeMap, path::PathBuf};

use serde::{Deserialize, Serialize};

use crate::error::{ClickUpError, Result};

pub const DEFAULT_BASE_URL: &str = "https://api.clickup.com/api";
pub const DEFAULT_PROFILE: &str = "default";
pub const DEFAULT_TIMEOUT_SECS: u64 = 30;

#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct ConfigFile {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub current_profile: Option<String>,
    #[serde(default)]
    pub profiles: BTreeMap<String, Profile>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct Profile {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub token: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub workspace_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default_list: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub timezone: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub timeout_secs: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default_task_limit: Option<usize>,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub aliases: BTreeMap<String, String>,
    /// Workspace id → token, for accounts whose tokens each reach a single
    /// workspace. `token` is the fallback for workspaces not listed here.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub tokens: BTreeMap<String, String>,
}

impl Profile {
    /// Store `token` for `workspace_id` and make it the default. A legacy
    /// single token is kept under its own workspace so it keeps working.
    pub fn add_token(&mut self, workspace_id: &str, token: String) {
        if let (Some(old), Some(ws)) = (&self.token, &self.workspace_id) {
            self.tokens.entry(ws.clone()).or_insert_with(|| old.clone());
        }
        self.tokens.insert(workspace_id.to_string(), token.clone());
        self.token = Some(token);
        self.workspace_id = Some(workspace_id.to_string());
    }
}

fn env(key: &str) -> Option<String> {
    std::env::var(key)
        .ok()
        .map(|v| v.trim().to_string())
        .filter(|v| !v.is_empty())
}

fn env_parse<T: std::str::FromStr>(key: &str) -> Result<Option<T>> {
    match env(key) {
        None => Ok(None),
        Some(v) => v
            .parse()
            .map(Some)
            .map_err(|_| ClickUpError::Config(format!("{key} has an invalid value '{v}'"))),
    }
}

impl ConfigFile {
    /// `$CLICKUP_CONFIG_DIR/config.toml`, else `<config dir>/clickupc/config.toml`.
    pub fn path() -> Result<PathBuf> {
        let dir = match env("CLICKUP_CONFIG_DIR") {
            Some(dir) => PathBuf::from(dir),
            None => dirs::config_dir()
                .ok_or_else(|| ClickUpError::Config("cannot locate a config directory".into()))?
                .join("clickupc"),
        };
        Ok(dir.join("config.toml"))
    }

    pub fn load() -> Result<Self> {
        let path = Self::path()?;
        if !path.exists() {
            return Ok(Self::default());
        }
        warn_if_world_readable(&path);
        let text = std::fs::read_to_string(&path)?;
        toml::from_str(&text).map_err(|e| ClickUpError::Config(format!("{}: {e}", path.display())))
    }

    pub fn save(&self) -> Result<PathBuf> {
        let path = Self::path()?;
        if let Some(dir) = path.parent() {
            std::fs::create_dir_all(dir)?;
        }
        let text = toml::to_string_pretty(self).map_err(|e| ClickUpError::Config(e.to_string()))?;
        std::fs::write(&path, text)?;
        restrict_permissions(&path)?;
        Ok(path)
    }

    /// `CLICKUP_PROFILE` > `current_profile` > `default`.
    pub fn selected_profile_name(&self) -> String {
        env("CLICKUP_PROFILE")
            .or_else(|| self.current_profile.clone())
            .unwrap_or_else(|| DEFAULT_PROFILE.to_string())
    }

    pub fn profile_mut(&mut self, name: &str) -> &mut Profile {
        self.profiles.entry(name.to_string()).or_default()
    }
}

/// `"Acme Corp (EU)"` → `"acme-corp-eu"`, for suggesting profile names.
pub fn profile_slug(name: &str) -> String {
    let mut out = String::new();
    for c in name.trim().chars() {
        if c.is_ascii_alphanumeric() {
            out.push(c.to_ascii_lowercase());
        } else if !out.ends_with('-') && !out.is_empty() {
            out.push('-');
        }
    }
    let out = out.trim_end_matches('-').to_string();
    if out.is_empty() {
        DEFAULT_PROFILE.to_string()
    } else {
        out
    }
}

#[cfg(unix)]
fn restrict_permissions(path: &std::path::Path) -> Result<()> {
    use std::os::unix::fs::PermissionsExt;
    std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600))?;
    Ok(())
}

#[cfg(not(unix))]
fn restrict_permissions(_path: &std::path::Path) -> Result<()> {
    Ok(())
}

#[cfg(unix)]
fn warn_if_world_readable(path: &std::path::Path) {
    use std::os::unix::fs::PermissionsExt;
    if let Ok(meta) = std::fs::metadata(path) {
        if meta.permissions().mode() & 0o077 != 0 {
            tracing::warn!(
                "{} is readable by other users; run `chmod 600` on it",
                path.display()
            );
        }
    }
}

#[cfg(not(unix))]
fn warn_if_world_readable(_path: &std::path::Path) {}

/// Effective settings after merging the selected profile with env overrides.
#[derive(Debug, Clone)]
pub struct ClickUpConfig {
    pub profile: String,
    pub token: Option<String>,
    pub workspace_id: Option<String>,
    pub default_list: Option<String>,
    pub timezone: Option<String>,
    pub timeout_secs: u64,
    pub default_task_limit: Option<usize>,
    pub aliases: BTreeMap<String, String>,
    /// Per-workspace tokens of the profile; empty when `CLICKUP_TOKEN` is set.
    pub tokens: BTreeMap<String, String>,
    pub base_url: String,
    pub cache_dir: PathBuf,
    pub config_path: PathBuf,
}

impl ClickUpConfig {
    /// Load using the profile chosen by env / `current_profile`.
    pub fn load() -> Result<Self> {
        Self::load_profile(None)
    }

    /// Load an explicit profile (e.g. `--profile work`), falling back to the
    /// usual selection when `None`.
    pub fn load_profile(profile: Option<&str>) -> Result<Self> {
        let file = ConfigFile::load()?;
        Self::from_file(&file, profile)
    }

    pub fn from_file(file: &ConfigFile, profile: Option<&str>) -> Result<Self> {
        let name = profile
            .map(str::to_string)
            .unwrap_or_else(|| file.selected_profile_name());
        let p = file.profiles.get(&name).cloned().unwrap_or_default();
        let cache_dir = match env("CLICKUP_CACHE_DIR") {
            Some(dir) => PathBuf::from(dir),
            None => dirs::cache_dir()
                .unwrap_or_else(std::env::temp_dir)
                .join("clickupc"),
        };
        let env_token = env("CLICKUP_TOKEN");
        let tokens = if env_token.is_some() {
            BTreeMap::new()
        } else {
            p.tokens
        };
        let workspace_id = env("CLICKUP_WORKSPACE_ID").or(p.workspace_id);
        let token = env_token
            .or_else(|| workspace_id.as_ref().and_then(|w| tokens.get(w).cloned()))
            .or(p.token);
        Ok(Self {
            profile: name,
            token,
            workspace_id,
            tokens,
            default_list: env("CLICKUP_DEFAULT_LIST").or(p.default_list),
            timezone: env("CLICKUP_TIMEZONE").or(p.timezone),
            timeout_secs: env_parse("CLICKUP_TIMEOUT_SECS")?
                .or(p.timeout_secs)
                .unwrap_or(DEFAULT_TIMEOUT_SECS),
            default_task_limit: env_parse("CLICKUP_TASK_LIMIT")?.or(p.default_task_limit),
            aliases: p.aliases,
            base_url: env("CLICKUP_BASE_URL").unwrap_or_else(|| DEFAULT_BASE_URL.to_string()),
            cache_dir,
            config_path: ConfigFile::path()?,
        })
    }

    /// Minimal config for tests and embedding: token + base URL, no file.
    pub fn with_token(token: impl Into<String>, base_url: impl Into<String>) -> Self {
        Self {
            profile: DEFAULT_PROFILE.to_string(),
            token: Some(token.into()),
            workspace_id: None,
            default_list: None,
            timezone: None,
            timeout_secs: DEFAULT_TIMEOUT_SECS,
            default_task_limit: None,
            aliases: BTreeMap::new(),
            tokens: BTreeMap::new(),
            base_url: base_url.into(),
            cache_dir: std::env::temp_dir().join("clickupc"),
            config_path: PathBuf::new(),
        }
    }

    pub fn token(&self) -> Result<&str> {
        self.token.as_deref().ok_or_else(|| {
            ClickUpError::Auth(
                "no API token configured; run `clickupc auth login` or set CLICKUP_TOKEN".into(),
            )
        })
    }

    pub fn workspace_id(&self) -> Result<&str> {
        self.workspace_id.as_deref().ok_or_else(|| {
            ClickUpError::Config(
                "no workspace selected; run `clickupc workspace use <name>` or set CLICKUP_WORKSPACE_ID"
                    .into(),
            )
        })
    }

    /// Switch to `workspace_id`, and to its own token when the profile has one.
    pub fn set_workspace(&mut self, workspace_id: impl Into<String>) {
        let id = workspace_id.into();
        if let Some(t) = self.tokens.get(&id) {
            self.token = Some(t.clone());
        }
        self.workspace_id = Some(id);
    }

    /// Every distinct token of the profile, the active one first.
    pub fn all_tokens(&self) -> Vec<String> {
        let mut out: Vec<String> = self.token.iter().cloned().collect();
        for t in self.tokens.values() {
            if !out.contains(t) {
                out.push(t.clone());
            }
        }
        out
    }

    /// `pk_12…AB` style preview that is safe to print.
    pub fn masked_token(&self) -> Option<String> {
        self.token.as_deref().map(mask_token)
    }
}

pub fn mask_token(token: &str) -> String {
    let chars: Vec<char> = token.chars().collect();
    if chars.len() <= 8 {
        return "***".into();
    }
    let head: String = chars[..5].iter().collect();
    let tail: String = chars[chars.len() - 2..].iter().collect();
    format!("{head}…{tail}")
}

#[cfg(test)]
mod tests {
    use super::*;
    use serial_test::serial;

    const VARS: &[&str] = &[
        "CLICKUP_CONFIG_DIR",
        "CLICKUP_TOKEN",
        "CLICKUP_WORKSPACE_ID",
        "CLICKUP_PROFILE",
        "CLICKUP_DEFAULT_LIST",
        "CLICKUP_TIMEOUT_SECS",
        "CLICKUP_TASK_LIMIT",
        "CLICKUP_BASE_URL",
        "CLICKUP_TIMEZONE",
    ];

    fn isolated() -> tempfile::TempDir {
        for v in VARS {
            std::env::remove_var(v);
        }
        let dir = tempfile::tempdir().unwrap();
        std::env::set_var("CLICKUP_CONFIG_DIR", dir.path());
        dir
    }

    #[test]
    #[serial]
    fn save_load_round_trip() {
        let _dir = isolated();
        let mut file = ConfigFile {
            current_profile: Some("work".into()),
            ..Default::default()
        };
        let p = file.profile_mut("work");
        p.token = Some("pk_abc".into());
        p.workspace_id = Some("901".into());
        p.aliases.insert("sprint".into(), "123".into());
        let _path = file.save().unwrap();
        assert_eq!(ConfigFile::load().unwrap(), file);

        #[cfg(unix)]
        {
            let path = _path;
            use std::os::unix::fs::PermissionsExt;
            let mode = std::fs::metadata(path).unwrap().permissions().mode();
            assert_eq!(mode & 0o777, 0o600);
        }
    }

    #[test]
    #[serial]
    fn env_overrides_profile() {
        let _dir = isolated();
        let mut file = ConfigFile::default();
        file.profile_mut("default").token = Some("pk_file".into());
        file.profile_mut("default").workspace_id = Some("1".into());
        file.save().unwrap();

        std::env::set_var("CLICKUP_TOKEN", "pk_env");
        std::env::set_var("CLICKUP_TIMEOUT_SECS", "5");
        let cfg = ClickUpConfig::load().unwrap();
        assert_eq!(cfg.token.as_deref(), Some("pk_env"));
        assert_eq!(cfg.workspace_id.as_deref(), Some("1"));
        assert_eq!(cfg.timeout_secs, 5);
        assert_eq!(cfg.base_url, DEFAULT_BASE_URL);
        isolated();
    }

    #[test]
    #[serial]
    fn profile_selection_priority() {
        let _dir = isolated();
        let mut file = ConfigFile {
            current_profile: Some("a".into()),
            ..Default::default()
        };
        file.profile_mut("a").workspace_id = Some("A".into());
        file.profile_mut("b").workspace_id = Some("B".into());

        assert_eq!(
            ClickUpConfig::from_file(&file, None).unwrap().workspace_id,
            Some("A".into())
        );
        std::env::set_var("CLICKUP_PROFILE", "b");
        assert_eq!(
            ClickUpConfig::from_file(&file, None).unwrap().workspace_id,
            Some("B".into())
        );
        assert_eq!(
            ClickUpConfig::from_file(&file, Some("a"))
                .unwrap()
                .workspace_id,
            Some("A".into())
        );
        isolated();
    }

    #[test]
    #[serial]
    fn missing_token_is_auth_error() {
        let _dir = isolated();
        let cfg = ClickUpConfig::load().unwrap();
        assert!(matches!(cfg.token(), Err(ClickUpError::Auth(_))));
        assert!(matches!(cfg.workspace_id(), Err(ClickUpError::Config(_))));
    }

    #[test]
    #[serial]
    fn per_workspace_tokens() {
        let _dir = isolated();
        let mut file = ConfigFile::default();
        let p = file.profile_mut("default");
        p.token = Some("pk_one".into());
        p.workspace_id = Some("1".into());
        p.add_token("2", "pk_two".into());
        assert_eq!(p.tokens.get("1").map(String::as_str), Some("pk_one"));
        assert_eq!(p.token.as_deref(), Some("pk_two"));

        let mut cfg = ClickUpConfig::from_file(&file, None).unwrap();
        assert_eq!(cfg.token.as_deref(), Some("pk_two"));
        assert_eq!(cfg.all_tokens(), vec!["pk_two", "pk_one"]);
        cfg.set_workspace("1");
        assert_eq!(cfg.token.as_deref(), Some("pk_one"));
        cfg.set_workspace("3");
        assert_eq!(cfg.token.as_deref(), Some("pk_one"));

        std::env::set_var("CLICKUP_TOKEN", "pk_env");
        let mut cfg = ClickUpConfig::from_file(&file, None).unwrap();
        cfg.set_workspace("1");
        assert_eq!(cfg.token.as_deref(), Some("pk_env"));
        isolated();
    }

    #[test]
    fn slugs() {
        assert_eq!(profile_slug("Acme Corp (EU)"), "acme-corp-eu");
        assert_eq!(profile_slug("  Side Project  "), "side-project");
        assert_eq!(profile_slug("!!!"), "default");
    }

    #[test]
    fn masks_token() {
        assert_eq!(mask_token("pk_1234567890AB"), "pk_12…AB");
        assert_eq!(mask_token("short"), "***");
    }
}