Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# Variables), baked into the build so the sign-in dialog doesn't ask for them:
# KIVI_GITHUB_CLIENT_ID the GitHub App's Client ID (Iv23…)
# KIVI_GITHUB_APP_SLUG its URL name (github.com/apps/<slug>)
# KIVI_GITLAB_CLIENT_ID the gitlab.com OAuth application's ID (device flow)

name: CI

Expand Down Expand Up @@ -53,6 +54,7 @@ jobs:
run: |
gjs -m tests/models.js
gjs -m tests/review.js
gjs -m tests/gitlab.js
gjs -m tests/diff.js
gjs -m tests/nets.js

Expand Down Expand Up @@ -134,6 +136,7 @@ jobs:
env:
KIVI_GITHUB_CLIENT_ID: ${{ vars.KIVI_GITHUB_CLIENT_ID }}
KIVI_GITHUB_APP_SLUG: ${{ vars.KIVI_GITHUB_APP_SLUG }}
KIVI_GITLAB_CLIENT_ID: ${{ vars.KIVI_GITLAB_CLIENT_ID }}
APPIMAGE_EXTRACT_AND_RUN: "1" # the AppImage tooling runs without FUSE
run: cargo tauri build

Expand Down Expand Up @@ -178,6 +181,7 @@ jobs:
env:
KIVI_GITHUB_CLIENT_ID: ${{ vars.KIVI_GITHUB_CLIENT_ID }}
KIVI_GITHUB_APP_SLUG: ${{ vars.KIVI_GITHUB_APP_SLUG }}
KIVI_GITLAB_CLIENT_ID: ${{ vars.KIVI_GITLAB_CLIENT_ID }}
run: cargo tauri build --no-bundle

- name: Name the .exe after the version
Expand Down Expand Up @@ -229,6 +233,7 @@ jobs:
env:
KIVI_GITHUB_CLIENT_ID: ${{ vars.KIVI_GITHUB_CLIENT_ID }}
KIVI_GITHUB_APP_SLUG: ${{ vars.KIVI_GITHUB_APP_SLUG }}
KIVI_GITLAB_CLIENT_ID: ${{ vars.KIVI_GITLAB_CLIENT_ID }}
run: cargo tauri build --target universal-apple-darwin

- uses: actions/upload-artifact@v4
Expand Down
159 changes: 110 additions & 49 deletions CLAUDE.md

Large diffs are not rendered by default.

112 changes: 68 additions & 44 deletions README.md

Large diffs are not rendered by default.

202 changes: 138 additions & 64 deletions docs/README.md

Large diffs are not rendered by default.

Binary file modified docs/screenshots/home-projects.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/screenshots/issues.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/screenshots/pr-closes-issue.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/screenshots/pull-requests.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/screenshots/review-comment.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/screenshots/settings-kicad.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/screenshots/viewer-issues.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/screenshots/visual-diff.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions inbox.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<div class="inbox-head">
<h1 id="home-title">Projects</h1>
<div class="row gap">
<button id="reload" class="btn small" title="Reload every GitHub repository, including those removed from projects">Reload</button>
<button id="reload" class="btn small" title="Reload every repository, including those removed from projects">Reload</button>
</div>
</div>

Expand All @@ -47,12 +47,12 @@ <h1 id="home-title">Projects</h1>
</div>

<div id="home-prs" hidden>
<p id="signed-out" class="inbox-note" hidden>Sign in to see pull requests waiting for your review and your own pull requests. Remote projects in public repositories work without signing in.</p>
<p id="signed-out" class="inbox-note" hidden>Sign in to GitHub or GitLab to see the pull and merge requests waiting for your review, and your own. Remote projects in public repositories work without signing in.</p>
<div id="lists"></div>
</div>

<div id="home-issues" hidden>
<p class="inbox-note">Hardware change requests: GitHub issues labelled <span class="mono">kivi</span>, pinned to a part, pad, net or spot. Open one in a project with <b>Request change</b> (key C).</p>
<p class="inbox-note">Hardware change requests: GitHub or GitLab issues labelled <span class="mono">kivi</span>, pinned to a part, pad, net or spot. Open one in a project with <b>Request change</b> (key C).</p>
<div id="issue-lists"></div>
</div>
</section>
Expand Down
343 changes: 224 additions & 119 deletions src-tauri/src/git.rs

