Skip to content

Commit

Permalink
🎨 Format files
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinVignal committed Nov 27, 2021
1 parent 0ec9a9a commit 6ce8b9c
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/Actions/Github/EventName.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export type EventName = 'pull_request'|'push';
export type EventName = 'pull_request' | 'push';
2 changes: 1 addition & 1 deletion src/format/Format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ export async function format(params: { modifiedFiles: ModifiedFiles, ignoredFile
return new FormatResult({
files: fileNotFormatted,
});
}
}
2 changes: 1 addition & 1 deletion src/format/FormatResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ export class FormatResult {
}
return comments.join('\n');
}
}
}
4 changes: 1 addition & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Result } from './result/Result';
import { IgnoredFiles } from './utils/IgnoredFiles';
import { ModifiedFiles } from './utils/ModifiedFiles';


/**
* Run the action
*/
Expand Down Expand Up @@ -36,10 +35,9 @@ async function main(): Promise<void> {
await result.comment();
}
result.log();
} catch (error) {
} catch (error: any) {
core.setFailed(`error: ${error.message}`);
}

}

main();
22 changes: 11 additions & 11 deletions src/result/Result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class Result {
/**
* Whether it is a success or not
*/
public get success():boolean {
public get success(): boolean {
return this.analyze.success && this.format.success;
}

Expand All @@ -41,7 +41,7 @@ export class Result {
*/
public async comment(): Promise<void> {
const messages: string[] = [
this.issueCountMessage({emojis: true})
this.issueCountMessage({ emojis: true })
];

const analyzeBody = this.analyze.commentBody;
Expand All @@ -53,7 +53,7 @@ export class Result {
messages.push(formatBody);
}

await comment({message: messages.join('\n---\n')});
await comment({ message: messages.join('\n---\n') });
}

/**
Expand All @@ -62,7 +62,7 @@ export class Result {
* @param params
* @returns
*/
private issueCountMessage(params?: {emojis?: boolean}): string {
private issueCountMessage(params?: { emojis?: boolean }): string {
const messages: string[] = [
this.title(params),
this.titleLineAnalyze({
Expand Down Expand Up @@ -90,7 +90,7 @@ export class Result {
* @param params
* @returns
*/
private title(params?: {emojis?: boolean}): string {
private title(params?: { emojis?: boolean }): string {
const title = `Dart Analyzer found ${this.count} issue${Result.pluralS(this.count)}`;
if (params?.emojis && actionOptions.emojis) {
let emoji = ':tada:';
Expand All @@ -111,7 +111,7 @@ export class Result {
* @param params
* @returns
*/
private titleLineAnalyze(params: {emojis?: boolean, type: DartAnalyzeLogTypeEnum}): string {
private titleLineAnalyze(params: { emojis?: boolean, type: DartAnalyzeLogTypeEnum }): string {
const isFail = DartAnalyzeLogType.isFail(params.type);
let emoji = '';
let count: number;
Expand Down Expand Up @@ -146,10 +146,10 @@ export class Result {
* @param params
* @returns
*/
private titleLineFormat(params: {emojis?: boolean}):string {
private titleLineFormat(params: { emojis?: boolean }): string {
let emoji = `:${this.format.count ? 'poop' : 'art'}: `;
const highlight = params.emojis && this.format.count && actionOptions.failOn === FailOnEnum.Format ? '**' : '';
return `- ${params.emojis && actionOptions.emojis ? emoji : '' }${highlight}${this.format.count} formatting issue${Result.pluralS(this.format.count)}${highlight}`;
return `- ${params.emojis && actionOptions.emojis ? emoji : ''}${highlight}${this.format.count} formatting issue${Result.pluralS(this.format.count)}${highlight}`;
}

/**
Expand All @@ -158,7 +158,7 @@ export class Result {
public log(): void {
const logger = this.success ? (this.count ? core.warning : core.info) : core.setFailed;
logger(this.issueCountMessage());

}

/**
Expand All @@ -167,7 +167,7 @@ export class Result {
* @returns 's' if count > 1, else ''
*/
private static pluralS(count: number): string {
return count > 1 ? 's': '';
return count > 1 ? 's' : '';
}

/**
Expand All @@ -176,4 +176,4 @@ export class Result {
private get count() {
return this.analyze.counts.total + this.format.count;
}
}
}
4 changes: 2 additions & 2 deletions src/utils/ActionOptions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as core from '@actions/core';
import * as path from 'path';
import { FailOn, FailOnEnum } from "./FailOn"
import { FailOn, FailOnEnum } from "./FailOn";

/**
* Contains all the options of the action
Expand Down Expand Up @@ -31,4 +31,4 @@ class ActionOptions {
/**
* Singleton with the option of the action
*/
export const actionOptions = new ActionOptions();
export const actionOptions = new ActionOptions();
10 changes: 5 additions & 5 deletions src/utils/Comment.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import * as github from '@actions/github';
import * as core from '@actions/core';
import * as github from '@actions/github';
import { context } from '@actions/github/lib/utils';

export type CommentReact = '+1'|'-1'|'laugh'|'confused'|'heart'|'hooray'|'rocket'|'eyes';
export type CommentReact = '+1' | '-1' | 'laugh' | 'confused' | 'heart' | 'hooray' | 'rocket' | 'eyes';

export async function comment(params: {message: string, reacts?: CommentReact[]}): Promise<void> {
export async function comment(params: { message: string, reacts?: CommentReact[] }): Promise<void> {

if (!github.context.payload.pull_request) {
// Can only comment on Pull Requests
return;
}
const octokit = github.getOctokit(core.getInput('token', {required: true}));
const octokit = github.getOctokit(core.getInput('token', { required: true }));

// Create the comment
try {
Expand All @@ -36,6 +36,6 @@ export async function comment(params: {message: string, reacts?: CommentReact[]}
}
} catch (error) {
console.log(`Couldn't comment "${params.message} with reacts ${params.reacts}`);
}
}
}

4 changes: 2 additions & 2 deletions src/utils/FailOn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export enum FailOnEnum{
export enum FailOnEnum {
Error = 0,
Warning = 1,
Info = 2,
Expand All @@ -8,7 +8,7 @@ export enum FailOnEnum{

export class FailOn {
static fromInput(input: string) {
switch(input) {
switch (input) {
case 'nothing':
return FailOnEnum.Nothing;
case 'format':
Expand Down
5 changes: 2 additions & 3 deletions src/utils/IgnoredFiles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as yaml from 'js-yaml';
import * as fs from 'fs';
// import { minimatch } from 'minimatch';
import * as yaml from 'js-yaml';
import * as minimatch from 'minimatch';
import * as path from 'path';
import { actionOptions } from './ActionOptions';
Expand All @@ -13,7 +12,7 @@ export class IgnoredFiles {
constructor() {
let patterns: string[];
try {
const yamlFile = yaml.load(fs.readFileSync(path.resolve(actionOptions.workingDirectory, './analysis_options.yaml'), 'utf8')) as { analyzer?: { exclude?: string[] } };
const yamlFile = yaml.load(fs.readFileSync(path.resolve(actionOptions.workingDirectory, 'analysis_options.yaml'), 'utf8')) as { analyzer?: { exclude?: string[] } };
patterns = yamlFile?.analyzer?.exclude ?? [];
} catch (error) {
console.log(`Could not load analysis_options.yaml:\n${error}`);
Expand Down

0 comments on commit 6ce8b9c

Please sign in to comment.