@@ -4,9 +4,10 @@ import { mkdir, rm } from 'node:fs/promises';
44import { Injectable , Logger } from '@nestjs/common' ;
55import { simpleGit , type SimpleGit } from 'simple-git' ;
66import { AppConfigService } from '../config/config.service.js' ;
7+ import { GithubService } from '../github/github.service.js' ;
78import { type DiffSummary , type Workspace , type WorkspacePrepareInput } from './workspace.model.js' ;
89import {
9- sshRemoteUrl ,
10+ authenticatedRemoteUrl ,
1011 changedFilesFromStatus ,
1112 workspaceDir ,
1213} from './workspace.utility.js' ;
@@ -20,13 +21,17 @@ import {
2021export class WorkspaceService {
2122 private readonly logger = new Logger ( WorkspaceService . name ) ;
2223
23- constructor ( private readonly config : AppConfigService ) { }
24+ constructor (
25+ private readonly config : AppConfigService ,
26+ private readonly app : GithubService ,
27+ ) { }
2428
2529 /** Clone (if needed) and check out the job's branch. Idempotent across attempts. */
2630 async prepare ( input : WorkspacePrepareInput ) : Promise < Workspace > {
2731 const root = this . config . get ( 'WORKSPACE_ROOT' ) ;
2832 const dir = workspaceDir ( root , input . jobId ) ;
29- const remote = sshRemoteUrl ( input . owner , input . repo ) ;
33+ const token = await this . app . getInstallationToken ( input . installationId ) ;
34+ const remote = authenticatedRemoteUrl ( input . owner , input . repo , token ) ;
3035
3136 if ( existsSync ( `${ dir } /.git` ) ) {
3237 const git = simpleGit ( dir ) ;
@@ -81,11 +86,13 @@ export class WorkspaceService {
8186 return ( await git . revparse ( [ 'HEAD' ] ) ) . trim ( ) ;
8287 }
8388
84- /** Push the job branch using the host machine's SSH credentials . */
89+ /** Push the job branch, refreshing the remote token first (tokens expire ~1h) . */
8590 async push ( input : WorkspacePrepareInput ) : Promise < string > {
8691 const root = this . config . get ( 'WORKSPACE_ROOT' ) ;
8792 const dir = workspaceDir ( root , input . jobId ) ;
8893 const git = simpleGit ( dir ) ;
94+ const token = await this . app . getInstallationToken ( input . installationId ) ;
95+ await git . remote ( [ 'set-url' , 'origin' , authenticatedRemoteUrl ( input . owner , input . repo , token ) ] ) ;
8996 await git . push ( [ '-u' , 'origin' , input . branchName ] ) ;
9097 const sha = ( await git . revparse ( [ 'HEAD' ] ) ) . trim ( ) ;
9198 this . logger . log ( `[job ${ input . jobId } ] pushed ${ input . branchName } @ ${ sha } ` ) ;
0 commit comments