Large diffs are not rendered by default.

18 changes: 11 additions & 7 deletions src-tauri/src/git_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
//! this transport for `https://` (as git2-curl does with curl): libgit2 still
//! speaks the git protocol, and each step of it is one HTTP request here.
//!
//! The GitHub token travels in the remote URL's user part
//! (`https://x-access-token:TOKEN@github.com/…`), which libgit2 hands to
//! The forge token travels in the remote URL's user part
//! (`https://x-access-token:TOKEN@github.com/…`, `https://oauth2:TOKEN@gitlab.com/…`), which libgit2 hands to
//! `action`; it is moved into an `Authorization` header and never sent in a URL.

use std::io::{self, Read, Write};
Expand Down Expand Up @@ -103,11 +103,15 @@ impl Stream {
if status.is_success() {
return Ok(res);
}
let why = match status.as_u16() {
401 => "GitHub asked for authentication: sign in to KiVi",
403 => "GitHub refused access: the KiVi app may lack write access to this repository (Contents: read and write), or isn't installed on it",
404 => "repository not found, or private and KiVi can't reach it",
_ => "unexpected reply from GitHub",
let gitlab = self.url.starts_with("https://gitlab.com/");
let why = match (status.as_u16(), gitlab) {
(401, false) => "GitHub asked for authentication: sign in to GitHub in KiVi",
(403, false) => "GitHub refused access: the KiVi app may lack write access to this repository (Contents: read and write), or isn't installed on it",
(401, true) => "GitLab asked for authentication: sign in to GitLab in KiVi",
(403, true) => "GitLab refused access: your account needs the Developer role on this project, or the branch is protected",
(404, _) => "repository not found, or private and KiVi can't reach it",
(_, false) => "unexpected reply from GitHub",
(_, true) => "unexpected reply from GitLab",
};
Err(io::Error::other(format!("{why} (HTTP {status})")))
}
Expand Down
84 changes: 34 additions & 50 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,61 +211,48 @@ fn startup_folder() -> Option<String> {
Path::new(&arg).exists().then_some(arg)
}

// ---- GitHub sign-in (GitHub App, device flow) ----------------------------------
// github.com/login/* sends no CORS headers, so the web page can't call it itself.
// A GitHub App's user tokens expire after 8 h and come with a refresh token;
// device-flow tokens refresh without a client secret, so no server is needed.

/// GitHub App client ID baked in at build time (`KIVI_GITHUB_CLIENT_ID=Iv23… cargo tauri build`).
// ---- forge sign-in (OAuth device flow) ------------------------------------------
// github.com/login/* sends no CORS headers, so the web page can't call it itself;
// gitlab.com's /oauth does, but one path for every forge is simpler. Device-flow
// tokens refresh without a client secret, so no server is needed. Each forge's
// steps (which URL, which fields) live in its adapter (src/forge/*.js).

/// The only addresses `oauth_post` sends to: each forge's device-code and token endpoints.
const OAUTH_URLS: &[&str] = &[
"https://github.com/login/device/code",
"https://github.com/login/oauth/access_token",
"https://gitlab.com/oauth/authorize_device",
"https://gitlab.com/oauth/token",
];

/// Sign-in settings baked in at build time, per forge id:
/// `KIVI_GITHUB_CLIENT_ID=Iv23… KIVI_GITHUB_APP_SLUG=… KIVI_GITLAB_CLIENT_ID=… cargo tauri build`.
#[tauri::command]
fn github_client_id() -> Option<&'static str> {
option_env!("KIVI_GITHUB_CLIENT_ID").filter(|s| !s.is_empty())
fn forge_build_config() -> Value {
let env = |v: Option<&'static str>| v.filter(|s| !s.is_empty());
serde_json::json!({
"github": { "clientId": env(option_env!("KIVI_GITHUB_CLIENT_ID")), "appSlug": env(option_env!("KIVI_GITHUB_APP_SLUG")) },
"gitlab": { "clientId": env(option_env!("KIVI_GITLAB_CLIENT_ID")) },
})
}

/// The App's URL name (github.com/apps/<slug>), for the "install on your repositories" link.
/// POST a form to a forge's OAuth endpoint and return its JSON reply, errors included
/// (`{"error": "authorization_pending"}` comes with HTTP 400 on some forges).
#[tauri::command]
fn github_app_slug() -> Option<&'static str> {
option_env!("KIVI_GITHUB_APP_SLUG").filter(|s| !s.is_empty())
}

