Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add change calculation tool #1975

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

Add change calculation tool #1975

wants to merge 11 commits into from

Conversation

denik
Copy link
Contributor

@denik denik commented Dec 6, 2024

Changes

Add a new tool changecalc that calculates all packages that need to be re-tested given a list of changed files. Without a list, uses git to compare with main branch.

'make viewchanges' runs the tool.
'make testchanges' runs the test suite over affected packages.

New tests-changes github job is added in shadow mode that takes advantage of this tool.

Tests

Manually. If we land this, we can observe the job on real PRs and how it compares with full runs.

@denik denik temporarily deployed to test-trigger-is December 6, 2024 15:17 — with GitHub Actions Inactive
@denik denik temporarily deployed to test-trigger-is December 6, 2024 15:17 — with GitHub Actions Inactive
@denik denik requested a review from pietern December 6, 2024 15:18
Add a new tool changecalc that calculates all packages that need to be re-tested given
a list of changed files. Without a list, uses git to compare with main branch.

'make viewchanges' runs the tool.
'make testchanges' runs the test suite over affected packages.

New tests-changes github job is added in shadow mode that takes advantage of this tool.
@denik denik force-pushed the denis.bilenko/changecalc branch from a081a1e to b0c4645 Compare December 6, 2024 15:27
@denik denik temporarily deployed to test-trigger-is December 6, 2024 15:27 — with GitHub Actions Inactive
@denik denik temporarily deployed to test-trigger-is December 6, 2024 15:28 — with GitHub Actions Inactive
@denik
Copy link
Contributor Author

denik commented Dec 6, 2024

Example test:

  1. Checkout this branch
  2. Modify changecalc.yml to point to this branch
  3. Modify bundle/config/mutator/load_git_details.go
% git diff
diff --git a/bundle/config/mutator/load_git_details.go b/bundle/config/mutator/load_git_details.go
index 82255552..fe4a2639 100644
--- a/bundle/config/mutator/load_git_details.go
+++ b/bundle/config/mutator/load_git_details.go
@@ -21,6 +21,7 @@ func (m *loadGitDetails) Name() string {
 }

 func (m *loadGitDetails) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
