All files / domain operation.runner.ts

100% Statements 19/19
100% Branches 1/1
100% Functions 2/2
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 501x       1x   1x   2x 2x       2x 1x     1x   1x         1x   1x 2x   2x   2x   2x           1x   1x   1x   1x        
import { flow } from "lodash"
import { GithubSessionFactory } from "../github/session"
import { Tree } from "../github/types"
import { Operation } from "./operation.dto"
import { TreeItem } from './tree-item.dto';
 
export class OperationRunner {
  private readonly _operation: Operation
  constructor(private readonly gitubSessionFactory: GithubSessionFactory, operationBuilder: { build: () => Operation }) {
    this._operation = operationBuilder.build()
  }
 
  async commit(message: string): Promise<any> {
    if(!this._operation.changes.length) {
      return
    }
    
    const { source: sourceBranch, output: outputBranch, changes } = this._operation
    
    const githubSession = this.gitubSessionFactory.create({
      sourceBranch,
      outputBranch
    })
 
    const tree: Tree[] = []
 
    for(const change of changes) {
      const { data } = await githubSession.getFile(change.sourcePath)
 
      const newData: string = flow(change.transforms)(data)
 
      const blob = await githubSession.createBlob(newData)
 
      tree.push(TreeItem.create({
        sha: blob.sha,
        path: change.outputPath
      }))
    }
 
    const parentCommit = await githubSession.getCurrentCommit()
 
    const githubTree = await githubSession.createTree(tree)
 
    const { url } = await githubSession.commit(message, parentCommit.commitSha, githubTree.sha)
 
    return {
      url
    }
  }
}