import { put, call, retry } from 'redux-saga/effects'; 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'; import { gitea } from '../../util/gitea'; export function* fetchIssuesSaga(action: any) { const { project } = action; let issues = []; try { 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++; } } catch(error) { yield put({ type: FETCH_ISSUES_FAILURE, project, error }); return; } yield put({ type: FETCH_ISSUES_SUCCESS, project, issues }); } export function* addLabelSaga(action: any) { const { project, issueNumber, label } = action; const labels = yield call(gitea.fetchProjectLabels.bind(gitea), project); const giteaLabel = labels.find((l: any) => l.name === label) 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 }); } export function* removeLabelSaga(action: any) { const { project, issueNumber, label } = action; const labels = yield call(gitea.fetchProjectLabels.bind(gitea), project); const giteaLabel = labels.find((l: any) => l.name === label) 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 }); } export function* createIssueSaga(action: any) { const { project, title, label, body } = action; const labels = yield call(gitea.fetchProjectLabels.bind(gitea), project); const giteaLabel = labels.find((l: any) => l.name === label) 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; } yield put({ type: CREATE_ISSUE_SUCCESS, project, title, label, body, issue }); }