/
gitInfo.js
85 lines (73 loc) · 2.86 KB
/
gitInfo.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import picomatch from 'picomatch';
import { getCommitAndBranch } from '../git/getCommitAndBranch';
import { getBaselineCommits, getSlug, getVersion } from '../git/git';
import { createTask, transitionTo } from '../lib/tasks';
import {
initial,
pending,
skipFailed,
skippingBuild,
skippedForCommit,
skippedRebuild,
success,
} from '../ui/tasks/gitInfo';
const TesterSkipBuildMutation = `
mutation TesterSkipBuildMutation($commit: String!) {
skipBuild(commit: $commit)
}
`;
const TesterLastBuildQuery = `
query TesterLastBuildQuery($commit: String!, $branch: String!) {
app {
lastBuild(ref: $commit, branch: $branch) {
id
status(legacy: false)
}
}
}
`;
export const setGitInfo = async (ctx, task) => {
const { branchName, patchBaseRef, fromCI: ci } = ctx.options;
ctx.git = await getCommitAndBranch({ branchName, patchBaseRef, ci, log: ctx.log });
ctx.git.version = await getVersion();
if (!ctx.git.slug) {
ctx.git.slug = await getSlug().catch((e) => ctx.log.warn('Failed to retrieve slug', e));
}
if (ctx.git.slug && ctx.options.ownerName) {
ctx.git.slug = ctx.git.slug.replace(/[^/]+/, ctx.options.ownerName);
}
const { branch, commit } = ctx.git;
const matchesBranch = (glob) => (glob && glob.length ? picomatch(glob)(branch) : !!glob);
ctx.git.matchesBranch = matchesBranch;
if (matchesBranch(ctx.options.skip)) {
transitionTo(skippingBuild)(ctx, task);
// The SkipBuildMutation ensures the commit is tagged properly.
if (await ctx.client.runQuery(TesterSkipBuildMutation, { commit })) {
ctx.skip = true;
return transitionTo(skippedForCommit, true)(ctx, task);
}
throw new Error(skipFailed(ctx).output);
}
const baselineCommits = await getBaselineCommits(ctx, {
branch,
ignoreLastBuildOnBranch: matchesBranch(ctx.options.ignoreLastBuildOnBranch),
});
ctx.git.baselineCommits = baselineCommits;
ctx.log.debug(`Found baselineCommits: ${baselineCommits}`);
// If the sole baseline is the most recent ancestor, then this is likely a rebuild (rerun of CI job).
// If the MRA is all green, there's no need to rerun the build, we just want the CLI to exit 0 so the CI job succeeds.
// This is especially relevant for (unlinked) projects that don't use --exit-zero-on-changes.
// There's no need for a SkipBuildMutation because we don't have to tag the commit again.
if (baselineCommits.length === 1 && baselineCommits[0] === commit) {
const mostRecentAncestor = await ctx.client.runQuery(TesterLastBuildQuery, { commit, branch });
if (mostRecentAncestor && ['PASSED', 'ACCEPTED'].includes(mostRecentAncestor.status)) {
ctx.skip = true;
return transitionTo(skippedRebuild, true)(ctx, task);
}
}
return transitionTo(success, true)(ctx, task);
};
export default createTask({
title: initial.title,
steps: [transitionTo(pending), setGitInfo],
});