Skip to content

Commit

Permalink
updated CreateWhoAreYouIssue to be abit cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
garethahealy committed Jan 8, 2024
1 parent e4e739f commit 74ff585
Show file tree
Hide file tree
Showing 17 changed files with 201 additions and 186 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ jobs:
env:
GITHUB_LOGIN: ${{ github.repository_owner }}
GITHUB_OAUTH: ${{ secrets.RHUKI_READ_PAT }}
run: ./github-stats-*-runner create-who-are-you-issues --dry-run=true --organization=RedHat-Consulting-UK --issue-repo=helm3 --members-csv=tests/members.csv --fail-if-no-vpn=false
run: ./github-stats-*-runner create-who-are-you-issues --dry-run=true --organization=RedHat-Consulting-UK --issue-repo=helm3 --members-csv=tests/members.csv --supplementary-csv=tests/supplementary.csv --permission=write

sign-image:
needs: [ build ]
Expand All @@ -170,7 +170,7 @@ jobs:
cosign sign --yes ${image_uri}
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@91713af97dc80187565512baba96e4364e983601 # 0.16.0
uses: aquasecurity/trivy-action@d43c1f16c00cfd3978dde6c07f4bbcf9eb6993ca # 0.16.1
env:
TRIVY_USERNAME: ${{ github.repository_owner }}
TRIVY_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -181,7 +181,7 @@ jobs:
output: "cosign-vuln.json"

- name: Run Trivy SBOM generator
uses: aquasecurity/trivy-action@91713af97dc80187565512baba96e4364e983601 # 0.16.0
uses: aquasecurity/trivy-action@d43c1f16c00cfd3978dde6c07f4bbcf9eb6993ca # 0.16.1
env:
TRIVY_USERNAME: ${{ github.repository_owner }}
TRIVY_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ nb-configuration.xml
.env

creds.source
gh-members.csv
supplementary.csv
/gh-members.csv
/supplementary.csv
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ Loop over the GitHub members and see if we can find them in LDAP. Output what we
### GitHubMemberInRedHatLdap
Loop over the GitHub members and see if we can find them in LDAP. Output what we find to a CSV.

`--supplementary-csv` is a list of known members that been created via `CollectRedHatLdapSupplementaryList`

```bash
./target/github-stats-1.0.0-SNAPSHOT-runner github-member-in-ldap --dry-run=true --organization={your-org} --issue-repo={a-repo-in-that-org} --members-csv={list-of-known-members} --supplementary-csv={list-of-supplementary-members} -fail-if-no-vpn=false
```
Expand All @@ -62,5 +64,5 @@ Loop over the GitHub members and see if we can find them in LDAP. Output what we
`--members-csv` is a list of known members that have validated their GitHub ID against their RH ID. See: `tests/members.csv` as an example.

```bash
./target/github-stats-1.0.0-SNAPSHOT-runner create-who-are-you-issues --dry-run=true --organization={your-org} --issue-repo={a-repo-in-that-org} --members-csv={list-of-known-members} --fail-if-no-vpn=false
./target/github-stats-1.0.0-SNAPSHOT-runner create-who-are-you-issues --dry-run=true --organization={your-org} --issue-repo={a-repo-in-that-org} --members-csv={list-of-known-members} --supplementary-csv={list-of-supplementary-members} --fail-if-no-vpn=false
```
7 changes: 6 additions & 1 deletion scripts/run-redhat-cop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ scripts/download-memebers-sheet.sh

./mvnw clean install -Pnative

source creds.source

#./target/github-stats-1.0.0-SNAPSHOT-runner collect-stats --organization=redhat-cop

