Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore(switch): Merge master into switch update #3235

Merged
merged 5 commits into from
Jul 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,8 @@ const SAUCE_LAUNCHERS = {
// },
};

const getLaunchers = () => {
if (USING_TRAVISCI) {
return USING_SL ? SAUCE_LAUNCHERS : LOCAL_LAUNCHERS;
} else {
return ['Chrome'];
}
};

const getBrowsers = () => Object.keys(getLaunchers());
const getLaunchers = () => USING_SL ? SAUCE_LAUNCHERS : LOCAL_LAUNCHERS;
const getBrowsers = () => USING_TRAVISCI ? Object.keys(getLaunchers()) : ['Chrome'];

module.exports = function(config) {
config.set({
Expand Down
15 changes: 15 additions & 0 deletions test/screenshot/infra/commands/travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ function print_travis_env_vars() {
echo
}

function maybe_add_git_branch() {
if [[ -n "$1" ]]; then
# https://github.com/marionebl/commitlint/issues/6#issuecomment-231186598
git remote set-branches --add origin "$1"
fi
}

function maybe_fetch_git_branches() {
maybe_add_git_branch 'master'
maybe_add_git_branch "$TRAVIS_BRANCH"
maybe_add_git_branch "$TRAVIS_PULL_REQUEST_BRANCH"
git fetch --tags
}

function maybe_extract_api_credentials() {
if [[ -z "$encrypted_eead2343bb54_key" ]] || [[ -z "$encrypted_eead2343bb54_iv" ]]; then
log_error
Expand Down Expand Up @@ -83,6 +97,7 @@ function maybe_install_gcloud_sdk() {

if [[ "$TEST_SUITE" == 'screenshot' ]]; then
print_travis_env_vars
maybe_fetch_git_branches
maybe_extract_api_credentials
maybe_install_gcloud_sdk
fi
27 changes: 14 additions & 13 deletions test/screenshot/infra/lib/diff-base-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ class DiffBaseParser {
// E.g.: `--diff-base=https://storage.googleapis.com/.../golden.json`
const isUrl = HTTP_URL_REGEX.test(rawDiffBase);
if (isUrl) {
return this.createPublicUrlDiffBase_(rawDiffBase);
return await this.createPublicUrlDiffBase_(rawDiffBase);
}

// Diff against a local `golden.json` file.
// E.g.: `--diff-base=/tmp/golden.json`
const isLocalFile = await fs.exists(rawDiffBase);
if (isLocalFile) {
return this.createLocalFileDiffBase_(rawDiffBase);
return await this.createLocalFileDiffBase_(rawDiffBase);
}

const [inputGoldenRef, inputGoldenPath] = rawDiffBase.split(':');
Expand All @@ -141,26 +141,26 @@ class DiffBaseParser {
// Diff against a specific git commit.
// E.g.: `--diff-base=abcd1234`
if (!fullGoldenRef) {
return this.createCommitDiffBase_(inputGoldenRef, goldenFilePath);
return await this.createCommitDiffBase_(inputGoldenRef, goldenFilePath);
}

const {remoteRef, localRef, tagRef} = this.getRefType_(fullGoldenRef);

// Diff against a remote git branch.
// E.g.: `--diff-base=origin/master` or `--diff-base=origin/feat/button/my-fancy-feature`
if (remoteRef) {
return this.createRemoteBranchDiffBase_(remoteRef, goldenFilePath);
return await this.createRemoteBranchDiffBase_(remoteRef, goldenFilePath);
}

// Diff against a remote git tag.
// E.g.: `--diff-base=v0.34.1`
if (tagRef) {
return this.createRemoteTagDiffBase_(tagRef, goldenFilePath);
return await this.createRemoteTagDiffBase_(tagRef, goldenFilePath);
}

// Diff against a local git branch.
// E.g.: `--diff-base=master` or `--diff-base=HEAD`
return this.createLocalBranchDiffBase_(localRef, goldenFilePath);
return await this.createLocalBranchDiffBase_(localRef, goldenFilePath);
}

/**
Expand Down Expand Up @@ -201,12 +201,17 @@ class DiffBaseParser {
const travisPrNumber = Number(process.env.TRAVIS_PULL_REQUEST);
const travisPrBranch = process.env.TRAVIS_PULL_REQUEST_BRANCH;
const travisPrSha = process.env.TRAVIS_PULL_REQUEST_SHA;
const travisCommit = process.env.TRAVIS_COMMIT;
const commit = travisPrSha || travisCommit;

const logInfo = {travisBranch, travisTag, travisPrNumber, travisPrBranch, travisPrSha};
if (!commit) {
return null;
}

const logInfo = {travisBranch, travisTag, travisPrNumber, travisPrBranch, travisPrSha, travisCommit};
const author = await this.gitRepo_.getCommitAuthor(commit, getStackTrace('getTravisGitRevision', logInfo));

if (travisPrNumber) {
const commit = await this.gitRepo_.getFullCommitHash(travisPrSha);
const author = await this.gitRepo_.getCommitAuthor(commit, getStackTrace('getTravisGitRevision', logInfo));
return GitRevision.create({
type: GitRevision.Type.TRAVIS_PR,
golden_json_file_path: GOLDEN_JSON_RELATIVE_PATH,
Expand All @@ -219,8 +224,6 @@ class DiffBaseParser {
}

if (travisTag) {
const commit = await this.gitRepo_.getFullCommitHash(travisTag);
const author = await this.gitRepo_.getCommitAuthor(commit, getStackTrace('getTravisGitRevision', logInfo));
return GitRevision.create({
type: GitRevision.Type.REMOTE_TAG,
golden_json_file_path: GOLDEN_JSON_RELATIVE_PATH,
Expand All @@ -231,8 +234,6 @@ class DiffBaseParser {
}

if (travisBranch) {
const commit = await this.gitRepo_.getFullCommitHash(travisBranch);
const author = await this.gitRepo_.getCommitAuthor(commit, getStackTrace('getTravisGitRevision', logInfo));
return GitRevision.create({
type: GitRevision.Type.LOCAL_BRANCH,
golden_json_file_path: GOLDEN_JSON_RELATIVE_PATH,
Expand Down
22 changes: 19 additions & 3 deletions test/screenshot/infra/lib/git-repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,23 @@ class GitRepo {
* @return {!Promise<string>}
*/
async getFullCommitHash(ref = 'HEAD') {
return this.exec_('revparse', [ref]);
const hash = this.exec_('revparse', [ref]);
if (!hash) {
throw new Error(`Unable to get commit hash for git ref "${ref}"`);
}
return hash;
}

/**
* @param {string=} ref
* @return {!Promise<string>}
*/
async getBranchName(ref = 'HEAD') {
return this.exec_('revparse', ['--abbrev-ref', ref]);
const branch = this.exec_('revparse', ['--abbrev-ref', ref]);
if (!branch) {
throw new Error(`Unable to get branch name for git ref "${ref}"`);
}
return branch;
}

/**
Expand All @@ -102,7 +110,11 @@ class GitRepo {
* @return {!Promise<string>}
*/
async getFullSymbolicName(ref = 'HEAD') {
return this.exec_('revparse', ['--symbolic-full-name', ref]);
const fullName = this.exec_('revparse', ['--symbolic-full-name', ref]);
if (!fullName) {
throw new Error(`Unable to get full symbolic name for git ref "${ref}"`);
}
return fullName;
}

/**
Expand Down Expand Up @@ -184,6 +196,10 @@ class GitRepo {
}

const logEntry = logEntries[0];
if (!logEntry) {
throw new VError(err, `Unable to get author for commit "${commit}":\n${stackTrace}`);
}

return User.create({
name: logEntry.author_name,
email: logEntry.author_email,
Expand Down
29 changes: 25 additions & 4 deletions test/screenshot/infra/lib/github-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,23 @@ const VError = require('verror');
const debounce = require('debounce');
const octokit = require('@octokit/rest');

/** @type {!CliColor} */
const colors = require('colors');

const GitRepo = require('./git-repo');
const getStackTrace = require('./stacktrace')('GitHubApi');

class GitHubApi {
constructor() {
this.gitRepo_ = new GitRepo();
this.octokit_ = octokit();
this.isAuthenticated_ = false;
this.hasWarnedNoAuth_ = false;
this.authenticate_();
this.initStatusThrottle_();
}

/**
* @private
*/
/** @private */
authenticate_() {
let token;

Expand All @@ -46,6 +50,11 @@ class GitHubApi {
token: token,
});

this.isAuthenticated_ = true;
}

/** @private */
initStatusThrottle_() {
const throttle = (fn, delay) => {
let lastCall = 0;
return (...args) => {
Expand Down Expand Up @@ -163,7 +172,19 @@ class GitHubApi {
* @private
*/
async createStatusUnthrottled_({state, targetUrl, description = undefined}) {
const sha = process.env.TRAVIS_PULL_REQUEST_SHA || await this.gitRepo_.getFullCommitHash();
if (!this.isAuthenticated_) {
if (!this.hasWarnedNoAuth_) {
const warning = colors.magenta('WARNING');
console.warn(`${warning}: Cannot set GitHub commit status because no API credentials were found.`);
this.hasWarnedNoAuth_ = true;
}
return null;
}

const travisPrSha = process.env.TRAVIS_PULL_REQUEST_SHA;
const travisCommit = process.env.TRAVIS_COMMIT;
const sha = travisPrSha || travisCommit || await this.gitRepo_.getFullCommitHash();

let stackTrace;

try {
Expand Down
6 changes: 3 additions & 3 deletions test/screenshot/infra/lib/stacktrace.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ const colors = require('colors');

/**
* @param {string} className
* @param {*=} infoData
* @return {function(methodName: string): string}
*/
module.exports = function(className, infoData = undefined) {
module.exports = function(className) {
/**
* @param {string} methodName
* @param {*=} infoData
* @return {string}
*/
function getStackTrace(methodName) {
function getStackTrace(methodName, infoData = undefined) {
const infoStr = typeof infoData === 'object' ? '\n' + JSON.stringify(infoData, null, 2) : '';
const fullStack = new Error(`${className}.${methodName}()`).stack;
// Remove THIS function from the stack trace because it's not useful
Expand Down