Skip to content

Commit

Permalink
Update prettier and eslint configs
Browse files Browse the repository at this point in the history
Fixes #56
  • Loading branch information
philip-gai committed Sep 8, 2021
1 parent 62742bd commit 2a1e580
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 95 deletions.
52 changes: 3 additions & 49 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,54 +1,8 @@
{
"plugins": ["jest", "@typescript-eslint", "filenames", "i18n-text"],
"extends": ["plugin:github/recommended"],
"root": true,
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 9,
"sourceType": "module",
"project": "./tsconfig.json"
},
"rules": {
"eslint-comments/no-use": "off",
"import/no-namespace": "off",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/explicit-member-accessibility": ["error", {"accessibility": "no-public"}],
"@typescript-eslint/no-require-imports": "error",
"@typescript-eslint/array-type": "error",
"@typescript-eslint/await-thenable": "error",
"@typescript-eslint/ban-ts-comment": "error",
"camelcase": "off",
"@typescript-eslint/consistent-type-assertions": "error",
"@typescript-eslint/explicit-function-return-type": ["error", {"allowExpressions": true}],
"@typescript-eslint/func-call-spacing": ["error", "never"],
"@typescript-eslint/no-array-constructor": "error",
"@typescript-eslint/no-empty-interface": "error",
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/no-extraneous-class": "error",
"@typescript-eslint/no-for-in-array": "error",
"@typescript-eslint/no-inferrable-types": "error",
"@typescript-eslint/no-misused-new": "error",
"@typescript-eslint/no-namespace": "error",
"@typescript-eslint/no-non-null-assertion": "warn",
"@typescript-eslint/no-unnecessary-qualifier": "error",
"@typescript-eslint/no-unnecessary-type-assertion": "error",
"@typescript-eslint/no-useless-constructor": "error",
"@typescript-eslint/no-var-requires": "error",
"@typescript-eslint/prefer-for-of": "warn",
"@typescript-eslint/prefer-function-type": "warn",
"@typescript-eslint/prefer-includes": "error",
"@typescript-eslint/prefer-string-starts-ends-with": "error",
"@typescript-eslint/promise-function-async": "error",
"@typescript-eslint/require-array-sort-compare": "error",
"@typescript-eslint/restrict-plus-operands": "error",
"semi": 2,
"@typescript-eslint/type-annotation-spacing": "error",
"@typescript-eslint/unbound-method": "error",
"filenames/match-regex": 0,
"filenames/match-exported": 2,
"filenames/no-index": 2,
"i18n-text/no-en": 0
},
"plugins": ["@typescript-eslint", "jest"],
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended", "prettier"],
"env": {
"node": true,
"es6": true,
Expand Down
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"semi": true,
"trailingComma": "none",
"singleQuote": true,
"printWidth": 80
}
9 changes: 0 additions & 9 deletions .prettierrc.json

This file was deleted.

