<commit_before>const { labels, getAllCommitsByPR } = require("./common");

/**
 * Gets the combined state of a particular sha commit
 * @param {object} context - context given by the probot
 * @param {string} sha - git sha value
 * @returns {Promise.<string>} value of the state
 */
const getStatusBySha = async (context, sha) => {
    const { data: { state, statuses } } = await context.github.repos.getCombinedStatus(
        context.repo({
            ref: sha
        })
    );

    return statuses.length > 0 ? state : labels.successStatus;
};

/**
 * Get status for all the sha values
 * @param {object} context - context given by the probot
 * @param {Array<string>} allSha - All sha from a PR
 * @returns {Promise<Array<Promise>>} collection of promises
 */
const getStatusAllSha = (context, allSha) =>
    Promise.all(allSha.map((sha) => getStatusBySha(context, sha)));

/**
 * Get the sha values from all the commit objects
 * @param {Array< object>} commits - all the commits from the PR
 * @returns {Array<string>} sha values
 */
const pluckSha = async (commits) => commits.map((commit) => commit.sha);

/**
 * Check if every status is a success
 * @param {Array<string>} allStatus - collection of status
 * @returns {boolean} true if all are success
 */
const allStatusAreSuccess = (allStatus) =>
    allStatus.every((status) => status === labels.successStatus);

/**
 * Check to see if the PR build status is good or not
 * @param context - context given by the probot
 * @param {int} prId - pull request number
 * @returns {Promise<boolean>} true if its good
 */
const isPrStatusSuccess = (context, prId) =>
    getAllCommitsByPR(context, prId)
        .then(pluckSha)
        .then((shas) => getStatusAllSha(context, shas))
        .then(allStatusAreSuccess);

module.exports = {
    isPrStatusSuccess
};<commit_msg>Fix: Check status of latest commit for pr ready to merge<commit_after>/**
 * @fileoverview Checks if the status of the latest commit on a PR
 * @author <NAME>
 */

const { labels, getAllCommitsByPR } = require("./common");

/**
 * Gets the combined state of a particular sha commit
 * @param {object} context - context given by the probot
 * @param {string} sha - git sha value
 * @returns {Promise<string>} value of the state
 */
const getStatusBySha = async (context, sha) => {
    const { data: { state, statuses } } = await context.github.repos.getCombinedStatus(
        context.repo({
            ref: sha
        })
    );

    return statuses.length > 0 ? state : labels.successStatus;
};

/**
 * Get the sha values from the commit objects
 * @param {object} commit - Commit
 * @returns {Promise<string>} sha values
 * @private
 */
const pluckSha = async (commit) => commit.sha;

/**
 * Get the latest commit
 * @param {Array<object>} commits - all the Commit
 * @returns {Promise<Object>} latest commit object
 * @private
 */
const getLatestCommit = async (commits) => commits.pop();

/**
 * Check if status is a success
 * @param {string} status - Status of a commit
 * @returns {boolean} true if it is success
 * @private
 */
const isStatusSuccess = (status) => status === labels.successStatus;

/**
 * Check to see if the PR build status is good or not
 * @param context - context given by the probot
 * @param {int} prId - pull request number
 * @returns {Promise<boolean>} true if its good
 */
const isPrStatusSuccess = (context, prId) =>
    getAllCommitsByPR(context, prId)
        .then(getLatestCommit)
        .then(pluckSha)
        .then((shas) => getStatusBySha(context, shas))
        .then(isStatusSuccess);

module.exports = {
    isPrStatusSuccess
};
{'fraction_non_alphanumeric': 0.12583148558758314, 'fraction_numerical': 0.0005543237250554324, 'mean_word_length': 3.4391143911439115, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3860852', 'n_tokens_mistral': 1085, 'n_tokens_neox': 1074, 'n_words': 377}