...
 
Commits (2)
......@@ -8,6 +8,7 @@
"babel-plugin-transform-async-to-generator": "^6.24.1",
"babel-polyfill": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"delay": "^2.0.0",
"express": "^4.15.5",
"express-winston": "^2.4.0",
"nodemon": "^1.12.1",
......
const request = require('superagent-use')(require('superagent'));
const logger = require('./logger');
import fs from 'fs';
import delay from 'delay';
import logger from './logger';
request.use((req) => req.set('Accept', 'application/json'));
......@@ -10,6 +12,18 @@ export default class GitLabAPI {
this.url = url;
}
async waitUntilReady() {
while (true) {
try {
await this.getProjects();
return;
} catch (e) {
// nothing
}
await delay(1000);
}
}
setOAuth2TokenFromFile(filename) {
this.setOAuth2Token(JSON.parse(fs.readFileSync(filename, 'utf-8')));
}
......@@ -18,6 +32,10 @@ export default class GitLabAPI {
request.use((req) => req.set('Authorization', 'Bearer ' + token.access_token));
}
setImpersonationToken(token) {
request.use((req) => req.query('private_token', token));
}
async oauth2Connect(username, password) {
var token = null;
try {
......@@ -162,6 +180,13 @@ export default class GitLabAPI {
return res.body;
}
// https://amenda.test/admin/help/api/users.md#list-users
async getUsers() {
const res = await request.get(this.url + '/api/v4/users');
return res.body;
}
// https://docs.gitlab.com/ce/api/notes.html#create-new-merge-request-note
async addMergeRequestNote(projectId, mergeRequestIid, body) {
const res = await request
......@@ -173,4 +198,22 @@ export default class GitLabAPI {
return res.body;
}
// https://amenda.test/admin/help/api/users.md#get-all-impersonation-tokens-of-a-user
async getImpersonationTokens(userId) {
const res = await request.get(this.url + '/api/v4/users/' + userId + '/impersonation_tokens');
return res.body;
}
// https://amenda.test/admin/help/api/users.md#create-an-impersonation-token
async createImpersonationToken(userId, name, expiresAt, scopes) {
const res = await request.post(this.url + '/api/v4/users/' + userId + '/impersonation_tokens')
.send({
name: name,
expires_at: expiresAt,
scopes: scopes,
});
return res.body;
}
}
......@@ -28,7 +28,11 @@ import GitLabWebhook from './GitLabWebhook';
}));
gitlab.setOAuth2TokenFromFile('/etc/amenda/gitlab_token.json');
// await gitlab.oauth2Connect(config.gitlabBot.email, config.gitlabBot.password);
// wait for the GitLab API to be ready
logger.info('waiting for the GitLab API...');
await gitlab.waitUntilReady();
logger.info('GitLab API is ready!');
await GitLabWebhook.addMissingProjectHooks(gitlab);
await GitLabWebhook.addMissingSystemHooks(gitlab);
......
......@@ -4,7 +4,7 @@ const label = 'gitlab-bot';
const MESSAGE = Symbol.for('message');
const customFormat = format((info, opt) => {
info[MESSAGE] = info.timestamp + ' ' + info.level + ': ' + info.message + '\n' + JSON.stringify(
const meta = JSON.stringify(
Object.assign({}, info.meta || info, {
level: undefined,
message: undefined,
......@@ -16,6 +16,8 @@ const customFormat = format((info, opt) => {
2
);
info[MESSAGE] = info.timestamp + ' ' + info.level + ': ' + info.message + (meta == '{}' ? '' : '\n' + meta);
return info;
});
......