refactor: adjust to refactored GitLab API

This commit is contained in:
Karl-Philipp Wulfert
2019-02-05 15:52:28 +01:00
parent 201ec093b7
commit cd48929ca1
4 changed files with 37 additions and 57 deletions

View File

@@ -13,7 +13,7 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {Api} from '@openstapps/gitlab-api';
import {Issue, Project, User} from '@openstapps/gitlab-api/lib/types';
import {Issue, IssueState, MembershipScope, MergeRequestState, Project, User} from '@openstapps/gitlab-api/lib/types';
import {asyncPool} from 'async-pool-native/dist/async-pool';
import {readFileSync, writeFile} from 'fs';
import * as moment from 'moment';
@@ -30,7 +30,7 @@ const asyncWriteFile = promisify(writeFile);
*
* @param state State to check
*/
export function issueStateIsOpenedOrClosed(state: string): state is 'opened' | 'closed' {
export function issueStateIsOpenedOrClosed(state: IssueState): state is IssueState.OPENED | IssueState.CLOSED {
return ['opened', 'closed'].indexOf(state) >= 0;
}
@@ -191,8 +191,8 @@ export function groupIssuesByAssignee(issues: Issue[]): { [k: number]: AssigneeW
};
}
if (issue.state === 'reopened') {
issue.state = 'opened';
if (issue.state === IssueState.REOPENED) {
issue.state = IssueState.OPENED;
}
if (issueStateIsOpenedOrClosed(issue.state)) {
@@ -273,7 +273,7 @@ export async function getMergeRequestsForProjects(api: Api,
}
// get all merge requests for project
const mergeRequests = await api.getMergeRequestsForProject(project.id);
const mergeRequests = await api.getMergeRequests(MembershipScope.PROJECTS, project.id, MergeRequestState.OPENED);
// extract issue number from merge request
projectMergeRequests[project.id] = mergeRequests.map((mergeRequest) => {
@@ -332,11 +332,11 @@ export async function report(api: Api, label: string) {
}
});
if (a.state === 'closed') {
if (a.state === IssueState.CLOSED) {
weightA -= 10;
}
if (b.state === 'closed') {
if (b.state === IssueState.CLOSED) {
weightB -= 10;
}

View File

@@ -13,7 +13,14 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {Api} from '@openstapps/gitlab-api';
import {AccessLevel, Label, Milestone, Project} from '@openstapps/gitlab-api/lib/types';
import {
AccessLevel,
IssueState,
MembershipScope,
Milestone,
Project,
Scope,
} from '@openstapps/gitlab-api/lib/types';
import {asyncPool} from 'async-pool-native/dist/async-pool';
import {flatten2dArray, getProjects, logger} from '../common';
import {GROUPS, NEEDED_LABELS, NEEDED_MILESTONES, NOTE_PREFIX, PROTECTED_BRANCHES, SCHOOLS} from '../configuration';
@@ -31,7 +38,7 @@ export async function tidyIssuesWithoutMilestone(api: Api): Promise<void> {
return api.getIssues({
groupId: groupId,
milestone: 'No Milestone',
state: 'opened',
state: IssueState.OPENED,
});
});
@@ -66,6 +73,7 @@ export async function tidyIssuesWithoutMilestone(api: Api): Promise<void> {
await api.createNote(
issue.project_id,
Scope.ISSUES,
issue.iid,
`${NOTE_PREFIX} Milestone was set automatically to \`Meeting\`.`,
);
@@ -86,7 +94,7 @@ export async function tidyOpenIssuesWithoutMeetingLabel(api: Api): Promise<void>
const issueResults = await asyncPool(3, GROUPS, (groupId) => {
return api.getIssues({
groupId: groupId,
state: 'opened',
state: IssueState.OPENED,
});
});
@@ -110,6 +118,7 @@ export async function tidyOpenIssuesWithoutMeetingLabel(api: Api): Promise<void>
return api.createNote(
issue.project_id,
Scope.ISSUES,
issue.iid,
`${NOTE_PREFIX} Automatically adding label 'meeting'\n\n/label ~meeting`);
});
@@ -128,21 +137,21 @@ export async function tidyLabels(api: Api, projects: Project[]): Promise<void> {
const labels = await api.getLabels(project.id);
const neededLabels = NEEDED_LABELS.slice(0);
const extraneousLabels: Label[] = [];
// const extraneousLabels: Label[] = [];
labels.forEach((label) => {
let needed = false;
// let needed = false;
neededLabels.forEach((neededLabel, neededLabelIdx) => {
if (neededLabel.name.toLowerCase() === label.name.toLowerCase()) {
neededLabels.splice(neededLabelIdx, 1);
needed = true;
// needed = true;
}
});
if (!needed) {
/* if (!needed) {
extraneousLabels.push(label);
}
} */
});
await asyncPool(2, neededLabels, async (neededLabel) => {
@@ -225,7 +234,7 @@ export async function tidyProtectedBranches(api: Api, projects: Project[]): Prom
* @param api GitLab API instance to use for the requests
*/
export async function tidySubGroupMembers(api: Api): Promise<void> {
const stappsMembers = await api.getMembers('groups', GROUPS[0]);
const stappsMembers = await api.getMembers(MembershipScope.GROUPS, GROUPS[0]);
const stappsMemberIds = stappsMembers.map((member) => member.id);
const groupIdsToSchool: any = {};
@@ -235,12 +244,12 @@ export async function tidySubGroupMembers(api: Api): Promise<void> {
});
await asyncPool(2, GROUPS.slice(1), async (groupId) => {
const members = await api.getMembers('groups', groupId);
const members = await api.getMembers(MembershipScope.GROUPS, groupId);
const memberIds = members.map((member) => member.id);
await asyncPool(2, stappsMembers, async (stappsMember) => {
if (memberIds.indexOf(stappsMember.id) === -1) {
await api.addMember('groups', groupId, stappsMember.id, AccessLevel.Developer);
await api.addMember(MembershipScope.GROUPS, groupId, stappsMember.id, AccessLevel.Developer);
logger.log('Added ' + stappsMember.name + ' to group ' + groupIdsToSchool[groupId] + '.');
}
@@ -248,7 +257,7 @@ export async function tidySubGroupMembers(api: Api): Promise<void> {
await asyncPool(2, members, async (member) => {
if (stappsMemberIds.indexOf(member.id) === -1) {
await api.deleteMember('groups', groupId, member.id);
await api.deleteMember(MembershipScope.GROUPS, groupId, member.id);
logger.log('Deleted member ' + member.name + ' from group ' + groupIdsToSchool[groupId] + '.');
}
@@ -270,7 +279,7 @@ export async function tidyIssuesWithoutAssignee(api: Api): Promise<void> {
const issueResults = await asyncPool(3, GROUPS, (groupId) => {
return api.getIssues({
groupId: groupId,
state: 'opened',
state: IssueState.OPENED,
});
});
@@ -290,6 +299,7 @@ export async function tidyIssuesWithoutAssignee(api: Api): Promise<void> {
await api.createNote(
issue.project_id,
Scope.ISSUES,
issue.iid,
`${NOTE_PREFIX} Assignee was set automatically to author.`,
);

View File

@@ -13,6 +13,7 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {Api} from '@openstapps/gitlab-api';
import {IssueState, Scope} from '@openstapps/gitlab-api/lib/types';
import {asyncPool} from 'async-pool-native/dist/async-pool';
import {flatten2dArray, logger} from '../common';
import {GROUPS, NOTE_PREFIX} from '../configuration';
@@ -26,7 +27,7 @@ export async function unlabel(api: Api) {
const issueResults = await asyncPool(3, GROUPS, (groupId) => {
return api.getIssues({
groupId: groupId,
state: 'closed',
state: IssueState.CLOSED,
});
});
@@ -40,6 +41,7 @@ export async function unlabel(api: Api) {
await api.createNote(
issue.project_id,
Scope.ISSUES,
issue.iid,
`${NOTE_PREFIX} Removed label \`meeting\` automatically.
/unlabel ~meeting`,