Skip to content
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

feat(collaborators): listen to member changes #382

Merged
merged 3 commits into from
Feb 14, 2023

Conversation

anderssonjohan
Copy link
Contributor

@anderssonjohan anderssonjohan commented Jan 24, 2023

part of fixes #251

Add listeners for repository collaborator events:

  • member
  • team

Synchronizes settings when users or teams are added, removed, and when permissions for a member is changed.

Also includes minor fix to index.test.js to make the tests run.

Tasks

  • add new listeners with tests
  • remove excess members

@anderssonjohan
Copy link
Contributor Author

ping @decyjphr @martinm82

@decyjphr
Copy link
Collaborator

@anderssonjohan will work on it tomorrow. Thanks for the ping.

Copy link
Contributor

@martinm82 martinm82 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@martinm82
Copy link
Contributor

Should we update as well the README?

this.context = context
this.ref = ref
this.log = log
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be possible to use the logger through the context?

Suggested change
this.log = log
this.log = context.log

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, great! I wasn't aware of that. I added this because of null references for this.log when trying to run the tests in index.test.js. I'll push a change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, good catch, I obviously did not update all the places where ConfigManager were created in index.js! Fixup commit pushed (will rebase when review is done)

@anderssonjohan
Copy link
Contributor Author

I just updated the PR description because these changes currently do not fix #251. It syncs memberships defined as policy, but the collaborators plugin does currently not remove any manually added members (users/teams). Thus, this PR should not auto-close the related issue until that part is added.

@anderssonjohan anderssonjohan marked this pull request as draft January 24, 2023 16:47
@anderssonjohan
Copy link
Contributor Author

@decyjphr @martinm82 I found the issue with members not being removed. In my config I have only teams and no collaborators 😅 . So for starters, I need to add collaborators: [] to the config to enable the collaborators plugin so the code that can remove excess members is called (Collaborators.remove via Collaborators.sync). But with an empty array we never get hasChanges = true from compareDeep, because the loop over source has zero iterations when it's empty.
Thus, to remove manually added collaborators you need to configure a collaborator to enter that loop.

I'll add a test that verifies that compareDeep returns hasChanges: true when source (config object) is empty but target (entries from GitHub response) is not.

Example debugging session where I added manually added my github account to the repo with config set to collaborators: []. As you can see, the loop won't run.

Screenshot 2023-01-24 at 21 51 00

This also means that users can enable this behavior (removal of manually added collaborators) by activating the plugin. If the want the collaborators to stay there is an option like in my previous config where I didn't use that plugin at all.

Add listeners for repository collaborator events:

- member
- team

Synchronizes settings when users or teams are added, removed, and when
permissions for a member is changed.
Fix for empty collaborators config not removing excess members.
Using config `collaborators: []` this fix makes sure that manually
added collaborators are removed.
@anderssonjohan anderssonjohan marked this pull request as ready for review January 24, 2023 21:57
@anderssonjohan
Copy link
Contributor Author

@decyjphr @martinm82 Now the PR is complete in terms of fixing #251.

A small note on the fix for compareDeep: I first made a version where the source and target were swapped if the source was empty. In that way the recursive call would loop through the target instead and produce the same diff as in a case where GitHub "is empty" and config is not. That, however, made one test fail for compareDeep so my commit only ensures that hasChanges is true (additions and modifications are both empty) so the code that removes the unwanted collaborators is reached.

What we had before:

compareDeep({x:1}, {}) => { hasChanges: true, additions: [ {x:1} ], modifications: [] }
compareDeep({}, {x:1}) => { hasChanges: false, additions: [], modifications: [] }

What we have now is:

compareDeep({x:1}, {}) => { hasChanges: true, additions: [ {x:1} ], modifications: [] }
compareDeep({}, {x:1}) => { hasChanges: true, additions: [], modifications: [] }

I even started to look at lodash differenceWith to see if it could be used with a comparator function that would compare by value and username/name attributes, but that's a much larger change that I didn't dare to look into 😅
I hope you find this small change to compareDeep reasonable.

@anderssonjohan
Copy link
Contributor Author

I think my fixes here for empty list of collaborators also fixes the problem described by #301

Copy link
Collaborator

@decyjphr decyjphr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. 🚢

@decyjphr decyjphr merged commit b6d0029 into github:main-enterprise Feb 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Members of teams are not synced
3 participants