Search in sources :

Example 1 with IssueTracker

use of io.jenkins.infra.repository_permissions_updater.hosting.HostingRequest.IssueTracker in project repository-permissions-updater by jenkins-infra.

the class Hoster method run.

private void run(int issueID) {
    LOGGER.info("Approving hosting request " + issueID);
    JiraRestClient client = null;
    try {
        final HostingRequest hostingRequest = HostingRequestParser.retrieveAndParse(issueID);
        String defaultAssignee = hostingRequest.getJenkinsProjectUsers().get(0);
        String forkFrom = hostingRequest.getRepositoryUrl();
        List<String> users = hostingRequest.getGithubUsers();
        IssueTracker issueTrackerChoice = hostingRequest.getIssueTracker();
        String forkTo = hostingRequest.getNewRepoName();
        if (StringUtils.isBlank(forkFrom) || StringUtils.isBlank(forkTo) || users.isEmpty()) {
            LOGGER.info("Could not retrieve information (or information does not exist) from the Hosting request");
            return;
        }
        // Parse forkFrom in order to determine original repo owner and repo name
        Matcher m = Pattern.compile("(?:https://github\\.com/)?(\\S+)/(\\S+)", CASE_INSENSITIVE).matcher(forkFrom);
        if (m.matches()) {
            if (!forkGitHub(m.group(1), m.group(2), forkTo, users, issueTrackerChoice == IssueTracker.GITHUB)) {
                LOGGER.error("Hosting request failed to fork repository on Github");
                return;
            }
        } else {
            LOGGER.error("ERROR: Cannot parse the source repo: " + forkFrom);
            return;
        }
        // create the JIRA component
        if (issueTrackerChoice == IssueTracker.JIRA && !createComponent(forkTo, defaultAssignee)) {
            LOGGER.error("Hosting request failed to create component " + forkTo + " in JIRA");
            return;
        }
        client = JiraHelper.createJiraClient();
        String componentId = "";
        try {
            if (issueTrackerChoice == IssueTracker.JIRA) {
                BasicComponent component = JiraHelper.getBasicComponent(client, JIRA_PROJECT, forkTo);
                if (component.getId() != null) {
                    componentId = component.getId().toString();
                }
            }
        } catch (IOException | TimeoutException | ExecutionException | InterruptedException ex) {
            LOGGER.error("Could not get component ID for " + forkTo + " component in Jira");
            componentId = "";
        }
        String prUrl = createUploadPermissionPR(issueID, forkTo, users, hostingRequest.getJenkinsProjectUsers(), issueTrackerChoice == IssueTracker.GITHUB, componentId);
        if (StringUtils.isBlank(prUrl)) {
            LOGGER.error("Could not create upload permission pull request");
        }
        String prDescription = "";
        String repoPermissionsActionText = "Create PR for upload permissions";
        if (!StringUtils.isBlank(prUrl)) {
            prDescription = "\n\nA [pull request](" + prUrl + ") has been created against the repository permissions updater to " + "setup release permissions. Additional users can be added by modifying the created file.";
            repoPermissionsActionText = "Add additional users for upload permissions, if needed";
        }
        String issueTrackerText;
        if (issueTrackerChoice == IssueTracker.JIRA) {
            issueTrackerText = "\n\nA Jira component named " + forkTo + " has also been created with " + defaultAssignee + " as the default assignee for issues.";
        } else {
            issueTrackerText = "\n\nGitHub issues has been selected for issue tracking and was enabled for the forked repo.";
        }
        // update the issue with information on next steps
        String msg = "Hosting request complete, the code has been forked into the jenkinsci project on GitHub as " + "https://github.com/jenkinsci/" + forkTo + issueTrackerText + prDescription + "\n\nPlease remove your original repository (if there are no other forks) so that the jenkinsci organization repository " + "is the definitive source for the code. If there are other forks, please contact GitHub support to make the jenkinsci repo the root of the fork network (mention that Jenkins approval was given in support request 569994). " + "Also, please make sure you properly follow the [documentation on documenting your plugin](https://jenkins.io/doc/developer/publishing/documentation/) " + "so that your plugin is correctly documented. \n\n" + "You will also need to do the following in order to push changes and release your plugin: \n\n" + "* [Accept the invitation to the Jenkins CI Org on Github](https://github.com/jenkinsci)\n" + "* [" + repoPermissionsActionText + "](https://github.com/jenkins-infra/repository-permissions-updater/#requesting-permissions)\n" + "* [Releasing your plugin](https://jenkins.io/doc/developer/publishing/releasing/)\n" + "\n\nIn order for your plugin to be built by the [Jenkins CI Infrastructure](https://ci.jenkins.io) and check pull requests," + " please add a [Jenkinsfile](https://jenkins.io/doc/book/pipeline/jenkinsfile/) to the root of your repository with the following content:\n" + "`buildPlugin()`" + "\n\nWelcome aboard!";
        // add comment
        GitHub github = GitHub.connect();
        GHIssue issue = github.getRepository(HOSTING_REPO_SLUG).getIssue(issueID);
        issue.comment(msg);
        issue.close();
        LOGGER.info("Hosting setup complete");
    } catch (IOException e) {
        LOGGER.error("Failed setting up hosting for " + issueID + ". ", e);
    } finally {
        if (!JiraHelper.close(client)) {
            LOGGER.warn("Failed to close JIRA client, possible leaked file descriptors");
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) IssueTracker(io.jenkins.infra.repository_permissions_updater.hosting.HostingRequest.IssueTracker) GitHub(org.kohsuke.github.GitHub) GHIssue(org.kohsuke.github.GHIssue) IOException(java.io.IOException) JiraRestClient(com.atlassian.jira.rest.client.api.JiraRestClient) BasicComponent(com.atlassian.jira.rest.client.api.domain.BasicComponent) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

JiraRestClient (com.atlassian.jira.rest.client.api.JiraRestClient)1 BasicComponent (com.atlassian.jira.rest.client.api.domain.BasicComponent)1 IssueTracker (io.jenkins.infra.repository_permissions_updater.hosting.HostingRequest.IssueTracker)1 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Matcher (java.util.regex.Matcher)1 GHIssue (org.kohsuke.github.GHIssue)1 GitHub (org.kohsuke.github.GitHub)1