All files / libs/tools/plugin/src/executors/nx-get-shas nx-get-shas.ts

100% Statements 75/75
100% Branches 4/4
100% Functions 1/1
100% Lines 75/75

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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 761x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { assert } from '@amalia/ext/typescript';
import { getHeadSha, getPreviousCommitSha } from '@amalia/vendors/git';
import { findLastSuccessfulPipelineCommit, getMergeRequestBaseSha, makeGitlabClient } from '@amalia/vendors/gitlab';
 
import { debug } from './debug';
 
export const getHeadAndBaseShas = async ({
  token,
  projectId,
  ref,
  depth,
}: {
  token?: string;
  jobToken?: string;
  projectId?: string;
  ref?: string;
  depth?: number;
}): Promise<{ head: string; base: string }> => {
  const head = getHeadSha();
  debug('Using NX_HEAD: %s', head);
 
  const mergeRequestBaseSha = getMergeRequestBaseSha();
 
  if (mergeRequestBaseSha) {
    debug('This is a merge request pipeline.');
    debug('Using NX_BASE: %s', mergeRequestBaseSha);
 
    return {
      head,
      base: mergeRequestBaseSha,
    };
  }
 
  debug('This is not a merge request pipeline.');
  debug('Is there a previous successful pipeline run?');
 
  // if we are not in a merge request pipeline, the base sha is the sha of the last successful workflow run
  // on the current branch
 
  assert(token, 'token must be provided.');
 
  const client = makeGitlabClient({
    token,
    camelize: false,
  });
 
  const previousSuccessfulPipelineSha = await findLastSuccessfulPipelineCommit(client, {
    projectId,
    ref,
    head,
    depth,
  });
 
  if (previousSuccessfulPipelineSha) {
    debug('Found a previous successful pipeline run.');
    debug('Using NX_BASE: %s', previousSuccessfulPipelineSha);
 
    return {
      head,
      base: previousSuccessfulPipelineSha,
    };
  }
 
  debug('Could not find a previous successful pipeline run.');
 
  // Exhausting all options
  debug('Defaulting to using HEAD~1 as the base sha.');
  const previousCommitSha = getPreviousCommitSha();
  debug('Using NX_BASE: %s', previousCommitSha);
 
  return {
    head,
    base: previousCommitSha,
  };
};