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

Add debug; refactor a bit to make it easier #73

Merged
merged 3 commits into from
Jan 23, 2024
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
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- run: yarn install --frozen-lockfile --non-interactive
- run: yarn browserstack:connect
- run: yarn test:ember:browserstack
- run: yarn browserstack:results
- run: yarn browserstack:disconnect
- run: DEBUG=ember-cli-browserstack* yarn browserstack:connect
- run: DEBUG=ember-cli-browserstack* yarn test:ember:browserstack
- run: DEBUG=ember-cli-browserstack* yarn browserstack:results
- run: DEBUG=ember-cli-browserstack* yarn browserstack:disconnect

publish:
name: Publish to npm
Expand All @@ -98,4 +98,4 @@ jobs:
- name: publish to npm
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
5 changes: 5 additions & 0 deletions lib/commands/browserstack-results.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const buildNameFromEnv = require('../utils/build-name-from-env');
const debug = require('debug')('ember-cli-browserstack:browserstack-results');

module.exports = {
name: 'browserstack:results',
Expand All @@ -8,7 +9,11 @@ module.exports = {
availableOptions: [{ name: 'build', type: String }],

run: function (commandOptions) {
if (commandOptions.build) {
debug('build name passed as option');
}
let buildName = commandOptions.build || buildNameFromEnv();
debug('build name: %l', buildName);
if (!buildName) {
this.ui.writeLine('A build name is required to find results');
return;
Expand Down
10 changes: 10 additions & 0 deletions lib/tasks/launch-browserstack-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
/* eslint-disable no-console, no-process-exit */
const buildNameFromEnv = require('../utils/build-name-from-env');
const localIdentifier = require('../utils/local-identifier');
const debug = require('debug')(
'ember-cli-browserstack:launch-browserstack-browser'
);

let build = buildNameFromEnv();

debug('Build Name: %l', build);

let name = require('crypto').randomBytes(10).toString('hex');
debug('Name: %l', name);

let argv = require('yargs')
.option('os', {
Expand Down Expand Up @@ -113,6 +119,10 @@ for (let i in settings) {
}
}

debug(
'Creating BrowserStack worker with settings %s',
JSON.stringify(settings)
);
client.createWorker(settings, function (error, worker) {
if (error) {
console.log(error);
Expand Down
9 changes: 9 additions & 0 deletions lib/tasks/start-browserstack-tunnel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const RSVP = require('rsvp');
const fs = require('fs');
const debug = require('debug')(
'ember-cli-browserstack:start-browserstack-tunnel'
);
const localIdentifier = require('../utils/local-identifier');

module.exports = function (ui) {
Expand All @@ -13,6 +16,11 @@ module.exports = function (ui) {

options.localIdentifier = localIdentifier();

debug(
'Starting BrowserStack Tunnel with localIdentifier: %l',
options.localIdentifier
);

let bsLocal = new browserstackLocal.Local();

return new RSVP.Promise(function (resolve, reject) {
Expand All @@ -21,6 +29,7 @@ module.exports = function (ui) {
reject(error);
}

debug('BrowserStack Tunnel PID: %l', bsLocal.pid);
writePidFile(bsLocal.pid);
ui.writeLine('BrowserStack Tunnel Connected');
resolve();
Expand Down
5 changes: 5 additions & 0 deletions lib/tasks/stop-browserstack-tunnel.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const fs = require('fs');
const debug = require('debug')(
'ember-cli-browserstack:stop-browserstack-tunnel'
);

module.exports = function (ui) {
let pidFile = 'browserstack-local.pid';
Expand All @@ -12,7 +15,9 @@ module.exports = function (ui) {
return;
}
if (pid) {
debug('Stopping BrowserStack Tunnel with PID: %l', pid);
process.kill(pid, 'SIGINT');
}
debug('Removing BrowserStack Tunnel PID file');
fs.unlinkSync(pidFile);
};
62 changes: 46 additions & 16 deletions lib/utils/build-name-from-env.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,66 @@
const crypto = require('crypto');
const debug = require('debug')('ember-cli-browserstack:build-name-from-env');

module.exports = function () {
const env = environmentVariables();

debug('environment variables: %o', env);

let buildDesc =
process.env.BROWSERSTACK_BUILD_NAME ||
process.env.TRAVIS_JOB_NUMBER ||
process.env.BITBUCKET_BUILD_NUMBER ||
process.env.CIRCLE_BUILD_NUM ||
buildNameForGithubActions() ||
process.env.CI_JOB_ID ||
env.BROWSERSTACK_BUILD_NAME ||
env.TRAVIS_JOB_NUMBER ||
env.BITBUCKET_BUILD_NUMBER ||
env.CIRCLE_BUILD_NUM ||
buildNameForGithubActions({
runId: env.GITHUB_RUN_ID,
job: env.GITHUB_JOB,
workflow: env.GITHUB_WORKFLOW,
ref: env.GITHUB_REF,
headRef: env.GITHUB_HEAD_REF,
}) ||
env.CI_JOB_ID ||
crypto.randomBytes(10).toString('hex');

let prefix = process.env.BROWSERSTACK_BUILD_NAME_PREFIX;
let prefix = env.BROWSERSTACK_BUILD_NAME_PREFIX;
if (buildDesc && prefix) {
return `${prefix}_${buildDesc}`;
}
return buildDesc;
};

function buildNameForGithubActions() {
if (process.env.GITHUB_RUN_ID) {
let runDesc = githubRunDesc();
let workflow = process.env.GITHUB_WORKFLOW.replace(/\s/g, '_');
return `${runDesc}_${workflow}_${process.env.GITHUB_RUN_ID}_${process.env.GITHUB_JOB}`;
function buildNameForGithubActions({ runId, job, workflow, ref, headRef }) {
if (runId) {
let runDesc = githubRunDesc(ref, headRef);
let workflowEscaped = workflow.replace(/\s/g, '_');
const githubActionsBuildName = `${runDesc}_${workflowEscaped}_${runId}_${job}`;
debug('github actions build name: %s', githubActionsBuildName);
return githubActionsBuildName;
}
}

function githubRunDesc() {
if (process.env.GITHUB_HEAD_REF) {
let matches = /(\d+)/.exec(process.env.GITHUB_REF);
function githubRunDesc(ref, headRef) {
if (headRef) {
let matches = /(\d+)/.exec(ref);
let pr = matches[1];
return `PR_${pr}`;
} else {
return process.env.GITHUB_REF.replace('refs/heads/', '');
return ref.replace('refs/heads/', '');
}
}

function environmentVariables() {
return {
BROWSERSTACK_BUILD_NAME: process.env.BROWSERSTACK_BUILD_NAME,
BROWSERSTACK_BUILD_NAME_PREFIX: process.env.BROWSERSTACK_BUILD_NAME_PREFIX,
TRAVIS_JOB_NUMBER: process.env.TRAVIS_JOB_NUMBER,
BITBUCKET_BUILD_NUMBER: process.env.BITBUCKET_BUILD_NUMBER,
CIRCLE_BUILD_NUM: process.env.CIRCLE_BUILD_NUM,
CI_JOB_ID: process.env.CI_JOB_ID,
GITHUB_RUN_ID: process.env.GITHUB_RUN_ID,
GITHUB_WORKFLOW: process.env.GITHUB_WORKFLOW,
GITHUB_REF: process.env.GITHUB_REF,
GITHUB_HEAD_REF: process.env.GITHUB_HEAD_REF,
GITHUB_RUN_NUMBER: process.env.GITHUB_RUN_NUMBER,
GITHUB_JOB: process.env.GITHUB_JOB,
};
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"dependencies": {
"browserstack": "^1.6.1",
"browserstack-local": "^1.4.8",
"debug": "^4.3.4",
"rsvp": "^4.8.5",
"yargs": "^17.1.1"
},
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5047,6 +5047,13 @@ debug@^4.0.0, debug@^4.0.1, debug@^4.1.1:
dependencies:
ms "^2.1.1"

debug@^4.3.4:
version "4.3.4"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.4.tgz#1319f6579357f2338d3337d2cdd4914bb5dcc865"
integrity sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==
dependencies:
ms "2.1.2"

decamelize@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
Expand Down
Loading