./target/github-stats-1.0.0-SNAPSHOT-runner collect-members-from-ldap --organization=redhat-cop --members-csv=gh-members.csv --csv-output=supplementary.csv --fail-if-no-vpn=true
./target/github-stats-1.0.0-SNAPSHOT-runner github-member-in-ldap --organization=redhat-cop --issue-repo=org --members-csv=gh-members.csv --supplementary-csv=supplementary.csv --fail-if-no-vpn=true
./target/github-stats-1.0.0-SNAPSHOT-runner github-member-in-ldap --dry-run=true --organization=redhat-cop --issue-repo=org --members-csv=gh-members.csv --supplementary-csv=supplementary.csv --fail-if-no-vpn=true

./target/github-stats-1.0.0-SNAPSHOT-runner create-who-are-you-issues --dry-run=true --organization=redhat-cop --issue-repo=org --members-csv=gh-members.csv --supplementary-csv=supplementary.csv --permission=admin
./target/github-stats-1.0.0-SNAPSHOT-runner create-who-are-you-issues --dry-run=true --organization=redhat-cop --issue-repo=org --members-csv=gh-members.csv --supplementary-csv=supplementary.csv --permission=write
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.garethahealy.githubstats.commands.users;

import com.garethahealy.githubstats.services.users.CreateWhoAreYouIssueService;
import freemarker.template.TemplateException;
import jakarta.enterprise.context.Dependent;
import jakarta.inject.Inject;
import org.apache.directory.api.ldap.model.exception.LdapException;
import org.kohsuke.github.GHPermissionType;
import picocli.CommandLine;

