Skip to content

Commit e7f0473

Browse files
authored
Add paths to projects configuration (#12)
* Now it is possible to list project paths that needs to be changed
1 parent 647a126 commit e7f0473

File tree

5 files changed

+54
-18
lines changed

5 files changed

+54
-18
lines changed

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,15 @@ messenger:
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 (optional if groups are defined)
54-
- 42
5553
groups: # List of your project’s groups (optional if projects are defined)
5654
- id: 4 # Group id
5755
excluded: [1, 2, 3] # List of projects to exclude from the current group projects (optional)
5856
- id: 5
57+
projects: # List of your project (optional if groups are defined)
58+
- id: 42 # Project id
59+
paths: # List of paths that should be changed in merge requests
60+
- src/**/*
61+
- id: 43
5962

6063
# tasks config
6164
unapproved: # Config for `unapproved` command

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@umbrellio/gbot",
3-
"version": "1.6.0",
3+
"version": "1.7.0",
44
"bin": "gbot",
55
"repository": "git@github.com:umbrellio/gbot.git",
66
"author": "Aleksei Bespalov <nulldefiner@gmail.com>",
@@ -13,6 +13,7 @@
1313
"dependencies": {
1414
"js-yaml": "^4.1.0",
1515
"lodash": "^4.17.21",
16+
"minimatch": "^5.1.0",
1617
"winston": "^3.3.3",
1718
"yargs": "^17.3.0"
1819
},

tasks/BaseCommand.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,27 @@ class BaseCommand {
1919

2020
get projects () {
2121
const configProjects = _.get(this.config, "gitlab.projects", [])
22-
const groups = _.get(this.config, "gitlab.groups")
22+
const groups = _.get(this.config, "gitlab.groups", [])
2323

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

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

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-
})
30+
const promises = groups.map(({ id, excluded = [] }) => (
31+
this.gitlab.groupProjects(id).then(groupProjects => (
32+
groupProjects.filter(p => !excluded.includes(p))
33+
))
34+
))
3635

37-
return Promise.all(promises)
38-
.then(projects => _.uniq([...configProjects, ...projects.flat()]))
36+
const mergeProjects = _.flow([
37+
groupProjects => groupProjects.flat().map(id => ({ id })),
38+
groupProjects => [...configProjects, ...groupProjects],
39+
totalProjects => _.uniqBy(totalProjects, ({ id }) => id),
40+
])
41+
42+
return Promise.all(promises).then(mergeProjects)
3943
}
4044
}
4145

tasks/Unapproved.js

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const _ = require("lodash")
2+
const minimatch = require("minimatch")
23

34
const BaseCommand = require("./BaseCommand")
45
const UnapprovedRequestDescription = require("./unapproved/UnapprovedRequestDescription")
@@ -42,16 +43,15 @@ class Unapproved extends BaseCommand {
4243
const header = markup.makeHeader(headText)
4344
const bodyParts = markup.flatten(list)
4445

45-
const message = markup.composeMsg(header, bodyParts)
46-
return message
46+
return markup.composeMsg(header, bodyParts)
4747
} else {
4848
const headText = "Hey, there is a couple of nothing"
4949
const bodyText = "There are no pending requests! Let's do a new one!"
5050

5151
const header = markup.makeHeader(headText)
5252
const body = markup.makePrimaryInfo(markup.makeText(bodyText))
53-
const message = markup.composeMsg(header, body)
54-
return message
53+
54+
return markup.composeMsg(header, body)
5555
}
5656
}
5757

@@ -61,16 +61,30 @@ class Unapproved extends BaseCommand {
6161
return descriptionBuilder.build()
6262
}
6363

64-
__getUnapprovedRequests = projectId => this.__getExtendedRequests(projectId)
64+
__getUnapprovedRequests = project => this.__getExtendedRequests(project.id)
6565
.then(requests => requests.filter(req => {
6666
const isCompleted = !req.work_in_progress
6767
const isUnapproved = req.approvals_left > 0
6868
const hasUnresolvedDiscussions = req.discussions.some(dis => {
6969
return dis.notes.some(note => note.resolvable && !note.resolved)
7070
})
71-
return isCompleted && (isUnapproved || hasUnresolvedDiscussions)
71+
const hasPathsChanges = this.__hasPathsChanges(req.changes, project.paths)
72+
73+
return isCompleted && hasPathsChanges && (isUnapproved || hasUnresolvedDiscussions)
7274
}))
7375

76+
__hasPathsChanges = (changes, paths) => {
77+
if (_.isEmpty(paths)) {
78+
return true
79+
}
80+
81+
return changes.some(change => (
82+
paths.some(path => (
83+
minimatch(change.old_path, path) || minimatch(change.new_path, path)
84+
))
85+
))
86+
}
87+
7488
__getExtendedRequests = projectId => this.gitlab
7589
.project(projectId)
7690
.then(project => this.gitlab.requests(project.id).then(requests => {

yarn.lock

+14
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,13 @@ brace-expansion@^1.1.7:
340340
balanced-match "^1.0.0"
341341
concat-map "0.0.1"
342342

343+
brace-expansion@^2.0.1:
344+
version "2.0.1"
345+
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-2.0.1.tgz#1edc459e0f0c548486ecf9fc99f2221364b9a0ae"
346+
integrity sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==
347+
dependencies:
348+
balanced-match "^1.0.0"
349+
343350
browserslist@^4.17.5:
344351
version "4.18.1"
345352
resolved "https://registry.yarnpkg.com/browserslist/-/browserslist-4.18.1.tgz#60d3920f25b6860eb917c6c7b185576f4d8b017f"
@@ -1209,6 +1216,13 @@ minimatch@^3.0.4:
12091216
dependencies:
12101217
brace-expansion "^1.1.7"
12111218

1219+
minimatch@^5.1.0:
1220+
version "5.1.0"
1221+
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-5.1.0.tgz#1717b464f4971b144f6aabe8f2d0b8e4511e09c7"
1222+
integrity sha512-9TPBGGak4nHfGZsPBohm9AWg6NoT7QTCehS3BIJABslyZbzxfV78QM2Y6+i741OPZIafFAaiiEMh5OyIrJPgtg==
1223+
dependencies:
1224+
brace-expansion "^2.0.1"
1225+
12121226
minimist@^1.2.0, minimist@^1.2.5:
12131227
version "1.2.5"
12141228
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.5.tgz#67d66014b66a6a8aaa0c083c5fd58df4e4e97602"

0 commit comments

Comments
 (0)