Skip to content

Commit

Permalink
fix: context when calling GitHub teams API (#713)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinmayerhofer committed May 12, 2023
1 parent d9fb002 commit 590a40f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 44 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CHANGELOG
=====================================
| May 12, 2023: fix: Loading teams for team option of author filter/validator `#713 <https://github.com/mergeability/mergeable/pull/713>`_
| May 11, 2023: fix: Send correct payload for changing labels `#715 <https://github.com/mergeability/mergeable/pull/715>`_
| April 25, 2023: feat: Add author validator `#710 <https://github.com/mergeability/mergeable/pull/710>`_
| March 13, 2023: fix: Replace delete with remove in changeset validator `#705 <https://github.com/mergeability/mergeable/pull/705>`_
Expand Down
11 changes: 10 additions & 1 deletion lib/filters/author.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { Filter } = require('./filter')

const Teams = require('../validators/options_processor/teams')
class Author extends Filter {
constructor () {
super('author')
Expand All @@ -24,6 +24,15 @@ class Author extends Filter {

async filter (context, settings) {
const payload = this.getPayload(context)

if (settings.team) {
const result = await Teams.processTeamOption(context, settings, payload)
if (result.status !== 'pass') {
return result
}
delete settings.team
}

return this.processOptions(context, payload.user.login, settings)
}
}
Expand Down
21 changes: 0 additions & 21 deletions lib/filters/options_processor/options/team.js

This file was deleted.

11 changes: 10 additions & 1 deletion lib/validators/author.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { Validator } = require('./validator')

const Teams = require('./options_processor/teams')
class Author extends Validator {
constructor () {
super('author')
Expand All @@ -24,6 +24,15 @@ class Author extends Validator {

async validate (context, settings) {
const payload = this.getPayload(context)

if (settings.team) {
const result = await Teams.processTeamOption(context, settings, payload)
if (result.status !== 'pass') {
return result
}
delete settings.team
}

return this.processOptions(settings, payload.user.login)
}
}
Expand Down
21 changes: 0 additions & 21 deletions lib/validators/options_processor/options/team.js

This file was deleted.

22 changes: 22 additions & 0 deletions lib/validators/options_processor/teams.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const _ = require('lodash')
const GithubAPI = require('../../github/api')
const constructOutput = require('./options/lib/constructOutput')
const consolidateResult = require('./options/lib/consolidateResults')

class Teams {
static async extractTeamMembers (context, teams) {
Expand Down Expand Up @@ -29,6 +31,26 @@ class Teams {
}
return _.uniq(teamMembers)
}

static async processTeamOption (context, settings, payload) {
const teamName = settings.team
const userName = payload.user.login

const teamMemberships = await Teams.extractTeamMemberships(context, [teamName], [userName])
const isMember = teamMemberships.includes(userName)
const successMessage = `'${userName}' is part of the '${teamName}' team'`
const failureMessage = `'${userName}' is not part of the '${teamName}' team'`

const output = [
constructOutput(
context, teamMemberships, settings, {
status: isMember ? 'pass' : 'fail',
description: isMember ? successMessage : failureMessage
}, null
)
]
return consolidateResult(output, context)
}
}

const getTeamMembers = async (context, team) => {
Expand Down

0 comments on commit 590a40f

Please sign in to comment.