5 changes: 5 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"esbenp.prettier-vscode"
]
}
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
{
"editor.formatOnSave": true,
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
11 changes: 7 additions & 4 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@typescript-eslint/parser": "^4.30.0",
"@vercel/ncc": "^0.29.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-github": "^4.2.0",
"eslint-plugin-jest": "^24.4.0",
"jest": "^27.0.6",
Expand Down
61 changes: 50 additions & 11 deletions src/azureDevOpsClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import * as azdev from 'azure-devops-node-api';
import * as core from '@actions/core';
import { AppConfig, ConfigService } from './configService';
import { GitRefUpdate, GitRefUpdateResult, GitRepository } from 'azure-devops-node-api/interfaces/GitInterfaces';
import {
GitRefUpdate,
GitRefUpdateResult,
GitRepository
} from 'azure-devops-node-api/interfaces/GitInterfaces';
import { CreateBranchOptions } from './azureDevOpsService';
import { IGitApi } from 'azure-devops-node-api/GitApi';

Expand All @@ -22,7 +26,10 @@ export class AzureDevOpsClient {
return new AzureDevOpsClient(appConfig, azDevClient);
}

async createBranch(branchName: string, options: CreateBranchOptions): Promise<GitRefUpdateResult> {
async createBranch(
branchName: string,
options: CreateBranchOptions
): Promise<GitRefUpdateResult> {
try {
core.info('Getting the ADO git API...');
const gitClient = await this._azDevClient.getGitApi();
Expand All @@ -37,15 +44,23 @@ export class AzureDevOpsClient {
sourceBranch = this.getDefaultBranch(repo);
core.info('Got it.');
}
const result = await this.createBranchInner(branchName, options, gitClient, repo);
const result = await this.createBranchInner(
branchName,
options,
gitClient,
repo
);
return result;
} catch (error: unknown) {
core.error(`Failed to create branch: ${branchName}`);
throw new Error(`Failed to create branch: ${branchName}`);
}
}

async deleteBranch(refName?: string, refObjectId?: string): Promise<GitRefUpdateResult> {
async deleteBranch(
refName?: string,
refObjectId?: string
): Promise<GitRefUpdateResult> {
const gitClient = await this._azDevClient.getGitApi();

const gitRefUpdates: GitRefUpdate[] = [
Expand All @@ -57,17 +72,30 @@ export class AzureDevOpsClient {
];

// create a new branch from the source
const updateResults = await gitClient.updateRefs(gitRefUpdates, this._appConfig.ado_repo, this._appConfig.ado_project);
const updateResults = await gitClient.updateRefs(
gitRefUpdates,
this._appConfig.ado_repo,
this._appConfig.ado_project
);
const refDeleteResult = updateResults[0];

return refDeleteResult;
}

private async createBranchInner(branchName: string, options: CreateBranchOptions, gitClient: IGitApi, repo: GitRepository): Promise<GitRefUpdateResult> {
private async createBranchInner(
branchName: string,
options: CreateBranchOptions,
gitClient: IGitApi,
repo: GitRepository
): Promise<GitRefUpdateResult> {
core.debug(`Creating branch from ${options.sourceBranch}.`);

core.debug(`Getting ${options.sourceBranch} refs...`);
const gitRefs = await gitClient.getRefs(repo.id as string, this._appConfig.ado_project, options.sourceBranch);
const gitRefs = await gitClient.getRefs(
repo.id as string,
this._appConfig.ado_project,
options.sourceBranch
);
const sourceRef = gitRefs[0];
core.debug("Got 'em.");

Expand All @@ -81,7 +109,11 @@ export class AzureDevOpsClient {

// create a new branch from the source
core.debug('Creating the new branch...');
const updateResults = await gitClient.updateRefs(gitRefUpdates, this._appConfig.ado_repo, this._appConfig.ado_project);
const updateResults = await gitClient.updateRefs(
gitRefUpdates,
this._appConfig.ado_repo,
this._appConfig.ado_project
);
const refCreateResult = updateResults[0];

return refCreateResult;
Expand All @@ -97,10 +129,17 @@ export class AzureDevOpsClient {
}

private async getRepo(gitClient: IGitApi): Promise<GitRepository> {
const repo = await gitClient.getRepository(this._appConfig.ado_repo, this._appConfig.ado_project);
const repo = await gitClient.getRepository(
this._appConfig.ado_repo,
this._appConfig.ado_project
);
if (!repo.id) {
core.error(`Repo ${this._appConfig.ado_repo} does not exist in project ${this._appConfig.ado_project}`);
throw new Error(`Repo ${this._appConfig.ado_repo} does not exist in project ${this._appConfig.ado_project}`);
core.error(
`Repo ${this._appConfig.ado_repo} does not exist in project ${this._appConfig.ado_project}`
);
throw new Error(
`Repo ${this._appConfig.ado_repo} does not exist in project ${this._appConfig.ado_project}`
);
}
return repo;
}
Expand Down
16 changes: 12 additions & 4 deletions src/azureDevOpsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export class AzureDevOpsService {
this._adoClient = adoClient;
}

static async build(configService: ConfigService): Promise<AzureDevOpsService> {
static async build(
configService: ConfigService
): Promise<AzureDevOpsService> {
const appConfig = configService.appConfig;
const adoClient = await AzureDevOpsClient.build(configService);
return new AzureDevOpsService(appConfig, adoClient);
Expand All @@ -39,7 +41,9 @@ export class AzureDevOpsService {
this._adoClient.createBranch(branchName, options);

// Create a comment with a link to the newly created branch
const successMessage = `Branch [${branchName}](${this.getBranchUrl(branchName)}) has been created in Azure DevOps`;
const successMessage = `Branch [${branchName}](${this.getBranchUrl(
branchName
)}) has been created in Azure DevOps`;
return successMessage;
} catch (e: unknown) {
const errorMessage = `Failed creating the branch in ADO: ${e}`;
Expand All @@ -54,8 +58,12 @@ export class AzureDevOpsService {
}

private buildBranchName(options: CreateBranchOptions): string {
const issueInfo = `${options.issueNumber}-${options.issueTitle.toLowerCase()}`;
const branchName = `users/${this.makeGitSafe(options.username)}/${this.makeGitSafe(issueInfo)}`;
const issueInfo = `${
options.issueNumber
}-${options.issueTitle.toLowerCase()}`;
const branchName = `users/${this.makeGitSafe(
options.username
)}/${this.makeGitSafe(issueInfo)}`;
return branchName.substr(0, this.maxNumOfChars);
}

Expand Down
15 changes: 10 additions & 5 deletions src/chatOpService.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { ChatOpCommand, ChatOpParam, ParamValueMap, chatOpInfo, chatOps } from './chatOps';
import {
ChatOpCommand,
ChatOpParam,
ParamValueMap,
chatOpInfo,
chatOps
} from './chatOps';

export class ChatOpService {
private constructor() {}

static build(): ChatOpService {
return new ChatOpService();
}

getChatOpCommand(comment: string): ChatOpCommand {
const command = this.getCommandString(comment);
return chatOps.find(op => op === command) || 'None';
return chatOps.find((op) => op === command) || 'None';
}

getParameterValues(command: ChatOpCommand, comment: string): ParamValueMap {
const possibleParams = chatOpInfo.find(info => info.commands.includes(command))?.params || [];
const possibleParams =
chatOpInfo.find((info) => info.commands.includes(command))?.params || [];
if (possibleParams.length === 0) return {};
const paramValueMap: ParamValueMap = {};
for (const pp of possibleParams) {
Expand Down
21 changes: 14 additions & 7 deletions src/configService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export class ConfigService {

private static mergeDefaults(loadedConfig: AppConfig): AppConfig {
return {
ado_domain: loadedConfig.ado_domain || ConfigService.defaultAppConfig.ado_domain,
ado_domain:
loadedConfig.ado_domain || ConfigService.defaultAppConfig.ado_domain,
ado_org: loadedConfig.ado_org,
ado_pat: loadedConfig.ado_pat,
ado_project: loadedConfig.ado_project,
Expand All @@ -41,12 +42,18 @@ export class ConfigService {
private static validateConfig(config: AppConfig): string[] {
const errorMessages: string[] = [];
// There are better ways to do this but I'm being lazy
if (!config.ado_domain) errorMessages.push('No ado_org was found. Check your inputs');
if (!config.ado_org) errorMessages.push('No ado_org was found. Check your inputs');
if (!config.ado_pat) errorMessages.push('No ado_pat was found. Check your inputs');
if (!config.ado_project) errorMessages.push('No ado_project was found. Check your inputs');
if (!config.ado_repo) errorMessages.push('No ado_repo was found. Check your inputs');
if (!config.github_token) errorMessages.push('No github_token was found. Check your inputs');
if (!config.ado_domain)
errorMessages.push('No ado_org was found. Check your inputs');
if (!config.ado_org)
errorMessages.push('No ado_org was found. Check your inputs');
if (!config.ado_pat)
errorMessages.push('No ado_pat was found. Check your inputs');
if (!config.ado_project)
errorMessages.push('No ado_project was found. Check your inputs');
if (!config.ado_repo)
errorMessages.push('No ado_repo was found. Check your inputs');
if (!config.github_token)
errorMessages.push('No github_token was found. Check your inputs');
return errorMessages;
}

Expand Down
Loading

0 comments on commit 2a1e580

Please sign in to comment.