Skip to content

Commit c6e003e

Browse files
committed
feat: add feature to move permissions for every users in very repos (#197)
Closes #197 Signed-off-by: Pierre-Yves Lapersonne <pierreyves.lapersonne@orange.com>
1 parent 1ab5ec9 commit c6e003e

File tree

9 files changed

+92
-12
lines changed

9 files changed

+92
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- Feature to change permission for everyone absed on other permission (Orange-OpenSource/floss-toolbox#197)
1213
- GitLab Python script to update permissions
1314

1415
### Changed

toolbox/github/Gemfile.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ GEM
2222

2323
PLATFORMS
2424
x86_64-darwin-21
25+
x86_64-darwin-23
2526

2627
DEPENDENCIES
2728
git (~> 1.18)

toolbox/github/GitHubWizard.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
# Description.........: Received from arguments a feature to launch and gives it to the Ruby wizard.
1515
# Parses the RUBY_CONFIGURATION_FILE to get the GitHub personal acces token to set as Ruby env. variable (OCTOKIT_ACCESS_TOKEN).
1616

17-
#set -euxo pipefail
18-
VERSION="1.5.0"
17+
set -euo pipefail # set -euxo pipefail
18+
19+
VERSION="1.6.0"
1920

2021
# Common files
2122
# ------------
@@ -55,6 +56,7 @@ UsageAndExit(){
5556
echo -e "\t set-users-permissions-to-push..................: For all projects update each user permission to 'push' except for teams and administrators"
5657
echo -e "\t set-teams-permissions-to-push..................: For all projects update each team permission to 'push'"
5758
echo -e "\t set-teams-permissions-to-read..................: For all projects update each team permission to 'read'"
59+
echo -e "\t downgrade-user-permissions-if-admin............: For all projects update each suer from 'admin' to 'maintain''"
5860
echo -e "\t backup-all-repositories-from-org...............: Dump all repositories in GitHub to a specific location in the disk"
5961
echo -e "\t vulnerabilities-alerts-for-all-repositories....: Check if there are vulnerabilities alerts in repositories of the defined organisation"
6062
echo -e "\t look-for-leaks.................................: Checks with gitleaks if there are leaks in all repositories"
@@ -90,7 +92,7 @@ if [ -z "$feature_to_run" ]; then
9092
fi
9193

9294
# TODO: Refactor this line. Some day. I have a very big screen. Haven't you?
93-
if [ $feature_to_run != "get-members-2fa-disabled" -a $feature_to_run != "get-all-members" -a $feature_to_run != "get-members-without-company" -a $feature_to_run != "get-projects-without-team" -a $feature_to_run != "get-users-with-bad-email" -a $feature_to_run != "get-users-with-bad-fullname" -a $feature_to_run != "get-projects-conformity" -a $feature_to_run != "get-projects-without-licenses" -a $feature_to_run != "get-empty-projects" -a $feature_to_run != "set-users-permissions-to-push" -a $feature_to_run != "set-teams-permissions-to-push" -a $feature_to_run != "set-teams-permissions-to-read" -a $feature_to_run != "backup-all-repositories-from-org" -a $feature_to_run != "vulnerabilities-alerts-for-all-repositories" -a $feature_to_run != "look-for-leaks" ]; then
95+
if [ $feature_to_run != "get-members-2fa-disabled" -a $feature_to_run != "get-all-members" -a $feature_to_run != "get-members-without-company" -a $feature_to_run != "get-projects-without-team" -a $feature_to_run != "get-users-with-bad-email" -a $feature_to_run != "get-users-with-bad-fullname" -a $feature_to_run != "get-projects-conformity" -a $feature_to_run != "get-projects-without-licenses" -a $feature_to_run != "get-empty-projects" -a $feature_to_run != "set-users-permissions-to-push" -a $feature_to_run != "set-teams-permissions-to-push" -a $feature_to_run != "set-teams-permissions-to-read" -a $feature_to_run != "downgrade-user-permissions-if-admin" -a $feature_to_run != "backup-all-repositories-from-org" -a $feature_to_run != "vulnerabilities-alerts-for-all-repositories" -a $feature_to_run != "look-for-leaks" ]; then
9496
echo "ERROR: '$feature_to_run' is unknown feature. Exit now"
9597
UsageAndExit
9698
exit $EXIT_UNKNOWN_FEATURE

toolbox/github/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Table of Contents
1818
* [Get repositories which seems to be unconform (i.e. missing files)](#get-repositories-which-seems-to-be-unconform-ie-missing-files)
1919
* [Get repositories which seems to be empty or have not enough files](#get-repositories-which-seems-to-be-empty-or-have-not-enough-files)
2020
* [Define users permissions for all projects to "push"](#define-users-permissions-for-all-projects-to-push)
21+
* [Downgrade users permissions for all projects from admin to maintain](#downgrade-users-permissions-for-all-projects-from-admin-to-maintain)
2122
* [Define teams permissions for all projects to "push"](#define-teams-permissions-for-all-projects-to-push)
2223
* [Make a year review of the GitHub organization](#make-a-year-review-of-the-github-organization)
2324
* [Play with GitHub CLI (GH)](#play-with-github-cli-gh)
@@ -187,6 +188,17 @@ Permissions will be set to "push", i.e. "write".
187188
bash GitHubWizard.sh set-users-permissions-to-push
188189
```
189190

191+
### Downgrade users permissions for all projects from admin to maintain
192+
193+
_Keywords: #organisation #GitHub #permissions #members #admin #maintain_
194+
195+
Run the following command to update rights of all users except GitHub teams and GitHub administrators, for all projects.
196+
Permissions will be set to "maintain" if before "admin".
197+
198+
```shell
199+
bash GitHubWizard.sh downgrade-user-permissions-if-admin
200+
```
201+
190202
### Define teams permissions for all projects to "push"
191203

192204
_Keywords: #organisation #GitHub #permissions #teams #push_

toolbox/github/configuration.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# Software description: A toolbox of scripts to help work of forges admins and open source referents
1111

1212
# Allow debug message or not
13-
$LOG_DEBUG = false
13+
$LOG_DEBUG = true
1414

1515
# GitHub organization
1616
# -------------------
@@ -19,7 +19,7 @@
1919
$GITHUB_PERSONAL_ACCESS_TOKEN = ""
2020

2121
# Name of the organization name you want to deal with so as to request the GitHub API
22-
$GITHUB_ORGANIZATION_NAME = ""
22+
$GITHUB_ORGANIZATION_NAME = "Orange-OpenSource"
2323

2424
# Accurate list of users which are admins (i.e. GitHub organization owners), with login separated by ;
2525
$GITHUB_ORGANIZATION_ADMINS = ""
@@ -39,8 +39,8 @@
3939
# Results returned in one page (GitHub pagination), max 100
4040
$RESULTS_PER_PAGE = 100
4141

42-
# Expected numer of pages to use to load all elements (i.e. if 250 projects, we need 3 pages with 100 items per page)
43-
$EXPECTED_PAGE_COUNT = 3
42+
# Expected numer of pages to use to load all elements (i.e. if 398 projects, we need 4 pages with 100 items per page)
43+
$EXPECTED_PAGE_COUNT = 5
4444

4545
# Result files
4646
# ------------

toolbox/github/utils/GitHubFacade.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# Authors: See CONTRIBUTORS.txt
1111
# Software description: A toolbox of scripts to help work of forges admins and open source referents
1212

13-
# Version.............: 1.2.0
13+
# Version.............: 1.3.0
1414
# Since...............: 26/04/2021
1515
# Description.........: Received from arguments a feature to launch using GitHub API.
1616
# Loads configuration elements from configuration.rb, writes outputs using FileManager.rb and calls GitHubWrapper.rb for requests
@@ -54,7 +54,7 @@
5454
end
5555
feature_to_run=ARGV[0]
5656

57-
if feature_to_run != "get-members-2fa-disabled" && feature_to_run != "get-all-members" && feature_to_run != "get-members-without-company" && feature_to_run != "get-projects-without-team" && feature_to_run != "get-users-with-bad-email" && feature_to_run != "get-users-with-bad-fullname" && feature_to_run != "get-projects-conformity" && feature_to_run != "get-projects-without-licenses" && feature_to_run != "get-empty-projects" && feature_to_run != "set-users-permissions-to-push" && feature_to_run != "set-teams-permissions-to-push" && feature_to_run != "set-teams-permissions-to-read"
57+
if feature_to_run != "get-members-2fa-disabled" && feature_to_run != "get-all-members" && feature_to_run != "get-members-without-company" && feature_to_run != "get-projects-without-team" && feature_to_run != "get-users-with-bad-email" && feature_to_run != "get-users-with-bad-fullname" && feature_to_run != "get-projects-conformity" && feature_to_run != "get-projects-without-licenses" && feature_to_run != "get-empty-projects" && feature_to_run != "set-users-permissions-to-push" && feature_to_run != "set-teams-permissions-to-push" && feature_to_run != "set-teams-permissions-to-read" && feature_to_run != "downgrade-user-permissions-if-admin"
5858
Log.error "Unknown feature. Exit now."
5959
exit $EXIT_UNKNOWN_FEATURE
6060
end
@@ -277,4 +277,13 @@
277277
exit $EXIT_OK
278278
end
279279

280+
# FEATURE: downgrade-user-permissions-if-admin
281+
# For all projects, mvoe to 'admin' permission to 'maintain' for each user
282+
if feature_to_run == "downgrade-user-permissions-if-admin"
283+
Log.log "Updating all repositories with permission downgrade from admin to maintain..."
284+
GitHubWrapper.change_permissions_for_users(client, $GITHUB_ORGANIZATION_NAME, "admin", "maintain")
285+
Log.log "Task completed! Exits now."
286+
exit $EXIT_OK
287+
end
288+
280289
exit $EXIT_NO_FEATURE_STARTED

toolbox/github/utils/GitHubWrapper.rb

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,23 @@ def self.add_collaborator_to_repository_with_read_permission(octokit_client, rep
253253
# +repository_full_name+:: The full name identifier of the repository to get, like 'organization/project-name'
254254
# +user_login+:: The login of the user
255255
def self.add_collaborator_to_repository_with_write_permission(octokit_client, repository_full_name, user_login)
256+
return self.add_collaborator_to_repository_with_permission(octokit_client, repository_full_name, user_login, "push")
257+
end
258+
259+
##
260+
# Using the given Octokit client, for the repository which has that name, adds the user who has this login as a collaborator with the fiven permission level.
261+
# If the user was already added, just changes its permission level.
262+
# +octokit_client+:: The Octokit client to use to request the GitHub web API
263+
# +repository_full_name+:: The full name identifier of the repository to get, like 'organization/project-name'
264+
# +user_login+:: The login of the user
265+
# +new_permission+:: The permission for the user
266+
def self.add_collaborator_to_repository_with_permission(octokit_client, repository_full_name, user_login, new_permission)
256267
if octokit_client.nil?
257268
Log.error "Nil Octokit client. Returns now."
258269
return
259270
end
260-
return octokit_client.add_collaborator(repository_full_name, user_login, permission: "push")
271+
Log.debug "For repository '#{repository_full_name}' user named '#{user_login}' has now new permision '#{new_permission}'"
272+
return octokit_client.add_collaborator(repository_full_name, user_login, permission: new_permission)
261273
end
262274

263275
##
@@ -571,6 +583,49 @@ def self.set_permissions_for_teams(octokit_client, organization_name, permission
571583
end
572584
end
573585

586+
##
587+
# For each repository for the given organization, for all users, if the user permission
588+
# is "admin", downgrade to "maintain". Otherwise change nothing.
589+
# Do not change permission for teams and organisation admins.
590+
# +octokit_client+:: The Octokit client to use to request the GitHub web API
591+
# +organization_name+:: The name of the organization to retrieve from API
592+
# +old_permission+:: Must be "push" (write), "pull" (read), "maintain" (maintain) or "admin" (admin)
593+
# +new_permission+:: Must be "push" (write), "pull" (read), "maintain" (maintain) or "admin" (admin)
594+
def self.change_permissions_for_users(octokit_client, organization_name, old_permission, new_permission)
595+
if octokit_client.nil?
596+
Log.error "Nil Octokit client. Returns now."
597+
return
598+
end
599+
if old_permission != "push" && old_permission != "pull" && old_permission != "maintain" && old_permission != "admin"
600+
Log.warning "Old permission #{old_permission} is not managed. Returns now."
601+
return
602+
end
603+
if new_permission != "push" && new_permission != "pull" && new_permission != "maintain" && new_permission != "admin"
604+
Log.warning "New permission #{new_permission} is not managed. Returns now."
605+
return
606+
end
607+
owners = organization_owners
608+
Log.debug "Get all projects of organization '#{organization_name}'..."
609+
all_repositories = get_all_repositories(octokit_client, organization_name)
610+
Log.debug "Found #{all_repositories.length} projects!"
611+
all_repositories.each do |repository|
612+
repo_full_name = repository.full_name
613+
Log.debug "Processing #{repo_full_name}..."
614+
members = get_repository_collaborators(octokit_client, repo_full_name)
615+
members.each do |member|
616+
member_login = member.login
617+
unless owners.include? member_login
618+
if member.role_name == "admin"
619+
Log.debug "Update permision from '#{old_permission}' to '#{new_permission} 'for user with login '#{member_login}' on project '#{repo_full_name}'"
620+
add_collaborator_to_repository_with_permission(octokit_client, repo_full_name, member_login, new_permission)
621+
end
622+
else
623+
Log.debug "User with login '#{member_login}' is organization owner, permissions are not changed for project #{repo_full_name}"
624+
end
625+
end
626+
end
627+
end
628+
574629
private
575630

576631
##

toolbox/utils/text-generator/_templates/new-GitHub-repository-contributors.en.template.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,4 @@ Remaining available,
6969
[13] %RESSOURCES_WEB_GITHUB-SITE%
7070
[14] %RESSOURCE_DOC_CHARTER%
7171

72-
[This email is automatically generated]
72+
[This email is automatically generated]

toolbox/utils/text-generator/_templates/new-GitHub-repository-contributors.fr.template.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,4 @@ Restant à disposition,
7070
[13] %RESSOURCES_WEB_GITHUB-SITE%
7171
[14] %RESSOURCE_DOC_CHARTER%
7272

73-
[Ce courrier electronique est généré automatiquement]
73+
[Ce courrier electronique est généré automatiquement]

0 commit comments

Comments
 (0)