mirror of
https://github.com/bendtherules/github-delete-unused-repos.git
synced 2026-08-18 13:52:59 +00:00
Move core logic to core/ inner folder and import changes
This commit is contained in:
+60
-48
@@ -20,7 +20,14 @@ import {
|
||||
OwnerFromGetContributors,
|
||||
ResponseFromGetContributors,
|
||||
} from './types';
|
||||
import { createAppStore, ActionsType, ApplicationState, ActionNames, FilterSteps, FilterStatus } from './store';
|
||||
import {
|
||||
createAppStore,
|
||||
ActionsType,
|
||||
ApplicationState,
|
||||
ActionNames,
|
||||
FilterSteps,
|
||||
FilterStatus,
|
||||
} from './store';
|
||||
import { Store } from 'redux';
|
||||
|
||||
// +++ General init +++
|
||||
@@ -43,20 +50,20 @@ octokit.authenticate({
|
||||
});
|
||||
// --- End General init ---
|
||||
|
||||
const defaultStore: Store<ApplicationState, ActionsType> = createAppStore();
|
||||
|
||||
class GithubDetectUnusedRepos {
|
||||
private username: string;
|
||||
reduxStore: Store<ApplicationState, ActionsType>;
|
||||
|
||||
constructor(username: string) {
|
||||
this.username = username;
|
||||
this.reduxStore = createAppStore();
|
||||
|
||||
// Update username in redux store
|
||||
this.reduxStore.dispatch({
|
||||
defaultStore.dispatch({
|
||||
type: ActionNames.InitUserName,
|
||||
payload: {
|
||||
userName: username
|
||||
}
|
||||
userName: username,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -80,12 +87,12 @@ class GithubDetectUnusedRepos {
|
||||
|
||||
// Update all repo names in redux store
|
||||
repos.data.forEach(repoData => {
|
||||
this.reduxStore.dispatch({
|
||||
defaultStore.dispatch({
|
||||
type: ActionNames.InitRepo,
|
||||
payload: {
|
||||
repoName: repoData.name
|
||||
}
|
||||
})
|
||||
repoName: repoData.name,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
const forkedRepoNames = repos.data
|
||||
@@ -98,28 +105,26 @@ class GithubDetectUnusedRepos {
|
||||
// Update fork filter in redux store
|
||||
repos.data.forEach(repoData => {
|
||||
if (forkedRepoNames.includes(repoData.name)) {
|
||||
|
||||
this.reduxStore.dispatch({
|
||||
defaultStore.dispatch({
|
||||
type: ActionNames.AddRepoFilterStatus,
|
||||
payload: {
|
||||
repoName: repoData.name,
|
||||
filterStep: FilterSteps.forked,
|
||||
filterState: {
|
||||
status: FilterStatus.pass
|
||||
}
|
||||
}
|
||||
status: FilterStatus.pass,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
} else {
|
||||
this.reduxStore.dispatch({
|
||||
defaultStore.dispatch({
|
||||
type: ActionNames.AddRepoFilterStatus,
|
||||
payload: {
|
||||
repoName: repoData.name,
|
||||
filterStep: FilterSteps.forked,
|
||||
filterState: {
|
||||
status: FilterStatus.fail
|
||||
}
|
||||
}
|
||||
status: FilterStatus.fail,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -217,48 +222,42 @@ class GithubDetectUnusedRepos {
|
||||
{
|
||||
// Update redux state for these 3 filters
|
||||
allRepoWithFlagFromCommitAhead.forEach(tmp => {
|
||||
this.reduxStore.dispatch(
|
||||
{
|
||||
defaultStore.dispatch({
|
||||
type: ActionNames.AddRepoFilterStatus,
|
||||
payload: {
|
||||
repoName: tmp.repoName,
|
||||
filterStep: FilterSteps.eachBranchBehindOrEven,
|
||||
filterState: {
|
||||
status: tmp.unused ? FilterStatus.pass : FilterStatus.fail
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
status: tmp.unused ? FilterStatus.pass : FilterStatus.fail,
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
allRepoWithFlagFromForkContrib.forEach(tmp => {
|
||||
this.reduxStore.dispatch(
|
||||
{
|
||||
defaultStore.dispatch({
|
||||
type: ActionNames.AddRepoFilterStatus,
|
||||
payload: {
|
||||
repoName: tmp.repoName,
|
||||
filterStep: FilterSteps.notForkContributor,
|
||||
filterState: {
|
||||
status: tmp.unused ? FilterStatus.pass : FilterStatus.fail
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
status: tmp.unused ? FilterStatus.pass : FilterStatus.fail,
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
allRepoWithFlagFromParentContrib.forEach(tmp => {
|
||||
this.reduxStore.dispatch(
|
||||
{
|
||||
defaultStore.dispatch({
|
||||
type: ActionNames.AddRepoFilterStatus,
|
||||
payload: {
|
||||
repoName: tmp.repoName,
|
||||
filterStep: FilterSteps.notParentContributor,
|
||||
filterState: {
|
||||
status: tmp.unused ? FilterStatus.pass : FilterStatus.fail
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
status: tmp.unused ? FilterStatus.pass : FilterStatus.fail,
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -298,6 +297,21 @@ class GithubDetectUnusedRepos {
|
||||
tmpObjFromForkContrib.unused &&
|
||||
tmpObjFromParentContrib.unused,
|
||||
});
|
||||
|
||||
allRepoWithFlagTillStep4.forEach(tmp => {
|
||||
defaultStore.dispatch(
|
||||
{
|
||||
type: ActionNames.AddRepoFilterStatus,
|
||||
payload: {
|
||||
repoName: tmp.repoName,
|
||||
filterStep: FilterSteps.noCommits,
|
||||
filterState: {
|
||||
status: tmp.unused ? FilterStatus.pass : FilterStatus.fail
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -502,20 +516,18 @@ class GithubDetectUnusedRepos {
|
||||
}
|
||||
}
|
||||
|
||||
export function runMain(username: string = 'bendtherules') {
|
||||
function runMain(username: string = 'bendtherules') {
|
||||
const instance = new GithubDetectUnusedRepos(username);
|
||||
|
||||
instance
|
||||
.fetchUnusedForkedRepos()
|
||||
.then(unusedRepoNames => {
|
||||
instance.fetchUnusedForkedRepos().then(unusedRepoNames => {
|
||||
// tslint:disable-next-line:no-console
|
||||
console.log(unusedRepoNames);
|
||||
console.log(instance.reduxStore.getState());
|
||||
console.log(defaultStore.getState());
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
export { defaultStore, runMain };
|
||||
|
||||
// Next steps
|
||||
//
|
||||
// 1. Get all forked repos for a user
|
||||
@@ -29,6 +29,6 @@ export interface AddRepoFilterStatusArgs
|
||||
}
|
||||
|
||||
export type ActionsType =
|
||||
InitUserNameArgs
|
||||
| InitUserNameArgs
|
||||
| InitRepoArgs
|
||||
| AddRepoFilterStatusArgs;
|
||||
@@ -59,7 +59,7 @@ export type RepoFilterStateMap = Map<
|
||||
{
|
||||
filterState: RepoFilterState;
|
||||
}
|
||||
>;
|
||||
>;
|
||||
|
||||
export interface ApplicationState {
|
||||
username: string;
|
||||
@@ -0,0 +1,79 @@
|
||||
import { Reducer, createStore, Store } from 'redux';
|
||||
import { ApplicationState, FilterStatus, RepoFilterState } from './model';
|
||||
import {
|
||||
ActionNames,
|
||||
InitUserNameArgs,
|
||||
InitRepoArgs,
|
||||
AddRepoFilterStatusArgs,
|
||||
ActionsType,
|
||||
} from './actions';
|
||||
|
||||
export const initialState: ApplicationState = {
|
||||
username: '',
|
||||
repoStateMap: new Map(),
|
||||
};
|
||||
|
||||
export const reducer: Reducer<ApplicationState, ActionsType> = (
|
||||
state: ApplicationState = initialState,
|
||||
action
|
||||
) => {
|
||||
// We'll augment the action type on the switch case to make sure we have
|
||||
// all the cases handled.
|
||||
switch (action.type) {
|
||||
case ActionNames.InitUserName:
|
||||
return { ...state, username: action.payload.userName };
|
||||
|
||||
case ActionNames.InitRepo: {
|
||||
const clonedAppState = new Map(state.repoStateMap);
|
||||
clonedAppState.set(action.payload.repoName, {
|
||||
filterState: {
|
||||
forked: {
|
||||
status: FilterStatus.waiting,
|
||||
},
|
||||
eachBranchBehindOrEven: {
|
||||
status: FilterStatus.waiting,
|
||||
},
|
||||
notForkContributor: {
|
||||
status: FilterStatus.waiting,
|
||||
},
|
||||
notParentContributor: {
|
||||
status: FilterStatus.waiting,
|
||||
},
|
||||
noCommits: {
|
||||
status: FilterStatus.waiting,
|
||||
},
|
||||
},
|
||||
});
|
||||
return { ...state, repoStateMap: clonedAppState };
|
||||
}
|
||||
case ActionNames.AddRepoFilterStatus: {
|
||||
const clonedAppState = new Map(state.repoStateMap);
|
||||
let clonedRepoState = JSON.parse(
|
||||
JSON.stringify(clonedAppState.get(action.payload.repoName))
|
||||
) as
|
||||
| {
|
||||
filterState: RepoFilterState;
|
||||
}
|
||||
| undefined;
|
||||
|
||||
if (clonedRepoState !== undefined) {
|
||||
clonedRepoState.filterState[action.payload.filterStep] =
|
||||
action.payload.filterState;
|
||||
|
||||
clonedAppState.set(action.payload.repoName, clonedRepoState);
|
||||
return { ...state, repoStateMap: clonedAppState };
|
||||
} else {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
|
||||
export function createAppStore(): Store<ApplicationState, ActionsType> {
|
||||
return (createStore<ApplicationState, ActionsType, {}, {}>(
|
||||
reducer,
|
||||
initialState
|
||||
) as any) as Store<ApplicationState, ActionsType>;
|
||||
}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
import 'babel-polyfill';
|
||||
// tslint:disable-next-line:ordered-imports
|
||||
import { runMain } from './gh';
|
||||
import { runMain } from './core/gh';
|
||||
|
||||
// tslint:disable-next-line:only-arrow-functions
|
||||
(function(currentWindow: Window) {
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
import { Reducer, createStore, Store } from 'redux';
|
||||
import { ApplicationState, FilterStatus, RepoFilterState } from './model';
|
||||
import { ActionNames, InitUserNameArgs, InitRepoArgs, AddRepoFilterStatusArgs, ActionsType } from './actions';
|
||||
|
||||
export const initialState: ApplicationState = {
|
||||
username: '',
|
||||
repoStateMap: new Map()
|
||||
};
|
||||
|
||||
export const reducer: Reducer<ApplicationState, ActionsType> = (state: ApplicationState = initialState, action) => {
|
||||
// We'll augment the action type on the switch case to make sure we have
|
||||
// all the cases handled.
|
||||
switch (action.type) {
|
||||
case ActionNames.InitUserName:
|
||||
return { ...state, username: action.payload.userName };
|
||||
|
||||
case ActionNames.InitRepo:
|
||||
{
|
||||
const clonedAppState = new Map(state.repoStateMap);
|
||||
clonedAppState.set(action.payload.repoName, {
|
||||
filterState: {
|
||||
forked: {
|
||||
status: FilterStatus.waiting
|
||||
},
|
||||
eachBranchBehindOrEven: {
|
||||
status: FilterStatus.waiting
|
||||
},
|
||||
notForkContributor: {
|
||||
status: FilterStatus.waiting
|
||||
},
|
||||
notParentContributor: {
|
||||
status: FilterStatus.waiting
|
||||
},
|
||||
noCommits: {
|
||||
status: FilterStatus.waiting
|
||||
}
|
||||
}
|
||||
});
|
||||
return { ...state, repoStateMap: clonedAppState };
|
||||
}
|
||||
case ActionNames.AddRepoFilterStatus:
|
||||
{
|
||||
const clonedAppState = new Map(state.repoStateMap);
|
||||
let clonedRepoState = JSON.parse(JSON.stringify(clonedAppState.get(action.payload.repoName))) as (
|
||||
{
|
||||
filterState: RepoFilterState;
|
||||
} | undefined
|
||||
);
|
||||
|
||||
if (clonedRepoState !== undefined) {
|
||||
clonedRepoState.filterState[action.payload.filterStep] = action.payload.filterState;
|
||||
|
||||
clonedAppState.set(action.payload.repoName, clonedRepoState);
|
||||
return { ...state, repoStateMap: clonedAppState };
|
||||
} else {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
|
||||
export function createAppStore(): Store<ApplicationState, ActionsType> {
|
||||
return createStore<
|
||||
ApplicationState, ActionsType, {}, {}
|
||||
>(
|
||||
reducer,
|
||||
initialState
|
||||
) as any as Store<ApplicationState, ActionsType>;
|
||||
};
|
||||
Reference in New Issue
Block a user