Skip to content

Commit f2ee0d3

Browse files
authored
fix: go-mod-validator changed files bug (#1312)
* fix: diff endpoint for go-mod-validator * docs: update README, delete outdated example workflow * chore: update test payload * add changesetf
1 parent 0c19bf6 commit f2ee0d3

10 files changed

Lines changed: 2707 additions & 4618 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"go-mod-validator": minor
3+
---
4+
5+
fix: changed files endpoint bug

apps/go-mod-validator/README.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,32 @@ Requirements:
2323
upstream repositories.
2424
2. Exits 1, if a _dependency is not on the default branch_
2525

26-
## Example usage
26+
## Usage
27+
28+
### Org-wide Workflow
29+
30+
To integrate this into your repository, it is best to use the org-wide workflow.
31+
Ask in `#team-releng` to have it enabled for you. If your repository has private
32+
dependencies, then you will have to configurate the workflow yourself.
33+
34+
Here is the org-wide workflow:
35+
https://github.com/smartcontractkit/gha-org-workflows/blob/main/.github/workflows/go-mod-validation.yml
36+
37+
### Workflow Excerpt
2738

2839
```yaml
2940
steps:
3041
- name: Check out the repository
31-
uses: actions/checkout
42+
uses: actions/checkout@<version>
43+
3244
- name: Setup go
33-
uses: actions/setup-go
45+
uses: actions/setup-go@<version>
46+
3447
- name: Validate go.mod
35-
uses: smartcontractkit/.github/apps/go-mod-validator@<commit> # go-mod-validator@x.y.z
48+
uses: smartcontractkit/.github/apps/go-mod-validator@go-mod-validator/<version>
3649
```
3750
38-
## Running locally
51+
### Running locally
3952
4053
1. Update `scripts/test.sh` and `scripts/payload.json`
4154
2. Make sure to check out your local repo to the proper commit as per

apps/go-mod-validator/dist/index.js

Lines changed: 2650 additions & 4567 deletions
Large diffs are not rendered by default.

apps/go-mod-validator/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
"typescript": "^5.4.5"
2525
},
2626
"dependencies": {
27-
"@actions/core": "^1.10.1",
28-
"@actions/github": "^6.0.0",
27+
"@actions/core": "^1.11.1",
28+
"@actions/github": "^6.0.1",
2929
"@actions/glob": "^0.4.0",
3030
"@octokit/plugin-throttling": "^11.0.1",
3131
"@octokit/types": "^13.5.0",

apps/go-mod-validator/scripts/payload.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"pull_request": {
2+
"//pull_request": {
33
"base": { "sha": "1f5fbda7ae76d5494b9864db90a8bfe7183db5fb" },
44
"head": { "sha": "04c256fe307ced678afbebb5cf87abab747ccb8b" },
55
"number": 773

apps/go-mod-validator/scripts/test.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ pnpm nx build go-mod-validator
99

1010
export GITHUB_TOKEN=$(gh auth token)
1111
export GITHUB_ACTOR=$(gh api user --jq .login)
12-
export GITHUB_REPOSITORY="smartcontractkit/chainlink-common"
13-
export GITHUB_EVENT_NAME="pull_request"
12+
export GITHUB_REPOSITORY="smartcontractkit/billing-platform-service"
13+
export GITHUB_EVENT_NAME="push"
1414
export GITHUB_EVENT_PATH="apps/go-mod-validator/scripts/payload.json"
1515

1616

1717
tmp_file=$(mktemp)
1818
export GITHUB_STEP_SUMMARY="$tmp_file"
1919

2020
export INPUT_GITHUB_TOKEN="$GITHUB_TOKEN"
21-
export INPUT_GO_MOD_DIR="${INPUT_GO_MOD_DIR:-$(abspath "${REPO_ROOT}/../chainlink-common")}"
21+
export INPUT_GO_MOD_DIR="${INPUT_GO_MOD_DIR:-$(abspath "${REPO_ROOT}/../billing-platform-service")}"
2222
export INPUT_DEP_PREFIX="github.com/smartcontractkit"
2323
export CL_LOCAL_DEBUG="true"
2424

apps/go-mod-validator/src/diff.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ export type GithubFiles = CompareResponse["data"]["files"];
88

99
export async function getChangedGoModFiles(
1010
gh: Octokit,
11-
base: string,
12-
head: string,
11+
prNumber: number,
1312
owner: string,
1413
repo: string,
1514
depPrefix: string,
1615
): Promise<ParsedFile[]> {
17-
const files = await getComparison(gh, owner, repo, base, head);
16+
const files = await getChangedFilesForPR(gh, owner, repo, prNumber);
1817
const relevantFiles = filterForRelevantChanges(files);
1918
return parseAllAdditions(relevantFiles, depPrefix);
2019
}
@@ -23,23 +22,22 @@ function filterForRelevantChanges(files: GithubFiles): GithubFiles {
2322
return files?.filter(({ filename }) => filename.endsWith("go.mod"));
2423
}
2524

26-
async function getComparison(
25+
export async function getChangedFilesForPR(
2726
octokit: Octokit,
2827
owner: string,
2928
repo: string,
30-
base: string,
31-
head: string,
29+
prNumber: number,
3230
): Promise<GithubFiles> {
33-
core.debug(`Comparing ${owner}/${repo} commits ${base}...${head}`);
31+
core.debug(`Comparing ${owner}/${repo} for PR ${prNumber}`);
3432

35-
const diff = await octokit.rest.repos.compareCommitsWithBasehead({
33+
const prFiles = await octokit.paginate(octokit.rest.pulls.listFiles, {
3634
owner,
3735
repo,
38-
basehead: `${base}...${head}`,
39-
// <before>...<after> or <earlier>...<later>
36+
pull_number: prNumber,
37+
per_page: 100,
4038
});
4139

42-
return diff.data.files;
40+
return prFiles;
4341
}
4442

4543
// Taken from https://github.com/smartcontractkit/.github/blob/dc8b1a0b478151119d86ac1bf121ea7eb3c1c88c/actions/gha-workflow-validator/src/utils.ts#L59C1-L97C2

apps/go-mod-validator/src/go-mod-validator.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,39 +64,40 @@ function getContext() {
6464
export async function run(): Promise<string> {
6565
const { goModDir, octokit, depPrefix, isPullRequest } = getContext();
6666

67+
core.debug(`Go module directory: ${goModDir}`);
68+
core.debug(`Dependency prefix filter: ${depPrefix || "none"}`);
69+
core.debug(`Pull request mode: ${isPullRequest}`);
70+
6771
let depsToValidate = await getDeps(goModDir, depPrefix);
6872
if (isPullRequest) {
6973
core.info(
7074
"Running in pull request mode, filtering dependencies to validate based on changed files and only checking for pseudo-versions.",
7175
);
7276
const pr = github.context.payload.pull_request;
73-
if (!pr) {
77+
if (!pr || !pr.number) {
7478
throw new Error("Expected pull request context to be present");
7579
}
76-
const base: string = pr.base.sha;
77-
const head: string = pr.head.sha;
78-
const { owner, repo } = github.context.repo;
7980

81+
const { owner, repo } = github.context.repo;
8082
const changedFiles = await getChangedGoModFiles(
8183
octokit,
82-
base,
83-
head,
84+
pr.number,
8485
owner,
8586
repo,
8687
depPrefix,
8788
);
8889

8990
core.debug(
90-
`Changed files: ${JSON.stringify(changedFiles.map((f) => f.filename))}`,
91+
`Filtered changed files: ${JSON.stringify(changedFiles.map((f) => f.filename))}`,
9192
);
9293
core.debug(
9394
`Deps to validate: ${JSON.stringify(depsToValidate.map((d) => d.path))}`,
9495
);
9596
depsToValidate = depsToValidate.filter((d) => {
9697
return changedFiles.some(
97-
(f) =>
98-
d.goModFilePath.includes(f.filename) &&
99-
f.addedLines.some((l) => l.content.includes(d.path)),
98+
(changedFile) =>
99+
d.goModFilePath.includes(changedFile.filename) &&
100+
changedFile.addedLines.some((l) => l.content.includes(d.path)),
100101
);
101102
});
102103

apps/go-mod-validator/workflows/validate.yml

Lines changed: 0 additions & 13 deletions
This file was deleted.

pnpm-lock.yaml

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)