From 201aa9889bafc66c23d43799c22f78154d58db14 Mon Sep 17 00:00:00 2001 From: Prasanth Pendham - ppendha Date: Fri, 5 Apr 2019 10:39:57 -0500 Subject: [PATCH] git: add fork action to github task (#15) --- CHANGELOG.md | 8 ++++ .../concord/plugins/git/GitHubTask.java | 40 ++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7383ec5..24410672 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Change log +## [Unreleased] + +### Added + +- github: new action `forkRepo`. + + + ## [1.6.0] - 2019-04-03 ### Added diff --git a/tasks/git/src/main/java/com/walmartlabs/concord/plugins/git/GitHubTask.java b/tasks/git/src/main/java/com/walmartlabs/concord/plugins/git/GitHubTask.java index e3f1cfc4..ce74c130 100644 --- a/tasks/git/src/main/java/com/walmartlabs/concord/plugins/git/GitHubTask.java +++ b/tasks/git/src/main/java/com/walmartlabs/concord/plugins/git/GitHubTask.java @@ -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; @@ -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"; @@ -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); } @@ -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) { @@ -405,6 +442,7 @@ public enum Action { MERGE, CREATETAG, GETCOMMIT, - ADDSTATUS + ADDSTATUS, + FORKREPO } }