Skip to content

Commit

Permalink
fix: error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
peaceiris committed Mar 8, 2020
1 parent 35315b2 commit 6ed2bad
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Label Commenter
name: Commenter

on:
issues:
Expand All @@ -11,7 +11,7 @@ on:
- unlabeled

jobs:
label-commenter:
comment:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ on:
- unlabeled

jobs:
comment:
label-commenter:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
Expand Down
172 changes: 84 additions & 88 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,117 +32,113 @@ async function openIssue(
}

export async function run(): Promise<void> {
try {
const inps: Inputs = getInputs();
const inps: Inputs = getInputs();

if (core.isDebug()) {
console.log(context);
}
if (core.isDebug()) {
console.log(context);
}

const eventName = context.eventName;
const labelEvent = context.payload.action;
const eventName = context.eventName;
const labelEvent = context.payload.action;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const labelName = (context.payload as any).label.name;
let issueNumber = 0;
if (eventName === 'issues') {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const labelName = (context.payload as any).label.name;
let issueNumber = 0;
if (eventName === 'issues') {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
issueNumber = (context.payload as any).issue.number;
} else if (eventName === 'pull_request') {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
issueNumber = (context.payload as any).number;
}
issueNumber = (context.payload as any).issue.number;
} else if (eventName === 'pull_request') {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
issueNumber = (context.payload as any).number;
}

core.info(`\
core.info(`\
[INFO] config_file: ${inps.ConfigFilePath}
[INFO] labelName: ${labelName}
[INFO] labelEvent: ${labelEvent}
[INFO] issueNumber: ${issueNumber}\
`);

const configFilePath = inps.ConfigFilePath;
const config = yaml.safeLoad(fs.readFileSync(configFilePath, 'utf8'));
if (core.isDebug()) {
console.log(config);
}
const configFilePath = inps.ConfigFilePath;
const config = yaml.safeLoad(fs.readFileSync(configFilePath, 'utf8'));
if (core.isDebug()) {
console.log(config);
}

let isExistLabel = false;
let labelIndex = '';
Object.keys(config.labels).forEach(label => {
if (config.labels[label].name === labelName) {
isExistLabel = true;
if (labelIndex === '') {
labelIndex = label;
}
let isExistLabel = false;
let labelIndex = '';
Object.keys(config.labels).forEach(label => {
if (config.labels[label].name === labelName) {
isExistLabel = true;
if (labelIndex === '') {
labelIndex = label;
}
});

if (!isExistLabel) {
core.info(`[INFO] no configuration labels.${labelName}`);
return;
}
});

if (config.labels[labelIndex][`${labelEvent}`] === void 0) {
core.info(`[INFO] no configuration labels.${labelName}.${labelEvent}`);
return;
}
if (!isExistLabel) {
core.info(`[INFO] no configuration labels.${labelName}`);
return;
}

if (
config.labels[labelIndex][`${labelEvent}`].issue === void 0 &&
config.labels[labelIndex][`${labelEvent}`].pr === void 0
) {
throw new Error(
`not found any definition labels.${labelName}.${labelEvent}`
);
}
if (config.labels[labelIndex][`${labelEvent}`] === void 0) {
core.info(`[INFO] no configuration labels.${labelName}.${labelEvent}`);
return;
}

let eventType = '';
if (eventName === 'issues') {
eventType = 'issue';
if (config.labels[labelIndex][`${labelEvent}`].issue === void 0) {
eventType = 'pr';
}
} else if (eventName === 'pull_request') {
if (
config.labels[labelIndex][`${labelEvent}`].issue === void 0 &&
config.labels[labelIndex][`${labelEvent}`].pr === void 0
) {
throw new Error(
`not found any definition labels.${labelName}.${labelEvent}`
);
}

let eventType = '';
if (eventName === 'issues') {
eventType = 'issue';
if (config.labels[labelIndex][`${labelEvent}`].issue === void 0) {
eventType = 'pr';
if (config.labels[labelIndex][`${labelEvent}`].pr === void 0) {
eventType = 'issue';
}
}
} else if (eventName === 'pull_request') {
eventType = 'pr';
if (config.labels[labelIndex][`${labelEvent}`].pr === void 0) {
eventType = 'issue';
}
}

const commentBody =
config.labels[labelIndex][`${labelEvent}`][`${eventType}`].body;
const finalAction =
config.labels[labelIndex][`${labelEvent}`][`${eventType}`].action;
core.info(`\
const commentBody =
config.labels[labelIndex][`${labelEvent}`][`${eventType}`].body;
const finalAction =
config.labels[labelIndex][`${labelEvent}`][`${eventType}`].action;
core.info(`\
[INFO] commentBody: ${commentBody}
[INFO] finalAction: ${finalAction}\
`);

const githubToken = inps.GithubToken;
const githubClient = new GitHub(githubToken);
await githubClient.issues.createComment({
// eslint-disable-next-line @typescript-eslint/camelcase
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody
});

if (finalAction === 'close') {
await closeIssue(githubClient, issueNumber);
} else if (finalAction === 'open') {
await openIssue(githubClient, issueNumber);
} else if (finalAction === '') {
core.info(
`[INFO] no configuration labels.${labelName}.${labelEvent}.${eventType}.action`
);
} else {
throw new Error(
`invalid value "${finalAction}" labels.${labelName}.${labelEvent}.${eventType}.action`
);
}
const githubToken = inps.GithubToken;
const githubClient = new GitHub(githubToken);
await githubClient.issues.createComment({
// eslint-disable-next-line @typescript-eslint/camelcase
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody
});

return;
} catch (error) {
throw new Error(error.message);
if (finalAction === 'close') {
await closeIssue(githubClient, issueNumber);
} else if (finalAction === 'open') {
await openIssue(githubClient, issueNumber);
} else if (finalAction === '') {
core.info(
`[INFO] no configuration labels.${labelName}.${labelEvent}.${eventType}.action`
);
} else {
throw new Error(
`invalid value "${finalAction}" labels.${labelName}.${labelEvent}.${eventType}.action`
);
}

return;
}

0 comments on commit 6ed2bad

Please sign in to comment.