debian-mirror-gitlab/spec/frontend_integration/test_helpers/mock_server/routes/repository.js

61 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-10-24 23:57:45 +05:30
import { createNewCommit, createCommitIdGenerator } from 'test_helpers/factories';
2021-03-08 18:12:59 +05:30
export default (server) => {
2020-10-24 23:57:45 +05:30
const commitIdGenerator = createCommitIdGenerator();
2021-03-08 18:12:59 +05:30
server.get('/api/v4/projects/:id/repository/branches', (schema) => {
2020-10-24 23:57:45 +05:30
return schema.db.branches;
});
server.get('/api/v4/projects/:id/repository/branches/:name', (schema, request) => {
const { name } = request.params;
const branch = schema.branches.findBy({ name });
return branch.attrs;
});
2021-03-08 18:12:59 +05:30
server.get('*/-/files/:id', (schema) => {
2020-10-24 23:57:45 +05:30
return schema.db.files.map(({ path }) => path);
});
2021-02-22 17:27:13 +05:30
server.get('/:namespace/:project/-/blob/:sha/*path', (schema, request) => {
const { path } = schema.db.files.findBy({ path: request.params.path });
return { path, rawPath: request.url.replace('/-/blob', '/-/raw') };
});
server.get('/:namespace/:project/-/raw/:sha/*path', (schema, request) => {
const { path } = request.params;
return schema.db.filesRaw.findBy({ path })?.raw || 'Sample content';
});
2020-10-24 23:57:45 +05:30
server.post('/api/v4/projects/:id/repository/commits', (schema, request) => {
const { branch: branchName, commit_message: message, actions } = JSON.parse(
request.requestBody,
);
const branch = schema.branches.findBy({ name: branchName });
2021-03-08 18:12:59 +05:30
const prevCommit = branch
? branch.attrs.commit
: schema.branches.findBy({ name: 'master' }).attrs.commit;
2020-10-24 23:57:45 +05:30
const commit = {
2021-03-08 18:12:59 +05:30
...createNewCommit({ id: commitIdGenerator.next(), message }, prevCommit),
2020-10-24 23:57:45 +05:30
__actions: actions,
};
2021-03-08 18:12:59 +05:30
if (branch) {
branch.update({ commit });
} else {
schema.branches.create({
name: branchName,
commit,
});
}
2020-10-24 23:57:45 +05:30
return commit;
});
};