Skip to content

Commit

Permalink
added ignore and ldap lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
garethahealy committed Feb 14, 2022
1 parent c4b4681 commit 0ec5d2a
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 7 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# github-stats
Generates a CSV for a Git Hub org

# CollectStatsService
## Run
As this is a simple mvn project, just:
```
Expand All @@ -14,4 +15,17 @@ Once the build is complete, you can view the CSV:
```
ls -lh target/github-output.csv
open target/github-output.csv
```

# CreateWhoAreYouIssueService
## Download Responses
The current form responses can be excluded from the issue creation by downloading and putting in the root directory:
- GitHub Red Hat CoP Members (Responses) - Form Responses 1.csv

## Run
As this is a simple mvn project, just:
```
export GITHUB_LOGIN=replace
export GITHUB_OAUTH=replace
mvn clean install -Dtest=CreateWhoAreYouIssueServiceTest
```
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@
<artifactId>log4j-jul</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>org.apache.directory.api</groupId>
<artifactId>api-all</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
package com.garethahealy.githubstats.rest.client;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.apache.directory.api.ldap.model.cursor.EntryCursor;
import org.apache.directory.api.ldap.model.entry.Entry;
import org.apache.directory.api.ldap.model.exception.LdapException;
import org.apache.directory.api.ldap.model.message.SearchScope;
import org.apache.directory.api.ldap.model.name.Dn;
import org.apache.directory.ldap.client.api.LdapConnection;
import org.apache.directory.ldap.client.api.LdapNetworkConnection;
import org.apache.directory.ldap.client.api.exception.InvalidConnectionException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.kohsuke.github.GHIssueBuilder;
import org.kohsuke.github.GHOrganization;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHUser;
Expand All @@ -18,8 +34,9 @@
public class CreateWhoAreYouIssueService {

private static final Logger logger = LogManager.getLogger(CreateWhoAreYouIssueService.class);
private final boolean isDryRun = true;

public void run() throws IOException {
public void run() throws IOException, LdapException {
logger.info("Starting...");

Cache cache = new Cache(new File("/tmp/github-okhttp"), 10 * 1024 * 1024); // 10MB cache
Expand Down Expand Up @@ -48,20 +65,98 @@ public void run() throws IOException {

logger.info("There are {} members", members.size());

Set<String> usernamesToIgnore = getUsernamesToIgnore();

logger.info("There are {} members we already have emails for who will be ignored", usernamesToIgnore.size());

for (GHUser current : members) {
if (current.getLogin().equalsIgnoreCase("garethahealy")) {
orgRepo.createIssue("@" + current.getLogin() + " please complete form")
if (usernamesToIgnore.contains(current.getLogin())) {
logger.info("Ignoring: {}", current.getLogin());
} else {
GHIssueBuilder builder = orgRepo.createIssue("@" + current.getLogin() + " please complete form")
.assignee(current)
.label("admin")
.body("To be a member of the Red Hat CoP GitHub org, you are required to be a Red Hat employee. " +
"Non-employees are invited to be outside-collaborators (https://github.com/orgs/redhat-cop/outside-collaborators). " +
"As we currently do not know who is an employee and who is not, we are requiring all members to submit the following google form " +
"so that we can verify who are employees: " +
"https://red.ht/github-redhat-cop-username")
.create();
"https://red.ht/github-redhat-cop-username");

if (isDryRun) {
logger.info("DRY-RUN: Would have created issue for {}", current.getLogin());
} else {
builder.create();

logger.info("Created issue for {}", current.getLogin());
}
}
}

logger.info("Issues DONE");

Set<String> membersLogins = getMembersLogins(members);
for (String current : usernamesToIgnore) {
if (!membersLogins.contains(current)) {
logger.info("Have a google form response but they are not part the git hub org anymore for {}", current);
}
}

logger.info("Responses to GH Org Lookup DONE");

//ldapsearch -x -h ldap.corp.redhat.com -b dc=redhat,dc=com -s sub 'uid=gahealy'
Dn systemDn = new Dn("dc=redhat,dc=com");
try (LdapConnection connection = new LdapNetworkConnection("ldap.corp.redhat.com")) {
for (String current : getCollectedEmails()) {
String uid = current.split("@")[0];
try (EntryCursor cursor = connection.search(systemDn, "(uid=" + uid + ")", SearchScope.SUBTREE)) {
boolean found = false;
for (Entry entry : cursor) {
entry.getDn();
found = true;
}

if (!found) {
logger.info("Did not find {} in ldap", uid);
}
}
}
} catch (InvalidConnectionException ex) {
logger.error("Unable to search ldap for users. Are you on the VPN?", ex);
}

logger.info("Ldap Lookup DONE");
}

private Set<String> getUsernamesToIgnore() throws IOException {
Set<String> answer = new HashSet<>();
try (Reader in = new FileReader("GitHub Red Hat CoP Members (Responses) - Form Responses 1.csv")) {
Iterable<CSVRecord> records = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(in);
for (CSVRecord record : records) {
answer.add(record.get("What is your GitHub username?"));
}
}

return answer;
}

logger.info("Created issue for {}", current.getLogin());
private List<String> getCollectedEmails() throws IOException {
List<String> answer = new ArrayList<>();
try (Reader in = new FileReader("GitHub Red Hat CoP Members (Responses) - Form Responses 1.csv")) {
Iterable<CSVRecord> records = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(in);
for (CSVRecord record : records) {
answer.add(record.get("Email Address"));
}
}

return answer;
}

private Set<String> getMembersLogins(List<GHUser> members) {
Set<String> answer = new HashSet<>();
for (GHUser current : members) {
answer.add(current.getLogin());
}

return answer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;

import org.apache.directory.api.ldap.model.exception.LdapException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Assertions;
Expand All @@ -12,7 +13,7 @@ class CreateWhoAreYouIssueServiceTest {
private static final Logger logger = LogManager.getLogger(CreateWhoAreYouIssueServiceTest.class);

@Test
public void canRun() throws IOException {
public void canRun() throws IOException, LdapException {
logger.info("Running.");

CreateWhoAreYouIssueService service = new CreateWhoAreYouIssueService();
Expand Down

0 comments on commit 0ec5d2a

Please sign in to comment.