Search in sources :

Example 1 with UpdateReferenceRequest

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

the class GitHubEndPoint method updateReference.

@StarlarkMethod(name = "update_reference", doc = "Update a reference to point to a new commit. Returns the info of the reference.", parameters = { @Param(name = "ref", named = true, doc = "The name of the reference."), @Param(name = "sha", doc = "The id for the commit" + " status.", named = true), @Param(name = "force", named = true, doc = "Indicates whether to force the update or to make sure the update is a" + " fast-forward update. Leaving this out or setting it to false will make" + " sure you're not overwriting work. Default: false") })
public Ref updateReference(String sha, String ref, boolean force) throws EvalException, RepoException {
    try {
        checkCondition(GitRevision.COMPLETE_SHA1_PATTERN.matcher(sha).matches(), "Not a valid complete SHA-1: %s", sha);
        checkCondition(!Strings.isNullOrEmpty(ref), "ref cannot be empty");
        if (!ref.startsWith("refs/")) {
            // TODO(malcon): Remove this functionality and use a check once library migrated.
            console.warnFmt("Non-complete ref passed to update_reference '%s'. Assuming refs/heads/%s", ref, ref);
            ref = "refs/heads/" + ref;
        }
        String project = ghHost.getProjectNameFromUrl(url);
        return apiSupplier.load(console).updateReference(project, ref, new UpdateReferenceRequest(sha, force));
    } catch (ValidationException | RuntimeException e) {
        throw Starlark.errorf("Error calling update_reference: %s", e.getMessage());
    }
}
Also used : UpdateReferenceRequest(com.google.copybara.git.github.api.UpdateReferenceRequest) ValidationException(com.google.copybara.exception.ValidationException) StarlarkMethod(net.starlark.java.annot.StarlarkMethod)

Example 2 with UpdateReferenceRequest

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

the class AbstractGitHubApiTest method testUpdateReference.

@Test
public void testUpdateReference() throws Exception {
    trainMockPost("/repos/octocat/Hello-World/git/refs/heads/test", createValidator(TestUpdateReferenceRequest.class, urr -> urr.getSha1().equals("6dcb09b5b57875f334f61aebed695e2e4193db5e") && urr.getForce()), getResource("update_reference_response_testdata.json"));
    Ref response = api.updateReference("octocat/Hello-World", "refs/heads/test", new UpdateReferenceRequest("6dcb09b5b57875f334f61aebed695e2e4193db5e", true));
    assertThat(response.getRef()).isEqualTo("refs/heads/test");
    assertThat(response.getSha()).isEqualTo("6dcb09b5b57875f334f61aebed695e2e4193db5e");
    assertThat(response.getUrl()).isEqualTo("https://api.github.com/repos/octocat/Hello-World/git/refs/heads/test");
}
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) UpdateReferenceRequest(com.google.copybara.git.github.api.UpdateReferenceRequest) Ref(com.google.copybara.git.github.api.Ref) Test(org.junit.Test)

Aggregations

ValidationException (com.google.copybara.exception.ValidationException)2 UpdateReferenceRequest (com.google.copybara.git.github.api.UpdateReferenceRequest)2 GsonFactory (com.google.api.client.json.gson.GsonFactory)1 Preconditions (com.google.common.base.Preconditions)1 Ticker (com.google.common.base.Ticker)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 RepoException (com.google.copybara.exception.RepoException)1 AddLabels (com.google.copybara.git.github.api.AddLabels)1 CheckRuns (com.google.copybara.git.github.api.CheckRuns)1 CombinedStatus (com.google.copybara.git.github.api.CombinedStatus)1 CommentBody (com.google.copybara.git.github.api.CommentBody)1 CreatePullRequest (com.google.copybara.git.github.api.CreatePullRequest)1 CreateStatusRequest (com.google.copybara.git.github.api.CreateStatusRequest)1 GitHubApi (com.google.copybara.git.github.api.GitHubApi)1 PullRequestListParams (com.google.copybara.git.github.api.GitHubApi.PullRequestListParams)1 ASC (com.google.copybara.git.github.api.GitHubApi.PullRequestListParams.DirectionFilter.ASC)1