mirror of
https://github.com/bendtherules/github-delete-unused-repos.git
synced 2026-08-18 13:52:59 +00:00
Rename + Actually push redux filter states
This commit is contained in:
@@ -2,7 +2,26 @@ import * as Octokit from '@octokit/rest';
|
||||
import assert = require('assert');
|
||||
import Bottleneck from 'bottleneck';
|
||||
|
||||
import { OctokitMod, ResponseWithDataArray, RepoFromGetUserRepo, ResponseFromGetUserRepo, RepoNameWithBranchesAndParent, RepoNameWithUnusedFlag, ObjectWithPerPage, ResponseWithDataArrayAndMeta, RepoNameWithBranches, BranchFromGetBranches, ResponseFromGetBranches, RepoNameWithParentRepo, ResponseFromGetRepo, ResponseFromCompareCommits, OwnerFromGetContributors, ResponseFromGetContributors } from './types';
|
||||
import {
|
||||
OctokitMod,
|
||||
ResponseWithDataArray,
|
||||
RepoFromGetUserRepo,
|
||||
ResponseFromGetUserRepo,
|
||||
RepoNameWithBranchesAndParent,
|
||||
RepoNameWithUnusedFlag,
|
||||
ObjectWithPerPage,
|
||||
ResponseWithDataArrayAndMeta,
|
||||
RepoNameWithBranches,
|
||||
BranchFromGetBranches,
|
||||
ResponseFromGetBranches,
|
||||
RepoNameWithParentRepo,
|
||||
ResponseFromGetRepo,
|
||||
ResponseFromCompareCommits,
|
||||
OwnerFromGetContributors,
|
||||
ResponseFromGetContributors,
|
||||
} from './types';
|
||||
import { createAppStore, ActionsType, ApplicationState, ActionNames, FilterSteps, FilterStatus } from './store';
|
||||
import { Store } from 'redux';
|
||||
|
||||
// +++ General init +++
|
||||
const octokit = new Octokit() as OctokitMod;
|
||||
@@ -24,12 +43,21 @@ octokit.authenticate({
|
||||
});
|
||||
// --- End General init ---
|
||||
|
||||
|
||||
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({
|
||||
type: ActionNames.InitUserName,
|
||||
payload: {
|
||||
userName: username
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async fetchUnusedForkedRepos() {
|
||||
@@ -37,18 +65,29 @@ class GithubDetectUnusedRepos {
|
||||
username: this.username,
|
||||
};
|
||||
|
||||
const repos: ResponseWithDataArray<RepoFromGetUserRepo> =
|
||||
await this.paginate(
|
||||
const repos: ResponseWithDataArray<
|
||||
RepoFromGetUserRepo
|
||||
> = await this.paginate(
|
||||
(
|
||||
tmpFirstParam: Octokit.ReposGetForUserParams
|
||||
): Promise<ResponseFromGetUserRepo> => {
|
||||
return (
|
||||
octokit.repos.getForUser(tmpFirstParam) as any
|
||||
) as Promise<ResponseFromGetUserRepo>;
|
||||
return (octokit.repos.getForUser(tmpFirstParam) as any) as Promise<
|
||||
ResponseFromGetUserRepo
|
||||
>;
|
||||
},
|
||||
params
|
||||
);
|
||||
|
||||
// Update all repo names in redux store
|
||||
repos.data.forEach(repoData => {
|
||||
this.reduxStore.dispatch({
|
||||
type: ActionNames.InitRepo,
|
||||
payload: {
|
||||
repoName: repoData.name
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
const forkedRepoNames = repos.data
|
||||
.filter(repo => repo.fork)
|
||||
.map(repo => repo.name);
|
||||
@@ -56,6 +95,35 @@ class GithubDetectUnusedRepos {
|
||||
// tslint:disable-next-line:no-console
|
||||
console.log(forkedRepoNames);
|
||||
|
||||
// Update fork filter in redux store
|
||||
repos.data.forEach(repoData => {
|
||||
if (forkedRepoNames.includes(repoData.name)) {
|
||||
|
||||
this.reduxStore.dispatch({
|
||||
type: ActionNames.AddRepoFilterStatus,
|
||||
payload: {
|
||||
repoName: repoData.name,
|
||||
filterStep: FilterSteps.forked,
|
||||
filterState: {
|
||||
status: FilterStatus.pass
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
} else {
|
||||
this.reduxStore.dispatch({
|
||||
type: ActionNames.AddRepoFilterStatus,
|
||||
payload: {
|
||||
repoName: repoData.name,
|
||||
filterStep: FilterSteps.forked,
|
||||
filterState: {
|
||||
status: FilterStatus.fail
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const allPromiseRepoNameWithBranches = forkedRepoNames.map(repoName => {
|
||||
return this.fetchRepoNameWithBranches(repoName);
|
||||
});
|
||||
@@ -134,7 +202,7 @@ class GithubDetectUnusedRepos {
|
||||
}
|
||||
);
|
||||
|
||||
const allRepoWithFlagFromCommit: RepoNameWithUnusedFlag[] = await Promise.all(
|
||||
const allRepoWithFlagFromCommitAhead: RepoNameWithUnusedFlag[] = await Promise.all(
|
||||
allPromiseRepoWithFlagFromCommit
|
||||
);
|
||||
|
||||
@@ -146,23 +214,71 @@ class GithubDetectUnusedRepos {
|
||||
allPromiseRepoWithFlagFromParentContrib
|
||||
);
|
||||
|
||||
{
|
||||
// Update redux state for these 3 filters
|
||||
allRepoWithFlagFromCommitAhead.forEach(tmp => {
|
||||
this.reduxStore.dispatch(
|
||||
{
|
||||
type: ActionNames.AddRepoFilterStatus,
|
||||
payload:{
|
||||
repoName: tmp.repoName,
|
||||
filterStep: FilterSteps.eachBranchBehindOrEven,
|
||||
filterState:{
|
||||
status: tmp.unused ? FilterStatus.pass : FilterStatus.fail
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
});
|
||||
|
||||
allRepoWithFlagFromForkContrib.forEach(tmp => {
|
||||
this.reduxStore.dispatch(
|
||||
{
|
||||
type: ActionNames.AddRepoFilterStatus,
|
||||
payload:{
|
||||
repoName: tmp.repoName,
|
||||
filterStep: FilterSteps.notForkContributor,
|
||||
filterState:{
|
||||
status: tmp.unused ? FilterStatus.pass : FilterStatus.fail
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
});
|
||||
|
||||
allRepoWithFlagFromParentContrib.forEach(tmp => {
|
||||
this.reduxStore.dispatch(
|
||||
{
|
||||
type: ActionNames.AddRepoFilterStatus,
|
||||
payload:{
|
||||
repoName: tmp.repoName,
|
||||
filterStep: FilterSteps.notParentContributor,
|
||||
filterState:{
|
||||
status: tmp.unused ? FilterStatus.pass : FilterStatus.fail
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
assert.strictEqual(
|
||||
allRepoWithFlagFromCommit.length,
|
||||
allRepoWithFlagFromCommitAhead.length,
|
||||
allRepoWithFlagFromForkContrib.length,
|
||||
'Length of `allRepoWithFlagFromCommit` and `allRepoWithFlagFromContrib` should be same'
|
||||
);
|
||||
|
||||
const repoCount = allRepoWithFlagFromCommit.length;
|
||||
const repoCount = allRepoWithFlagFromCommitAhead.length;
|
||||
|
||||
for (let index = 0; index < repoCount; index++) {
|
||||
const tmpObjFromCommit = allRepoWithFlagFromCommit[index];
|
||||
const tmpObjFromCommitAhead = allRepoWithFlagFromCommitAhead[index];
|
||||
const tmpObjFromForkContrib = allRepoWithFlagFromForkContrib[index];
|
||||
const tmpObjFromParentContrib =
|
||||
allRepoWithFlagFromParentContrib[index];
|
||||
|
||||
assert.strictEqual(
|
||||
tmpObjFromCommit.repoName,
|
||||
tmpObjFromCommitAhead.repoName,
|
||||
tmpObjFromForkContrib.repoName,
|
||||
'Reponame from same index of `allRepoWithFlagFromCommit` and `allRepoWithFlagFromContrib` should be same'
|
||||
);
|
||||
@@ -173,12 +289,12 @@ class GithubDetectUnusedRepos {
|
||||
'Reponame from same index of `tmpObjFromForkContrib` and `tmpObjFromParentContrib` should be same'
|
||||
);
|
||||
|
||||
const tmpRepoName = tmpObjFromCommit.repoName;
|
||||
const tmpRepoName = tmpObjFromCommitAhead.repoName;
|
||||
|
||||
allRepoWithFlagTillStep4.push({
|
||||
repoName: tmpRepoName,
|
||||
unused:
|
||||
tmpObjFromCommit.unused &&
|
||||
tmpObjFromCommitAhead.unused &&
|
||||
tmpObjFromForkContrib.unused &&
|
||||
tmpObjFromParentContrib.unused,
|
||||
});
|
||||
|
||||
+15
-6
@@ -18,8 +18,10 @@ export namespace FailReasons {
|
||||
}
|
||||
|
||||
export enum FilterSteps {
|
||||
forked = 'forked',
|
||||
eachBranchBehindOrEven = 'eachBranchBehindOrEven',
|
||||
notContributor = 'notContributor',
|
||||
notForkContributor = 'notForkContributor',
|
||||
notParentContributor = 'notParentContributor',
|
||||
noCommits = 'noCommits',
|
||||
}
|
||||
|
||||
@@ -30,18 +32,25 @@ export enum FilterStatus {
|
||||
}
|
||||
|
||||
export interface RepoFilterState {
|
||||
[FilterSteps.forked]: {
|
||||
status: FilterStatus;
|
||||
};
|
||||
[FilterSteps.eachBranchBehindOrEven]: {
|
||||
status: FilterStatus;
|
||||
failReason?: FailReasons.EachBranchBehindOrEven;
|
||||
failBranchName?: string;
|
||||
// failReason?: FailReasons.EachBranchBehindOrEven;
|
||||
// failBranchName?: string;
|
||||
};
|
||||
[FilterSteps.notContributor]: {
|
||||
[FilterSteps.notForkContributor]: {
|
||||
status: FilterStatus;
|
||||
failReason?: FailReasons.NotContributor;
|
||||
// failReason?: FailReasons.NotContributor;
|
||||
};
|
||||
[FilterSteps.notParentContributor]: {
|
||||
status: FilterStatus;
|
||||
// failReason?: FailReasons.NotContributor;
|
||||
};
|
||||
[FilterSteps.noCommits]: {
|
||||
status: FilterStatus;
|
||||
failReason?: FailReasons.NoCommits;
|
||||
// failReason?: FailReasons.NoCommits;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -19,10 +19,16 @@ export const reducer: Reducer<ApplicationState, ActionsType> = (state: Applicati
|
||||
const clonedAppState = new Map(state.repoStateMap);
|
||||
clonedAppState.set(action.payload.repoName, {
|
||||
filterState: {
|
||||
forked: {
|
||||
status: FilterStatus.waiting
|
||||
},
|
||||
eachBranchBehindOrEven: {
|
||||
status: FilterStatus.waiting
|
||||
},
|
||||
notContributor: {
|
||||
notForkContributor: {
|
||||
status: FilterStatus.waiting
|
||||
},
|
||||
notParentContributor: {
|
||||
status: FilterStatus.waiting
|
||||
},
|
||||
noCommits: {
|
||||
|
||||
Reference in New Issue
Block a user