Skip to content

Commit

Permalink
release version 1.0.7-beta
Browse files Browse the repository at this point in the history
  • Loading branch information
tuanddd committed Mar 19, 2020
1 parent 63bc427 commit c900f4f
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 43 deletions.
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Jira Ticket Transitioner
name: Jira Ticket Transitioner Action
author: Tuan Dao ([email protected])
description: This action can update Jira issue status by event

Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jira-automatee-transition",
"version": "1.0.6",
"name": "jira-automate-transition",
"version": "1.0.7-beta",
"description": "Github Action to automate Jira ticket transition",
"dependencies": {
"@actions/core": "^1.2.3",
Expand Down
24 changes: 16 additions & 8 deletions src/get-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ import { ParsedResult, JiraConfig, JiraConfigFile } from "./interfaces";

const configPath = `${process.env.HOME}/jira/config.yml`;

const AsyncFunction = Object.getPrototypeOf(async function() {}).constructor;

const getArgs: () => ParsedResult | void = () => {
const resolveTicketIdsScript = core.getInput("resolve-ticket-ids-script");

const resolveTicketIdsFunc =
resolveTicketIdsScript !== ""
? new AsyncFunction("branchName", resolveTicketIdsScript)
: undefined;

let jiraConfig: Partial<JiraConfig> = {};
jiraConfig.jiraIssueId = core.getInput("jira-issue-id");
try {
Expand All @@ -15,20 +24,18 @@ const getArgs: () => ParsedResult | void = () => {

const { jiraAccount, jiraEndpoint, jiraIssueId, jiraToken } = jiraConfig;

if (!jiraIssueId || jiraIssueId === "") {
if ((!jiraIssueId || jiraIssueId === "") && !resolveTicketIdsFunc) {
return {
exit: true,
success: false,
message: "Jira issue id not found, exiting...",
message: "Jira issue id and ticket id resolver not found, exiting...",
parsedInput: undefined
};
}

Array.from([jiraAccount, jiraEndpoint, jiraIssueId, jiraToken]).forEach(
value => {
if (value === "" || !value) throw new Error("");
}
);
Array.from([jiraAccount, jiraEndpoint, jiraToken]).forEach(value => {
if (value === "" || !value) throw new Error("");
});
} catch (error) {
console.log(`Missing input, using config file instead...`);
const {
Expand Down Expand Up @@ -73,7 +80,8 @@ const getArgs: () => ParsedResult | void = () => {
jiraIssueNumber,
jiraTokenEncoded: Buffer.from(`${jiraAccount}:${jiraToken}`).toString(
"base64"
)
),
resolveTicketIdsFunc
}
};
};
Expand Down
43 changes: 33 additions & 10 deletions src/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { JiraClient } from "./jira";
import { HandleTransitionParams } from "./interfaces";
import { warning } from "@actions/core";
import { warning, info } from "@actions/core";

const handleTransitionIssue = async ({
const transitionIssue = async ({
jiraTokenEncoded,
jiraEndpoint,
jiraIssueId,
colName
}: HandleTransitionParams) => {
const jira = new JiraClient(jiraTokenEncoded);

const issueDetail = await jira.request(
`${jiraEndpoint}/rest/api/3/issue/${jiraIssueId}`
);
Expand All @@ -19,9 +20,9 @@ const handleTransitionIssue = async ({
} = issueDetail;
if (name === colName) {
warning(`
The issue ${jiraIssueId} is already in status ${colName}.
Action will exit without doing anything.
`);
The issue ${jiraIssueId} is already in status ${colName}.
Action will exit without doing anything.
`);
} else {
const availableTransitions = await jira.request(
`${jiraEndpoint}/rest/api/3/issue/${jiraIssueId}/transitions`
Expand All @@ -31,11 +32,12 @@ const handleTransitionIssue = async ({
)?.id;
if (!transitionId)
throw new Error(`
There was an error trying to transition issue ${jiraIssueId}:
Transition to status "${colName}" cannot be found.
Check all of the transitions' rules, conditions of yourr project's workflow.
See more: https://confluence.atlassian.com/adminjiraserver073/working-with-workflows-861253510.html
`);
There was an error trying to transition issue ${jiraIssueId}:
Transition to status "${colName}" cannot be found.
Check all of the transitions' rules, conditions of yourr project's workflow.
See more: https://confluence.atlassian.com/adminjiraserver073/working-with-workflows-861253510.html
`);
info(`Changing issue ${jiraIssueId} to ${colName}`);
await jira.request(
`${jiraEndpoint}/rest/api/3/issue/${jiraIssueId}/transitions`,
"POST",
Expand All @@ -44,4 +46,25 @@ const handleTransitionIssue = async ({
}
};

const handleTransitionIssue = async ({
resolveTicketIdsFunc,
branchName,
...rest
}: HandleTransitionParams) => {
if (resolveTicketIdsFunc && branchName) {
const result = await resolveTicketIdsFunc(branchName);
if (Array.isArray(result)) {
result.forEach(ticketId => {
transitionIssue({ ...rest, jiraIssueId: ticketId });
});
} else if (typeof result === "string") {
transitionIssue({ ...rest, jiraIssueId: result });
} else {
transitionIssue({ ...rest });
}
} else {
transitionIssue({ ...rest });
}
};

export { handleTransitionIssue };
49 changes: 30 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,33 @@ async function run() {
const context = github.context;
const {
repo: { owner, repo },
payload: { action },
payload,
eventName
} = context;
if (eventName === "pull_request" && action === "review_requested") {
core.info(
`Changing issue ${parsedInput.jiraIssueId} to ${parsedInput.columnToMoveToWhenReviewRequested}`
);
if (
eventName === "pull_request" &&
payload.action === "review_requested"
) {
const {
pull_request: {
head: { ref }
}
} = payload as Webhooks.WebhookPayloadPullRequest;
core.info(`Branch name: ${ref}`);
await handleTransitionIssue({
...parsedInput,
colName: parsedInput.columnToMoveToWhenReviewRequested
colName: parsedInput.columnToMoveToWhenReviewRequested,
branchName: ref
});
} else if (
eventName === "pull_request_review" &&
action === "submitted"
payload.action === "submitted"
) {
const {
pull_request: { number },
pull_request: {
number,
head: { ref }
},
review: { id }
} = context.payload as Webhooks.WebhookPayloadPullRequestReview;
const { githubToken } = parsedInput;
Expand All @@ -43,25 +53,26 @@ async function run() {
review_id: id
});
if (isRequestChange) {
core.info(
`Changing issue ${parsedInput.jiraIssueId} to ${parsedInput.columnToMoveToWhenChangesRequested}`
);
core.info(`Branch name: ${ref}`);
await handleTransitionIssue({
...parsedInput,
colName: parsedInput.columnToMoveToWhenChangesRequested
colName: parsedInput.columnToMoveToWhenChangesRequested,
branchName: ref
});
}
} else if (eventName === "pull_request" && action === "closed") {
} else if (eventName === "pull_request" && payload.action === "closed") {
const {
merged
} = (context.payload as Webhooks.WebhookPayloadPullRequest).pull_request;
pull_request: {
merged,
head: { ref }
}
} = payload as Webhooks.WebhookPayloadPullRequest;
if (merged && parsedInput.columnToMoveToWhenMerged) {
core.info(
`Changing issue ${parsedInput.jiraIssueId} to ${parsedInput.columnToMoveToWhenMerged}`
);
core.info(`Branch name: ${ref}`);
await handleTransitionIssue({
...parsedInput,
colName: parsedInput.columnToMoveToWhenMerged
colName: parsedInput.columnToMoveToWhenMerged,
branchName: ref
});
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ interface _Params {
columnToMoveToWhenReviewRequested: string;
columnToMoveToWhenChangesRequested: string;
columnToMoveToWhenMerged?: string;
resolveTicketIdsFunc?: (
branchName: string
) => Promise<string[] | string | void>;
}

interface AdditionalJiraConfig {
Expand All @@ -34,7 +37,10 @@ export interface ParsedResult {
}
type HandlerParams = Pick<
ParsedInput,
"jiraTokenEncoded" | "jiraEndpoint" | "jiraIssueId"
"jiraTokenEncoded" | "jiraEndpoint" | "jiraIssueId" | "resolveTicketIdsFunc"
>;

export type HandleTransitionParams = HandlerParams & { colName: string };
export type HandleTransitionParams = HandlerParams & {
colName: string;
branchName?: string;
};

0 comments on commit c900f4f

Please sign in to comment.