-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathgithub.js
More file actions
188 lines (146 loc) · 4.68 KB
/
github.js
File metadata and controls
188 lines (146 loc) · 4.68 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const inquirer = require('inquirer');
const { Octokit } = require("@octokit/rest");
class GitHub {
constructor() {
this.REPOSITORY_NAME = 'browser-logos';
this.REPOSITORY_OWNER = 'alrra';
this.REPOSITORY_URL = `https://github.com/${this.REPOSITORY_OWNER}/${this.REPOSITORY_NAME}`;
this.octokit;
this.tokenID;
this.userCredentials = {
otp: null,
password: null,
username: null
};
}
get repositoryURL () {
return this.REPOSITORY_URL;
}
async askForCredentials (configurations) {
const configs = Object.assign(
{
otp: true,
password: true,
username: true
},
configurations
);
const questions = [];
if (configs.username) {
questions.push({
message: 'GitHub username:',
name: 'username',
type: 'input'
});
}
if (configs.password) {
questions.push({
message: 'GitHub password:',
name: 'password',
type: 'password'
});
}
if (configs.otp) {
questions.push({
message: 'GitHub OTP:',
name: 'otp',
type: 'input'
});
}
Object.assign(
this.userCredentials,
await inquirer.prompt(questions)
);
console.log();
}
async createRelease (configs) {
await this.octokit.repos.createRelease({
body: configs.body,
name: configs.name,
owner: this.REPOSITORY_OWNER,
repo: this.REPOSITORY_NAME,
tag_name: configs.tag_name
});
}
async createToken () {
await this.askForCredentials();
const octokit = new Octokit({
auth: {
on2fa: (() => this.userCredentials.otp),
password: this.userCredentials.password,
username: this.userCredentials.username
}
});
let oauthAuthorization;
try {
oauthAuthorization = await octokit.oauthAuthorizations.createAuthorization({
note: `browser-logos release script (${new Date()})`,
scopes: ['repo']
});
} catch (e) {
if (e.status === 401) {
return await this.createToken();
} else {
throw new Error(e);
}
}
this.tokenID = oauthAuthorization.data.id;
this.octokit = new Octokit({
auth: `token ${oauthAuthorization.data.token}`
});
}
async getCommitAuthorInfo (sha) {
let commitAuthorInfo;
let commitInfo;
try {
// Get commit information.
commitInfo = await this.octokit.repos.getCommit({
owner: this.REPOSITORY_OWNER,
ref: sha,
repo: this.REPOSITORY_NAME
});
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Get commit author related info.
// This is done because the previous request doesn't provide
// the name of the user, only the name of the user associated
// with the commit, which in most cases, is wrongly set.
commitAuthorInfo = await this.octokit.users.getByUsername({
username: commitInfo.data.author.login
});
} catch (e) {
return null;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
return {
gitHubProfileURL: commitAuthorInfo.data.html_url,
// Get the user name, and if one is not provided,
// use the name associated with the commit.
name: commitAuthorInfo.data.name || commitInfo.data.commit.author.name
};
}
async deleteToken () {
await this.askForCredentials({
password: false,
username: false
});
const octokit = new Octokit({
auth: {
on2fa: (() => this.userCredentials.otp),
password: this.userCredentials.password,
username: this.userCredentials.username
}
});
try {
await octokit.oauthAuthorizations.deleteAuthorization({
authorization_id: this.tokenID
});
} catch (e) {
if (e.status === 401) {
await this.deleteToken();
} else {
throw new Error(e);
}
}
}
}
module.exports = GitHub;