|
1
|
use { |
|
2
|
crate::{cli::StatusCommand, context::Ctx, git, ui}, |
|
3
|
anyhow::{Context, Result}, |
|
4
|
sigit_si_api::NewStatus, |
|
5
|
}; |
|
6
|
|
|
7
|
pub async fn run(ctx: &Ctx, command: StatusCommand) -> Result<()> { |
|
8
|
ctx.require_token()?; |
|
9
|
|
|
10
|
match command { |
|
11
|
StatusCommand::Set { |
|
12
|
sha, |
|
13
|
state, |
|
14
|
context, |
|
15
|
description, |
|
16
|
url, |
|
17
|
repo, |
|
18
|
} => set(ctx, sha, state, context, description, url, repo).await, |
|
19
|
StatusCommand::View { sha, repo } => view(ctx, sha, repo).await, |
|
20
|
} |
|
21
|
} |
|
22
|
|
|
23
|
fn resolve(rev: &str) -> Result<String> { |
|
24
|
let cwd = std::env::current_dir().context("Could not read the current directory.")?; |
|
25
|
git::resolve_sha(&cwd, rev) |
|
26
|
} |
|
27
|
|
|
28
|
async fn set( |
|
29
|
ctx: &Ctx, |
|
30
|
rev: String, |
|
31
|
state: String, |
|
32
|
context: Option<String>, |
|
33
|
description: Option<String>, |
|
34
|
url: Option<String>, |
|
35
|
repo: Option<String>, |
|
36
|
) -> Result<()> { |
|
37
|
let slug = ctx.resolve_repo(repo)?; |
|
38
|
let sha = resolve(&rev)?; |
|
39
|
|
|
40
|
let status = NewStatus::new(state) |
|
41
|
.context(context) |
|
42
|
.description(description) |
|
43
|
.target_url(url); |
|
44
|
let created = ctx |
|
45
|
.client |
|
46
|
.create_status(&slug.owner, &slug.name, &sha, &status) |
|
47
|
.await?; |
|
48
|
|
|
49
|
ui::emit(ctx.json, &created, |created| { |
|
50
|
ui::success(format!( |
|
51
|
"Reported {} for {} on {}.", |
|
52
|
created.state, |
|
53
|
ui::highlight(&created.context), |
|
54
|
&created.sha[..created.sha.len().min(12)] |
|
55
|
)); |
|
56
|
}) |
|
57
|
} |
|
58
|
|
|
59
|
async fn view(ctx: &Ctx, rev: Option<String>, repo: Option<String>) -> Result<()> { |
|
60
|
let slug = ctx.resolve_repo(repo)?; |
|
61
|
let sha = resolve(rev.as_deref().unwrap_or("HEAD"))?; |
|
62
|
|
|
63
|
let combined = ctx |
|
64
|
.client |
|
65
|
.combined_status(&slug.owner, &slug.name, &sha) |
|
66
|
.await?; |
|
67
|
ui::emit(ctx.json, &combined, ui::render::combined_status) |
|
68
|
} |