gitlab_service.js 1.5 KB
Newer Older
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
const request = require('request-promise');
const gitService = require('./git_service');
const apiRoot = 'https://gitlab.com/api/v4'; // FIXME: Get domain dynamically

async function fetch(path) {
  const config = {
    url: `${apiRoot}${path}`,
    headers: {
      'PRIVATE-TOKEN': 'TOKEN', // FIXME
    }
  };

  const response = await request(config);

  try {
    return JSON.parse(response);
  } catch (e) {
    return { error: e };
  }
}

// FIXME: Don't rely created-by-me
// FIXME: Fix project id
async function fetchMyOpenMergeRequests() {
  return await fetch('/projects/13083/merge_requests?scope=created-by-me&state=opened');
}

async function fetchOpenMergeRequestForCurrentBranch() {
  const branchName = await gitService.fetchBranchName();
  const mrs = await fetchMyOpenMergeRequests(); // FIXME: I doesn't have to be my MR

  return mrs.filter(mr => mr.source_branch === branchName)[0];
}

async function fetchLastPipelineForCurrentBranch() {
  const mr = await fetchOpenMergeRequestForCurrentBranch();

  if (mr) {
    const path = `/projects/${mr.target_project_id}/pipelines`;
    const branchName = await gitService.fetchBranchName();
    const pipelines = await fetch(`${path}?ref=${branchName}`);

    if (pipelines.length) {
      return await fetch(`${path}/${pipelines[0].id}`);
    }

    return null;
  }

  return null;
}

exports.fetchMyOpenMergeRequests = fetchMyOpenMergeRequests;
exports.fetchOpenMergeRequestForCurrentBranch = fetchOpenMergeRequestForCurrentBranch;
exports.fetchLastPipelineForCurrentBranch = fetchLastPipelineForCurrentBranch;