Skip to content

Commit

Permalink
filter out own check
Browse files Browse the repository at this point in the history
  • Loading branch information
wechuli committed Jan 22, 2024
1 parent 26c4185 commit 2d45121
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 107 deletions.
116 changes: 62 additions & 54 deletions __tests__/checks/checksFilters.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import { FilterTypes,checkOneOfTheChecksInputIsEmpty,removeDuplicateEntriesChecksInputsFromSelf,removeChecksWithMatchingNameAndAppId,returnChecksWithMatchingNameAndAppId,filterChecksWithMatchingNameAndAppId,removeDuplicateChecksEntriesFromSelf} from "../../src/checks/checksFilters";
import {ICheck,ICheckInput} from "../../src/checks/checksInterfaces";
import {checkStatus,checkConclusion,commitStatusState } from "../../src/checks/checksConstants";


const checksData:ICheck[] = [
import {
FilterTypes,
checkOneOfTheChecksInputIsEmpty,
removeDuplicateEntriesChecksInputsFromSelf,
removeChecksWithMatchingNameAndAppId,
returnChecksWithMatchingNameAndAppId,
filterChecksWithMatchingNameAndAppId,
removeDuplicateChecksEntriesFromSelf
} from "../../src/checks/checksFilters";
import {ICheck, ICheckInput} from "../../src/checks/checksInterfaces";
import {checkStatus, checkConclusion, commitStatusState} from "../../src/checks/checksConstants";


const checksData: ICheck[] = [
{
id: 1,
name: "check1",
status: checkStatus.COMPLETED,
conclusion: checkConclusion.FAILURE,
started_at: "2023-06-12T22:25:32Z",
completed_at: "2023-06-12T22:26:32Z",
app:{
app: {
id: 1,
slug: "slug1",
name: "name1",
},
check_suite:{
check_suite: {
id: 142233,
}
},
Expand All @@ -27,12 +35,12 @@ const checksData:ICheck[] = [
conclusion: null,
started_at: "started_at3",
completed_at: "completed_at3",
app:{
app: {
id: 1,
slug: "slug3",
name: "name3",
},
check_suite:{
check_suite: {
id: 1,
}
},
Expand All @@ -43,12 +51,12 @@ const checksData:ICheck[] = [
conclusion: checkConclusion.FAILURE,
started_at: "started_at2",
completed_at: "completed_at2",
app:{
app: {
id: 2,
slug: "slug2",
name: "name2",
},
check_suite:{
check_suite: {
id: 24342,
}
},
Expand All @@ -59,12 +67,12 @@ const checksData:ICheck[] = [
conclusion: checkConclusion.SUCCESS,
started_at: "started_at4",
completed_at: "completed_at4",
app:{
app: {
id: 2,
slug: "slug4",
name: "name4",
},
check_suite:{
check_suite: {
id: 355,
}
},
Expand All @@ -75,12 +83,12 @@ const checksData:ICheck[] = [
conclusion: checkConclusion.FAILURE,
started_at: "started_at2",
completed_at: "completed_at2",
app:{
app: {
id: 3,
slug: "slug2",
name: "name2",
},
check_suite:{
check_suite: {
id: 23,
}
}
Expand All @@ -90,74 +98,74 @@ const checksData:ICheck[] = [

describe("filterChecksWithMatchingNameAndAppId", () => {
it("should return checks with matching name and app id", () => {
let input:ICheckInput[] = [{app_id:1,name:"check1"},{app_id:2,name:"check2"}];
let expected = {filteredChecks:[checksData[1],checksData[2]],missingChecks:[]};
let input: ICheckInput[] = [{app_id: 1, name: "check1"}, {app_id: 2, name: "check2"}];
let expected = {filteredChecks: [checksData[1], checksData[2]], missingChecks: []};

expect(filterChecksWithMatchingNameAndAppId(checksData,input)).toStrictEqual(expected);
expect(filterChecksWithMatchingNameAndAppId(checksData, input)).toStrictEqual(expected);
});

it("should return missing checks if checks with matching name and app id are not found", () => {
let input:ICheckInput[] = [{app_id:1,name:"check1"},{app_id:2,name:"check3"}];
let expected = {filteredChecks:[checksData[1]],missingChecks:[{app_id:2,name:"check3"}]};
let input: ICheckInput[] = [{app_id: 1, name: "check1"}, {app_id: 2, name: "check3"}];
let expected = {filteredChecks: [checksData[1]], missingChecks: [{app_id: 2, name: "check3"}]};
});
});

describe("returnChecksWithMatchingNameAndAppId", () => {
it("should return checks with matching name and app id", () => {
let input:ICheckInput = {app_id:1,name:"check1"};
let expected:ICheck[] = [checksData[1]];
let input: ICheckInput = {app_id: 1, name: "check1"};
let expected: ICheck[] = [checksData[1]];

expect(returnChecksWithMatchingNameAndAppId(checksData,input.name,input.app_id)).toStrictEqual(expected);
expect(returnChecksWithMatchingNameAndAppId(checksData, input.name, input.app_id)).toStrictEqual(expected);
});

it("returns unique checks if checks from each app if there are multiple checks with the same name", () => {
let input:ICheckInput = {app_id:-1,name:"check2"};
let expected:ICheck[] = [checksData[2],checksData[4]];
let input: ICheckInput = {app_id: -1, name: "check2"};
let expected: ICheck[] = [checksData[2], checksData[4]];

expect(returnChecksWithMatchingNameAndAppId(checksData,input.name,input.app_id)).toStrictEqual(expected);
expect(returnChecksWithMatchingNameAndAppId(checksData, input.name, input.app_id)).toStrictEqual(expected);
});

it("should correctly handle regular expressions", () => {
let input:ICheckInput = {app_id:-1,name:"check."};
let expected:ICheck[] = [checksData[1],checksData[2],checksData[3],checksData[4]];
let input: ICheckInput = {app_id: -1, name: "check."};
let expected: ICheck[] = [checksData[1], checksData[2], checksData[3], checksData[4]];

expect(returnChecksWithMatchingNameAndAppId(checksData,input.name,input.app_id)).toStrictEqual(expected);
expect(returnChecksWithMatchingNameAndAppId(checksData, input.name, input.app_id)).toStrictEqual(expected);
});

it("should return null if no checks match the input", () => {
let input:ICheckInput = {app_id:1000,name:"check1"};
let input: ICheckInput = {app_id: 1000, name: "check1"};
let expected = null;
expect(returnChecksWithMatchingNameAndAppId(checksData,input.name,input.app_id)).toStrictEqual(expected);
expect(returnChecksWithMatchingNameAndAppId(checksData, input.name, input.app_id)).toStrictEqual(expected);
});
});


describe("checkOneOfTheChecksInputIsEmpty", () => {
it("should pass if one of the input checks is empty", () => {
let input1:ICheckInput[] = [{app_id:1,name:"check1"},{app_id:2,name:"check2"}];
let input2:ICheckInput[] = [];
it("should pass if one of the input checks is empty", () => {
let input1: ICheckInput[] = [{app_id: 1, name: "check1"}, {app_id: 2, name: "check2"}];
let input2: ICheckInput[] = [];

expect(checkOneOfTheChecksInputIsEmpty(input1,input2)).toBe(true)
expect(checkOneOfTheChecksInputIsEmpty(input1, input2)).toBe(true)

});
it("should throw an error if both of the input checks are not empty", () => {
let input1:ICheckInput[] = [{app_id:1,name:"check1"},{app_id:2,name:"check2"}];
let input2:ICheckInput[] = [{app_id:3,name:"check3"},{app_id:4,name:"check4"}];
});
it("should throw an error if both of the input checks are not empty", () => {
let input1: ICheckInput[] = [{app_id: 1, name: "check1"}, {app_id: 2, name: "check2"}];
let input2: ICheckInput[] = [{app_id: 3, name: "check3"}, {app_id: 4, name: "check4"}];

expect(checkOneOfTheChecksInputIsEmpty(input1,input2)).toBe(false);
expect(checkOneOfTheChecksInputIsEmpty(input1, input2)).toBe(false);

});
});

});

describe("removeDuplicateEntries", () => {
it("it should return check inputs as is if no duplicates exist", () => {
let input:ICheckInput[] = [{app_id:1,name:"check1"},{app_id:2,name:"check2"}];
let input: ICheckInput[] = [{app_id: 1, name: "check1"}, {app_id: 2, name: "check2"}];


expect(removeDuplicateEntriesChecksInputsFromSelf(input)).toStrictEqual(input);
expect(removeDuplicateEntriesChecksInputsFromSelf(input)).toStrictEqual(input);

}
}
);

it("it should remove duplicate entries", () => {
Expand All @@ -172,24 +180,24 @@ describe("removeDuplicateEntries", () => {

describe("removeChecksWithMatchingNameAndAppId", () => {
it("should remove checks with matching name and app id", () => {
let input:ICheckInput[] = [{app_id:1,name:"check1"},{app_id:2,name:"check2"}];
let expectedOutput = [checksData[3],checksData[4]];
let input: ICheckInput[] = [{app_id: 1, name: "check1"}, {app_id: 2, name: "check2"}];
let expectedOutput = [checksData[3], checksData[4]];

expect(removeChecksWithMatchingNameAndAppId(checksData,input)).toStrictEqual(expectedOutput);
expect(removeChecksWithMatchingNameAndAppId(checksData, input)).toStrictEqual(expectedOutput);
});

it("should remove all checks if all checks match the input", () => {
let input:ICheckInput[] = [{app_id:-1,name:"check."}];
let expectedOutput:ICheck[] = [];
let input: ICheckInput[] = [{app_id: -1, name: "check."}];
let expectedOutput: ICheck[] = [];

expect(removeChecksWithMatchingNameAndAppId(checksData,input)).toStrictEqual(expectedOutput);
expect(removeChecksWithMatchingNameAndAppId(checksData, input)).toStrictEqual(expectedOutput);
})

it("should return checks as is if no checks match the input", () => {
let input:ICheckInput[] = [{app_id:1000,name:"check1"}];
let expectedOutput:ICheck[] = checksData;
let input: ICheckInput[] = [{app_id: 1000, name: "check1"}];
let expectedOutput: ICheck[] = checksData;

expect(removeChecksWithMatchingNameAndAppId(checksData,input)).toStrictEqual(expectedOutput);
expect(removeChecksWithMatchingNameAndAppId(checksData, input)).toStrictEqual(expectedOutput);
})
});

Expand Down
51 changes: 28 additions & 23 deletions src/checks/checks.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

import * as core from '@actions/core';
import { IInputs} from '../utils/inputsExtractor';
import {getAllChecks,getAllStatusCommits} from './checksAPI';
import { ICheckInput,ICheck,IStatus } from './checksInterfaces';
import {IInputs} from '../utils/inputsExtractor';
import {getAllChecks, getAllStatusCommits} from './checksAPI';
import {ICheckInput, ICheck, IStatus} from './checksInterfaces';
import {
checkOneOfTheChecksInputIsEmpty,
filterChecksWithMatchingNameAndAppId,
Expand All @@ -11,13 +10,15 @@ import {
removeDuplicateEntriesChecksInputsFromSelf
} from './checksFilters';
import {sleep} from "../utils/timeFuncs";
import {extractOwnCheckNameFromWorkflow} from "../utils/fileExtractor";
import {GitHubActionsBotId} from "./checksConstants";

interface IRepo{
interface IRepo {
owner: string;
repo: string;
}

export default class Checks{
export default class Checks {
// data
public allChecks: ICheck[] = [];
private allStatuses: IStatus[] = [];
Expand All @@ -26,6 +27,7 @@ export default class Checks{
private allChecksPassed: boolean = false;
private allStatusesPassed: boolean = false;
private missingChecks: ICheckInput[] = [];
private ownCheck: ICheck | undefined; //the check from the workflow run itself

// inputs
private owner: string;
Expand All @@ -44,7 +46,7 @@ export default class Checks{
private failFast: boolean;


constructor(props: IRepo & IInputs){
constructor(props: IRepo & IInputs) {
this.owner = props.owner;
this.repo = props.repo;
this.ref = props.commitSHA;
Expand All @@ -62,38 +64,38 @@ export default class Checks{

}

async fetchAllChecks(){
async fetchAllChecks() {
try {
this.allChecks = await getAllChecks(this.owner, this.repo, this.ref) as ICheck[];
} catch (error: any) {
throw new Error("Error getting all checks: " + error.message);
}
}

async fetchAllStatusCommits(){
async fetchAllStatusCommits() {
try {
this.allStatuses = await getAllStatusCommits(this.owner, this.repo, this.ref) as IStatus[];
} catch (error: any) {
throw new Error("Error getting all statuses: " + error.message);
}
}

async filterChecks(){
async filterChecks() {
// start by checking if the user has defined both checks_include and checks_exclude inputs and fail if that is the case
let ambigousChecks = checkOneOfTheChecksInputIsEmpty(this.checksInclude,this.checksExclude);
if(!ambigousChecks){
let ambigousChecks = checkOneOfTheChecksInputIsEmpty(this.checksInclude, this.checksExclude);
if (!ambigousChecks) {
throw new Error("You cannot define both checks_include and checks_exclude inputs, please use only one of them");
}
// if neither checks_include nor checks_exclude are defined, then we will use all checks

if(this.checksInclude.length === 0 && this.checksExclude.length === 0){
if (this.checksInclude.length === 0 && this.checksExclude.length === 0) {
this.filteredChecks = [...this.allChecks];
return;
}

// if only checks_include is defined, then we will use only the checks that are included
if(this.checksInclude.length > 0 && this.checksExclude.length === 0){
let firstPassthrough = filterChecksWithMatchingNameAndAppId(this.allChecks,this.checksInclude);
if (this.checksInclude.length > 0 && this.checksExclude.length === 0) {
let firstPassthrough = filterChecksWithMatchingNameAndAppId(this.allChecks, this.checksInclude);
// lets separate the object

let filteredChecks = firstPassthrough["filteredChecks"];
Expand All @@ -104,28 +106,31 @@ export default class Checks{
return;
}

if(this.checksExclude.length > 0 && this.checksInclude.length === 0){
let firstPassthrough =removeChecksWithMatchingNameAndAppId(this.allChecks,this.checksExclude);
this.filteredChecks = removeDuplicateChecksEntriesFromSelf(firstPassthrough);
return;
if (this.checksExclude.length > 0 && this.checksInclude.length === 0) {
let firstPassthrough = removeChecksWithMatchingNameAndAppId(this.allChecks, this.checksExclude);
this.filteredChecks = removeDuplicateChecksEntriesFromSelf(firstPassthrough);
return;
}

let ownCheckName = await extractOwnCheckNameFromWorkflow();
let gitHubActionsBotId = GitHubActionsBotId;

this.ownCheck = this.filteredChecks.find(check => check.name === ownCheckName && check.app.id === gitHubActionsBotId);
this.filteredChecks = this.filteredChecks.filter(check => check.name !== ownCheckName && check.app.id !== gitHubActionsBotId);
};

reportChecks(){
reportChecks() {
// create table showing the filtered checks, with names and conclusion, created at, updated at, and app id and status
core.info("Filtered checks:");
core.info("Name | Conclusion | Created at | Updated at | | Status");

};

async runLogic(){
async runLogic() {
sleep(this.delay);
await this.fetchAllChecks();
await this.fetchAllStatusCommits();
await this.filterChecks();
}



}
Loading

0 comments on commit 2d45121

Please sign in to comment.