+
        var diags diag.Diagnostics
        info, err := git.FetchRepositoryInfo(ctx, b.BundleRoot.Native(), b.WorkspaceClient())
        if err != nil {
diff --git a/changecalc.yml b/changecalc.yml
index d4586f44..d95ae7fb 100644
--- a/changecalc.yml
+++ b/changecalc.yml
@@ -1,4 +1,4 @@
-base_branch: main
+base_branch: denis.bilenko/changecalc
 go_mod: go.mod
 reset_list:
   - go.mod
  1. Use 'make viewchanges' to preview affected packages
% make viewchanges | wc -l                                                                                                                                 denis.bilenko/changecalc*
     148
  1. Run unit tests over affected packages only:
% make testchanges | tail -n 1
DONE 821 tests, 265 skipped in 9.188s
  1. Compare with running all unit tests:
% make test | tail -n 1
DONE 2318 tests, 280 skipped in 24.037s

@denik denik temporarily deployed to test-trigger-is December 6, 2024 15:40 — with GitHub Actions Inactive
@denik denik temporarily deployed to test-trigger-is December 6, 2024 15:41 — with GitHub Actions Inactive
@denik denik temporarily deployed to test-trigger-is December 6, 2024 15:47 — with GitHub Actions Inactive
@denik denik temporarily deployed to test-trigger-is December 6, 2024 15:47 — with GitHub Actions Inactive
Run make testchanges
cat: changed-packages.txt: No such file or directory
✓ Running tests based on changes relative to main...
changecalc/changecalc > changed-packages.txt || echo "./..." > changed-packages.txt
fatal: bad revision 'main'
failed to execute [git diff main --name-only -- .]: exit status 128
gotestsum --format pkgname-and-test-fails --no-summary=skipped --raw-command go test -v -json -short -coverprofile=coverage.txt
✓  . (32ms) (coverage: 0.0% of statements)
@denik denik temporarily deployed to test-trigger-is December 6, 2024 15:53 — with GitHub Actions Inactive
@denik denik temporarily deployed to test-trigger-is December 6, 2024 15:53 — with GitHub Actions Inactive
Getting this error

changecalc/changecalc > changed-packages.txt || echo "./..." > changed-packages.txt
fatal: bad revision 'main'
failed to execute [git diff main --name-only -- .]: exit status 128

https://stackoverflow.com/a/76769122
@denik denik temporarily deployed to test-trigger-is December 6, 2024 15:58 — with GitHub Actions Inactive
@denik denik temporarily deployed to test-trigger-is December 6, 2024 15:58 — with GitHub Actions Inactive
@denik denik temporarily deployed to test-trigger-is December 6, 2024 16:01 — with GitHub Actions Inactive
@denik denik temporarily deployed to test-trigger-is December 6, 2024 16:01 — with GitHub Actions Inactive
@denik denik temporarily deployed to test-trigger-is December 6, 2024 16:07 — with GitHub Actions Inactive
@denik denik temporarily deployed to test-trigger-is December 6, 2024 16:07 — with GitHub Actions Inactive
@denik denik temporarily deployed to test-trigger-is December 6, 2024 16:08 — with GitHub Actions Inactive
@denik denik temporarily deployed to test-trigger-is December 6, 2024 16:09 — with GitHub Actions Inactive
@denik denik temporarily deployed to test-trigger-is December 6, 2024 16:11 — with GitHub Actions Inactive
@denik denik temporarily deployed to test-trigger-is December 6, 2024 16:11 — with GitHub Actions Inactive
@denik denik temporarily deployed to test-trigger-is December 6, 2024 16:22 — with GitHub Actions Inactive
@denik denik temporarily deployed to test-trigger-is December 6, 2024 16:22 — with GitHub Actions Inactive
@denik denik force-pushed the denis.bilenko/changecalc branch from 64b0958 to bc6fa65 Compare December 6, 2024 16:23
@denik denik temporarily deployed to test-trigger-is December 6, 2024 16:23 — with GitHub Actions Inactive
@denik denik temporarily deployed to test-trigger-is December 6, 2024 16:23 — with GitHub Actions Inactive
@denik denik temporarily deployed to test-trigger-is December 6, 2024 16:27 — with GitHub Actions Inactive
Copy link

github-actions bot commented Dec 6, 2024

If integration tests don't run automatically, an authorized user can run them manually by following the instructions below:

Trigger:
go/deco-tests-run/cli

Inputs:

  • PR number: 1975
  • Commit SHA: 41cc96c0fc0dc3f358dae0a83c8651ff46f4b164

Checks will be approved automatically on success.

@denik denik temporarily deployed to test-trigger-is December 6, 2024 16:27 — with GitHub Actions Inactive
@eng-dev-ecosystem-bot
Copy link
Collaborator

Test Details: go/deco-tests/12202420953

Copy link
Contributor

@shreyas-goenka shreyas-goenka left a comment

Choose a reason for hiding this comment

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

Thanks! This would be nice to have for local development but I'm not sure we can use this for our unit testing workflow since there might be implicit dependencies that are not captured by the import dependencies.

Could you clarify the intention behind adding this functionality?

- windows-latest

steps:
- name: Checkout repository and submodules
Copy link
Contributor

Choose a reason for hiding this comment

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

Create a reusable workflow for the first 5 steps since they are the same for both all and partial unit tests?
https://dev.to/n3wt0n/avoid-duplication-github-actions-reusable-workflows-3ae8

Copy link
Contributor

Choose a reason for hiding this comment

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

The command to run can be passed as an arg to the common workflow.

Copy link
Contributor

Choose a reason for hiding this comment

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

The intent is to replace the existing one with this one, so we can keep the dup for a little while.

@@ -57,6 +57,57 @@ jobs:
- name: Publish test coverage
uses: codecov/codecov-action@v4

tests-changes:
Copy link
Contributor

Choose a reason for hiding this comment

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

I presume the intention behind this is to facilitate faster local development since now you can simply run make testchanges? For the push workflow we would still need to run everything since import dependencies are not enough to track all relevant tests.

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a comment here explaining the intention of running this as a workflow? Presumably to ensure the test changes section works correctly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The intention for this tool to be used for running both unit tests and integration tests on CI. It can save significant time, especially in this cases:

  • test only change; in that case
  • change is local affects only set of tests (e.g. bundle tests).

In that case we won't need 2 workflows, only workflow via this tool will remain. Engineers will be able to opt out, e.g. by adding /fulltests in description or comment.

For the push workflow we would still need to run everything since import dependencies are not enough to track all relevant tests.

Can you give counter-example? Note, it also establishes data dependencies (with some assumptions).

Copy link
Contributor

Choose a reason for hiding this comment

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

changecalc can be a package in internal instead of a top-level CLI package.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1, or "tools". I'm sure there are more we'd like to check in and take advantage of.

As long as it is not top-level.

// GetChangedFiles compares the current branch to the base branch
// and returns a slice of file paths that have been modified.
func GetChangedFiles(baseBranch string) ([]string, error) {
command := []string{"git", "diff", baseBranch, "--name-only", "--", "."}
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we use git status here if this is meant to facilitate local development?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure what you mean? Could you clarify how "git status" would be used?

Copy link
Contributor

@pietern pietern left a comment

Choose a reason for hiding this comment

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

Looks cool!

- windows-latest

steps:
- name: Checkout repository and submodules
Copy link
Contributor

Choose a reason for hiding this comment

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

The intent is to replace the existing one with this one, so we can keep the dup for a little while.

xargs gotestsum --format pkgname-and-test-fails --no-summary=skipped --raw-command go test -v -json -short -coverprofile=coverage.txt < affected-packages.txt

changecalc/changecalc: changecalc/*.go
@go build -o changecalc/changecalc changecalc/main.go
Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer to not have targets for Go builds if we can rely on go run to do the caching and invalidation for us.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1, or "tools". I'm sure there are more we'd like to check in and take advantage of.

As long as it is not top-level.

@@ -0,0 +1,6 @@
base_branch: refs/remotes/origin/main
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to configure this in a config file?

}
}
return false
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Some of these functions could move into a dedicated and testable package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants