gengitkan/client/src/store/sagas/issues.ts

95 lines
2.7 KiB
TypeScript
Raw Normal View History

2019-12-01 22:12:13 +01:00
import { put, call, retry } from 'redux-saga/effects';
2020-04-30 13:02:56 +02:00
import {
FETCH_ISSUES_SUCCESS, FETCH_ISSUES_FAILURE,
ADD_LABEL_FAILURE, ADD_LABEL_SUCCESS,
REMOVE_LABEL_FAILURE, REMOVE_LABEL_SUCCESS,
CREATE_ISSUE_FAILURE, CREATE_ISSUE_SUCCESS
} from '../actions/issues';
2019-12-01 22:12:13 +01:00
import { gitea } from '../../util/gitea';
2020-04-30 13:02:56 +02:00
export function* fetchIssuesSaga(action: any) {
2019-12-01 22:12:13 +01:00
const { project } = action;
2019-12-05 14:44:33 +01:00
let issues = [];
2019-12-01 22:12:13 +01:00
try {
2019-12-05 14:44:33 +01:00
let page = 1;
while(true) {
let pageIssues = yield call(gitea.fetchIssues.bind(gitea), action.project, page);
if (pageIssues.length === 0) {
break;
}
issues.push(...pageIssues);
page++;
}
2019-12-01 22:12:13 +01:00
} catch(error) {
yield put({ type: FETCH_ISSUES_FAILURE, project, error });
return;
}
yield put({ type: FETCH_ISSUES_SUCCESS, project, issues });
}
2020-04-30 13:02:56 +02:00
export function* addLabelSaga(action: any) {
2019-12-01 22:12:13 +01:00
const { project, issueNumber, label } = action;
const labels = yield call(gitea.fetchProjectLabels.bind(gitea), project);
2020-04-30 13:02:56 +02:00
const giteaLabel = labels.find((l: any) => l.name === label)
2019-12-01 22:12:13 +01:00
if (!giteaLabel) {
yield put({ type: ADD_LABEL_FAILURE, error: new Error(`Label "${label}" not found !`) });
return;
}
try {
yield retry(5, 250, gitea.addIssueLabel.bind(gitea), project, issueNumber, giteaLabel.id);
} catch(error) {
yield put({ type: ADD_LABEL_FAILURE, error });
return;
}
yield put({ type: ADD_LABEL_SUCCESS, project, issueNumber, label });
}
2020-04-30 13:02:56 +02:00
export function* removeLabelSaga(action: any) {
2019-12-01 22:12:13 +01:00
const { project, issueNumber, label } = action;
const labels = yield call(gitea.fetchProjectLabels.bind(gitea), project);
2020-04-30 13:02:56 +02:00
const giteaLabel = labels.find((l: any) => l.name === label)
2019-12-01 22:12:13 +01:00
if (!giteaLabel) {
yield put({ type: REMOVE_LABEL_FAILURE, error: new Error(`Label "${label}" not found !`) });
return;
}
try {
yield retry(5, 250, gitea.removeIssueLabel.bind(gitea), project, issueNumber, giteaLabel.id);
} catch(error) {
yield put({ type: REMOVE_LABEL_FAILURE, error });
return;
}
yield put({ type: REMOVE_LABEL_SUCCESS, project, issueNumber, label });
2019-12-05 22:37:09 +01:00
}
2020-04-30 13:02:56 +02:00
export function* createIssueSaga(action: any) {
2019-12-05 22:37:09 +01:00
const { project, title, label, body } = action;
const labels = yield call(gitea.fetchProjectLabels.bind(gitea), project);
2020-04-30 13:02:56 +02:00
const giteaLabel = labels.find((l: any) => l.name === label)
2019-12-05 22:37:09 +01:00
if (!giteaLabel) {
yield put({ type: CREATE_ISSUE_FAILURE, error: new Error(`Label "${label}" not found !`) });
return;
}
let issue;
try {
issue = yield call(gitea.createIssue.bind(gitea), project, title, body, giteaLabel.id);
} catch(error) {
yield put({ type: CREATE_ISSUE_FAILURE, error });
return;
}
2019-12-01 22:12:13 +01:00
2019-12-05 22:37:09 +01:00
yield put({ type: CREATE_ISSUE_SUCCESS, project, title, label, body, issue });
2019-12-01 22:12:13 +01:00
}