import java.io.IOException;
Expand All @@ -24,8 +26,11 @@ public class CreateWhoAreYouIssueCommand implements Runnable {
@CommandLine.Option(names = {"-i", "--members-csv"}, description = "CSV of current known members", required = true)
String membersCsv;

@CommandLine.Option(names = {"-vpn", "--fail-if-no-vpn"}, description = "Throw an exception if can't connect to LDAP")
boolean failNoVpn;
@CommandLine.Option(names = {"-s", "--supplementary-csv"}, description = "CSV of current known members, generated by 'collect-members-from-ldap'", required = true)
String supplementaryCsv;

@CommandLine.Option(names = {"-p", "--permission"}, description = "Permission to search against; ADMIN, WRITE, READ", required = true)
String permission;

@Inject
CreateWhoAreYouIssueService createWhoAreYouIssueService;
Expand All @@ -35,9 +40,25 @@ public void run() {
try {
//TODO: for time being, always dry-run
dryRun = true;
createWhoAreYouIssueService.run(organization, orgRepo, dryRun, membersCsv, failNoVpn);
} catch (IOException | LdapException e) {

createWhoAreYouIssueService.run(organization, orgRepo, dryRun, membersCsv, supplementaryCsv, convert(permission));
} catch (IOException | LdapException | TemplateException e) {
throw new RuntimeException(e);
}
}

private GHPermissionType convert(String permissions) {
GHPermissionType answer;
if (permissions.equalsIgnoreCase("ADMIN")) {
answer = GHPermissionType.ADMIN;
} else if (permissions.equalsIgnoreCase("WRITE")) {
answer = GHPermissionType.WRITE;
} else if (permissions.equalsIgnoreCase("READ")) {
answer = GHPermissionType.READ;
} else {
throw new IllegalArgumentException("--permission=" + permissions + " is invalid.");
}

return answer;
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/garethahealy/githubstats/model/WhoAreYou.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.garethahealy.githubstats.model;

import io.quarkus.runtime.annotations.RegisterForReflection;
import org.apache.commons.lang3.builder.CompareToBuilder;

@RegisterForReflection
public class WhoAreYou implements Comparable<WhoAreYou> {

private final String username;
private final String repo;

public String getUsername() {
return username;
}

public String getRepo() {
return repo;
}

public WhoAreYou(String username, String repo) {
this.username = username;
this.repo = repo;
}

@Override
public int compareTo(WhoAreYou o) {
return new CompareToBuilder().append(this.getUsername(), o.getUsername()).toComparison();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.garethahealy.githubstats.model;
package com.garethahealy.githubstats.model.csv;

import io.quarkus.runtime.annotations.RegisterForReflection;

import java.util.Arrays;
import java.util.List;

@RegisterForReflection
public class MembersInfo {
public class Members {
public enum Headers {
Timestamp,
EmailAddress,
Expand Down Expand Up @@ -34,7 +34,7 @@ public String getWhatIsYourGitHubUsername() {
return whatIsYourGitHubUsername;
}

public MembersInfo(String timestamp, String emailAddress, String whatIsYourGitHubUsername) {
public Members(String timestamp, String emailAddress, String whatIsYourGitHubUsername) {
this.timestamp = timestamp;
this.emailAddress = emailAddress;
this.whatIsYourGitHubUsername = whatIsYourGitHubUsername;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.garethahealy.githubstats.model;
package com.garethahealy.githubstats.model.csv;

import org.kohsuke.github.*;

Expand All @@ -9,7 +9,7 @@
import java.util.Arrays;
import java.util.List;

public class RepoInfo {
public class Repository {

public enum Headers {
RepoName,
Expand Down Expand Up @@ -51,22 +51,22 @@ public enum Headers {
private final boolean inConfig;
private final boolean isArchived;

public RepoInfo(String repoName,
GHCommit lastCommit,
List<GHRepository.Contributor> contributors,
List<GHCommit> commits,
List<GHIssue> issues,
List<GHPullRequest> pullRequests,
List<String> topics,
GHRepositoryCloneTraffic cloneTraffic,
GHRepositoryViewTraffic viewTraffic,
boolean hasOwners,
boolean hasCodeOwners,
boolean hasWorkflows,
boolean hasTravis,
boolean hasRenovate,
boolean inConfig,
boolean isArchived) throws IOException {
public Repository(String repoName,
GHCommit lastCommit,
List<GHRepository.Contributor> contributors,
List<GHCommit> commits,
List<GHIssue> issues,
List<GHPullRequest> pullRequests,
List<String> topics,
GHRepositoryCloneTraffic cloneTraffic,
GHRepositoryViewTraffic viewTraffic,
boolean hasOwners,
boolean hasCodeOwners,
boolean hasWorkflows,
boolean hasTravis,
boolean hasRenovate,
boolean inConfig,
boolean isArchived) throws IOException {
DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

this.repoName = repoName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.garethahealy.githubstats.services;

import com.garethahealy.githubstats.model.MembersInfo;
import com.garethahealy.githubstats.model.csv.Members;
import jakarta.enterprise.context.ApplicationScoped;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
Expand All @@ -14,21 +14,21 @@
@ApplicationScoped
public class CsvService {

public Map<String, MembersInfo> getKnownMembers(String membersCsv) throws IOException {
Map<String, MembersInfo> answer = new HashMap<>();
public Map<String, Members> getKnownMembers(String membersCsv) throws IOException {
Map<String, Members> answer = new HashMap<>();
CSVFormat csvFormat = CSVFormat.Builder.create(CSVFormat.DEFAULT)
.setHeader(MembersInfo.Headers.class)
.setHeader(Members.Headers.class)
.setSkipHeaderRecord(true)
.build();

try (Reader in = new FileReader(membersCsv)) {
Iterable<CSVRecord> records = csvFormat.parse(in);
for (CSVRecord record : records) {
String timestamp = record.get(MembersInfo.Headers.Timestamp);
String redhatEmail = record.get(MembersInfo.Headers.EmailAddress);
String username = record.get(MembersInfo.Headers.WhatIsYourGitHubUsername);
String timestamp = record.get(Members.Headers.Timestamp);
String redhatEmail = record.get(Members.Headers.EmailAddress);
String username = record.get(Members.Headers.WhatIsYourGitHubUsername);

answer.put(username, new MembersInfo(timestamp, redhatEmail, username));
answer.put(username, new Members(timestamp, redhatEmail, username));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,8 @@ private boolean hasFileContent(GHRepository repo, String path) {

return answer;
}

public PagedIterable<GHTeam> listTeams(GHOrganization org) throws IOException {
return org.listTeams();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.garethahealy.githubstats.services.stats;

import com.garethahealy.githubstats.model.RepoInfo;
import com.garethahealy.githubstats.model.csv.Repository;
import com.garethahealy.githubstats.services.GitHubService;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
Expand Down Expand Up @@ -47,13 +47,13 @@ public void run(String organization, boolean validateOrgConfig, String output) t
logger.infof("Found %s repos.", repos.size());

CSVFormat csvFormat = CSVFormat.Builder.create(CSVFormat.DEFAULT)
.setHeader((RepoInfo.Headers.class))
.setHeader((Repository.Headers.class))
.build();

int cores = Runtime.getRuntime().availableProcessors() * 2;
try (CSVPrinter csvPrinter = new CSVPrinter(Files.newBufferedWriter(Paths.get(output)), csvFormat)) {
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
List<Future<RepoInfo>> futures = new ArrayList<>();
List<Future<Repository>> futures = new ArrayList<>();
for (Map.Entry<String, GHRepository> current : repos.entrySet()) {
futures.add(executor.submit(() -> {
logger.infof("Working on: %s", current.getValue().getName());
Expand Down Expand Up @@ -94,12 +94,12 @@ public void run(String organization, boolean validateOrgConfig, String output) t
inConfig = configContent.contains(repoName);
}

return new RepoInfo(repoName, lastCommit, contributors, commits, issues, pullRequests, topics, cloneTraffic, viewTraffic,
return new Repository(repoName, lastCommit, contributors, commits, issues, pullRequests, topics, cloneTraffic, viewTraffic,
hasOwners, hasCodeOwners, hasWorkflows, hasTravis, hasRenovate, inConfig, isArchived);
}));

if (futures.size() == cores) {
for (Future<RepoInfo> future : futures) {
for (Future<Repository> future : futures) {
csvPrinter.printRecord(future.get().toArray());
}

Expand All @@ -110,7 +110,7 @@ public void run(String organization, boolean validateOrgConfig, String output) t
}
}

for (Future<RepoInfo> future : futures) {
for (Future<Repository> future : futures) {
csvPrinter.printRecord(future.get().toArray());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.garethahealy.githubstats.services.users;

import com.garethahealy.githubstats.model.MembersInfo;
import com.garethahealy.githubstats.model.csv.Members;
import com.garethahealy.githubstats.services.CsvService;
import com.garethahealy.githubstats.services.GitHubService;
import com.garethahealy.githubstats.services.LdapService;
Expand Down Expand Up @@ -42,20 +42,23 @@ public void run(String organization, String output, String membersCsv, boolean f
GHOrganization org = gitHubService.getOrganization(gitHubService.getGitHub(), organization);
List<GHUser> members = gitHubService.listMembers(org);

Map<String, MembersInfo> knownMembers = csvService.getKnownMembers(membersCsv);
Map<String, Members> knownMembers = csvService.getKnownMembers(membersCsv);

CSVFormat csvFormat = CSVFormat.Builder.create(CSVFormat.DEFAULT)
.setHeader((MembersInfo.Headers.class))
.setHeader((Members.Headers.class))
.build();

try (CSVPrinter csvPrinter = new CSVPrinter(Files.newBufferedWriter(Paths.get(output)), csvFormat)) {
// Hard code the bot user to be ignored
csvPrinter.printRecord(new Members("", "[email protected]", "redhat-cop-ci-bot").toArray());

if (ldapService.canConnect()) {
try (LdapConnection connection = ldapService.open()) {
for (GHUser user : members) {
if (!knownMembers.containsKey(user.getLogin())) {
String email = ldapService.searchOnGitHub(connection, user.getLogin());
if (!email.isEmpty()) {
csvPrinter.printRecord(new MembersInfo("", email, user.getLogin()).toArray());
csvPrinter.printRecord(new Members("", email, user.getLogin()).toArray());
}
}
}
Expand Down
Loading

0 comments on commit 74ff585

Please sign in to comment.