Skip to content

Commit

Permalink
add prerelease support
Browse files Browse the repository at this point in the history
  • Loading branch information
gramakri committed Sep 26, 2023
1 parent a2e16aa commit 2f50f7b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 20 deletions.
4 changes: 2 additions & 2 deletions backend/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ async function releasesAdd(release) {
release.id = uuid.v4();
release.createdAt = release.createdAt || 0;

await db.query('INSERT INTO releases (id, projectId, version, body, notified, createdAt) VALUES (?, ?, ?, ?, ?, ?)',
[ release.id, release.projectId, release.version, release.body, release.notified, release.createdAt ]);
await db.query('INSERT INTO releases (id, projectId, version, body, notified, prerelease, createdAt) VALUES (?, ?, ?, ?, ?, ?, ?)',
[ release.id, release.projectId, release.version, release.body, release.notified, release.prerelease, release.createdAt ]);

return release;
}
Expand Down
13 changes: 6 additions & 7 deletions backend/github.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var assert = require('assert'),
const assert = require('assert'),
{ Octokit } = require('@octokit/rest'),
{ retry } = require('@octokit/plugin-retry'),
{ throttling } = require('@octokit/plugin-throttling');
Expand All @@ -9,7 +9,7 @@ module.exports = exports = {
verifyToken,
getStarred,
getReleases,
getReleaseBody,
getRelease,
getCommit
};

Expand Down Expand Up @@ -106,7 +106,7 @@ async function getReleases(token, project) {
return releases;
}

async function getReleaseBody(token, project, version) {
async function getRelease(token, project, version) {
assert.strictEqual(typeof token, 'string');
assert.strictEqual(typeof project, 'object');
assert.strictEqual(typeof version, 'string');
Expand All @@ -124,11 +124,10 @@ async function getReleaseBody(token, project, version) {
rethrow(error);
}

if (!release.data.body) return '';
if (!release.data.body) return { body: '', prerelease: false };

const fullBody = release.data.body.replace(/\r\n/g, '\n');

return fullBody.length > 1000 ? fullBody.substring(0, 1000) + '...' : fullBody;
let body = release.data.body.replace(/\r\n/g, '\n');
return { body, prerelease: release.data.prerelease };
}

// Returns { createdAt, message }
Expand Down
8 changes: 4 additions & 4 deletions backend/gitlab.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';

var assert = require('assert'),
const assert = require('assert'),
superagent = require('superagent');

module.exports = exports = {
verifyToken,
getStarred,
getReleases,
getReleaseBody,
getRelease,
getCommit
};

Expand Down Expand Up @@ -54,12 +54,12 @@ async function getReleases(token, project) {
return releaseObjects;
}

function getReleaseBody(token, project, version) {
function getRelease(token, project, version) {
assert.strictEqual(typeof token, 'string');
assert.strictEqual(typeof project, 'object');
assert.strictEqual(typeof version, 'string');

return '';
return { body: '', prerelease: false };
}

// Returns { createdAt, message }
Expand Down
10 changes: 9 additions & 1 deletion backend/notification.template
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@

<table align="center" border="0" cellpadding="0" cellspacing="0" width="100%" style="border: 0; padding: 20px; font-family: Helvetica, sans-serif;">
<tr>
{{#if release.prerelease}}
<td align="center" class="header" style="padding: 40px 0 30px 0; background-color: #409eff; border-radius: 4px; font-size: 20px;">
{{else}}
<td align="center" class="header" style="padding: 40px 0 30px 0; background-color: #bf4c24; border-radius: 4px; font-size: 20px;">
{{/if}}
<a href="{{ versionLink }}" style="color: #fff;">{{ project.name }}</a>
</td>
</tr>
Expand All @@ -55,7 +59,11 @@
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td>
<a href="{{ versionLink }}" style="color: #606266;">{{ release.version }}</a>
<a href="{{ versionLink }}" style="color: #606266;">{{ release.version }}
{{#if release.prerelease}}
&nbsp; (prerelease)
{{/if}}
</a>
</td>
</tr>
{{#if release.body}}
Expand Down
14 changes: 8 additions & 6 deletions backend/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,22 +173,24 @@ async function syncReleasesByProject(user, project) {
release.notified = !project.lastSuccessfulSyncAt ? true : !project.enabled;
release.body = '';
release.createdAt = 0;
release.prerelease = false;

// skip fetching details for notification which will not be sent
if (release.notified) {
await database.releases.add(release);
continue;
}

let result;
try {
result = await api.getReleaseBody(user.githubToken, project, release.version);
const result = await api.getRelease(user.githubToken, project, release.version); // { body, prerelease }
release.body = result.body;
release.prerelease = result.prerelease;
} catch (error) {
console.error(`Failed to get release body for ${project.name} ${release.version}. Falling back to commit message.`, error);
release.body = '';
release.prerelease = false;
}

release.body = result || '';

const commit = await api.getCommit(user.githubToken, project, release.sha);

release.createdAt = new Date(commit.createdAt).getTime() || 0;
Expand Down Expand Up @@ -271,8 +273,8 @@ async function sendNotificationEmail(release) {
from: `ReleaseBell <${process.env.CLOUDRON_MAIL_FROM}>`,
to: user.email,
subject: `${project.name} ${release.version} released`,
text: `A new release at ${project.name} with version ${release.version} was published. ${release.body}. Read more about this release at ${versionLink}`,
html: EMAIL_TEMPLATE({ project: project, release: release, versionLink: versionLink, settingsLink: settingsLink })
text: `A new ${release.prerelease ? 'prerelease' : 'release'} at ${project.name} with version ${release.version} was published. ${release.body}. Read more about this release at ${versionLink}`,
html: EMAIL_TEMPLATE({ project, release, versionLink, settingsLink })
};

await transport.sendMail(mail);
Expand Down
10 changes: 10 additions & 0 deletions migrations/20230926130714-add-release-prerelease.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

exports.up = function(db, callback) {
db.runSql('ALTER TABLE releases ADD COLUMN prerelease BOOLEAN DEFAULT 0', callback);
};

exports.down = function(db, callback) {
db.runSql('ALTER TABLE releases DROP COLUMN prerelease', callback);
};

1 change: 1 addition & 0 deletions migrations/current_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ CREATE TABLE IF NOT EXISTS releases(
version VARCHAR(512) NOT NULL,
body TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
notified BOOLEAN DEFAULT false,
prereleases BOOLEAN DEFAULT false,
createdAt BIGINT NOT NULL,

FOREIGN KEY(projectId) REFERENCES projects(id),
Expand Down

0 comments on commit 2f50f7b

Please sign in to comment.