Skip to content

Commit

Permalink
src/pages/Schedule: Connect pulpito-ng with teuthology-api for schedu…
Browse files Browse the repository at this point in the history
…ling suites

Use axios post request to for run, dry-run and force-priority

Signed-off-by: Kamoltat Sirivadhna <[email protected]>
  • Loading branch information
kamoltat committed Jan 30, 2024
1 parent 1b77720 commit 9e02a60
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
36 changes: 30 additions & 6 deletions src/lib/teuthologyAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,51 @@ import { useQuery } from "@tanstack/react-query";
import { Cookies } from "react-cookie";
import type { UseQueryResult } from "@tanstack/react-query";

const TEUTHOLOGY_API_SERVER =
const TEUTHOLOGY_API_SERVER =
import.meta.env.VITE_TEUTHOLOGY_API || "";
const GH_USER_COOKIE = "GH_USER";

function getURL(relativeURL: URL|string): string {
if ( ! TEUTHOLOGY_API_SERVER ) return "";
function getURL(relativeURL: URL | string): string {
if (!TEUTHOLOGY_API_SERVER) return "";
return new URL(relativeURL, TEUTHOLOGY_API_SERVER).toString();
}

function doLogin() {
const url = getURL("/login/");
if ( url ) window.location.href = url;
if (url) window.location.href = url;
}

function doLogout() {
const cookies = new Cookies();
cookies.remove(GH_USER_COOKIE);

const url = getURL("/logout/");
window.location.href = url;
}

function doSchedule(commandValue: any, dryRun = false) {
console.log("doSchedule");
console.log(commandValue);
let url;
if (dryRun) {
url = getURL("/suite?dry_run=true");
} else {
url = getURL("/suite?dry_run=false");
}
if (commandValue['--user'] != useUserData().get("username")) {
console.log("Error: --user doesn't match username of current logged in account");
return false;
}
axios.post(url, commandValue, {
withCredentials: true,
headers: { "Content-Type": "application/json" },
}).then((resp) => {
console.log(resp);
}, (error) => {
console.log(error);
});
}

function useSession(): UseQueryResult {
const url = getURL("/");
const query = useQuery({
Expand Down Expand Up @@ -59,6 +82,7 @@ function useUserData(): Map<string, string> {
export {
doLogin,
doLogout,
doSchedule,
useSession,
useUserData
useUserData,
}
29 changes: 27 additions & 2 deletions src/pages/Schedule/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import MenuItem from '@mui/material/MenuItem';
import Checkbox from '@mui/material/Checkbox';
import Tooltip from '@mui/material/Tooltip';
import InfoIcon from '@mui/icons-material/Info';
import { useUserData, doSchedule } from '../../lib/teuthologyAPI';

export default function Schedule() {
const keyOptions =
Expand Down Expand Up @@ -83,20 +84,44 @@ export default function Schedule() {
const [rowData, setRowData] = useLocalStorage("rowData", []);
const [rowIndex, setRowIndex] = useLocalStorage("rowIndex", -1);
const [commandBarValue, setCommandBarValue] = useState([]);
const userData = useUserData();
let commandValue = {};

useEffect(() => {
setCommandBarValue(rowData);
}, [rowData])

function getCommandValue() {
let retCommandValue = {};
commandBarValue.map((data) => {
if (data.checked) {
retCommandValue[data.key] = data.value;
}
})
let username = userData.get("username");
if (!username) {
console.log("User is not logged in");
return {};
} else {
retCommandValue['--user'] = userData.get("username");
}
return retCommandValue;
}

const handleRun = () => {
return false;
let commandValue = getCommandValue();
doSchedule(commandValue);
};

const handleDryRun = () => {
return false;
let commandValue = getCommandValue();
doSchedule(commandValue, true);
};

const handleForcePriority = () => {
let commandValue = getCommandValue();
commandValue['--force-priority'] = true;
doSchedule(commandValue);
return false;
};

Expand Down

0 comments on commit 9e02a60

Please sign in to comment.