Search in sources :

Example 1 with CreateStatusRequest

use of com.google.copybara.git.github.api.CreateStatusRequest in project copybara by google.

the class AbstractGithubApiTest method testCreateStatus.

@Test
public void testCreateStatus() throws Exception {
    trainMockPost("/repos/octocat/Hello-World/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e", createValidator(TestCreateStatusRequest.class, csr -> csr.getContext().equals("continuous-integration/jenkins") && csr.getState().equals(State.SUCCESS)), getResource("create_status_response_testdata.json"));
    Status response = api.createStatus("octocat/Hello-World", "6dcb09b5b57875f334f61aebed695e2e4193db5e", new CreateStatusRequest(State.SUCCESS, "https://ci.example.com/1000/output", "Build has completed successfully", "continuous-integration/jenkins"));
    assertThat(response.getContext()).isEqualTo("continuous-integration/jenkins");
    assertThat(response.getTargetUrl()).isEqualTo("https://ci.example.com/1000/output");
    assertThat(response.getDescription()).isEqualTo("Build has completed successfully");
    assertThat(response.getState()).isEqualTo(State.SUCCESS);
    assertThat(response.getCreator()).isNotNull();
    assertThat(response.getCreator().getLogin()).isEqualTo("octocat");
}
Also used : GsonFactory(com.google.api.client.json.gson.GsonFactory) GithubApi(com.google.copybara.git.github.api.GithubApi) CombinedStatus(com.google.copybara.git.github.api.CombinedStatus) Review(com.google.copybara.git.github.api.Review) Lists(com.google.common.collect.Lists) State(com.google.copybara.git.github.api.Status.State) ImmutableList(com.google.common.collect.ImmutableList) Assert.fail(org.junit.Assert.fail) GitHubApiTransport(com.google.copybara.git.github.api.GitHubApiTransport) Issue(com.google.copybara.git.github.api.Issue) Profiler(com.google.copybara.profiler.Profiler) Before(org.junit.Before) Label(com.google.copybara.git.github.api.Issue.Label) GitHubApiException(com.google.copybara.git.github.api.GitHubApiException) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) ResponseCode(com.google.copybara.git.github.api.GitHubApiException.ResponseCode) IOException(java.io.IOException) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) Ticker(com.google.common.base.Ticker) CreatePullRequest(com.google.copybara.git.github.api.CreatePullRequest) List(java.util.List) PullRequest(com.google.copybara.git.github.api.PullRequest) Ref(com.google.copybara.git.github.api.Ref) Paths(java.nio.file.Paths) LogProfilerListener(com.google.copybara.profiler.LogProfilerListener) Status(com.google.copybara.git.github.api.Status) CreateStatusRequest(com.google.copybara.git.github.api.CreateStatusRequest) CombinedStatus(com.google.copybara.git.github.api.CombinedStatus) Status(com.google.copybara.git.github.api.Status) CreateStatusRequest(com.google.copybara.git.github.api.CreateStatusRequest) Test(org.junit.Test)

Example 2 with CreateStatusRequest

use of com.google.copybara.git.github.api.CreateStatusRequest in project copybara by google.

the class AbstractGitHubApiTest method testCreateStatus.

@Test
public void testCreateStatus() throws Exception {
    trainMockPost("/repos/octocat/Hello-World/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e", createValidator(TestCreateStatusRequest.class, csr -> csr.getContext().equals("continuous-integration/jenkins") && csr.getState().equals(State.SUCCESS)), getResource("create_status_response_testdata.json"));
    Status response = api.createStatus("octocat/Hello-World", "6dcb09b5b57875f334f61aebed695e2e4193db5e", new CreateStatusRequest(State.SUCCESS, "https://ci.example.com/1000/output", "Build has completed successfully", "continuous-integration/jenkins"));
    assertThat(response.getContext()).isEqualTo("continuous-integration/jenkins");
    assertThat(response.getTargetUrl()).isEqualTo("https://ci.example.com/1000/output");
    assertThat(response.getDescription()).isEqualTo("Build has completed successfully");
    assertThat(response.getState()).isEqualTo(State.SUCCESS);
    assertThat(response.getCreator()).isNotNull();
    assertThat(response.getCreator().getLogin()).isEqualTo("octocat");
}
Also used : UserPermissionLevel(com.google.copybara.git.github.api.UserPermissionLevel) GsonFactory(com.google.api.client.json.gson.GsonFactory) ZonedDateTime(java.time.ZonedDateTime) CombinedStatus(com.google.copybara.git.github.api.CombinedStatus) Review(com.google.copybara.git.github.api.Review) MockitoAnnotations(org.mockito.MockitoAnnotations) CREATED(com.google.copybara.git.github.api.GitHubApi.PullRequestListParams.SortFilter.CREATED) GitHubCommit(com.google.copybara.git.github.api.GitHubCommit) Assert.fail(org.junit.Assert.fail) GitHubApiTransport(com.google.copybara.git.github.api.GitHubApiTransport) Profiler(com.google.copybara.profiler.Profiler) User(com.google.copybara.git.github.api.User) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) ResponseCode(com.google.copybara.git.github.api.GitHubApiException.ResponseCode) Ticker(com.google.common.base.Ticker) CreatePullRequest(com.google.copybara.git.github.api.CreatePullRequest) CommentBody(com.google.copybara.git.github.api.CommentBody) UpdatePullRequest(com.google.copybara.git.github.api.UpdatePullRequest) List(java.util.List) GitHubApi(com.google.copybara.git.github.api.GitHubApi) PullRequest(com.google.copybara.git.github.api.PullRequest) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) ASC(com.google.copybara.git.github.api.GitHubApi.PullRequestListParams.DirectionFilter.ASC) LogProfilerListener(com.google.copybara.profiler.LogProfilerListener) AddLabels(com.google.copybara.git.github.api.AddLabels) CreateStatusRequest(com.google.copybara.git.github.api.CreateStatusRequest) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CheckRuns(com.google.copybara.git.github.api.CheckRuns) Mock(org.mockito.Mock) RepoException(com.google.copybara.exception.RepoException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PullRequestComment(com.google.copybara.git.github.api.PullRequestComment) Captor(org.mockito.Captor) Lists(com.google.common.collect.Lists) Label(com.google.copybara.git.github.api.Label) State(com.google.copybara.git.github.api.Status.State) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) Issue(com.google.copybara.git.github.api.Issue) Before(org.junit.Before) GitHubApiException(com.google.copybara.git.github.api.GitHubApiException) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) ValidationException(com.google.copybara.exception.ValidationException) IOException(java.io.IOException) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) GitHubUserPermission(com.google.copybara.git.github.api.UserPermissionLevel.GitHubUserPermission) Mockito.verify(org.mockito.Mockito.verify) UpdateReferenceRequest(com.google.copybara.git.github.api.UpdateReferenceRequest) PullRequestListParams(com.google.copybara.git.github.api.GitHubApi.PullRequestListParams) Ref(com.google.copybara.git.github.api.Ref) Paths(java.nio.file.Paths) CreateIssueRequest(com.google.copybara.git.github.api.Issue.CreateIssueRequest) Preconditions(com.google.common.base.Preconditions) Status(com.google.copybara.git.github.api.Status) CombinedStatus(com.google.copybara.git.github.api.CombinedStatus) Status(com.google.copybara.git.github.api.Status) CreateStatusRequest(com.google.copybara.git.github.api.CreateStatusRequest) Test(org.junit.Test)

