{"size": 888, "ext": "js", "max_stars_repo_path": "packages/webhooks-deploy/utils/github-verify.js", "max_stars_repo_name": "K-Fet/webhooks-deploy", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "packages/webhooks-deploy/utils/github-verify.js", "max_issues_repo_name": "K-Fet/webhooks-deploy", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "packages/webhooks-deploy/utils/github-verify.js", "max_forks_repo_name": "K-Fet/webhooks-deploy", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 21.1428571429, "max_line_length": 69, "alphanum_fraction": 0.6295045045}
const octokit = require('@octokit/rest')();

const BASE_REPO = {
  owner: 'K-Fet',
  repo: 'K-App',
};

async function verifyCommit(sha, { branch }) {
  let commit;
  try {
    commit = await octokit.repos.getCommit({
      ...BASE_REPO,
      commit_sha: sha,
    });
  } catch (e) {
    throw 'This commit doesn\'t exist';
  }

  if (!commit.data.commit.verification.verified) {
    throw 'Commit is not verified by github';
  }

  let branches;
  try {
    branches = await octokit.repos.listBranchesForHeadCommit({
      ...BASE_REPO,
      commit_sha: sha,
    });
  } catch (e) {
    throw 'Error while getting branches for head commit';
  }

  const gBranch = branches.data.find(value => value.name === branch);

  if (!gBranch) throw `Commit is not HEAD on ${gBranch}`;
  if (!gBranch.protected) throw`Branch ${gBranch} is not protected`;
}

module.exports = {
  verifyCommit,
};
