summaryrefslogtreecommitdiff
path: root/scripts/environment.js
blob: bb0be7788656a970d1da1cc9bd788c3913c67d47 (plain)
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
#!/usr/bin/env node

// Output some `export` commands with a few extra environment variables. They'll be evaluated
// into the build environment and available for use in later steps.

const github = require('@octokit/rest')();
const {execSync} = require('child_process');

const pr = process.env['CIRCLE_PULL_REQUEST'];
if (pr) {
    const number = +pr.match(/\/(\d+)\/?$/)[1];
    return github.pullRequests.get({
        owner: 'mapbox',
        repo: 'mapbox-gl-native',
        number
    }).then(({data}) => {
        const base = data.base.ref;
        const head = process.env['CIRCLE_SHA1'];
        const mergeBase = execSync(`git merge-base origin/${base} ${head}`).toString().trim();

        console.log(`export CIRCLE_TARGET_BRANCH=${base}`);
        console.log(`export CIRCLE_MERGE_BASE=${mergeBase}`);
    });
} else {
    const head = process.env['CIRCLE_SHA1'];
    for (const sha of execSync(`git rev-list --max-count=10 ${head}`).toString().trim().split('\n')) {
        const base = execSync(`git branch -r --contains ${sha} origin/master origin/release-*`).toString().replace(/$\n.*/m, '').trim().replace(/^origin\//, '');
        if (base) {
            const mergeBase = execSync(`git merge-base origin/${base} ${head}`).toString().trim();
            console.log(`export CIRCLE_TARGET_BRANCH=${base}`);
            console.log(`export CIRCLE_MERGE_BASE=${mergeBase}`);
            break;
        }
    }
}