|
| 1 | +import hudson.security.SecurityRealm |
| 2 | +import org.jenkinsci.plugins.GithubSecurityRealm |
| 3 | +import org.jenkinsci.plugins.GithubAuthorizationStrategy |
| 4 | +import hudson.security.AuthorizationStrategy |
| 5 | + |
| 6 | +String githubWebUri = '<%= p('jenkins.github.oauth.url.web') %>' |
| 7 | +String githubApiUri = '<%= p('jenkins.github.oauth.url.api') %>' |
| 8 | +String clientID = '<%= p('jenkins.github.oauth.client.id') %>' |
| 9 | +String clientSecret = '<%= p('jenkins.github.oauth.client.secret') %>' |
| 10 | +String oauthScopes = 'read:org' |
| 11 | + |
| 12 | +SecurityRealm github_realm = new GithubSecurityRealm(githubWebUri, githubApiUri, clientID, clientSecret, oauthScopes) |
| 13 | +//check for equality, no need to modify the runtime if no settings changed |
| 14 | +if(!github_realm.equals(Jenkins.instance.getSecurityRealm())) { |
| 15 | + Jenkins.instance.setSecurityRealm(github_realm) |
| 16 | + Jenkins.instance.save() |
| 17 | +} |
| 18 | + |
| 19 | +String adminUserNames = '<%= p('jenkins.github.oauth.admins.admin1') %>,<%= p('jenkins.github.oauth.admins.admin2') %>,<%= p('jenkins.github.oauth.admins.admin3') %>' |
| 20 | +String organizationNames = '<%= p('jenkins.github.oauth.org_name') %>' |
| 21 | +boolean useRepositoryPermissions = true |
| 22 | +boolean authenticatedUserReadPermission = false |
| 23 | +boolean authenticatedUserCreateJobPermission = false |
| 24 | +boolean allowGithubWebHookPermission = false |
| 25 | +boolean allowCcTrayPermission = false |
| 26 | +boolean allowAnonymousReadPermission = false |
| 27 | +boolean allowAnonymousJobStatusPermission = false |
| 28 | + |
| 29 | +AuthorizationStrategy github_authorization = new GithubAuthorizationStrategy(adminUserNames, |
| 30 | + authenticatedUserReadPermission, |
| 31 | + useRepositoryPermissions, |
| 32 | + authenticatedUserCreateJobPermission, |
| 33 | + organizationNames, |
| 34 | + allowGithubWebHookPermission, |
| 35 | + allowCcTrayPermission, |
| 36 | + allowAnonymousReadPermission, |
| 37 | + allowAnonymousJobStatusPermission) |
| 38 | + |
| 39 | +//check for equality, no need to modify the runtime if no settings changed |
| 40 | +if(!github_authorization.equals(Jenkins.instance.getAuthorizationStrategy())) { |
| 41 | + Jenkins.instance.setAuthorizationStrategy(github_authorization) |
| 42 | + Jenkins.instance.save() |
| 43 | +} |
0 commit comments