Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions controllers/scriptStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2308,6 +2308,7 @@ exports.webhook = function (aReq, aRes) {
var repos = {};
var repo = null;
var update = null;
var defaultBranch = null;

// Return if script storage is in read-only mode
if (process.env.READ_ONLY_SCRIPT_STORAGE === 'true') {
Expand Down Expand Up @@ -2375,12 +2376,6 @@ exports.webhook = function (aReq, aRes) {

//

// Only accept commits from the `master` branch
if (payload.ref !== 'refs/heads/master') {
aRes.status(403).send('Default branch is not `master`.'); // Forbidden
return;
}

// Gather all the info for the RepoManager
username = payload.repository.owner.name;
reponame = payload.repository.name;
Expand Down Expand Up @@ -2411,6 +2406,13 @@ exports.webhook = function (aReq, aRes) {
return;
}

// Only accept commits from the default branch (defaults to `master`, allows user migration to `main`)
defaultBranch = aUser.ghBranch || 'master';
if (payload.ref !== 'refs/heads/' + defaultBranch) {
aRes.status(403).send('Default branch is not `' + defaultBranch + '`.'); // Forbidden
return;
}

aRes.status(202).send('Your request is queued.'); // Close connection with Accepted but processing

// Gather the modified user scripts
Expand Down
38 changes: 38 additions & 0 deletions controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,44 @@ exports.userEditPreferencesPage = function (aReq, aRes, aNext) {
});
};

exports.userUpdatePreferences = function (aReq, aRes, aNext) {
var authedUser = aReq.session.user;
var branch = aReq.body.branch;

if (!authedUser) {
aRes.redirect('/login');
return;
}

User.findOne({
_id: authedUser._id
}, function (aErr, aUser) {
if (aErr || !aUser) {
aNext();
return;
}

// One-way migration to `main` for GitHub authed accounts only
if (branch === 'main' && aUser.strategies && aUser.strategies.indexOf('github') > -1) {
if (aUser.ghBranch !== 'main') {
aUser.ghBranch = 'main';
aUser.save(function (aErr) {
if (aErr) {
console.error(aErr);
}
if (aReq.session && aReq.session.user) {
aReq.session.user.ghBranch = 'main';
}
aRes.redirect('/user/preferences');
});
return;
}
}

aRes.redirect('/user/preferences');
});
};

exports.newScriptPage = function (aReq, aRes, aNext) {
function preRender() {
}
Expand Down
2 changes: 2 additions & 0 deletions libs/modelParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,8 @@ var parseUser = function (aUser) {
user.userStrategies = user.strategies;
user.hasGithub = user.strategies && user.strategies.indexOf('github') > -1; // NOTE: Watchpoint
user.canSync = user.hasGithub;
user.ghBranch = user.ghBranch || 'master';
user.isGhBranchMain = user.ghBranch === 'main';

// Dates
parseDateProperty(user, 'created');
Expand Down
8 changes: 5 additions & 3 deletions libs/repoManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,10 @@ RepoManager.prototype.loadSyncs = function (aUpdate, aCallback) {
// TODO: Alter usage of makeRepoArray since it causes redundant looping
arrayOfRepos.forEach(function (aRepo) {
async.each(aRepo.scripts, function (aScript, aInnerCallback) {
var branch = (that.user && that.user.ghBranch) || 'master';
var hostname = 'raw.githubusercontent.com';
var uri = '/' + aRepo.user + '/' + aRepo.repo
+ '/master' + aScript.path;
+ '/' + branch + aScript.path;

Sync.findOne(
{ _authorId: that.user.id, id: aUpdate, target: 'https://' + hostname + uri },
Expand Down Expand Up @@ -216,11 +217,12 @@ RepoManager.prototype.loadScripts = function (aUpdate, aCallback) {
// TODO: Alter usage of makeRepoArray since it causes redundant looping
arrayOfRepos.forEach(function (aRepo) {
async.each(aRepo.scripts, function (aScript, aInnerCallback) {
var branch = (that.user && that.user.ghBranch) || 'master';
var hostname = 'raw.githubusercontent.com';
var uri = '/' + aRepo.user + '/' + aRepo.repo
+ '/master' + aScript.path;
+ '/' + branch + aScript.path;
var url = '/' + encodeURI(aRepo.user) + '/' + encodeURI(aRepo.repo)
+ '/master' + aScript.path;
+ '/' + branch + aScript.path;

fetchRaw(hostname, url, function (aBufs) {
var msg = null;
Expand Down
1 change: 1 addition & 0 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var userSchema = new Schema({

// Store their GitHub username when they import scripts
ghUsername: String,
ghBranch: { type: String, default: 'master' },

// Moderation
role: Number,
Expand Down
4 changes: 3 additions & 1 deletion routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,9 @@ module.exports = function (aApp) {
aApp.route('/users/:username/profile/captcha').head(statusTMR).get(captchaCapLimiter, authentication.validateUser, user.userEditProfilePageCaptcha);
aApp.route('/users/:username/update').head(statusTMR).post(authentication.validateUser, admin.adminUserUpdate);
// NOTE: Some below inconsistent with priors
aApp.route('/user/preferences').head(statusTMR).get(authentication.validateUser, user.userEditPreferencesPage);
aApp.route('/user/preferences').head(statusTMR)
.get(authentication.validateUser, user.userEditPreferencesPage)
.post(authentication.validateUser, user.userUpdatePreferences);
aApp.route('/user').head(statusTMR).get(function (aReq, aRes) {
aRes.redirect(302, '/users');
});
Expand Down
28 changes: 28 additions & 0 deletions views/pages/userEditPreferencesPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,34 @@ <h2 class="edit-authentication">Authentication</h2>
</form>
{{/unusedStrategies}}
</div>
{{#user.hasGithub}}
<div class="edit-github-box">
<h2 class="edit-github">GitHub Integration</h2>
<div class="input-group col-xs-12">
<span class="input-group-addon">
<i class="fa fa-github fa-fw"></i>
</span>
<span class="list-group-item">
Default branch: <strong>{{user.ghBranch}}</strong>
{{#user.isGhBranchMain}}
<span class="text-muted small">&mdash; Migrated</span>
{{/user.isGhBranchMain}}
</span>
{{^user.isGhBranchMain}}
<div style="width: 100%;">
<div class="input-group-addon">
<form method="post" action="/user/preferences" style="display: inline;">
<input type="hidden" name="branch" value="main">
<button class="btn btn-sm btn-warning pull-right" type="submit" title="Permanent one-way migration to main">
Migrate to <code>main</code> <i class="fa fa-code-fork fa-fw"></i>
</button>
</form>
</div>
</div>
{{/user.isGhBranchMain}}
</div>
</div>
{{/user.hasGithub}}
<div class="edit-session-box">
<h2 class="edit-session">Session</h2>
<div class="session-area list-group">
Expand Down