This repository was archived by the owner on Mar 22, 2020. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathcreateTeam.js
More file actions
111 lines (89 loc) · 2.69 KB
/
Copy pathcreateTeam.js
File metadata and controls
111 lines (89 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const config = require("../../config");
const debug = require('debug')('init:createTeam');
const Octokit = require('@octokit/rest');
const path = require('path');
const fs = require('mz/fs');
const run = require('../run');
const octokit = new Octokit({
auth: `token ${config.secret.github.token}`,
previews: ['hellcat-preview'], // enables nested teams API
});
const graphql = require('@octokit/graphql').defaults({
headers: {
authorization: `token ${config.secret.github.token}`,
accept: 'application/vnd.github.elektra-preview+json'
}
});
// if original local repo doesn't exist => create it
// if translated local repo exists => we're done
// otherwise
// if translated remote repo exists => clone it, set upstream
// otherwise create translated local and remote repos
module.exports = async function(langInfo) {
debug("Getting parent team id");
const {data: parentTeam} = await octokit.teams.getByName({
org: config.org,
team_slug: config.teamMain
});
debug("Got parent team", parentTeam && parentTeam.id);
debug("Getting translate team id");
let translateTeam;
try {
translateTeam = await octokit.teams.getByName({
org: config.org,
team_slug: `${config.teamMain}-${langInfo.code}`
});
translateTeam = translateTeam.data;
} catch(e) {
if (e.status === 404) {
/* ok, no such team */
} else {
throw e;
}
}
if (translateTeam) {
debug("Got translate team", translateTeam.id);
} else {
debug("No translate team, creating");
// No graphql to create team
const response = await octokit.teams.create({
org: config.org,
name: `${config.teamMain}-${langInfo.code}`,
description: `Discuss the translation of Javascript Tutorial into ${langInfo.name}.`,
privacy: 'closed',
parent_team_id: parentTeam.id,
});
// console.log(response);
translateTeam = response.data;
debug("Created translate team");
}
debug("Allow team to admin repo");
await octokit.teams.addOrUpdateRepo({
team_id: translateTeam.id,
owner: config.org,
repo: `${langInfo.code}.${config.repoSuffix}`,
permission: 'admin'
});
debug("Adding maintainers");
for(let maintainer of langInfo.maintainers) {
await octokit.teams.addOrUpdateMembership({
team_id: translateTeam.id,
username: maintainer,
role: 'maintainer'
});
}
};
async function findTeamNodeIdBySlug(slug) {
const {organization: {team}} = await graphql(`
query($org: String!, $teamSlug: String!) {
organization(login:$org) {
team(slug:$teamSlug) {
id
}
}
}`, {
org: config.org,
teamSlug: slug
});
return team && team.id;
}