Skip to content
Snippets Groups Projects
Select Git revision
  • 0ecbf550b4b5607c9a75fb8000446da828a0dc08
  • master default protected
  • docs-improve_user_path_description
  • fix-pipeline
  • ci-change_exercises_image_registry
  • feat-merge_recorder_in_extension
  • feat-default_folder
  • feat-change_user
  • develop protected
  • refactor-mission
  • feat-exercise_automatic_save
  • docs-improve_documentation
  • feat-create_little_container_for_developer
  • feat-local-dev
  • 0.1.12
  • 0.1.11
  • 0.1.10
  • 0.1.5
18 results

gitMission.ts

Blame
  • gitMission.ts 3.52 KiB
    import simpleGit, { SimpleGit, SimpleGitOptions } from 'simple-git';
    import { error, log } from '../recorder/utils';
    import { PROJECT_SRC_PATH } from './config';
    import UserConfig from './userConfig';
    
    const util = require('util');
    const exec = util.promisify(require('child_process').exec);
    
    const DEFAULT_REMOTE = 'origin';
    
    export enum Branch {
      MASTER = 'master',
      DEFAULT = MASTER,
      LIVE = 'live',
    }
    
    export default class GitMission {
      private git: SimpleGit;
    
      constructor(private userConfig: UserConfig) {
        const options: Partial<SimpleGitOptions> = {
          baseDir: PROJECT_SRC_PATH,
          binary: 'git',
          maxConcurrentProcesses: 2,
        };
    
        this.git = simpleGit(options);
      }
    
      async setupSshAgent() {
        const { err, stderr, stdout } = await exec(
          `eval "$(ssh-agent -s)" && ssh-keyscan -p ${this.userConfig.getGiteaSshPort()} -H ${this.userConfig.getGiteaHost()} >> ~/.ssh/known_hosts`,
        );
    
        log(stdout);
    
        if (err) {
          error(stderr);
          throw new Error(err);
        }
      }
    
      get author() {
        return this.userConfig.getUsername();
      }
    
      async init() {
        try {
          log('Setup ssh agent');
          await this.setupSshAgent();
    
          log('Init Git mission..');
    
          const remote = await this.readRemote();
    
          if (remote === DEFAULT_REMOTE) {
            return Promise.resolve(this);
          }
    
          const remotePath = this.getRemotePath();
    
          await this.git.init();
    
          await this.git.addRemote(DEFAULT_REMOTE, remotePath);
          await this.git.addConfig('user.email', this.userConfig.getCurrentUserDetails().email, false, 'system');
          await this.git.addConfig(
            'user.name',
            `${this.userConfig.getCurrentUserDetails().lastName} ${this.userConfig.getCurrentUserDetails().firstName}`,
            false,
            'system',
          );
    
          return Promise.resolve(this);
        } catch (e) {
          console.error(e);
          return Promise.reject(e);
        }
      }
    
      private getRemotePath() {
        return `ssh://git@${this.userConfig.getGiteaHost()}:${this.userConfig.getGiteaSshPort()}/${this.userConfig.getRemoteGitUsername()}/${this.userConfig.getMissionId()}`;
      }
    
      async readRemote() {
        try {
          return ((await this.git.remote([])) || '').replace(/(\r\n|\n|\r)/gm, '');
        } catch (e) {
          error(e);
        }
        return '';
      }
    
      fetch() {
        return this.git.fetch(DEFAULT_REMOTE);
      }
    
      pull(branch?: string) {
        return this.git.pull(DEFAULT_REMOTE, branch ?? Branch.DEFAULT, { '--rebase': 'true' });
      }
    
      addAll() {
        return this.git.add('.');
      }
    
      commit(message: string, options?: string[]) {
        return this.git.commit(message, options ?? []);
      }
    
      push() {
        return this.git.push(DEFAULT_REMOTE);
      }
    
      createLocalBranch(branch: string) {
        return this.git.checkout(['-b', branch]);
      }
    
      createRemoteBranch(branch: string) {
        return this.git.push(['-u', DEFAULT_REMOTE, branch]);
      }
    
      setUpstream(branch: string) {
        return this.git.branch(['-u', DEFAULT_REMOTE, branch]);
      }
    
      createBranch(branch: string) {
        const createBranchLocally = this.createLocalBranch(branch);
        const createRemoteBranch = this.createRemoteBranch(branch);
    
        return Promise.all([createBranchLocally, createRemoteBranch]);
      }
    
      checkout(branch: string) {
        return this.git.checkout(branch);
      }
    
      merge(options?: string[]) {
        return this.git.merge(options ?? []);
      }
    
      async isRemoteRepoExist() {
        try {
          const remotes = await this.git.listRemote();
          return remotes.length !== 0;
        } catch {
          // error, Gitea throws Gitea: Unauthorized when not found
          return false;
        }
      }
    }