Skip to content

Commit

Permalink
refactor: Integrate RunContext into IContext
Browse files Browse the repository at this point in the history
  • Loading branch information
peaceiris committed Aug 15, 2021
1 parent c21af74 commit bc18f88
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
16 changes: 8 additions & 8 deletions __tests__/classes/comment.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Comment} from '../../src/classes/comment';
import {IConfig} from '../../src/classes/config';
import {RunContext} from '../../src/classes/context-loader';
import {IContext} from '../../src/classes/context-loader';
import {getDefaultInputs, cleanupEnvs} from '../../src/test-helper';

beforeEach(() => {
Expand Down Expand Up @@ -72,7 +72,7 @@ describe('header and footer', () => {
},
...config
};
const ctx: RunContext = {
const ctx: IContext = {
configFilePath: '.github/label-commenter-config.yml',
eventName: 'issues',
id: 'MDExOlB1bGxSZXF1ZXN0NzA2MTE5NTg0',
Expand Down Expand Up @@ -112,7 +112,7 @@ Thank you @userLogin for suggesting this. Please follow the issue templates.
});

describe('isDebug', () => {
const ctx: RunContext = {
const ctx: IContext = {
configFilePath: '.github/label-commenter-config.yml',
eventName: 'issues',
id: 'MDExOlB1bGxSZXF1ZXN0NzA2MTE5NTg0',
Expand Down Expand Up @@ -157,7 +157,7 @@ Thank you @userLogin for suggesting this. Please follow the issue templates.
});

describe('invalid.labeled.issue', () => {
const ctx: RunContext = {
const ctx: IContext = {
configFilePath: '.github/label-commenter-config.yml',
eventName: 'issues',
id: 'MDExOlB1bGxSZXF1ZXN0NzA2MTE5NTg0',
Expand Down Expand Up @@ -189,7 +189,7 @@ Thank you @userLogin for suggesting this. Please follow the issue templates.
});

describe('invalid.labeled.pr pull_request', () => {
const ctx: RunContext = {
const ctx: IContext = {
configFilePath: '.github/label-commenter-config.yml',
eventName: 'pull_request',
id: 'MDExOlB1bGxSZXF1ZXN0NzA2MTE5NTg0',
Expand Down Expand Up @@ -221,7 +221,7 @@ Thank you @userLogin for suggesting this. Please follow the pull request templat
});

describe('invalid.labeled.pr pull_request_target', () => {
const ctx: RunContext = {
const ctx: IContext = {
configFilePath: '.github/label-commenter-config.yml',
eventName: 'pull_request_target',
id: 'MDExOlB1bGxSZXF1ZXN0NzA2MTE5NTg0',
Expand Down Expand Up @@ -254,7 +254,7 @@ Thank you @userLogin for suggesting this. Please follow the pull request templat

describe('placeholders', () => {
test('comment.render returns expected comment, author', () => {
const ctx: RunContext = {
const ctx: IContext = {
configFilePath: '.github/label-commenter-config.yml',
eventName: 'discussion',
id: 'MDExOlB1bGxSZXF1ZXN0NzA2MTE5NTg0',
Expand Down Expand Up @@ -283,7 +283,7 @@ Thank you @userLogin for suggesting this. @senderLogin will reply.
});

test('comment.render returns expected comment, eventName, number, labelName', () => {
const ctx: RunContext = {
const ctx: IContext = {
configFilePath: '.github/label-commenter-config.yml',
eventName: 'pull_request',
id: 'MDExOlB1bGxSZXF1ZXN0NzA2MTE5NTg0',
Expand Down
8 changes: 4 additions & 4 deletions src/classes/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import Mustache from 'mustache';
import {ActionInfo} from '../constants';
import {groupConsoleLog, info} from '../logger';
import {IConfig} from './config';
import {RunContext} from './context-loader';
import {IContext} from './context-loader';

interface IComment {
readonly config: IConfig;
readonly runContext: RunContext;
readonly runContext: IContext;

readonly main: string;
readonly header: string;
Expand All @@ -33,15 +33,15 @@ interface ICommentGenerator extends IComment {

class Comment implements ICommentGenerator {
readonly config: IConfig;
readonly runContext: RunContext;
readonly runContext: IContext;

readonly main: string;
readonly header: string;
readonly footer: string;
readonly footerLinks: string;
readonly rawBody: string;

constructor(runContext: RunContext, config: IConfig) {
constructor(runContext: IContext, config: IConfig) {
this.config = config;
this.runContext = runContext;
this.main = this.getMain();
Expand Down
8 changes: 4 additions & 4 deletions src/classes/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import yaml from 'js-yaml';
import {get} from 'lodash-es';

import {groupConsoleLog} from '../logger';
import {RunContext} from './context-loader';
import {IContext} from './context-loader';
import {LockReason} from './issue';

type Locking = 'lock' | 'unlock' | undefined;
Expand All @@ -25,7 +25,7 @@ interface IConfig {
}

interface IConfigLoader extends IConfig {
readonly runContext: RunContext;
readonly runContext: IContext;

getConfig(): IConfig;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -39,7 +39,7 @@ interface IConfigLoader extends IConfig {
}

class ConfigLoader implements IConfigLoader {
readonly runContext: RunContext;
readonly runContext: IContext;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly config: any;
Expand All @@ -51,7 +51,7 @@ class ConfigLoader implements IConfigLoader {
readonly draft?: Draft;
readonly answer?: Answer;

constructor(runContext: RunContext) {
constructor(runContext: IContext) {
try {
this.runContext = runContext;
this.config = this.loadConfig();
Expand Down
26 changes: 13 additions & 13 deletions src/classes/context-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,28 @@ const eventTypeTable: EventTypeTable = {
const eventType = (eventName: EventName): EventAlias =>
eventTypeTable[eventName as keyof EventTypeTable];

interface RunContext {
interface IContext {
readonly configFilePath: string;

readonly eventName: EventName;
readonly id: string;
readonly eventAlias: EventAlias;
readonly labelEvent: LabelEvent;
readonly labelName: string;
readonly labelName: string | undefined;
readonly issueNumber: number;
readonly userLogin: string;
readonly senderLogin: string;
readonly locked: boolean;
}

interface IContext {
interface IContextLoader extends IContext {
readonly inputs: Inputs;
readonly context: Context;
readonly payload: Payload;
readonly runContext: IContext;

readonly runContext: RunContext;
}

interface IContextLoader extends IContext {
dumpContext(): void;
getRunContext(): RunContext;
getRunContext(): IContext;
getId(): string;
getEventName(): EventName;
getEventAlias(): EventAlias;
Expand All @@ -74,6 +71,9 @@ class ContextLoader implements IContextLoader {
readonly inputs: Inputs;
readonly context: Context;
readonly payload: Payload;
readonly runContext: IContext;

readonly configFilePath: string;

readonly eventName: EventName;
readonly id: string;
Expand All @@ -85,14 +85,14 @@ class ContextLoader implements IContextLoader {
readonly senderLogin: string;
readonly locked: boolean;

readonly runContext: RunContext;

constructor(inputs: Inputs, context: Context) {
try {
this.inputs = inputs;
this.context = context;
this.payload = context.payload as Payload;

this.configFilePath = this.inputs.ConfigFilePath;

this.eventName = this.getEventName();
this.id = this.getId();
this.eventAlias = this.getEventAlias();
Expand All @@ -115,8 +115,8 @@ class ContextLoader implements IContextLoader {
info(`Issue number: ${this.issueNumber}`);
}

getRunContext(): RunContext {
const runContext: RunContext = {
getRunContext(): IContext {
const runContext: IContext = {
configFilePath: this.inputs.ConfigFilePath,
eventName: this.eventName,
id: this.id,
Expand Down Expand Up @@ -210,4 +210,4 @@ class ContextLoader implements IContextLoader {
}
}

export {Payload, EventName, EventAlias, LabelEvent, RunContext, IContext, ContextLoader};
export {Payload, EventName, EventAlias, LabelEvent, IContext, ContextLoader};

0 comments on commit bc18f88

Please sign in to comment.