@@ -2,6 +2,8 @@ import { Router } from 'express';
22import { requireSession } from '../lib/requireSession.js' ;
33import { getGithubAccessTokenForUser } from '../lib/github-token.js' ;
44import { upsertWorkflowFile } from '../tools/github_adapter.js' ;
5+ import { query } from '../db.js' ;
6+ import { savePipelineVersion } from '../lib/pipelineVersions.js' ;
57
68const router = Router ( ) ;
79
@@ -52,6 +54,42 @@ router.post('/pipeline_commit', requireSession, async (req, res) => {
5254 message : 'Add CI workflow via OSP' ,
5355 } ) ;
5456
57+ await query (
58+ `
59+ INSERT INTO deployment_logs
60+ (user_id, provider, repo_full_name, environment, branch, action,
61+ status, started_at, summary, metadata)
62+ VALUES ($1, $2, $3, $4, $5, $6,
63+ 'success', NOW(), $7, $8::jsonb);
64+ ` ,
65+ [
66+ userId , // user_id
67+ 'github_actions' , // provider (or 'pipeline' if you prefer)
68+ repoFullName , // repo_full_name
69+ 'global' , // environment
70+ branchName , // branch
71+ 'pipeline_commit' , // action
72+ `Committed workflow ${ workflowPath } via OSP` , // summary
73+ JSON . stringify ( {
74+ workflow_path : workflowPath ,
75+ branch : branchName ,
76+ commit_sha : result ?. commit ?. sha || null ,
77+ commit_url : result ?. commit ?. html_url || null ,
78+ source : 'pipeline_commit' ,
79+ } ) ,
80+ ]
81+ ) ;
82+
83+ // Save a version of the pipelin YAML for history
84+ await savePipelineVersion ( {
85+ userId,
86+ repoFullName,
87+ branch : branchName ,
88+ workflowPath,
89+ yaml,
90+ source : 'pipeline_commit' ,
91+ } ) ;
92+
5593 return res . status ( 201 ) . json ( {
5694 ok : true ,
5795 message : 'Workflow committed successfully' ,
@@ -66,4 +104,72 @@ router.post('/pipeline_commit', requireSession, async (req, res) => {
66104 }
67105} ) ;
68106
107+ /**
108+ * GET /mcp/v1/pipeline_history
109+ * Query params:
110+ * repoFullName (required) - "owner/repo"
111+ * branch (optional) - default "main"
112+ * path (optional) - default ".github/workflows/ci.yml"
113+ * limit (optional) - default 20
114+ *
115+ * Example:
116+ * GET /mcp/v1/pipeline_history?repoFullName=lorencDedaj/NeatNest&branch=main
117+ */
118+
119+ router . get ( '/pipeline_history' , requireSession , async ( req , res ) => {
120+ try {
121+ const { repoFullName, branch, path, limit } = req . query || { } ;
122+
123+ if ( ! repoFullName ) {
124+ return res
125+ . status ( 400 )
126+ . json ( { error : 'repoFUllName query param is required' } ) ;
127+ }
128+
129+ const userId = req . user ?. user_id ;
130+ if ( ! userId ) {
131+ return res
132+ . status ( 400 )
133+ . json ( { error : 'userId session missing or invalid' } ) ;
134+ }
135+
136+ const branchName = branch || 'main' ;
137+ const workflowPath = path || '.github/workflows/ci.yml' ;
138+ const lim = Math . min ( parseInt ( limit || '20' , 10 ) || 20 , 100 ) ;
139+
140+ const rows = await query (
141+ `
142+ select
143+ id,
144+ user_id,
145+ repo_full_name,
146+ branch,
147+ workflow_path,
148+ yaml,
149+ yaml_hash,
150+ source,
151+ created_at
152+ from pipeline_versions
153+ where repo_full_name = $1
154+ and branch = $2
155+ and workflow_path = $3
156+ order by created_at desc
157+ limit $4;
158+ ` ,
159+ [ repoFullName , branchName , workflowPath , lim ]
160+ ) ;
161+
162+ return res . json ( {
163+ ok : true ,
164+ versions : rows ,
165+ } ) ;
166+ } catch ( err ) {
167+ console . error ( '[pipeline_history] error: ' , err ) ;
168+ const status = err . status || 500 ;
169+ return res . status ( status ) . json ( {
170+ error : err . message || 'Failed to fetch the pipeline commit history' ,
171+ } ) ;
172+ }
173+ } ) ;
174+
69175export default router ;
0 commit comments