Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

batches: add client methods for duplicate commit workflow#52749

Merged
courier-new merged 8 commits into
batches/commit-signingfrom
kr/commit-signing-new-client-methods
Jun 2, 2023
Merged

batches: add client methods for duplicate commit workflow#52749
courier-new merged 8 commits into
batches/commit-signingfrom
kr/commit-signing-new-client-methods

Conversation

@courier-new

@courier-new courier-new commented Jun 1, 2023

Copy link
Copy Markdown
Contributor

Closes https://github.com/sourcegraph/sourcegraph/issues/52488.

This PR adds support for three methods to the GitHub V3 (REST) Client: GetRef, CreateCommit, and UpdateRef. When chained together, these methods facilitate duplicating an existing commit and replacing it on a branch.

Test plan

Added VCR success/failure tests for each of the new client methods.

Manually tested with https://github.com/sourcegraph/sourcegraph/pull/52492 and confirmed a branch with one signed commit was produced at the end:

image

@BolajiOlajide BolajiOlajide left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good.

@courier-new courier-new marked this pull request as ready for review June 2, 2023 00:24
@sourcegraph-bot

sourcegraph-bot commented Jun 2, 2023

Copy link
Copy Markdown
Contributor

Codenotify: Notifying subscribers in CODENOTIFY files for diff 284027d...8245682.

Notify File(s)
@eseliger internal/extsvc/github/common.go
internal/extsvc/github/testdata/golden/TestV3Client_CreateCommit_failure
internal/extsvc/github/testdata/golden/TestV3Client_CreateCommit_success
internal/extsvc/github/testdata/golden/TestV3Client_GetRef_failure
internal/extsvc/github/testdata/golden/TestV3Client_GetRef_success
internal/extsvc/github/testdata/golden/TestV3Client_UpdateRef_failure
internal/extsvc/github/testdata/golden/TestV3Client_UpdateRef_success
internal/extsvc/github/testdata/vcr/TestV3Client_CreateCommit_failure.yaml
internal/extsvc/github/testdata/vcr/TestV3Client_CreateCommit_success.yaml
internal/extsvc/github/testdata/vcr/TestV3Client_GetRef_failure.yaml
internal/extsvc/github/testdata/vcr/TestV3Client_GetRef_success.yaml
internal/extsvc/github/testdata/vcr/TestV3Client_UpdateRef_failure.yaml
internal/extsvc/github/testdata/vcr/TestV3Client_UpdateRef_success.yaml
internal/extsvc/github/v3.go
internal/extsvc/github/v3_test.go
internal/extsvc/github/v4.go
@sashaostrikov internal/extsvc/github/common.go
internal/extsvc/github/testdata/golden/TestV3Client_CreateCommit_failure
internal/extsvc/github/testdata/golden/TestV3Client_CreateCommit_success
internal/extsvc/github/testdata/golden/TestV3Client_GetRef_failure
internal/extsvc/github/testdata/golden/TestV3Client_GetRef_success
internal/extsvc/github/testdata/golden/TestV3Client_UpdateRef_failure
internal/extsvc/github/testdata/golden/TestV3Client_UpdateRef_success
internal/extsvc/github/testdata/vcr/TestV3Client_CreateCommit_failure.yaml
internal/extsvc/github/testdata/vcr/TestV3Client_CreateCommit_success.yaml
internal/extsvc/github/testdata/vcr/TestV3Client_GetRef_failure.yaml
internal/extsvc/github/testdata/vcr/TestV3Client_GetRef_success.yaml
internal/extsvc/github/testdata/vcr/TestV3Client_UpdateRef_failure.yaml
internal/extsvc/github/testdata/vcr/TestV3Client_UpdateRef_success.yaml
internal/extsvc/github/v3.go
internal/extsvc/github/v3_test.go
internal/extsvc/github/v4.go

@sourcegraph-bot

sourcegraph-bot commented Jun 2, 2023

Copy link
Copy Markdown
Contributor

📖 Storybook live preview

