Skip to content
Merged
8 changes: 3 additions & 5 deletions cypress/e2e/autoApproved.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,9 @@ describe('Auto-Approved Push Test', () => {
.local()
.format('dddd, MMMM Do YYYY, h:mm:ss a');

cy.get('kbd')
.trigger('mouseover')
.then(() => {
cy.get('.MuiTooltip-tooltip').should('contain', expectedTooltipTimestamp);
});
cy.get('kbd').trigger('mouseover');

cy.get('.MuiTooltip-tooltip').should('contain', expectedTooltipTimestamp);

cy.contains('approved this contribution').should('not.exist');
});
Expand Down
10 changes: 5 additions & 5 deletions cypress/e2e/repo.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ describe('Repo', () => {
.next()
.get('svg.octicon-copy')
.should('exist')
.click()
.get('svg.octicon-copy')
.should('not.exist')
.get('svg.octicon-check')
.should('exist');
.click();

cy.get('svg.octicon-copy').should('not.exist');

cy.get('svg.octicon-check').should('exist');
});

after(() => {
Expand Down
82 changes: 40 additions & 42 deletions src/db/file/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,19 @@ export const createRepo = async (repo: Repo): Promise<Repo> => {

export const addUserCanPush = async (_id: string, user: string): Promise<void> => {
user = user.toLowerCase();
return new Promise(async (resolve, reject) => {
const repo = await getRepoById(_id);
if (!repo) {
reject(new Error('Repo not found'));
return;
}

if (repo.users?.canPush.includes(user)) {
resolve();
return;
}
repo.users?.canPush.push(user);

const options = { multi: false, upsert: false };
const repo = await getRepoById(_id);
if (!repo) {
throw new Error('Repo not found');
}

if (repo.users?.canPush.includes(user)) {
return;
}
repo.users?.canPush.push(user);

const options = { multi: false, upsert: false };

return new Promise<void>((resolve, reject) => {
db.update({ _id: _id }, repo, options, (err) => {
// ignore for code coverage as neDB rarely returns errors even for an invalid query
/* istanbul ignore if */
Expand All @@ -134,21 +133,20 @@ export const addUserCanPush = async (_id: string, user: string): Promise<void> =

export const addUserCanAuthorise = async (_id: string, user: string): Promise<void> => {
user = user.toLowerCase();
return new Promise(async (resolve, reject) => {
const repo = await getRepoById(_id);
if (!repo) {
reject(new Error('Repo not found'));
return;
}
const repo = await getRepoById(_id);
if (!repo) {
throw new Error('Repo not found');
}

if (repo.users.canAuthorise.includes(user)) {
resolve();
return;
}
if (repo.users.canAuthorise.includes(user)) {
return;
}

repo.users.canAuthorise.push(user);
repo.users.canAuthorise.push(user);

const options = { multi: false, upsert: false };
const options = { multi: false, upsert: false };

return new Promise((resolve, reject) => {
db.update({ _id: _id }, repo, options, (err) => {
// ignore for code coverage as neDB rarely returns errors even for an invalid query
/* istanbul ignore if */
Expand All @@ -163,16 +161,16 @@ export const addUserCanAuthorise = async (_id: string, user: string): Promise<vo

export const removeUserCanAuthorise = async (_id: string, user: string): Promise<void> => {
user = user.toLowerCase();
return new Promise(async (resolve, reject) => {
const repo = await getRepoById(_id);
if (!repo) {
reject(new Error('Repo not found'));
return;
}
const repo = await getRepoById(_id);
if (!repo) {
throw new Error('Repo not found');
}

repo.users.canAuthorise = repo.users.canAuthorise.filter((x: string) => x != user);
repo.users.canAuthorise = repo.users.canAuthorise.filter((x: string) => x != user);

const options = { multi: false, upsert: false };
const options = { multi: false, upsert: false };

return new Promise<void>((resolve, reject) => {
db.update({ _id: _id }, repo, options, (err) => {
// ignore for code coverage as neDB rarely returns errors even for an invalid query
/* istanbul ignore if */
Expand All @@ -187,16 +185,16 @@ export const removeUserCanAuthorise = async (_id: string, user: string): Promise

export const removeUserCanPush = async (_id: string, user: string): Promise<void> => {
user = user.toLowerCase();
return new Promise(async (resolve, reject) => {
const repo = await getRepoById(_id);
if (!repo) {
reject(new Error('Repo not found'));
return;
}
const repo = await getRepoById(_id);
if (!repo) {
throw new Error('Repo not found');
}

repo.users.canPush = repo.users.canPush.filter((x) => x != user);

repo.users.canPush = repo.users.canPush.filter((x) => x != user);
const options = { multi: false, upsert: false };

const options = { multi: false, upsert: false };
return new Promise<void>((resolve, reject) => {
db.update({ _id: _id }, repo, options, (err) => {
// ignore for code coverage as neDB rarely returns errors even for an invalid query
/* istanbul ignore if */
Expand Down
77 changes: 32 additions & 45 deletions src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,57 +100,44 @@ export const createRepo = async (repo: AuthorisedRepo) => {

export const isUserPushAllowed = async (url: string, user: string) => {
user = user.toLowerCase();
return new Promise<boolean>(async (resolve) => {
const repo = await getRepoByUrl(url);
if (!repo) {
resolve(false);
return;
}

if (repo.users?.canPush.includes(user) || repo.users?.canAuthorise.includes(user)) {
resolve(true);
} else {
resolve(false);
}
});
const repo = await getRepoByUrl(url);
if (!repo) {
return false;
}

return repo.users?.canPush.includes(user) || repo.users?.canAuthorise.includes(user);
};

export const canUserApproveRejectPush = async (id: string, user: string) => {
return new Promise(async (resolve) => {
const action = await getPush(id);
if (!action) {
resolve(false);
return;
}

const theRepo = await sink.getRepoByUrl(action.url);

if (theRepo?.users?.canAuthorise?.includes(user)) {
console.log(`user ${user} can approve/reject for repo ${action.url}`);
resolve(true);
} else {
console.log(`user ${user} cannot approve/reject for repo ${action.url}`);
resolve(false);
}
});
const action = await getPush(id);
if (!action) {
return false;
}

const theRepo = await sink.getRepoByUrl(action.url);

if (theRepo?.users?.canAuthorise?.includes(user)) {
console.log(`user ${user} can approve/reject for repo ${action.url}`);
return true;
} else {
console.log(`user ${user} cannot approve/reject for repo ${action.url}`);
return false;
}
};

export const canUserCancelPush = async (id: string, user: string) => {
return new Promise(async (resolve) => {
const action = await getPush(id);
if (!action) {
resolve(false);
return;
}

const isAllowed = await isUserPushAllowed(action.url, user);

if (isAllowed) {
resolve(true);
} else {
resolve(false);
}
});
const action = await getPush(id);
if (!action) {
return false;
}

const isAllowed = await isUserPushAllowed(action.url, user);

if (isAllowed) {
return true;
} else {
return false;
}
};

export const getSessionStore = (): MongoDBStore | null =>
Expand Down
2 changes: 0 additions & 2 deletions src/proxy/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import { ProxyOptions } from 'express-http-proxy';

enum ActionType {
ALLOWED = 'Allowed',

ERROR = 'Error',

BLOCKED = 'Blocked',
}

Expand Down
2 changes: 0 additions & 2 deletions test/chain.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ const mockPreProcessors = {
parseAction: sinon.stub(),
};

let mockPushProcessors;

const clearCache = (sandbox) => {
delete require.cache[require.resolve('../src/proxy/processors')];
delete require.cache[require.resolve('../src/proxy/chain')];
Expand Down
6 changes: 3 additions & 3 deletions test/integration/forcePush.integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('Force Push Integration Test', () => {
let rebasedCommitSHA;

before(async function () {
this.timeout(10000); // eslint-disable-line no-invalid-this
this.timeout(10000);

tempDir = path.join(__dirname, '../temp-integration-repo');
await fs.mkdir(tempDir, { recursive: true });
Expand Down Expand Up @@ -58,7 +58,7 @@ describe('Force Push Integration Test', () => {

describe('Complete force push pipeline', () => {
it('should handle valid diff after rebase scenario', async function () {
this.timeout(5000); // eslint-disable-line no-invalid-this
this.timeout(5000);

// Create action simulating force push with valid SHAs that have actual changes
const action = new Action(
Expand Down Expand Up @@ -101,7 +101,7 @@ describe('Force Push Integration Test', () => {
});

it('should handle unreachable commit SHA error', async function () {
this.timeout(5000); // eslint-disable-line no-invalid-this
this.timeout(5000);

// Invalid SHA to trigger error
const action = new Action(
Expand Down
2 changes: 1 addition & 1 deletion test/testProxy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ describe('Proxy', () => {
await proxy.start();

// simulate error in server close
mockHttpServer.close.callsFake((callback) => {
mockHttpServer.close.callsFake(() => {
throw new Error('Server close error');
});

Expand Down
Loading