The Settings upgrade can switch to a chosen branch (#954)

* The Settings upgrade can switch to a chosen branch

The Upgrade panel gains a Branch input: a dropdown listing the current
branch, master, and release that also takes a typed branch name.
/api/upgrade/start accepts an optional {"branch": ...}; without it the
current branch is upgraded as before.  A branch is written into the systemd
env file and passed to `upgrade.sh -b`, so only plain git branch names are
accepted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* The Controller upgrade refuses a branch that is not a plain branch name

The Controller's upgrade script wrote its branch straight into the systemd
env file, so a newline in it added a variable, and a leading dash became an
upgrade.sh option.  It now accepts the same branch names as the API, and no
script parameter may span lines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
lrnselfreliance 2026-11-08 08:48:58 -07:00 • committed by Roland
parent f3f762ef1b
commit d88db90ce9
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
10 changed files with 304 additions and 28 deletions

View file

@ -2,6 +2,7 @@ import React, {useContext, useState} from 'react';
import {ThemeContext} from '../contexts/contexts';
import {
ActionInput,
Autocomplete,
Button,
Card,
CardGroup,
@ -789,7 +790,11 @@ export function ThemeSamplePage() {
<NumberInput label='Depth' description='Follow links this far.'
defaultValue={2} min={1} max={4}/>
</div>
<div style={{marginTop: 12}}>
<div style={{marginTop: 12, display: 'grid', gridTemplateColumns: '1fr 2fr', gap: 12}}>
{/* A dropdown that also takes a typed value; open it to check the dropdown. */}
<Autocomplete label='Branch' description='Choose a branch, or type another.'
defaultValue='release' data={['release', 'master']}
filter={({options}) => options}/>
<Textarea label='Notes' placeholder='Notes about this download…' minRows={2}/>
</div>
<div style={{marginTop: 12}}>

View file

@ -17,6 +17,7 @@ import {ConfigsTable} from "./Configs";
import {navColorHexMap} from "../Vars";
import {Link} from "react-router";
import {
Autocomplete,
Button,
Divider,
Grid,
@ -101,11 +102,34 @@ export function ShutdownButton() {
</APIButton>
}
// The branches offered in the upgrade branch picker; any other branch name can be typed.
const UPGRADE_BRANCHES = ['master', 'release'];
export function UpgradeBranchInput({currentBranch, value, onChange, disabled}) {
const data = [...new Set([currentBranch, ...UPGRADE_BRANCHES].filter(Boolean))];
return <Autocomplete
label='Branch'
description='Choose a branch, or type the name of another.'
data={data}
value={value}
onChange={onChange}
disabled={disabled}
// Always list every branch; the typed value is a choice of its own, not a search.
filter={({options}) => options}
style={{maxWidth: '20em', marginBottom: '1em'}}
/>;
}
export function UpgradeSegment() {
const {status, fetchStatus, loaded} = React.useContext(StatusContext);
const dockerized = useDockerized();
const [upgrading, setUpgrading] = React.useState(false);
const [checking, setChecking] = React.useState(false);
const currentBranch = status?.git_branch;
// null until the user picks a branch, so the current branch is shown once status arrives.
const [chosenBranch, setChosenBranch] = React.useState(null);
const branch = (chosenBranch ?? currentBranch ?? '').trim();
const switchingBranch = Boolean(branch) && branch !== currentBranch;
const handleCheckUpgrade = async () => {
setChecking(true);
@ -126,10 +150,12 @@ export function UpgradeSegment() {
const handleUpgrade = async () => {
setUpgrading(true);
try {
const response = await triggerUpgrade();
const response = await triggerUpgrade(branch || null);
if (response.ok) {
// Redirect to Controller UI for upgrade status
window.location.href = '/controller/?upgrade=true';
} else {
setUpgrading(false);
}
} catch (e) {
setUpgrading(false);
@ -152,13 +178,35 @@ export function UpgradeSegment() {
</Panel>;
}
const branchInput = <UpgradeBranchInput
currentBranch={currentBranch}
value={branch}
onChange={setChosenBranch}
disabled={upgrading}
/>;
const upgradeButton = <APIButton
role='save'
size='big'
onClick={handleUpgrade}
disabled={upgrading || !branch}
confirmContent={switchingBranch
? `This will switch WROLPi from branch ${currentBranch || 'unknown'} to ${branch}. `
+ 'The system will be temporarily unavailable during the upgrade. Are you sure?'
: 'This will upgrade WROLPi. The system will be temporarily unavailable during the upgrade. Are you sure?'}
confirmButton='Start Upgrade'
>
{upgrading ? 'Starting Upgrade...' : switchingBranch ? `Upgrade to ${branch}` : 'Upgrade Now'}
</APIButton>;
// No update available
if (!status?.update_available) {
return <Panel id='upgrade'>
<Header as='h3'>System Upgrade</Header>
<p>Your WROLPi is up to date.</p>
<p>Version: <strong>v{status?.version}</strong> on branch <strong>{status?.git_branch || 'unknown'}</strong>
<p>Version: <strong>v{status?.version}</strong> on branch <strong>{currentBranch || 'unknown'}</strong>
</p>
{branchInput}
<APIButton
role='primary'
onClick={handleCheckUpgrade}
@ -166,6 +214,8 @@ export function UpgradeSegment() {
>
{checking ? 'Checking...' : 'Check for Upgrades'}
</APIButton>
{/* An up-to-date branch has nothing to upgrade; another branch can still be switched to. */}
{switchingBranch && upgradeButton}
</Panel>;
}
@ -174,21 +224,13 @@ export function UpgradeSegment() {
<Header as='h3' icon='arrow alternate circle up'>
Upgrade Available
</Header>
<p>Branch: <strong>{status?.git_branch}</strong></p>
<p>Branch: <strong>{currentBranch}</strong></p>
<p>Current commit: <strong>{status?.current_commit}</strong></p>
<p>Latest commit: <strong>{status?.latest_commit}</strong></p>
<p><strong>{status?.commits_behind}</strong> commit(s) behind</p>
<APIButton
role='save'
size='big'
onClick={handleUpgrade}
disabled={upgrading}
confirmContent='This will upgrade WROLPi. The system will be temporarily unavailable during the upgrade. Are you sure?'
confirmButton='Start Upgrade'
>
{upgrading ? 'Starting Upgrade...' : 'Upgrade Now'}
</APIButton>
{branchInput}
{upgradeButton}
</Panel>;
}

View file

@ -0,0 +1,63 @@
import React from 'react';
import {fireEvent, screen, waitFor} from '@testing-library/react';
import {render} from '../../test-utils';
import {UpgradeSegment} from './Settings';
import {triggerUpgrade} from '../../api';
jest.mock('../../api', () => ({
...jest.requireActual('../../api'),
// Not ok, so the page does not navigate to the Controller.
triggerUpgrade: jest.fn(() => Promise.resolve({ok: false})),
}));
const upToDate = {status: {update_available: false, version: '1.0', git_branch: 'feature'}, loaded: true};
const updateAvailable = {
status: {update_available: true, version: '1.0', git_branch: 'release', commits_behind: 2},
loaded: true,
};
const branchInput = () => screen.getByLabelText('Branch', {selector: 'input'});
const confirmUpgrade = async (buttonName) => {
fireEvent.click(screen.getByRole('button', {name: buttonName}));
fireEvent.click(await screen.findByRole('button', {name: 'Start Upgrade'}));
};
beforeEach(() => {
triggerUpgrade.mockClear();
});
describe('UpgradeSegment branch picker', () => {
test('starts on the current branch and lists it with master and release', async () => {
render(<UpgradeSegment/>, {status: upToDate});
expect(branchInput()).toHaveValue('feature');
fireEvent.focus(branchInput());
fireEvent.click(branchInput());
const options = (await screen.findAllByRole('option')).map(i => i.textContent);
expect(options).toEqual(['feature', 'master', 'release']);
});
test('an up to date branch offers no upgrade until another branch is chosen', async () => {
render(<UpgradeSegment/>, {status: upToDate});
expect(screen.queryByRole('button', {name: /^Upgrade/})).not.toBeInTheDocument();
fireEvent.change(branchInput(), {target: {value: 'master'}});
await confirmUpgrade('Upgrade to master');
await waitFor(() => expect(triggerUpgrade).toHaveBeenCalledWith('master'));
});
test('a typed branch name is upgraded to', async () => {
render(<UpgradeSegment/>, {status: updateAvailable});
fireEvent.change(branchInput(), {target: {value: ' my-branch '}});
await confirmUpgrade('Upgrade to my-branch');
await waitFor(() => expect(triggerUpgrade).toHaveBeenCalledWith('my-branch'));
});
test('an available update upgrades the current branch by default', async () => {
render(<UpgradeSegment/>, {status: updateAvailable});
expect(branchInput()).toHaveValue('release');
await confirmUpgrade('Upgrade Now');
await waitFor(() => expect(triggerUpgrade).toHaveBeenCalledWith('release'));
});
});

View file

@ -1,5 +1,6 @@
import React, {forwardRef, useId} from 'react';
import {
Autocomplete,
Checkbox as MCheckbox,
MultiSelect,
NumberInput,
@ -215,6 +216,7 @@ export function ColoredInput({
}
export {
Autocomplete,
MTextInput as TextInput,
MTextarea as Textarea,
MSelect as Select,