Example 3 with CreateStatusRequest

use of com.google.copybara.git.github.api.CreateStatusRequest in project copybara by google.

the class GitHubEndPoint method createStatus.

@StarlarkMethod(name = "create_status", doc = "Create or update a status for a commit. Returns the status created.", parameters = { @Param(name = "sha", named = true, doc = "The SHA-1 for which we want to create or update the status"), @Param(name = "state", named = true, doc = "The state of the commit status: 'success', 'error', 'pending' or 'failure'"), @Param(name = "context", doc = "The context for the commit status. Use a value like 'copybara/import_successful'" + " or similar", named = true), @Param(name = "description", named = true, doc = "Description about what happened"), @Param(name = "target_url", allowedTypes = { @ParamType(type = String.class), @ParamType(type = NoneType.class) }, named = true, doc = "Url with expanded information about the event", defaultValue = "None") })
public Status createStatus(String sha, String state, String context, String description, Object targetUrl) throws EvalException, RepoException, ValidationException {
    try {
        checkCondition(State.VALID_VALUES.contains(state), "Invalid value for state. Valid values: %s", State.VALID_VALUES);
        checkCondition(GitRevision.COMPLETE_SHA1_PATTERN.matcher(sha).matches(), "Not a valid complete SHA-1: %s", sha);
        checkCondition(!Strings.isNullOrEmpty(description), "description cannot be empty");
        checkCondition(!Strings.isNullOrEmpty(context), "context cannot be empty");
        String project = ghHost.getProjectNameFromUrl(url);
        return apiSupplier.load(console).createStatus(project, sha, new CreateStatusRequest(State.valueOf(state.toUpperCase()), convertFromNoneable(targetUrl, null), description, context));
    } catch (GitHubApiException gae) {
        if (gae.getResponseCode() == ResponseCode.UNPROCESSABLE_ENTITY) {
            throw new ValidationException("GitHub was unable to process the request " + gae.getError(), gae);
        }
        throw gae;
    } catch (ValidationException | RuntimeException e) {
        throw Starlark.errorf("Error calling create_status: %s", e.getMessage());
    }
}
Also used : CreateStatusRequest(com.google.copybara.git.github.api.CreateStatusRequest) ValidationException(com.google.copybara.exception.ValidationException) GitHubApiException(com.google.copybara.git.github.api.GitHubApiException) StarlarkMethod(net.starlark.java.annot.StarlarkMethod)

Aggregations

CreateStatusRequest (com.google.copybara.git.github.api.CreateStatusRequest)3 GitHubApiException (com.google.copybara.git.github.api.GitHubApiException)3 GsonFactory (com.google.api.client.json.gson.GsonFactory)2 Ticker (com.google.common.base.Ticker)2 ImmutableList (com.google.common.collect.ImmutableList)2 Lists (com.google.common.collect.Lists)2 Truth.assertThat (com.google.common.truth.Truth.assertThat)2 ValidationException (com.google.copybara.exception.ValidationException)2 CombinedStatus (com.google.copybara.git.github.api.CombinedStatus)2 CreatePullRequest (com.google.copybara.git.github.api.CreatePullRequest)2 ResponseCode (com.google.copybara.git.github.api.GitHubApiException.ResponseCode)2 GitHubApiTransport (com.google.copybara.git.github.api.GitHubApiTransport)2 Issue (com.google.copybara.git.github.api.Issue)2 PullRequest (com.google.copybara.git.github.api.PullRequest)2 Ref (com.google.copybara.git.github.api.Ref)2 Review (com.google.copybara.git.github.api.Review)2 Status (com.google.copybara.git.github.api.Status)2 State (com.google.copybara.git.github.api.Status.State)2 LogProfilerListener (com.google.copybara.profiler.LogProfilerListener)2 Profiler (com.google.copybara.profiler.Profiler)2