@courier-new courier-new force-pushed the kr/commit-signing-new-client-methods branch from f11ac01 to c4dc5b4 Compare June 2, 2023 00:53
@courier-new courier-new force-pushed the kr/commit-signing-new-client-methods branch from c4dc5b4 to 8245682 Compare June 2, 2023 04:00

@st0nebreaker st0nebreaker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thank you! Just a few questions below to better my Go understanding when you have a chance ~ 🙇🏻‍♀️

"sha": "9f815d128eae9f1066353ca41c297cdc0ef94662",
"node_id": "C_kwDODS5xedoAKDlmODE1ZDEyOGVhZTlmMTA2NjM1M2NhNDFjMjk3Y2RjMGVmOTQ2NjI",
"author": {
"name": "Sourcegraph VCR Test",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's a VCR test? And what's a golden test? I couldn't find anything from our docs on this. How do we run them?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah great question!! @adeola-ak actually started a docs PR about this a while ago, but we never got around to finishing it. We should really do that!

VCR tests are, like the name suggests (yes, that VCR! Video Cassette Recorders!), tests that record HTTP interactions in order to replay them later as a way of testing that the behavior and outputs of a method that interacts with an external API stay consistent and match expectations so long as the "inputs" from the external API are the same. For our external service client methods, the things that actually send requests to code host servers, VCR tests are essential.

So the way they work is as follows:

  • You create a special HTTP client that is capable of intercepting HTTP interactions and recording them to or reading them from files (the "cassettes"). In our case, we've built common utilities to enable this in the package internal/httptestutil based on a Go library for VCR testing.
  • You write a test mostly as normal, invoking your method that would talk to an external API with a certain pre-determined set of inputs and then asserting things about the results. But you make sure your method is using the special HTTP client now.
    • For the tests I wrote for the GitHub V3 client, this setup is accomplished by newV3TestClient helper method.
  • You run the test once in "update" mode, to record the interactions. Usually these tests will require you have a couple environment variables set from the shell environment in which you are running the tests, such as a GITHUB_TOKEN to authenticate requests to GitHub with. To target which tests you want to record, you provide an -update flag and a path to the cassette files you wish to be recorded over. The path doesn't have to be a full path, either, so for example if I ran:
    go test ./internal/extsvc/github -run TestV3Client_GetRef -update TestV3Client_GetRef
    then the test would record both TestV3Client_GetRef_success and TestV3Client_GetRef_failure.
    • This will instruct the special HTTP client to actually issue real requests to the API and then record the details of any HTTP interactions (both request and response objects) made over the course of the test as YAML files in the testdata/vcr directory.
    • It will also record a result struct of my choosing as JSON into the testdata/golden directory. This represents the output I hope to get from my method, given these precise API interactions.
  • For subsequent runs, you can now omit the -update flag (so running it just like any other test), and this time the special HTTP client will "replay" the recorded interactions, rather than talking to the external API. This means we're still able to test with "real" responses, but the test is more deterministic and reliable, since it isn't subject to network instability, dynamic parts of the response (such as timestamps), or environment. The test will also make sure the result at the end of it is exactly the same as the result from testdata/golden.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, that's really neat. It looks like pretty much just the batches team has been using VCR tests. I wonder if there's a standard coverage we want with VCR tests?

@courier-new courier-new Jun 6, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think we tend to use it the most because we have the most in quantity and most complex interactions with code hosts out of any part of the application, and often we depend very closely on the response for things to work out right for the end user in the UI. But technically these internal/extsvc packages are for all of Sourcegraph, not just Batch Changes, so there is pretty good coverage across even the non-BC client interactions at least. 😄 I think it's wise to write VCR tests for any external API interactions that we're critically dependent upon. We could definitely express that sentiment in the docs.

Comment thread internal/extsvc/github/v3.go
Comment thread internal/extsvc/github/v3_test.go
Comment thread internal/extsvc/github/v3_test.go
@courier-new courier-new merged commit 9f20b61 into batches/commit-signing Jun 2, 2023
@courier-new courier-new deleted the kr/commit-signing-new-client-methods branch June 2, 2023 20:21
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

batch-changes Issues related to Batch Changes cla-signed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants