-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: add gitlab support #545
base: main
Are you sure you want to change the base?
Changes from 2 commits
a0e4d64
360c7c7
c5e8659
43f348d
b5cc493
e56f894
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
{ | ||
"proxyUrl": "https://github.com", | ||
"proxyConfig": [ | ||
{ | ||
"path": "/github.com", | ||
"url": "https://github.com", | ||
"enabled": true | ||
}, | ||
{ | ||
"path": "/gitlab.com", | ||
"url": "https://gitlab.com", | ||
"enabled": true | ||
Comment on lines
+4
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like your proposed configuration. Would it not be better to recommend |
||
} | ||
], | ||
"cookieSecret": "cookie secret", | ||
"sessionMaxAgeHours": 12, | ||
"tempPassword": { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,8 +94,8 @@ | |
const canUserCancelPush = async (id, user) => { | ||
return new Promise(async (resolve) => { | ||
const pushDetail = await getPush(id); | ||
const repoName = pushDetail.repoName.replace('.git', ''); | ||
const isAllowed = await repo.isUserPushAllowed(repoName, user); | ||
const repoUrl = pushDetail.repo.url; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about pre-existing repositories using the |
||
const isAllowed = await repo.isUserPushAllowed(repoUrl, user); | ||
|
||
if (isAllowed) { | ||
resolve(true); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,22 @@ | |
}); | ||
}; | ||
|
||
exports.getRepoByUrl = async (url) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep me honest, but can we not enforce uniqueness with Can we take advantage of the fact that both GitHub and GitLab follow the same naming convention for repositories and enforce uniqueness this way? GitHub: |
||
return new Promise((resolve, reject) => { | ||
db.findOne({ url: url }, (err, doc) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
if (!doc) { | ||
resolve(null); | ||
} else { | ||
resolve(doc); | ||
} | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
exports.createRepo = async (repo) => { | ||
repo.users = { | ||
canPush: [], | ||
|
@@ -140,10 +156,9 @@ | |
}); | ||
}; | ||
|
||
exports.isUserPushAllowed = async (name, user) => { | ||
name = name.toLowerCase(); | ||
exports.isUserPushAllowed = async (url, user) => { | ||
return new Promise(async (resolve, reject) => { | ||
const repo = await exports.getRepo(name); | ||
const repo = await exports.getRepoByUrl(url); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, same point here - this is fine moving forward for updated data properties but what about previous stored data? Do we need a transformer? |
||
console.log(repo.users.canPush); | ||
console.log(repo.users.canAuthorise); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,8 +102,8 @@ const canUserApproveRejectPush = async (id, user) => { | |
const canUserCancelPush = async (id, user) => { | ||
return new Promise(async (resolve) => { | ||
const pushDetail = await getPush(id); | ||
const repoName = pushDetail.repoName.replace('.git', ''); | ||
const isAllowed = await repo.isUserPushAllowed(repoName, user); | ||
const repoUrl = pushDetail.repo.url; | ||
const isAllowed = await repo.isUserPushAllowed(repoUrl, user); | ||
Comment on lines
+105
to
+106
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same point as the above comments here. |
||
|
||
if (isAllowed) { | ||
resolve(true); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,11 @@ exports.getRepo = async (name) => { | |
return collection.findOne({ name: { $eq: name } }); | ||
}; | ||
|
||
exports.getRepoByUrl = async (url) => { | ||
const collection = await connect(cnName); | ||
return collection.findOne({ url: { $eq: url}}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Thoughts? We should probably step back a little and define a data model for repository together that will serve us well moving forward and isn't too opinionated. I am not sure about URL as only as this is quite un-atomic in its nature and includes content that we don't need in DB, i.e. |
||
} | ||
|
||
exports.createRepo = async (repo) => { | ||
console.log(`creating new repo ${JSON.stringify(repo)}`); | ||
|
||
|
@@ -67,10 +72,9 @@ exports.deleteRepo = async (name) => { | |
await collection.deleteMany({ name: name }); | ||
}; | ||
|
||
exports.isUserPushAllowed = async (name, user) => { | ||
name = name.toLowerCase(); | ||
exports.isUserPushAllowed = async (url, user) => { | ||
return new Promise(async (resolve, reject) => { | ||
const repo = await exports.getRepo(name); | ||
const repo = await exports.getRepoByUrl(url); | ||
console.log(repo.users.canPush); | ||
console.log(repo.users.canAuthorise); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Regex inspector - https://www.debuggex.com/ | ||
// eslint-disable-next-line no-useless-escape | ||
const GIT_URL_REGEX = new RegExp("^(https)://(github\.com|gitlab\.com)/([a-zA-Z0-9\-\.]+)/([a-zA-Z0-9\-]+)(\.git)(/)?$"); | ||
|
||
|
||
/** Class representing a Repo. */ | ||
class Repo { | ||
url; | ||
protocol; | ||
host; | ||
project; | ||
name; | ||
|
||
/** | ||
* | ||
* @param {string} url The url for the repo. | ||
*/ | ||
constructor(url) { | ||
const parsedUrl = url?.match(GIT_URL_REGEX); | ||
if (parsedUrl) { | ||
this.url = url; | ||
this.protocol = parsedUrl[1]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this required? |
||
this.host = parsedUrl[2]; | ||
this.project = parsedUrl[3]; | ||
this.name = parsedUrl[4] + parsedUrl[5]; // repo name + .git | ||
return; | ||
} | ||
throw new Error(`Invalid repo url: "${url}"`); | ||
} | ||
} | ||
|
||
exports.Repo = Repo; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exports.Repo = require('./Repo').Repo; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
/** Class representing a Push. */ | ||
const config = require('../../config'); | ||
|
||
/** | ||
* Create a new action | ||
|
@@ -35,9 +34,9 @@ class Action { | |
this.type = type; | ||
this.method = method; | ||
this.timestamp = timestamp; | ||
this.project = repo.split('/')[0]; | ||
this.repoName = repo.split('/')[1]; | ||
this.url = `${config.getProxyUrl()}/${repo}`; | ||
this.project = repo.project; | ||
this.repoName = repo.name; | ||
this.url = repo.url; | ||
Comment on lines
+37
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice 👍 |
||
this.repo = repo; | ||
} | ||
|
||
|
@@ -97,7 +96,7 @@ class Action { | |
} | ||
|
||
/** | ||
*` | ||
* | ||
*/ | ||
setAllowPush() { | ||
this.allowPush = true; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have type
url
?