async fn github_form(url: &str, form: &[(&str, &str)]) -> Result<Value, String> {
async fn oauth_post(url: String, form: std::collections::HashMap<String, String>) -> Result<Value, String> {
if !OAUTH_URLS.contains(&url.as_str()) {
return Err(format!("not a sign-in address: {url}"));
}
let res = reqwest::Client::new()
.post(url)
.post(&url)
.header("Accept", "application/json")
.header("User-Agent", "KiVi-desktop")
.form(form)
.form(&form)
.send()
.await
.map_err(|e| e.to_string())?;
let status = res.status();
let body: Value = res.json().await.map_err(|e| format!("GitHub {status}: {e}"))?;
Ok(body)
}

/// Step 1: a code for the user to enter at github.com/login/device.
#[tauri::command]
async fn github_device_start(client_id: String) -> Result<Value, String> {
github_form("https://github.com/login/device/code", &[("client_id", &client_id)]).await
}

/// Step 2 (repeat every `interval` s): `access_token` (+ `expires_in`, `refresh_token`, `refresh_token_expires_in`), or `error` = authorization_pending / slow_down / expired_token / access_denied.
#[tauri::command]
async fn github_device_poll(client_id: String, device_code: String) -> Result<Value, String> {
github_form(
"https://github.com/login/oauth/access_token",
&[("client_id", &client_id), ("device_code", &device_code), ("grant_type", "urn:ietf:params:oauth:grant-type:device_code")],
)
.await
}

/// A new token pair before the old one expires. The refresh token is single use.
#[tauri::command]
async fn github_device_refresh(client_id: String, refresh_token: String) -> Result<Value, String> {
github_form(
"https://github.com/login/oauth/access_token",
&[("client_id", &client_id), ("refresh_token", &refresh_token), ("grant_type", "refresh_token")],
)
.await
res.json().await.map_err(|e| format!("HTTP {status}: {e}"))
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
Expand Down Expand Up @@ -302,15 +289,12 @@ pub fn run() {
git::git_remote_state,
git::git_branches,
git::git_switch_branch,
git::git_link_github,
git::git_link_remote,
git::git_init,
git::git_log,
startup_folder,
github_client_id,
github_app_slug,
github_device_start,
github_device_poll,
github_device_refresh
forge_build_config,
oauth_post
])
.run(tauri::generate_context!())
.expect("error while running KiVi");
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"default-src": "'self'",
"script-src": "'self' 'wasm-unsafe-eval'",
"style-src": "'self' 'unsafe-inline'",
"img-src": "'self' data: blob: https://avatars.githubusercontent.com",
"img-src": "'self' data: blob: https://avatars.githubusercontent.com https://github.githubassets.com https://gitlab.com https://secure.gravatar.com",
"connect-src": "'self' ipc: http://ipc.localhost blob: data: https:",
"worker-src": "'self' blob:"
}
Expand Down
20 changes: 10 additions & 10 deletions src/app/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// the point where its branch left main.

import { DIFFABLE, RULE_FILES } from "../project.js";
import { forge, forgeInHash } from "../forge/index.js";
import { forge, forgeInHash, splitNwo } from "../forge/index.js";
import { parseSchematic } from "../sch/model.js";
import { renderSchematic } from "../sch/render.js";
import { parsePcb } from "../pcb/model.js";
Expand All @@ -19,7 +19,7 @@ import { ReviewStore, ReviewPanel, openCounts, COMMENT_ICON } from "./review-ui.
import { pickPcb, pickSch, locatePcb, locateSch } from "../diff/anchors.js";
import { buildConnectivity } from "../sch/connectivity.js";
import { refPicker } from "./ref-picker.js";
import { $, ago, escapeHtml, toast, settings, refCompare } from "./ui.js";
import { $, ago, escapeHtml, toast, refCompare, tokenFor } from "./ui.js";

