Skip to content

Commit

Permalink
Improve consistency/clarity in gitlab/github classes
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Jan 16, 2019
1 parent fb10e89 commit 8e6100e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 53 deletions.
18 changes: 8 additions & 10 deletions lib/github.js
Expand Up @@ -41,8 +41,8 @@ class GitHub extends Release {
throw ghError;
}

getGitHubClient() {
if (this.client) return this.client;
get client() {
if (this.ghClient) return this.ghClient;
const { proxy, timeout } = this.options;
const host = this.options.host || this.repo.host;
const isGitHub = host === 'github.com';
Expand All @@ -60,15 +60,15 @@ class GitHub extends Release {
options.proxy = proxy;
}

const client = new GitHubApi(options);
const ghClient = new GitHubApi(options);

client.authenticate({
ghClient.authenticate({
type: 'oauth',
token: this.token
});

this.client = client;
return client;
this.ghClient = ghClient;
return ghClient;
}

async release({ version, isPreRelease, changelog } = {}) {
Expand All @@ -85,7 +85,6 @@ class GitHub extends Release {
return noop;
}

const client = this.getGitHubClient();
const { draft } = this.options;
const { owner, project: repo } = this.repo;

Expand All @@ -101,7 +100,7 @@ class GitHub extends Release {
draft
};
debug(options);
const response = await client.repos.createRelease(options);
const response = await this.client.repos.createRelease(options);
debug(response.data);
const { html_url, upload_url } = response.data;
this.log.verbose(`octokit releases#createRelease: done (${response.headers.location})`);
Expand All @@ -116,7 +115,6 @@ class GitHub extends Release {
}

uploadAsset(filePath) {
const client = this.getGitHubClient();
const url = this.uploadUrl;
const name = path.basename(filePath);
const contentType = mime.contentType(name) || 'application/octet-stream';
Expand All @@ -134,7 +132,7 @@ class GitHub extends Release {
}
};
debug(options);
const response = await client.repos.uploadReleaseAsset(options);
const response = await this.client.repos.uploadReleaseAsset(options);
debug(response.data);
this.log.verbose(`octokit releases#uploadAsset: done (${response.data.browser_download_url})`);
return response.data;
Expand Down
78 changes: 35 additions & 43 deletions lib/gitlab.js
Expand Up @@ -26,11 +26,33 @@ class GitLab extends Release {
super.remoteUrl = remoteUrl;
if (this.repo) {
this.origin = this.options.origin || `https://${this.repo.host}`;
this.api = `${this.origin}/api/v4`;
this.baseUrl = `${this.origin}/api/v4`;
this.id = encodeURIComponent(this.repo.repository);
}
}

get client() {
if (this.glClient) return this.glClient;
this.glClient = got.extend({
baseUrl: this.baseUrl,
method: 'POST',
json: true,
headers: {
'user-agent': 'webpro/release-it',
'Private-Token': this.token
}
});
return this.glClient;
}

async request(endpoint, options) {
debug(Object.assign({ url: this.baseUrl + endpoint }, options));
const response = await this.client.post(endpoint, options);
const body = typeof response.body === 'string' ? JSON.parse(response.body) : response.body || {};
debug(body);
return body;
}

async release({ version, changelog } = {}) {
const { tagName, releaseName, releaseNotes } = this.options;
const tag_name = format(tagName, { version });
Expand All @@ -45,18 +67,12 @@ class GitLab extends Release {
return noop;
}

const url = `${this.api}/projects/${this.id}/releases`;
const endpoint = `/projects/${this.id}/releases`;
const options = {
method: 'POST',
json: true,
body: {
name,
tag_name,
description
},
headers: {
'user-agent': 'webpro/release-it',
'Private-Token': this.token
}
};

Expand All @@ -68,14 +84,12 @@ class GitLab extends Release {

return this.retry(async bail => {
try {
debug(Object.assign({ url }, options));
const response = await got(url, options);
debug(response.body);
const body = await this.request(endpoint, options);
this.log.verbose(`gitlab releases#createRelease: done`);
this.releaseUrl = `${this.origin}/${this.repo.repository}/releases`;
this.isReleased = true;
this.assets = [];
return response.body;
return body;
} catch (err) {
debug(err);
if (_.includes(NO_RETRIES_NEEDED, err.statusCode)) {
Expand All @@ -95,33 +109,20 @@ class GitLab extends Release {
uploadAsset(filePath) {
const name = path.basename(filePath);
const { repository } = this.repo;
const url = `${this.api}/projects/${this.id}/uploads`;
const endpoint = `/projects/${this.id}/uploads`;

const body = new FormData();
body.append('file', fs.createReadStream(filePath));

const options = {
method: 'POST',
body,
headers: {
'user-agent': 'webpro/release-it',
'Private-Token': this.token
}
};
const options = { json: false, body };

return this.retry(async bail => {
try {
debug(Object.assign({ url }, options));
const response = await got(url, options);
const body = typeof response.body === 'string' ? JSON.parse(response.body) : response.body || {};
debug(body);
const body = await this.request(endpoint, options);
this.log.verbose(`gitlab releases#uploadAsset: done (${body.url})`);
const asset = {
this.assets.push({
name,
url: `${this.origin}/${repository}${body.url}`
};
this.assets.push(asset);
return asset;
});
} catch (err) {
debug(err);
if (_.includes(NO_RETRIES_NEEDED, err.statusCode)) {
Expand Down Expand Up @@ -158,25 +159,16 @@ class GitLab extends Release {

this.log.exec(`gitlab releases#addReleaseNotesToTag "${name}" (${tag_name})`);

const url = `${this.api}/projects/${this.id}/repository/tags/${tag_name}/release`;
const body = {
description
};
const options = {
method: 'POST',
json: true,
body
};
const endpoint = `/projects/${this.id}/repository/tags/${tag_name}/release`;
const options = { body: { description } };

return this.retry(async bail => {
try {
debug(Object.assign({ url }, options));
const response = await got(url, options);
debug(response.body);
const body = await this.request(endpoint, options);
this.log.verbose(`gitlab releases#addReleaseNotesToTag: done`);
this.releaseUrl = `${this.origin}/${this.repo.repository}/tags/${tag_name}`;
this.isReleased = true;
return response.body;
return body;
} catch (err) {
debug(err);
if (err.statusCode === 404) {
Expand Down

0 comments on commit 8e6100e

Please sign in to comment.