Skip to content

Commit

Permalink
Merge pull request #12 from farmersdog/rfarine/ch43418/fixes
Browse files Browse the repository at this point in the history
[ch43418] Fixes
  • Loading branch information
rfarine authored Oct 15, 2020
2 parents 2fc26b9 + dc372df commit bc4872d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 75 deletions.
88 changes: 35 additions & 53 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,9 @@ __webpack_require__.r(__webpack_exports__);
/* harmony export */ "updatePullRequest": () => /* binding */ updatePullRequest,
/* harmony export */ "run": () => /* binding */ run
/* harmony export */ });
/* harmony import */ var _actions_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2186);
/* harmony import */ var _actions_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_actions_core__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _actions_github__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(5438);
/* harmony import */ var _actions_github__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_actions_github__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var clubhouse_lib__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(6986);
/* harmony import */ var clubhouse_lib__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(clubhouse_lib__WEBPACK_IMPORTED_MODULE_2__);



const core = __webpack_require__(2186);
const github = __webpack_require__(5438);
const Clubhouse = __webpack_require__(6986);

function formatMatches(matches) {
const values = [];
Expand All @@ -36,10 +30,8 @@ function formatMatches(matches) {
return values;
}

function getStoryIds() {
const {
payload: { pull_request: pullRequest },
} = (_actions_github__WEBPACK_IMPORTED_MODULE_1___default().context);
function getStoryIds(githubCtx) {
const { pull_request: pullRequest } = githubCtx.payload;
const branchName = pullRequest.head.ref;
// Only when a Github user formats their branchName as: text/ch123/something
const branchStoryIds = branchName.match(/\/(ch)(\d+)\//g);
Expand All @@ -49,26 +41,26 @@ function getStoryIds() {
// Github user can include more than one CH story ID
let storyIds = '';

_actions_core__WEBPACK_IMPORTED_MODULE_0__.info(`Branch Name: ${branchName}`);
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info(`PR Title: ${prTitle}`);
core.info(`Branch Name: ${branchName}`);
core.info(`PR Title: ${prTitle}`);

if (branchStoryIds) {
storyIds = formatMatches(branchStoryIds);

_actions_core__WEBPACK_IMPORTED_MODULE_0__.info(`Found Clubhouse ID(s) in Branch Name: ${storyIds.join(', ')}`);
core.info(`Found Clubhouse ID(s) in Branch Name: ${storyIds.join(', ')}`);

return storyIds;
}

if (prTitleStoryIds) {
storyIds = prTitleStoryIds;

_actions_core__WEBPACK_IMPORTED_MODULE_0__.info(`Found Clubhouse ID(s) in PR Title: ${storyIds.join(', ')}`);
core.info(`Found Clubhouse ID(s) in PR Title: ${storyIds.join(', ')}`);

return storyIds;
}

return _actions_core__WEBPACK_IMPORTED_MODULE_0__.setFailed(
return core.setFailed(
'Action failed to find a Clubhouse ID in both the branch name and PR title.'
);
}
Expand All @@ -81,66 +73,68 @@ async function getClubhouseStory(client, storyIds) {
.then((res) => res)
.catch((err) => err.response);
} catch (error) {
return _actions_core__WEBPACK_IMPORTED_MODULE_0__.setFailed(error);
return core.setFailed(error);
}
}

async function updatePullRequest(metadata) {
const ghToken = _actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput('ghToken');
const octokit = _actions_github__WEBPACK_IMPORTED_MODULE_1___default().getOctokit(ghToken);
async function updatePullRequest(githubCtx, metadata) {
const ghToken = core.getInput('ghToken');
const octokit = github.getOctokit(ghToken);
const {
payload: { pull_request: pullRequest },
repository,
repository_owner: owner,
} = (_actions_github__WEBPACK_IMPORTED_MODULE_1___default().context);
pull_request: pullRequest,
repository: {
name: repo,
owner: { login },
},
} = githubCtx.payload;
const { title, url } = metadata;
const originalBody = pullRequest.body;
const body = `${url} \n \n${originalBody}`;

try {
octokit.pulls.update({
owner,
repo: repository,
return await octokit.pulls.update({
repo,
owner: login,
pull_number: pullRequest.number,
title,
body,
});
} catch (error) {
_actions_core__WEBPACK_IMPORTED_MODULE_0__.setFailed(error);
return core.setFailed(error);
}
}

async function run() {
try {
const ghToken = _actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput('ghToken');
const chToken = _actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput('chToken');
const ghToken = core.getInput('ghToken');
const chToken = core.getInput('chToken');

if (!ghToken) {
return _actions_core__WEBPACK_IMPORTED_MODULE_0__.setFailed('Input ghToken is required.');
return core.setFailed('Input ghToken is required.');
}

if (!chToken) {
return _actions_core__WEBPACK_IMPORTED_MODULE_0__.setFailed('Input chToken is required.');
return core.setFailed('Input chToken is required.');
}

// Mask tokens:
_actions_core__WEBPACK_IMPORTED_MODULE_0__.setSecret('ghToken');
_actions_core__WEBPACK_IMPORTED_MODULE_0__.setSecret('chToken');
core.setSecret('ghToken');
core.setSecret('chToken');

const client = clubhouse_lib__WEBPACK_IMPORTED_MODULE_2___default().create(chToken);
const storyIds = getStoryIds();
const client = Clubhouse.create(chToken);
const storyIds = getStoryIds(github.context);
const story = await getClubhouseStory(client, storyIds);
const formattedStoryIds = storyIds.map((id) => `[ch${id}]`).join(' ');
const storyNameAndId = `${story.name} ${formattedStoryIds}`;

await updatePullRequest({
await updatePullRequest(github.context, {
title: storyNameAndId,
url: story.app_url,
});

return _actions_core__WEBPACK_IMPORTED_MODULE_0__.setOutput('prTitle', storyNameAndId);
return core.setOutput('prTitle', storyNameAndId);
} catch (error) {
return _actions_core__WEBPACK_IMPORTED_MODULE_0__.setFailed(error.message);
return core.setFailed(error.message);
}
}

Expand Down Expand Up @@ -16759,18 +16753,6 @@ module.exports = require("zlib");
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat get default export */
/******/ (() => {
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = (module) => {
/******/ var getter = module && module.__esModule ?
/******/ () => module['default'] :
/******/ () => module;
/******/ __webpack_require__.d(getter, { a: getter });
/******/ return getter;
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/define property getters */
/******/ (() => {
/******/ // define getter functions for harmony exports
Expand Down
36 changes: 18 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as core from '@actions/core';
import github from '@actions/github';
import Clubhouse from 'clubhouse-lib';
const core = require('@actions/core');
const github = require('@actions/github');
const Clubhouse = require('clubhouse-lib');

export function formatMatches(matches) {
const values = [];
Expand All @@ -14,10 +14,8 @@ export function formatMatches(matches) {
return values;
}

export function getStoryIds() {
const {
payload: { pull_request: pullRequest },
} = github.context;
export function getStoryIds(githubCtx) {
const { pull_request: pullRequest } = githubCtx.payload;
const branchName = pullRequest.head.ref;
// Only when a Github user formats their branchName as: text/ch123/something
const branchStoryIds = branchName.match(/\/(ch)(\d+)\//g);
Expand Down Expand Up @@ -63,28 +61,30 @@ export async function getClubhouseStory(client, storyIds) {
}
}

export async function updatePullRequest(metadata) {
export async function updatePullRequest(githubCtx, metadata) {
const ghToken = core.getInput('ghToken');
const octokit = github.getOctokit(ghToken);
const {
payload: { pull_request: pullRequest },
repository,
repository_owner: owner,
} = github.context;
pull_request: pullRequest,
repository: {
name: repo,
owner: { login },
},
} = githubCtx.payload;
const { title, url } = metadata;
const originalBody = pullRequest.body;
const body = `${url} \n \n${originalBody}`;

try {
octokit.pulls.update({
owner,
repo: repository,
return await octokit.pulls.update({
repo,
owner: login,
pull_number: pullRequest.number,
title,
body,
});
} catch (error) {
core.setFailed(error);
return core.setFailed(error);
}
}

Expand All @@ -106,12 +106,12 @@ export async function run() {
core.setSecret('chToken');

const client = Clubhouse.create(chToken);
const storyIds = getStoryIds();
const storyIds = getStoryIds(github.context);
const story = await getClubhouseStory(client, storyIds);
const formattedStoryIds = storyIds.map((id) => `[ch${id}]`).join(' ');
const storyNameAndId = `${story.name} ${formattedStoryIds}`;

await updatePullRequest({
await updatePullRequest(github.context, {
title: storyNameAndId,
url: story.app_url,
});
Expand Down
8 changes: 4 additions & 4 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('Update Pull Request', () => {

describe('getStoryIds', () => {
test('should return storyIds from branchName', () => {
expect(action.getStoryIds()).toEqual(['1']);
expect(action.getStoryIds(github.context)).toEqual(['1']);
});

test('should return [ch#] storyIds from PR title', () => {
Expand All @@ -60,7 +60,7 @@ describe('Update Pull Request', () => {
},
};

expect(action.getStoryIds()).toEqual(['2']);
expect(action.getStoryIds(github.context)).toEqual(['2']);
});

test('should return ch# storyIds from PR title', () => {
Expand All @@ -73,7 +73,7 @@ describe('Update Pull Request', () => {
},
};

expect(action.getStoryIds()).toEqual(['2']);
expect(action.getStoryIds(github.context)).toEqual(['2']);
});

test('should exit if no ch id in PR title or branchName', () => {
Expand All @@ -86,7 +86,7 @@ describe('Update Pull Request', () => {
},
};

action.getStoryIds();
action.getStoryIds(github.context);
expect(core.setFailed).toHaveBeenCalledTimes(1);
});
});
Expand Down

0 comments on commit bc4872d

Please sign in to comment.