const LAYER_PRESETS = {
top: (l) => ["F.Cu", "F.SilkS", "Edge.Cuts", "Vias", "Holes", "Dwgs.User"].includes(l),
Expand Down Expand Up @@ -122,7 +122,7 @@ class DiffApp {
if (p.get("local")) return { local: p.get("local"), base: p.get("base"), head: p.get("head") };
const at = forgeInHash(p);
if (!at) return null;
const [owner, repo] = at.value.split("/");
const { owner, repo } = splitNwo(at.value);
const f = at.forge.id;
if (p.get("pr")) return { forge: f, owner, repo, pr: Number(p.get("pr")) };
if (p.get("commit")) return { forge: f, owner, repo, commit: p.get("commit") };
Expand Down Expand Up @@ -163,7 +163,7 @@ class DiffApp {
this.proCache = new Map(); // the sides may be other commits now
try {
this.busy(spec.local ? "Reading local changes…" : `Reading changes from ${forge(spec.forge).name}…`);
const cmp = spec.local ? await this.localComparison(spec) : await forge(spec.forge).compare(spec, settings().token);
const cmp = spec.local ? await this.localComparison(spec) : await forge(spec.forge).compare(spec, tokenFor(forge(spec.forge)));
this.cmp = cmp;
$("#diff-bar").hidden = false;
$("#diff-main").hidden = false;
Expand Down Expand Up @@ -242,11 +242,11 @@ class DiffApp {
let branchList = null;
const branches = () =>
(branchList ??= local
? this.localGit.branches(cmp.root).then((bs) => bs.map((b) => ({ name: b.name, note: b.current ? "checked out" : b.remote ? "on GitHub" : "" })))
: f.branches(spec.owner, spec.repo, settings().token));
? this.localGit.branches(cmp.root).then((bs) => bs.map((b) => ({ name: b.name, note: b.current ? "checked out" : b.remote ? `on ${b.forge ?? "the remote"}` : "" })))
: f.branches(spec.owner, spec.repo, tokenFor(f)));
const commits = local
? (branch, offset, count) => this.localGit.commits(cmp.root, branch, offset, count)
: (branch, offset, count) => f.commits(spec.owner, spec.repo, branch, offset, count, settings().token);
: (branch, offset, count) => f.commits(spec.owner, spec.repo, branch, offset, count, tokenFor(f));
const sha = /^[0-9a-f]{7,40}\^?$/i;
// What each side is now: the URL's choice, else what it was resolved to
// (a local copy's default base is e.g. origin/main, listed as main).
Expand All @@ -263,7 +263,7 @@ class DiffApp {
refPicker({
current: cmp.base.ref ?? cmp.base.sha?.slice(0, 7) ?? "(none)",
value: baseValue,
hint: "A branch: the changes since the after side left it, like a pull request. A commit: compared with it as it is.",
hint: `A branch: the changes since the after side left it, like a ${local ? "pull or merge request" : forge(spec.forge).words.pr}. A commit: compared with it as it is.`,
commitsOf: !sha.test(baseValue ?? "") ? baseValue : headBranch,
branches,
commits,
Expand Down Expand Up @@ -314,7 +314,7 @@ class DiffApp {
const ref = this.cmp[side];
if (ref.src) return ref.src.readText(path); // a local copy
if (!ref.sha) return null;
const src = forge(this.spec.forge).source({ owner: ref.owner, repo: ref.repo, ref: ref.sha, sha: ref.sha, token: settings().token });
const src = forge(this.spec.forge).source({ owner: ref.owner, repo: ref.repo, ref: ref.sha, sha: ref.sha, token: tokenFor(forge(this.spec.forge)) });
return src.readText(path);
}

Expand All @@ -333,7 +333,7 @@ class DiffApp {
this.proCache.set(
key,
(async () => {
const src = ref.src ?? forge(this.spec.forge).source({ owner: ref.owner, repo: ref.repo, ref: ref.sha, sha: ref.sha, path: dir, token: settings().token });
const src = ref.src ?? forge(this.spec.forge).source({ owner: ref.owner, repo: ref.repo, ref: ref.sha, sha: ref.sha, path: dir, token: tokenFor(forge(this.spec.forge)) });
const pro = (await src.list()).find((p) => p.endsWith(".kicad_pro") && dirname(p) === dir) ?? null;
if (!pro) return { pro, vars: {} };
return { pro, vars: JSON.parse(await src.readText(pro)).text_variables ?? {} };
Expand Down
Loading
Loading