Skip to content

Commit f61289a

Browse files
authored
Add groups in the config (#11)
* add groups in config * new version * packages * review fixes * fix readme
1 parent 9c2694f commit f61289a

7 files changed

+282
-296
lines changed

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,17 @@ messenger:
4545
# - "markdown" (for Mattermost)
4646
# - "slack" (for Slack)
4747
sender:
48-
username: "@ubmrellio/gbot" # Sender's display name
48+
username: "@umbrellio/gbot" # Sender's display name
4949
icon: "<icon url>" # Sender's icon url
5050
gitlab:
5151
token: "<TOKEN>" # GitLab Private Access Token
5252
url: "<gitlab api url>" # Gitlab API base url
53-
projects: # List of your project ids
53+
projects: # List of your project ids (optional if groups are defined)
5454
- 42
55+
groups: # List of your project’s groups (optional if projects are defined)
56+
- id: 4 # Group id
57+
excluded: [1, 2, 3] # List of projects to exclude from the current group projects (optional)
58+
- id: 5
5559

5660
# tasks config
5761
unapproved: # Config for `unapproved` command
@@ -68,6 +72,8 @@ unapproved: # Config for `unapproved` command
6872
diffs: false # Show changed lines count or not (default - false)
6973
```
7074
75+
Groups in the config are [Gitlab project groups](https://docs.gitlab.com/ee/user/group/). You must specify the group or the project, or both.
76+
7177
## Contributing
7278
7379
Bug reports and pull requests are welcome on GitHub at https://github.com/umbrellio/gbot.

api/GitLab.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ class GitLab {
55
constructor ({ gitlab }) {
66
this.baseUrl = gitlab.url
77
this.token = gitlab.token
8-
this.projects = gitlab.projects
98
}
109

1110
approvals = (project, request) => {
@@ -33,6 +32,12 @@ class GitLab {
3332
return this.__getPaginated(uri, query)
3433
}
3534

35+
groupProjects = group => {
36+
const uri = this.__getUrl("groups", group, "projects")
37+
return this.__getPaginated(uri)
38+
.then(projects => projects.map(project => project.id))
39+
}
40+
3641
discussions = (project, request) => {
3742
const query = { page: 1, per_page: 100 }
3843
const uri = this.__getUrl("projects", project, "merge_requests", request, "discussions")

gbot.example.yml

+8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ gitlab:
1010
url: "<gitlab api url>"
1111
projects:
1212
- 42
13+
- 43
14+
- 44
15+
groups:
16+
- id: 4
17+
excluded: [1, 2, 3]
18+
- id: 5
19+
excluded: [11, 12]
20+
- id: 6
1321

1422
# tasks config
1523
unapproved:

package.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@umbrellio/gbot",
3-
"version": "1.5.0",
3+
"version": "1.6.0",
44
"bin": "gbot",
55
"repository": "git@github.com:umbrellio/gbot.git",
66
"author": "Aleksei Bespalov <nulldefiner@gmail.com>",
@@ -14,16 +14,16 @@
1414
"js-yaml": "^4.1.0",
1515
"lodash": "^4.17.21",
1616
"winston": "^3.3.3",
17-
"yargs": "^17.2.1"
17+
"yargs": "^17.3.0"
1818
},
1919
"devDependencies": {
2020
"@babel/core": "^7.14.6",
21-
"@babel/eslint-parser": "^7.16.0",
22-
"eslint": "^8.1.0",
21+
"@babel/eslint-parser": "^7.16.3",
22+
"eslint": "^8.4.1",
2323
"eslint-config-umbrellio": "^5.0.1",
24-
"eslint-plugin-import": "^2.25.2",
24+
"eslint-plugin-import": "^2.25.3",
2525
"eslint-plugin-node": "^11.1.0",
2626
"eslint-plugin-prefer-object-spread": "^1.2.1",
27-
"eslint-plugin-promise": "^5.1.1"
27+
"eslint-plugin-promise": "^5.2.0"
2828
}
2929
}

tasks/BaseCommand.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,32 @@ class BaseCommand {
1111
this.logger = logger
1212
this.gitlab = new GitLab(config)
1313
this.messenger = new Messenger(config)
14-
15-
this.projects = _.get(config, "gitlab.projects")
1614
}
1715

1816
perform = () => {
1917
throw new Error("Not implemented")
2018
}
19+
20+
get projects () {
21+
const configProjects = _.get(this.config, "gitlab.projects", [])
22+
const groups = _.get(this.config, "gitlab.groups")
23+
24+
if (_.isEmpty(configProjects) && _.isEmpty(groups)) {
25+
throw new Error("You should provide projects or groups in your config")
26+
}
27+
28+
if (_.isEmpty(groups)) return Promise.resolve(configProjects)
29+
30+
const promises = groups.map(group => {
31+
return this.gitlab.groupProjects(group.id).then(groupProjects => {
32+
const excludeProjects = group.excluded || []
33+
return groupProjects.filter(p => !excludeProjects.includes(p))
34+
})
35+
})
36+
37+
return Promise.all(promises)
38+
.then(projects => _.uniq([...configProjects, ...projects.flat()]))
39+
}
2140
}
2241

2342
module.exports = BaseCommand

tasks/Unapproved.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ const { NetworkError } = require("../utils/errors")
99

1010
class Unapproved extends BaseCommand {
1111
perform = () => {
12-
const promises = this.projects.map(this.__getUnapprovedRequests)
13-
14-
return Promise.all(promises)
15-
.then(_.flatten)
16-
.then(requests => requests.sort((a, b) => new Date(a.updated_at) - new Date(b.updated_at)))
12+
return this.projects
13+
.then(projects => Promise.all(projects.map(this.__getUnapprovedRequests)))
14+
.then(requests => {
15+
return requests.flat().sort((a, b) => new Date(a.updated_at) - new Date(b.updated_at))
16+
})
1717
.then(this.__buildMessage)
1818
.then(message => {
1919
this.logger.info("Sending message")

0 commit comments

Comments
 (0)