Skip to content
This repository has been archived by the owner on Nov 6, 2023. It is now read-only.

Commit

Permalink
refactor: stop using .netrc file, instead include credentials in the …
Browse files Browse the repository at this point in the history
…HTTPS URL
  • Loading branch information
Ryan Park committed May 11, 2022
1 parent a2a603c commit 191e9ce
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 158 deletions.
62 changes: 8 additions & 54 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8689,7 +8689,6 @@ const comments = __nccwpck_require__(4975);
const core = __nccwpck_require__(2186);
const git = __nccwpck_require__(109);
const heroku = __nccwpck_require__(7213);
const netrc = __nccwpck_require__(8063);

async function createController(params) {
const { pipelineName, pipelineId, appName, refName } = params;
Expand Down Expand Up @@ -8734,9 +8733,7 @@ async function createController(params) {
`[Step ${currentStep}/${stepCount}] Deploying the app to Heroku for the first time -- this may take a few minutes...\n`
);
const credentials = heroku.getCredentials();
netrc.createNetrc(credentials);
const pushResult = git.push(appName, refName);
netrc.deleteNetrc();
const pushResult = git.push(credentials, appName, refName);
if (pushResult.status !== 0) {
throw new Error(`Created Heroku app "${appName}" but ran into errors deploying for the first time.`);
}
Expand Down Expand Up @@ -8806,7 +8803,6 @@ const comments = __nccwpck_require__(4975);
const core = __nccwpck_require__(2186);
const git = __nccwpck_require__(109);
const heroku = __nccwpck_require__(7213);
const netrc = __nccwpck_require__(8063);

async function updateController(params) {
const { pipelineName, appName, refName } = params;
Expand All @@ -8827,9 +8823,7 @@ async function updateController(params) {
core.info(`Deploying to Heroku app "${appName}" -- this may take a few minutes...\n`);

const credentials = heroku.getCredentials();
netrc.createNetrc(credentials);
const pushResult = git.push(appName, refName);
netrc.deleteNetrc();
const pushResult = git.push(credentials, appName, refName);
if (pushResult.status !== 0) {
throw new Error(`Ran into errors deploying the app "${appName}"`);
}
Expand Down Expand Up @@ -8884,8 +8878,11 @@ module.exports.refExists = ref => {
* Returns the result of child_process.spawn(); for documentation see
* https://nodejs.org/api/child_process.html.
*/
module.exports.push = (appName, ref) => {
return git('push', ['--force', `https://git.heroku.com/${appName}.git`, `${ref}:refs/heads/master`]);
module.exports.push = (credentials, appName, ref) => {
const url = new URL(`https://git.heroku.com/${appName}.git`);
url.username = credentials.email;
url.password = credentials.apiKey;
return git('push', ['--force', url.href, `${ref}:refs/heads/master`]);
};


Expand Down Expand Up @@ -8954,7 +8951,7 @@ module.exports.initializeCredentials = function () {
};

/*
* Returns the Heroku credentials which are needed for the .netrc file
* Returns the Heroku credentials, used for pushing code to Heroku
*/
module.exports.getCredentials = function () {
return { email: HEROKU_EMAIL, apiKey: HEROKU_API_KEY };
Expand Down Expand Up @@ -9104,7 +9101,6 @@ const core = __nccwpck_require__(2186);
const git = __nccwpck_require__(109);
const github = __nccwpck_require__(5438);
const heroku = __nccwpck_require__(7213);
const netrc = __nccwpck_require__(8063);
const updateController = __nccwpck_require__(2477);

/*
Expand Down Expand Up @@ -9182,54 +9178,12 @@ async function main() {
}
} catch (error) {
core.setFailed(error.message);
} finally {
// extra cleanup just to make sure we don't leave a .netrc file
netrc.deleteNetrc();
}
}

module.exports = main;


/***/ }),

/***/ 8063:
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {

const core = __nccwpck_require__(2186);
const fs = __nccwpck_require__(7147);
const path = __nccwpck_require__(1017);

module.exports.getNetrcPath = function () {
return path.join(process.env.HOME, '.netrc');
};

module.exports.createNetrc = function (credentials) {
const filename = module.exports.getNetrcPath();
if (fs.existsSync(filename)) {
throw new Error(`Credentials file ${filename} already exists`); // NOCOMMIT?
}
fs.writeFileSync(
filename,
`machine git.heroku.com\n login ${credentials.email}\n password ${credentials.apiKey}\n`
);
if (process.env.NODE_ENV !== 'test') {
core.info(`Saved Heroku login credentials to ${filename}`);
}
};

module.exports.deleteNetrc = function () {
const filename = module.exports.getNetrcPath();
if (!fs.existsSync(filename)) {
return;
}
fs.unlinkSync(filename);
if (process.env.NODE_ENV !== 'test') {
core.info(`Deleted Heroku login credentials from ${filename}`);
}
};


/***/ }),

/***/ 2877:
Expand Down
5 changes: 1 addition & 4 deletions src/controllers/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const comments = require('../comments');
const core = require('@actions/core');
const git = require('../git');
const heroku = require('../heroku');
const netrc = require('../netrc');

async function createController(params) {
const { pipelineName, pipelineId, appName, refName } = params;
Expand Down Expand Up @@ -47,9 +46,7 @@ async function createController(params) {
`[Step ${currentStep}/${stepCount}] Deploying the app to Heroku for the first time -- this may take a few minutes...\n`
);
const credentials = heroku.getCredentials();
netrc.createNetrc(credentials);
const pushResult = git.push(appName, refName);
netrc.deleteNetrc();
const pushResult = git.push(credentials, appName, refName);
if (pushResult.status !== 0) {
throw new Error(`Created Heroku app "${appName}" but ran into errors deploying for the first time.`);
}
Expand Down
5 changes: 1 addition & 4 deletions src/controllers/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const comments = require('../comments');
const core = require('@actions/core');
const git = require('../git');
const heroku = require('../heroku');
const netrc = require('../netrc');

async function updateController(params) {
const { pipelineName, appName, refName } = params;
Expand All @@ -23,9 +22,7 @@ async function updateController(params) {
core.info(`Deploying to Heroku app "${appName}" -- this may take a few minutes...\n`);

const credentials = heroku.getCredentials();
netrc.createNetrc(credentials);
const pushResult = git.push(appName, refName);
netrc.deleteNetrc();
const pushResult = git.push(credentials, appName, refName);
if (pushResult.status !== 0) {
throw new Error(`Ran into errors deploying the app "${appName}"`);
}
Expand Down
7 changes: 5 additions & 2 deletions src/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ module.exports.refExists = ref => {
* Returns the result of child_process.spawn(); for documentation see
* https://nodejs.org/api/child_process.html.
*/
module.exports.push = (appName, ref) => {
return git('push', ['--force', `https://git.heroku.com/${appName}.git`, `${ref}:refs/heads/master`]);
module.exports.push = (credentials, appName, ref) => {
const url = new URL(`https://git.heroku.com/${appName}.git`);
url.username = credentials.email;
url.password = credentials.apiKey;
return git('push', ['--force', url.href, `${ref}:refs/heads/master`]);
};
2 changes: 1 addition & 1 deletion src/heroku.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ module.exports.initializeCredentials = function () {
};

/*
* Returns the Heroku credentials which are needed for the .netrc file
* Returns the Heroku credentials, used for pushing code to Heroku
*/
module.exports.getCredentials = function () {
return { email: HEROKU_EMAIL, apiKey: HEROKU_API_KEY };
Expand Down
4 changes: 0 additions & 4 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const core = require('@actions/core');
const git = require('./git');
const github = require('@actions/github');
const heroku = require('./heroku');
const netrc = require('./netrc');
const updateController = require('./controllers/update');

/*
Expand Down Expand Up @@ -82,9 +81,6 @@ async function main() {
}
} catch (error) {
core.setFailed(error.message);
} finally {
// extra cleanup just to make sure we don't leave a .netrc file
netrc.deleteNetrc();
}
}

Expand Down
32 changes: 0 additions & 32 deletions src/netrc.js

This file was deleted.

10 changes: 8 additions & 2 deletions test/git.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const git = require('../src/git');

const SAMPLE_APP_NAME = 'dr-owlbert-pr-1234';
const SAMPLE_REF = 'refs/heads/dev';
const SAMPLE_EMAIL = '[email protected]';
const SAMPLE_API_KEY = '12345678-1234-1234-1234-1234567890ab';

describe('#src/git', () => {
afterEach(() => {
Expand Down Expand Up @@ -48,13 +50,17 @@ describe('#src/git', () => {
const EXPECTED_ARGS = [
'push',
'--force',
`https://git.heroku.com/${SAMPLE_APP_NAME}.git`,
`https://${encodeURIComponent(SAMPLE_EMAIL)}:${encodeURIComponent(
SAMPLE_API_KEY
)}@git.heroku.com/${SAMPLE_APP_NAME}.git`,
`${SAMPLE_REF}:refs/heads/master`,
];

const SAMPLE_CREDENTIALS = { email: SAMPLE_EMAIL, apiKey: SAMPLE_API_KEY };

it('should run "git push" with the correct arguments', () => {
const spy = jest.spyOn(childProcess, 'spawnSync').mockReturnValue({ status: 0 });
expect(git.push(SAMPLE_APP_NAME, SAMPLE_REF)).toStrictEqual({ status: 0 });
expect(git.push(SAMPLE_CREDENTIALS, SAMPLE_APP_NAME, SAMPLE_REF)).toStrictEqual({ status: 0 });
expect(spy.mock.lastCall[0]).toBe('git');
expect(spy.mock.lastCall[1]).toStrictEqual(EXPECTED_ARGS);
});
Expand Down
55 changes: 0 additions & 55 deletions test/netrc.test.js

This file was deleted.

0 comments on commit 191e9ce

Please sign in to comment.