Skip to content

Commit

Permalink
git: add fork action to github task (walmartlabs#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppendha authored and ibodrov committed Apr 5, 2019
1 parent 7106122 commit 201aa98
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Change log

## [Unreleased]

### Added

- github: new action `forkRepo`.



## [1.6.0] - 2019-04-03

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.eclipse.egit.github.core.service.DataService;
import org.eclipse.egit.github.core.service.IssueService;
import org.eclipse.egit.github.core.service.PullRequestService;
import org.eclipse.egit.github.core.service.RepositoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -68,6 +69,7 @@ public class GitHubTask implements Task {
private static final String GITHUB_MERGEHEAD = "head";
private static final String GITHUB_MERGEBASE = "base";
private static final String GITHUB_MERGECOMMITMSG = "commitMessage";
private static final String GITHUB_FORKTARGETORG = "targetOrg";

private static final String STATUS_CHECK_STATE = "state";
private static final String STATUS_CHECK_TARGET_URL = "targetUrl";
Expand Down Expand Up @@ -118,6 +120,10 @@ public void execute(Context ctx) throws Exception {
addStatus(ctx, gitHubUri);
break;
}
case FORKREPO: {
forkRepo(ctx, gitHubUri);
break;
}
default:
throw new IllegalArgumentException("Unsupported action type: " + action);
}
Expand Down Expand Up @@ -371,6 +377,37 @@ private static void addStatus(Context ctx, String gitHubUri) {
}
}

private static void forkRepo(Context ctx, String gitHubUri) throws Exception {
String gitHubAccessToken = assertString(ctx, GITHUB_ACCESSTOKEN);
String gitHubOrgName = assertString(ctx, GITHUB_ORGNAME);
String gitHubRepoName = assertString(ctx, GITHUB_REPONAME);
String targetOrg = getString(ctx, GITHUB_FORKTARGETORG);

GitHubClient client = GitHubClient.createClient(gitHubUri);

try {
//Connect to GitHub
client.setOAuth2Token(gitHubAccessToken);
IRepositoryIdProvider repo = RepositoryId.create(gitHubOrgName, gitHubRepoName);

//Fork a Git Repo
RepositoryService repoService = new RepositoryService(client);
if (targetOrg != null && !targetOrg.isEmpty()) {
log.info("Forking '{}/{}' into '{}' org...", gitHubOrgName, gitHubRepoName, targetOrg);
repoService.forkRepository(repo, targetOrg);
log.info("Fork action completed");
}
else {
log.info("Forking '{}/{}' into your personal repo...", gitHubOrgName, gitHubRepoName);
repoService.forkRepository(repo);
log.info("Fork action completed");
}
}catch (Exception e) {
throw new IllegalArgumentException("Error occured during fork: " + e.getMessage());
}

}

private static Action getAction(Context ctx) {
Object v = ctx.getVariable(ACTION_KEY);
if (v instanceof String) {
Expand Down Expand Up @@ -405,6 +442,7 @@ public enum Action {
MERGE,
CREATETAG,
GETCOMMIT,
ADDSTATUS
ADDSTATUS,
FORKREPO
}
}

0 comments on commit 201aa98

Please # to comment.