Skip to content

Commit

Permalink
feat: add INPUT_MODE = (write|sync)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkweon committed Sep 13, 2021
1 parent a21384d commit 9897409
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 16 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ members:
steps:
- uses: "codingpot/github-org-member-manage-action:v0"
with:
gh_token: ${{ secrets.GH_TOKEN }} # NEEDS ORG RW PERMISSION
members_filepath: members.yaml
gh_token: ${{ secrets.GH_TOKEN }} # (required) Needs admin:org permission
members_filepath: members.yaml # (optional)
dry_run: false # (optional)
mode: sync # (optional) write or sync
```
11 changes: 11 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ inputs:
description: Filepath to members.yaml.
required: true
default: members.yaml
dry_run:
description: Does not update any memberships but just fetch only
required: false
mode:
description: >
There are 2 modes (sync|write).
"sync" mode will try to update memberships.
"write" mode will just fetch data and write YAML in `members_filepath`.
required: false
default: sync

runs:
using: docker
image: Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.github.codingpot.github_org_member_manage_action;

import com.github.codingpot.github_org_member_manage_action.config.ConfigData;
import com.github.codingpot.github_org_member_manage_action.config.ConfigManager;
import com.github.codingpot.github_org_member_manage_action.context.Context;
import com.github.codingpot.github_org_member_manage_action.github.GitHubService;
import com.github.codingpot.github_org_member_manage_action.producers.ProducersComponent;
import com.github.codingpot.github_org_member_manage_action.status.Status;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import javax.inject.Inject;

Expand All @@ -13,14 +15,37 @@ public class App {
@Inject Context context;
@Inject GitHubService gitHubService;
@Inject ProducersComponent.Builder builder;
@Inject ConfigManager manager;

public static void main(String[] args) throws ExecutionException, InterruptedException {
new App().run();
public static void main(String[] args) {
try {
new App().run();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}

private void run() throws ExecutionException, InterruptedException {
private void run() throws ExecutionException, InterruptedException, IOException {
DaggerAppComponent.create().inject(this);
System.out.println("context = " + context);

if (context.getMode() == AppMode.SYNC) {
runSync();
} else if (context.getMode() == AppMode.WRITE) {
runWrite();
}

System.exit(0);
}

private void runWrite() throws ExecutionException, InterruptedException, IOException {
final ProducersComponent producersComponent = builder.build();
final ConfigData data = producersComponent.configDataFromGitHub().get();
manager.write(data);
}

private void runSync() throws ExecutionException, InterruptedException {
final ProducersComponent producersComponent = builder.build();

final ConfigData localConfigData = producersComponent.configDataFromLocal().get();
Expand All @@ -34,7 +59,5 @@ private void run() throws ExecutionException, InterruptedException {
System.out.println(status.getErrorMessage().orElse(""));
System.exit(1);
}

System.exit(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.codingpot.github_org_member_manage_action;

/**
* SYNC tries to update Org memberships based on YAML. WRITE will just create a YAML(members.yaml)
* based on GitHub state.
*/
public enum AppMode {
SYNC,
WRITE
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/** Constants hold static string values that are shared across the application. */
public class Constants {
public static final String INPUT_DRY_RUN = "INPUT_DRY_RUN";
public static String INPUT_GH_TOKEN = "INPUT_GH_TOKEN";
public static String INPUT_MEMBERS_FILEPATH = "INPUT_MEMBERS_FILEPATH";
public static final String INPUT_GH_TOKEN = "INPUT_GH_TOKEN";
public static final String INPUT_MEMBERS_FILEPATH = "INPUT_MEMBERS_FILEPATH";
public static final String INPUT_MODE = "INPUT_MODE";
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,16 @@ public ConfigData readFromLocal() throws IOException {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
return mapper.readValue(new File(filepath), ConfigData.class);
}

public void write(ConfigData data) throws IOException {
if (context.isDryRun()) {
System.out.printf(
"Not going to write because it's a dry run but %s will be written to %s%n",
data, context.getMembersFilePath());
return;
}
var filepath = this.context.getMembersFilePath();
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
mapper.writeValue(new File(filepath), data);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.codingpot.github_org_member_manage_action.context;

import com.github.codingpot.github_org_member_manage_action.AppMode;
import com.github.codingpot.github_org_member_manage_action.annotations.DryRun;
import com.github.codingpot.github_org_member_manage_action.annotations.GitHubToken;
import com.github.codingpot.github_org_member_manage_action.annotations.MembersFilePath;
Expand All @@ -17,14 +18,17 @@ public class Context {
String membersFilePath;
String githubToken;
boolean dryRun;
AppMode mode;

@Inject
public Context(
@MembersFilePath String membersFilePath,
@GitHubToken String githubToken,
@DryRun boolean dryRun) {
@DryRun boolean dryRun,
AppMode mode) {
this.membersFilePath = membersFilePath;
this.githubToken = githubToken;
this.dryRun = dryRun;
this.mode = mode;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.github.codingpot.github_org_member_manage_action.env;

import static com.github.codingpot.github_org_member_manage_action.Constants.INPUT_GH_TOKEN;
import static com.github.codingpot.github_org_member_manage_action.Constants.INPUT_MEMBERS_FILEPATH;
import static com.github.codingpot.github_org_member_manage_action.Constants.*;

import com.github.codingpot.github_org_member_manage_action.AppMode;
import com.github.codingpot.github_org_member_manage_action.Constants;
import com.github.codingpot.github_org_member_manage_action.annotations.DryRun;
import com.github.codingpot.github_org_member_manage_action.annotations.GitHubToken;
Expand Down Expand Up @@ -30,11 +30,18 @@ static String provideMembersFilePath() {
return withDefault(System.getenv(INPUT_MEMBERS_FILEPATH), "members.yaml");
}

@Singleton
@Provides
static AppMode provideMode() {
return AppMode.valueOf(withDefault(System.getenv(INPUT_MODE), "sync").toUpperCase());
}

@Singleton
@Provides
@DryRun
static boolean provideDryRun() {
return System.getenv(Constants.INPUT_DRY_RUN) != null;
final String env = System.getenv(Constants.INPUT_DRY_RUN);
return env != null && !env.isBlank() && !env.equalsIgnoreCase("false");
}

private static String withDefault(String env, String defaultValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Status addMembers(Iterable<String> newMembers) {
u -> {
try {
if (isDryRun) {
System.out.printf("Adding a new member: %s", u);
System.out.printf("Adding a new member: %s%n", u);
return;
}

Expand All @@ -61,7 +61,7 @@ public Status addAdmins(Iterable<String> newAdmins) {
u -> {
try {
if (isDryRun) {
System.out.printf("Adding a new admin: %s", u);
System.out.printf("Adding a new admin: %s%n", u);
return;
}
final GHUser user = github.getUser(u);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class ConfigManagerTest {
void setUp() throws IOException {
path = Files.createTempFile("members", ".yaml");
configManager =
new ConfigManager(new Context(path.toUri().getPath(), "FAKE_GITHUB_TOKEN", true));
new ConfigManager(
new Context(
path.toUri().getPath(), "FAKE_GITHUB_TOKEN", true, AppMode.SYNC));

String yaml =
"org_name: orgName"
Expand Down

0 comments on commit 9897409

Please sign in to comment.