Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ enum TokenCommand {
/// Comma-separated repo UUIDs for SELECTED_REPO.
#[arg(long)]
repository_ids: Option<String>,
/// Comma-separated Amendable branch names this token may push. Empty means all branches.
#[arg(long)]
branches: Option<String>,
#[arg(long)]
json: bool,
},
Expand Down Expand Up @@ -244,12 +247,14 @@ fn execute(cli: Cli) -> Result<(), Error> {
scope,
grants,
repository_ids,
branches,
json,
} => token_create(
name.as_deref(),
&scope,
&grants,
repository_ids.as_deref(),
branches.as_deref(),
json,
),
TokenCommand::Delete { token_id } => token_delete(&token_id),
Expand Down Expand Up @@ -559,13 +564,21 @@ fn token_create(
scope: &str,
grants: &str,
repository_ids: Option<&str>,
branches: Option<&str>,
json: bool,
) -> Result<(), Error> {
let cfg = Config::load()?;
let grant_list = split_csv(grants);
let repo_ids = repository_ids.map(split_csv);
let allowed_branches = branches.map(split_csv);
let client = Client::new(&cfg.api_url, Some(cfg.require_token()?))?;
let created = client.create_access_token(name, scope, &grant_list, repo_ids.as_deref())?;
let created = client.create_access_token(
name,
scope,
&grant_list,
repo_ids.as_deref(),
allowed_branches.as_deref(),
)?;
if json {
print_json(&created)?;
return Ok(());
Expand Down
4 changes: 4 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ impl Client {
scope: &str,
grants: &[String],
repository_ids: Option<&[String]>,
allowed_branches: Option<&[String]>,
) -> Result<Value, Error> {
let mut body = serde_json::json!({
"name": name,
Expand All @@ -196,6 +197,9 @@ impl Client {
if let Some(ids) = repository_ids.filter(|v| !v.is_empty()) {
body["repository_ids"] = serde_json::json!(ids);
}
if let Some(branches) = allowed_branches.filter(|v| !v.is_empty()) {
body["allowed_branches"] = serde_json::json!(branches);
}
self.request(
reqwest::Method::POST,
"/v1/access-tokens",
Expand Down
16 changes: 16 additions & 0 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,22 @@ fn token_create_and_list() {
.assert()
.success()
.stdout(contains("NEWTOKENSHOWNONCE"));
harness
.cmd()
.args([
"token",
"create",
"--name",
"main-only",
"--grants",
"GIT_HTTP_WRITE",
"--branches",
"main,release/1.0",
"--json",
])
.assert()
.success()
.stdout(contains("main").and(contains("release/1.0")));
harness
.cmd()
.args(["token", "list", "--json"])
Expand Down
1 change: 1 addition & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ fn do_post(
"name": body.get("name"),
"scope": body.get("scope"),
"grants": body.get("grants"),
"allowed_branches": body.get("allowed_branches").cloned().unwrap_or(json!([])),
"token": "NEWTOKENSHOWNONCE",
"repositories": [],
});
Expand Down
Loading