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

Optionally mark the GitHub Action as failed if the PR isn't connected to any Clubhouse story #26

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ This action is configured to extract the clubhouse story id form either the bran

**Default** `ch`

### `throwErrorIfNoChStory`

**Optional** Should this GitHub Action throw an error if the PR is not connected to any Clubhouse story

**Default** `true`

## Outputs

### `prTitle`
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ inputs:
description: 'When a PR is opened with this string as the title, fetch the story name from clubhouse'
required: false
default: 'ch'
throwErrorIfNoChStory:
description: Should this GitHub Action throw an error if the PR is not connected to any Clubhouse story
required: false
default: true
runs:
using: 'node12'
main: 'dist/index.js'
35 changes: 22 additions & 13 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function formatMatches(matches) {
return values;
}

function getStoryIds(pullRequest) {
function getStoryIds(pullRequest, throwErrorIfNoChStory) {
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 @@ -60,21 +60,23 @@ function getStoryIds(pullRequest) {
return storyIds;
}

return core.setFailed(
'Action failed to find a Clubhouse ID in both the branch name and PR title.'
);
const message = 'Action failed to find a Clubhouse ID in both the branch name and PR title.';

if (throwErrorIfNoChStory) {
throw new Error(message);
}

core.info(message);

return [];
}

async function getClubhouseStory(client, storyIds) {
// Even if there's more than one storyId, fetch only first story name:
try {
return client
.getStory(storyIds[0])
.then((res) => res)
.catch((err) => err.response);
} catch (error) {
return core.setFailed(error);
}
return client
.getStory(storyIds[0])
.then((res) => res)
.catch((err) => err.response);
}

async function updatePullRequest(ghToken, pullRequest, repository, metadata) {
Expand Down Expand Up @@ -117,10 +119,16 @@ async function fetchStoryAndUpdatePr(params) {
useStoryNameTrigger,
pullRequest,
repository,
throwErrorIfNoChStory,
dryRun,
} = params;
const client = Clubhouse.create(chToken);
const storyIds = getStoryIds(pullRequest);
const storyIds = getStoryIds(pullRequest, throwErrorIfNoChStory);

if (!storyIds) {
return null;
}

const story = await getClubhouseStory(client, storyIds);
const newTitle = getTitle(
storyIds,
Expand Down Expand Up @@ -165,6 +173,7 @@ async function run() {
useStoryNameTrigger: core.getInput('useStoryNameTrigger'),
pullRequest,
repository,
throwErrorIfNoChStory: core.getInput('throwErrorIfNoChStory') !== 'false',
dryRun: false,
};
const prTitle = await fetchStoryAndUpdatePr(params);
Expand Down
35 changes: 22 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function formatMatches(matches) {
return values;
}

function getStoryIds(pullRequest) {
function getStoryIds(pullRequest, throwErrorIfNoChStory) {
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 @@ -43,21 +43,23 @@ function getStoryIds(pullRequest) {
return storyIds;
}

return core.setFailed(
'Action failed to find a Clubhouse ID in both the branch name and PR title.'
);
const message = 'Action failed to find a Clubhouse ID in both the branch name and PR title.';

if (throwErrorIfNoChStory) {
throw new Error(message);
}

core.info(message);

return [];
}

async function getClubhouseStory(client, storyIds) {
// Even if there's more than one storyId, fetch only first story name:
try {
return client
.getStory(storyIds[0])
.then((res) => res)
.catch((err) => err.response);
} catch (error) {
return core.setFailed(error);
}
return client
.getStory(storyIds[0])
.then((res) => res)
.catch((err) => err.response);
}

async function updatePullRequest(ghToken, pullRequest, repository, metadata) {
Expand Down Expand Up @@ -100,10 +102,16 @@ async function fetchStoryAndUpdatePr(params) {
useStoryNameTrigger,
pullRequest,
repository,
throwErrorIfNoChStory,
dryRun,
} = params;
const client = Clubhouse.create(chToken);
const storyIds = getStoryIds(pullRequest);
const storyIds = getStoryIds(pullRequest, throwErrorIfNoChStory);

if (!storyIds) {
return null;
}

const story = await getClubhouseStory(client, storyIds);
const newTitle = getTitle(
storyIds,
Expand Down Expand Up @@ -148,6 +156,7 @@ async function run() {
useStoryNameTrigger: core.getInput('useStoryNameTrigger'),
pullRequest,
repository,
throwErrorIfNoChStory: core.getInput('throwErrorIfNoChStory') !== 'false',
dryRun: false,
};
const prTitle = await fetchStoryAndUpdatePr(params);
Expand Down