Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show triggered by #39

Merged
merged 4 commits into from
Sep 9, 2024
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
import { Tables } from "@/lib/database.types";
import { RunWithUser } from "@/data/user/runs";
import { ToSnakeCase } from "@/lib/utils";
import { AnimatePresence, motion } from "framer-motion";
import { Activity } from "lucide-react";
Expand All @@ -23,8 +23,7 @@ export const statusColors: StatusColor = {
discarded: 'bg-neutral-200 text-neutral-800 dark:bg-neutral-800 dark:text-neutral-200',
};


export const AllRunsTable = ({ runs, projectSlug }: { runs: Tables<'digger_runs'>[], projectSlug: string }) => {
export const AllRunsTable = ({ runs, projectSlug }: { runs: RunWithUser[], projectSlug: string }) => {
const sortedRuns = [...runs].sort((a, b) => {
// Sort primarily by created_at in descending order (most recent first)
return moment(b.created_at).valueOf() - moment(a.created_at).valueOf();
Expand All @@ -38,7 +37,7 @@ export const AllRunsTable = ({ runs, projectSlug }: { runs: Tables<'digger_runs'
<TableHead className="text-left">Commit ID</TableHead>
<TableHead className="text-left">Status</TableHead>
<TableHead className="text-left">Last updated</TableHead>
<TableHead className="text-left">User</TableHead>
<TableHead className="text-left">Triggered By</TableHead>
</TableRow>
</TableHeader>
<TableBody>
Expand Down Expand Up @@ -66,7 +65,7 @@ export const AllRunsTable = ({ runs, projectSlug }: { runs: Tables<'digger_runs'
</span>
</TableCell>
<TableCell>{moment(run.updated_at).fromNow()}</TableCell>
<TableCell>{run.approval_author}</TableCell>
<TableCell>{run.user_profiles?.full_name || run.triggertype}</TableCell>
</motion.tr>
))
) : (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,18 @@ function RenderContent({

export const ProjectRunDetails: React.FC<{
run: Table<'digger_runs'>,
loggedInUser: Table<'user_profiles'>
approverUser: Table<'user_profiles'> | null
isUserOrgAdmin: boolean
loggedInUser: Table<'user_profiles'>,
approverUser: Table<'user_profiles'> | null,
triggeredByUser: Table<'user_profiles'> | null,
isUserOrgAdmin: boolean,
tfOutput: string | null,
workflowRunUrl: string | null,
applyTerraformOutput: string | null,
applyWorkflowRunUrl: string | null,
fullRepoName: string | null
planBatchId: string | null
applyBatchId: string | null
}> = ({ run: initialRun, loggedInUser, approverUser, isUserOrgAdmin, tfOutput: initialTfOutput,
fullRepoName: string | null,
planBatchId: string | null,
applyBatchId: string | null,
}> = ({ run: initialRun, loggedInUser, approverUser, triggeredByUser, isUserOrgAdmin, tfOutput: initialTfOutput,
workflowRunUrl: initialWorkflowRunUrl,
applyTerraformOutput: initialApplyTerraformOutput,
applyWorkflowRunUrl: initialApplyWorkflowRunUrl,
Expand Down Expand Up @@ -242,13 +243,12 @@ export const ProjectRunDetails: React.FC<{
<DetailItem label="Triggered at" value={new Date(run.created_at).toLocaleString()} />
<DetailItem label="Project" value={run.project_name || 'N/A'} />
<DetailItem label="Commit" value={run.commit_id.substring(0, 8)} link={`https://github.com/${fullRepoName}/commit/${run.commit_id}`} />
<DetailItem label="Trigger type" value={run.triggertype} />
<DetailItem label="Triggered by" value={triggeredByUser?.full_name || run.triggertype} />
<DetailItem label="Status" value={
<Badge className={`${statusColors[ToSnakeCase(run.status)]} pointer-events-none`}>
{run.status.toUpperCase()}
</Badge>
} />
{run.pr_number && <DetailItem label="PR Number" value={run.pr_number.toString()} />}
</div>
</CardContent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type ProjectRunDetailsProps = {
planBatchId: string | null
applyBatchId: string | null
approverUser: Table<'user_profiles'> | null
triggeredByUser: Table<'user_profiles'> | null
}


Expand Down Expand Up @@ -75,6 +76,10 @@ export default async function RunDetailPage({
if (run.approver_user_id) {
approverUserProfile = await getUserProfile(run.approver_user_id);
}
let triggeredByUserProfile
if (run.triggered_by_user_id) {
triggeredByUserProfile = await getUserProfile(run.triggered_by_user_id);
}

// Fetch organization role and batch IDs in parallel
const [organizationRole, planBatchId, applyBatchId] = await Promise.all([
Expand Down Expand Up @@ -112,6 +117,7 @@ export default async function RunDetailPage({
<DynamicProjectRunDetails run={run}
loggedInUser={userProfile}
approverUser={approverUserProfile}
triggeredByUser={triggeredByUserProfile}
isUserOrgAdmin={isOrganizationAdmin}
tfOutput={planData.terraform_output}
workflowRunUrl={planData.workflow_run_url}
Expand Down
15 changes: 13 additions & 2 deletions src/data/user/runs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use server';

import { Tables } from '@/lib/database.types';
import { createSupabaseUserServerComponentClient } from '@/supabase-clients/user/createSupabaseUserServerComponentClient';
import { SAPayload } from '@/types';

Expand Down Expand Up @@ -112,16 +113,26 @@ export async function getRunsByRepoId(repoId: string) {
return data;
}

export type RunWithUser = Tables<'digger_runs'> & {
user_profiles: {
full_name: string | null;
} | null;
};

export async function getRunsByProjectId(projectId: string) {
//TODO figure out a non-admin way to query users
const supabase = createSupabaseUserServerComponentClient();
const { data, error } = await supabase
.from('digger_runs')
.select('*')
.select(`*, user_profiles (full_name)`)
.eq('project_id', projectId)
.order('created_at', { ascending: false });

if (error) throw error;
return data;
console.log('RUN 0', data[0]);
// supabase typings are wrong - it returns user_profiles as a single object, not a list
// @ts-ignore
return data as RunWithUser[];
}

export async function getAllRunsByOrganizationId(organizationId: string) {
Expand Down
14 changes: 14 additions & 0 deletions supabase/migrations/20240906173227_fk_user_profiles_runs.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- First, drop the existing foreign key constraint
ALTER TABLE digger_runs
DROP CONSTRAINT IF EXISTS fk_triggered_by_user;

-- Then, modify the triggered_by_user_id column to match the data type of the id in user_profiles
-- Assuming the id in user_profiles is UUID. If it's different, adjust accordingly.
ALTER TABLE digger_runs
ALTER COLUMN triggered_by_user_id TYPE UUID;

-- Finally, add the new foreign key constraint referencing user_profiles
ALTER TABLE digger_runs
ADD CONSTRAINT fk_triggered_by_user
FOREIGN KEY (triggered_by_user_id)
REFERENCES user_profiles(id);
Loading