Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@doodle/doodle-node-cli / src / apis / github.js
Size: Mime:
const fetch = require('fetch-everywhere');
const chalk = require('chalk');
const constants = require('../constants');

const API_HOST = 'https://api.github.com';
const NON_PRODUCTION_TAG_PREFIX = 'dtn.';
const PRODUCTION_TAG_PREFIX = 'rel.';

const createTagRef = async (token, repository, tagName, tagSha) => {
  const requestBody = {
    ref: `refs/tags/${tagName}`,
    sha: tagSha,
  };

  const githubURL = `${API_HOST}/repos/DoodleScheduling/${repository}/git/refs`;
  const fetchOptions = {
    method: 'POST',
    headers: {
      Authorization: `token ${token}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(requestBody),
  };

  const response = await fetch(githubURL, fetchOptions);
  if (response.status === constants.STATUS_OK || constants.STATUS_CREATED) {
    console.log(`${chalk.green('Tag successfully created')}`);
  } else {
    const responseBody = await response.json();
    throw new Error(`Github returned an error: ${responseBody.message}`);
  }
};

const createTag = async (token, repository, tagName, branchName, commitSha) => {
  const requestBody = {
    tag: tagName,
    message: `CLI-deployment of ${repository}@${branchName}`,
    object: commitSha,
    type: 'commit',
  };

  const githubURL = `${API_HOST}/repos/DoodleScheduling/${repository}/git/tags`;
  const fetchOptions = {
    method: 'POST',
    headers: {
      Authorization: `token ${token}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(requestBody),
  };

  const response = await fetch(githubURL, fetchOptions);
  const responseBody = await response.json();
  if (response.status === constants.STATUS_OK || constants.STATUS_CREATED) {
    const tagSha = responseBody.sha;
    await createTagRef(token, repository, tagName, tagSha);
  } else {
    throw new Error(`Github returned an error: ${responseBody.message}`);
  }
};
const updateTag = async (token, repository, tagName, tagSha) => {
  const requestBody = {
    ref: `refs/tags/${tagName}`,
    sha: tagSha,
  };

  const githubURL = `${API_HOST}/repos/DoodleScheduling/${repository}/git/refs/tags/${tagName}`;
  const fetchOptions = {
    method: 'PUT',
    headers: {
      Authorization: `token ${token}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(requestBody),
  };

  const response = await fetch(githubURL, fetchOptions);
  if (response.status === constants.STATUS_OK) {
    console.log(`${chalk.green('Tag successfully updated')}`);
  } else {
    const responseBody = await response.json();
    throw new Error(`Github returned an error: ${responseBody.message}`);
  }
};

const getCommitShaFromBranchName = async (token, repository, branchName) => {
  const githubURL = `${API_HOST}/repos/DoodleScheduling/${repository}/git/refs/heads/${branchName}`;
  const fetchOptions = {
    headers: {
      Authorization: `token ${token}`,
    },
  };

  const response = await fetch(githubURL, fetchOptions);
  const branchExists = response.status === constants.STATUS_OK;

  const responseBody = await response.json();
  if (branchExists) {
    return responseBody.object.sha;
  }
  throw new Error(`Github returned an error: ${responseBody.message}`);
};

const checkForTagInRepository = async (token, repository, tagName) => {
  const githubURL = `${API_HOST}/repos/DoodleScheduling/${repository}/git/refs/tags/${tagName}`;
  const fetchOptions = {
    headers: {
      Authorization: `token ${token}`,
    },
  };

  const response = await fetch(githubURL, fetchOptions);
  const tagExists = response.status === constants.STATUS_OK;
  return tagExists;
};

const createOrUpdateTag = async (token, repository, tagName, branchName) => {
  const branchCommitSha = await getCommitShaFromBranchName(token, repository, branchName);

  const tagDoesExist = await checkForTagInRepository(token, repository, tagName);
  if (tagDoesExist) {
    await updateTag(token, repository, tagName, branchCommitSha);
  } else {
    await createTag(token, repository, tagName, branchName, branchCommitSha);
  }
};

const getRecentDeploymentTags = async (token, repository) => {
  const githubURL = `${API_HOST}/repos/DoodleScheduling/${repository}/git/refs/tags`;
  const fetchOptions = {
    headers: {
      Authorization: `token ${token}`,
    },
  };

  const response = await fetch(githubURL, fetchOptions);
  if (response.status === constants.STATUS_OK) {
    const responseBody = await response.json();

    return responseBody
      .map(tag => {
        if (tag.ref.indexOf(NON_PRODUCTION_TAG_PREFIX) >= 0 || tag.ref.indexOf(PRODUCTION_TAG_PREFIX) >= 0) {
          return [tag.ref.replace('refs/tags/', ''), tag.object.sha];
        }
        return null;
      })
      .filter(item => item); // remove empty items
  }
  return [];
};

module.exports = {
  createOrUpdateTag,
  getRecentDeploymentTags,
};