Skip to content

Commit

Permalink
Add groups in the config (#11)
Browse files Browse the repository at this point in the history
* add groups in config

* new version

* packages

* review fixes

* fix readme
  • Loading branch information
tysky authored Dec 13, 2021
1 parent 9c2694f commit f61289a
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 296 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,17 @@ messenger:
# - "markdown" (for Mattermost)
# - "slack" (for Slack)
sender:
username: "@ubmrellio/gbot" # Sender's display name
username: "@umbrellio/gbot" # Sender's display name
icon: "<icon url>" # Sender's icon url
gitlab:
token: "<TOKEN>" # GitLab Private Access Token
url: "<gitlab api url>" # Gitlab API base url
projects: # List of your project ids
projects: # List of your project ids (optional if groups are defined)
- 42
groups: # List of your project’s groups (optional if projects are defined)
- id: 4 # Group id
excluded: [1, 2, 3] # List of projects to exclude from the current group projects (optional)
- id: 5

# tasks config
unapproved: # Config for `unapproved` command
Expand All @@ -68,6 +72,8 @@ unapproved: # Config for `unapproved` command
diffs: false # Show changed lines count or not (default - false)
```
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.
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/umbrellio/gbot.
Expand Down
7 changes: 6 additions & 1 deletion api/GitLab.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class GitLab {
constructor ({ gitlab }) {
this.baseUrl = gitlab.url
this.token = gitlab.token
this.projects = gitlab.projects
}

approvals = (project, request) => {
Expand Down Expand Up @@ -33,6 +32,12 @@ class GitLab {
return this.__getPaginated(uri, query)
}

groupProjects = group => {
const uri = this.__getUrl("groups", group, "projects")
return this.__getPaginated(uri)
.then(projects => projects.map(project => project.id))
}

discussions = (project, request) => {
const query = { page: 1, per_page: 100 }
const uri = this.__getUrl("projects", project, "merge_requests", request, "discussions")
Expand Down
8 changes: 8 additions & 0 deletions gbot.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ gitlab:
url: "<gitlab api url>"
projects:
- 42
- 43
- 44
groups:
- id: 4
excluded: [1, 2, 3]
- id: 5
excluded: [11, 12]
- id: 6

# tasks config
unapproved:
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umbrellio/gbot",
"version": "1.5.0",
"version": "1.6.0",
"bin": "gbot",
"repository": "[email protected]:umbrellio/gbot.git",
"author": "Aleksei Bespalov <[email protected]>",
Expand All @@ -14,16 +14,16 @@
"js-yaml": "^4.1.0",
"lodash": "^4.17.21",
"winston": "^3.3.3",
"yargs": "^17.2.1"
"yargs": "^17.3.0"
},
"devDependencies": {
"@babel/core": "^7.14.6",
"@babel/eslint-parser": "^7.16.0",
"eslint": "^8.1.0",
"@babel/eslint-parser": "^7.16.3",
"eslint": "^8.4.1",
"eslint-config-umbrellio": "^5.0.1",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prefer-object-spread": "^1.2.1",
"eslint-plugin-promise": "^5.1.1"
"eslint-plugin-promise": "^5.2.0"
}
}
23 changes: 21 additions & 2 deletions tasks/BaseCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,32 @@ class BaseCommand {
this.logger = logger
this.gitlab = new GitLab(config)
this.messenger = new Messenger(config)

this.projects = _.get(config, "gitlab.projects")
}

perform = () => {
throw new Error("Not implemented")
}

get projects () {
const configProjects = _.get(this.config, "gitlab.projects", [])
const groups = _.get(this.config, "gitlab.groups")

if (_.isEmpty(configProjects) && _.isEmpty(groups)) {
throw new Error("You should provide projects or groups in your config")
}

if (_.isEmpty(groups)) return Promise.resolve(configProjects)

const promises = groups.map(group => {
return this.gitlab.groupProjects(group.id).then(groupProjects => {
const excludeProjects = group.excluded || []
return groupProjects.filter(p => !excludeProjects.includes(p))
})
})

return Promise.all(promises)
.then(projects => _.uniq([...configProjects, ...projects.flat()]))
}
}

module.exports = BaseCommand
10 changes: 5 additions & 5 deletions tasks/Unapproved.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ const { NetworkError } = require("../utils/errors")

class Unapproved extends BaseCommand {
perform = () => {
const promises = this.projects.map(this.__getUnapprovedRequests)

return Promise.all(promises)
.then(_.flatten)
.then(requests => requests.sort((a, b) => new Date(a.updated_at) - new Date(b.updated_at)))
return this.projects
.then(projects => Promise.all(projects.map(this.__getUnapprovedRequests)))
.then(requests => {
return requests.flat().sort((a, b) => new Date(a.updated_at) - new Date(b.updated_at))
})
.then(this.__buildMessage)
.then(message => {
this.logger.info("Sending message")
Expand Down
Loading

0 comments on commit f61289a

Please sign in to comment.