-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathtransition-checker.js
153 lines (138 loc) · 5.26 KB
/
transition-checker.js
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* @file Check if the state of document can be transferred, relayed on issue and comments in [w3c/transitions](https://github.com/w3c/transitions)
*/
'use strict';
import Octokat from 'octokat';
import pkg from 'immutable';
const { List } = pkg;
/**
* @exports lib/transition-checker
*/
const TransitionChecker = {};
/**
* @returns {Promise.<List.<String>>}
*/
/**
* @description Check if the document can be transferred. Accounting:
* - Getting Issues in [w3c/transitions](https://github.com/w3c/transitions) and check the comments.
* - directorApproval: [request-a-transition-request](https://github.com/w3c/echidna/wiki/CR-publication-workflow#request-a-transition-request)
* - commApproval: [draft-announcement-to-the-members](https://github.com/w3c/echidna/wiki/CR-publication-workflow#draft-announcement-to-the-members)
* @param {*} profile
* @param {*} latestVersion
* @param {*} previousVersion
* @param {*} isUpdate
* @param {*} isDryRun
*/
TransitionChecker.check = (
profile,
latestVersion,
previousVersion,
isUpdate,
isDryRun,
) =>
new Promise((resolve, reject) => {
let errors = new List();
if (
['WD', 'CRD', 'REC', 'DNOTE', 'NOTE', 'DRY'].includes(profile) ||
isUpdate
) {
resolve({ errors, requiresCfE: false });
} else if (profile === 'CR') {
if (
previousVersion.includes('/WD-') ||
previousVersion.includes('/CR-') ||
previousVersion.includes('/CRD-')
) {
const octo = new Octokat({
token: global.GH_TOKEN,
});
const repo = octo.repos('w3c', 'transitions');
const shortname = latestVersion.match(/.*\/([^/]+)\/$/)[1];
const directorApproval = 'transition approved.';
const commApproval = 'draft transition received.';
let directorApprovalFound = false;
let commApprovalFound = false;
let issueNumber;
repo.issues
.fetch({
labels: 'Awaiting Publication',
state: 'open',
sort: 'updated',
per_page: 100, // TODO: get all the issues, not just the first 100
})
.then(content => {
const issueFound = content.items.some(issue => {
if (issue.title.endsWith(` ${shortname}`)) {
issueNumber = issue.number;
repo
.issues(issue.number)
.comments.fetch()
.then(comments => {
const promises = [];
comments.items.every(comment => {
const commentText = comment.body.toLowerCase();
if (commentText.indexOf(directorApproval) > -1) {
// Director's approval
const p1 = octo
.teams(global.GH_DIRECTOR_TEAM_ID)
.members(comment.user.login)
.fetch(err => {
if (!err) directorApprovalFound = true;
});
promises.push(p1);
} else if (commentText.indexOf(commApproval) > -1) {
// Comm's approval
const p2 = octo
.teams(global.GH_COMM_TEAM_ID)
.members(comment.user.login)
.fetch(err => {
if (!err) commApprovalFound = true;
});
promises.push(p2);
}
return true;
});
Promise.all(promises).then(() => {
if (!directorApprovalFound)
errors = errors.push(
'Approval from Director not found.',
);
if (!commApprovalFound)
errors = errors.push(
'Approval from Communication Team not found.',
);
if (
!isDryRun &&
directorApprovalFound &&
commApprovalFound
) {
// Close issue
repo.issues(issueNumber).update({ state: 'closed' });
}
return resolve({ errors, requiresCfE: true });
});
});
return true;
}
return false;
});
if (!issueFound) {
// Issue not found
return reject(
new Error(
'Issue not found on the github repository w3c/transitions. Make sure all the requirements are met: https://github.com/w3c/echidna/wiki/CR-publication-workflow#request-a-transition-request',
),
);
}
})
.catch(error =>
reject(
new Error(
`An error occurred while looking for the transition issue: ${error}`,
),
),
);
} else reject(new Error('Only CR coming from CR or WD are allowed!'));
} else reject(new Error('Only WD, CR, Notes and DRY are allowed!'));
});
export default TransitionChecker;