2019-12-01 22:12:13 +01:00
|
|
|
import { put, call, retry } from 'redux-saga/effects';
|
2019-12-05 22:37:09 +01: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';
|
|
|
|
|
|
|
|
export function* fetchIssuesSaga(action) {
|
|
|
|
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 });
|
|
|
|
}
|
|
|
|
|
|
|
|
export function* addLabelSaga(action) {
|
|
|
|
const { project, issueNumber, label } = action;
|
|
|
|
const labels = yield call(gitea.fetchProjectLabels.bind(gitea), project);
|
|
|
|
const giteaLabel = labels.find(l => 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) {
|
|
|
|
const { project, issueNumber, label } = action;
|
|
|
|
const labels = yield call(gitea.fetchProjectLabels.bind(gitea), project);
|
|
|
|
const giteaLabel = labels.find(l => 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 });
|
2019-12-05 22:37:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function* createIssueSaga(action) {
|
|
|
|
const { project, title, label, body } = action;
|
|
|
|
const labels = yield call(gitea.fetchProjectLabels.bind(gitea), project);
|
|
|
|
const giteaLabel = labels.find(l => 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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|