Skip to content

Commit 474ef14

Browse files
cgundybasvandijk
andauthored
chore(IDX): replace changed files step with gh action (#155) (#156)
* chore(IDX): replace changed files step with gh action (#155) * chore(IDX): update README, introduce git flow (#131) * refactor(IDX): change triggers (#130) * refactor(IDX): change triggers * Update .github/workflows/internal_vs_external.yml Co-authored-by: Bas van Dijk <bas@van.dijk.ch> --------- Co-authored-by: Bas van Dijk <bas@van.dijk.ch> * chore(IDX): combine workflow files (#129) * refactor(IDX): rename workflows (#133) * refactor(IDX): update bot policies workflow (#134) * Merge Develop Into Main (#132) (#136) * Merge main into develop (#140) * Merge Develop Into Main (#132) * chore(IDX): update README, introduce git flow (#131) * refactor(IDX): change triggers (#130) * refactor(IDX): change triggers * Update .github/workflows/internal_vs_external.yml Co-authored-by: Bas van Dijk <bas@van.dijk.ch> --------- Co-authored-by: Bas van Dijk <bas@van.dijk.ch> * chore(IDX): combine workflow files (#129) * refactor(IDX): rename workflows (#133) --------- Co-authored-by: Bas van Dijk <bas@van.dijk.ch> * chore(IDX): refactor bot policies (#138) --------- Co-authored-by: Bas van Dijk <bas@van.dijk.ch> * chore(IDX): update cla workflow (#139) * Merge Develop Into Main (#132) * chore(IDX): update README, introduce git flow (#131) * refactor(IDX): change triggers (#130) * refactor(IDX): change triggers * Update .github/workflows/internal_vs_external.yml Co-authored-by: Bas van Dijk <bas@van.dijk.ch> --------- Co-authored-by: Bas van Dijk <bas@van.dijk.ch> * chore(IDX): combine workflow files (#129) * refactor(IDX): rename workflows (#133) --------- Co-authored-by: Bas van Dijk <bas@van.dijk.ch> * chore(IDX): refactor bot policies (#138) * chore(IDX): update cla workflow --------- Co-authored-by: Bas van Dijk <bas@van.dijk.ch> * refactor(IDX): replace checkout with github action * remove test * add mock * fix test * update * update * mock gh login * update test * remove * set use_rest_api --------- Co-authored-by: Bas van Dijk <bas@van.dijk.ch> * Update repo_policies.yml * Update check_is_bot.yml * split by whitespace * update test * remove pin to develop * remove pin to develop --------- Co-authored-by: Bas van Dijk <bas@van.dijk.ch>
1 parent 10d46ad commit 474ef14

File tree

4 files changed

+32
-116
lines changed

4 files changed

+32
-116
lines changed

.github/workflows/check_cla_ruleset.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,6 @@ jobs:
7676
labels: ["external-contributor"]
7777
})
7878
79-
- name: Checkout
80-
uses: actions/checkout@v4
81-
if: ${{ steps.accepts_external_contrib.outputs.accepts_contrib != 'false' }}
82-
with:
83-
repository: 'dfinity/public-workflows'
84-
8579
- name: Check CLA
8680
id: check-cla
8781
run: |

.github/workflows/repo_policies.yml

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,17 @@ jobs:
2222
repository: dfinity/public-workflows
2323
path: public-workflows
2424

25-
# Then switch back to this repository to make sure it's run from current
26-
- name: Checkout Original Repository
27-
uses: actions/checkout@v4
28-
with:
29-
path: current-repo # need to specify another path to avoid overwriting the first checkout
30-
repository: ${{ github.event.pull_request.head.repo.full_name }}
31-
ref: ${{ github.head_ref }}
32-
fetch-depth: 100
33-
3425
- name: Python Setup
3526
uses: ./public-workflows/.github/workflows/python-setup
3627
with:
3728
working-directory: public-workflows
3829

30+
- name: Get changed files
31+
id: changed-files
32+
uses: tj-actions/changed-files@ed68ef82c095e0d48ec87eccea555d944a631a4c # v46.0.5
33+
with:
34+
use_rest_api: 'true'
35+
3936
- name: Bot Checks
4037
id: bot-checks
4138
run: |
@@ -44,10 +41,8 @@ jobs:
4441
python public-workflows/reusable_workflows/repo_policies/check_bot_approved_files.py
4542
shell: bash
4643
env:
44+
CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
4745
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} # no actual token stored, read-only permissions
4846
GH_ORG: ${{ github.repository_owner }}
4947
USER: ${{ github.event.pull_request.user.login }}
5048
REPO: ${{ github.event.repository.name }}
51-
MERGE_BASE_SHA: ${{ github.event.pull_request.base.sha }}
52-
BRANCH_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
53-
REPO_PATH: current-repo

reusable_workflows/repo_policies/check_bot_approved_files.py

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,11 @@
11
import fnmatch
2-
import subprocess
3-
from typing import Optional
42

53
import github3
64

75
from shared.utils import download_gh_file, load_env_vars
86

97
BOT_APPROVED_FILES_PATH = ".github/repo_policies/BOT_APPROVED_FILES"
10-
REQUIRED_ENV_VARS = [
11-
"USER",
12-
"GH_TOKEN",
13-
"GH_ORG",
14-
"REPO",
15-
"REPO_PATH",
16-
"MERGE_BASE_SHA",
17-
"BRANCH_HEAD_SHA",
18-
]
19-
20-
21-
def get_changed_files(
22-
merge_base_sha: str, branch_head_sha: str, repo_path: Optional[str] = None
23-
) -> list[str]:
24-
"""
25-
Compares the files changed in the current branch to the merge base.
26-
"""
27-
commit_range = f"{merge_base_sha}..{branch_head_sha}"
28-
result = subprocess.run(
29-
["git", "diff", "--name-only", commit_range],
30-
capture_output=True,
31-
text=True,
32-
cwd=repo_path,
33-
)
34-
if result.returncode != 0:
35-
raise RuntimeError(
36-
f"git diff failed with exit code {result.returncode}: {result.stderr}"
37-
)
38-
changed_files = result.stdout.strip().split("\n")
39-
return changed_files
8+
REQUIRED_ENV_VARS = ["CHANGED_FILES", "USER", "GH_TOKEN", "GH_ORG", "REPO"]
409

4110

4211
def get_approved_files_config(repo: github3.github.repo) -> str:
@@ -51,11 +20,11 @@ def get_approved_files_config(repo: github3.github.repo) -> str:
5120
f"No config file found. Make sure you have a file saved at {BOT_APPROVED_FILES_PATH} in the default branch"
5221
)
5322

54-
55-
def get_approved_files(config_file: str) -> list[str]:
23+
def get_approved_files(repo: github3.github.repo) -> list[str]:
5624
"""
5725
Extracts the list of approved files from the config file.
5826
"""
27+
config_file = get_approved_files_config(repo)
5928
approved_files = [
6029
line for line in config_file.splitlines() if line.strip() and not line.strip().startswith("#")
6130
]
@@ -77,12 +46,8 @@ def check_if_pr_is_blocked(env_vars: dict) -> None:
7746
"""
7847
gh = github3.login(token=env_vars["GH_TOKEN"])
7948
repo = gh.repository(owner=env_vars["GH_ORG"], repository=env_vars["REPO"])
80-
repo_path = env_vars["REPO_PATH"]
81-
changed_files = get_changed_files(
82-
env_vars["MERGE_BASE_SHA"], env_vars["BRANCH_HEAD_SHA"], repo_path
83-
)
84-
config = get_approved_files_config(repo)
85-
approved_files = get_approved_files(config)
49+
approved_files = get_approved_files(repo)
50+
changed_files = env_vars["CHANGED_FILES"].split()
8651
block_pr = not check_files_in_approved_list(changed_files, approved_files)
8752
print(f"changed_files: {changed_files}")
8853
print(f"approved_files: {approved_files}")

reusable_workflows/tests/test_repo_policies.py

Lines changed: 20 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,9 @@
99
check_if_pr_is_blocked,
1010
get_approved_files,
1111
get_approved_files_config,
12-
get_changed_files,
1312
)
1413

1514

16-
@mock.patch("repo_policies.check_bot_approved_files.subprocess.run")
17-
def test_get_changed_files(mock_subprocess_run):
18-
mock_subprocess_run.return_value = mock.Mock(
19-
stdout="file1.py\nfile2.py\n", returncode=0, stderr=""
20-
)
21-
22-
changed_files = get_changed_files("merge_base_sha", "branch_head_sha")
23-
24-
assert changed_files == ["file1.py", "file2.py"]
25-
mock_subprocess_run.assert_called_once_with(
26-
["git", "diff", "--name-only", "merge_base_sha..branch_head_sha"],
27-
capture_output=True,
28-
text=True,
29-
cwd=None,
30-
)
31-
32-
3315
@mock.patch("repo_policies.check_bot_approved_files.download_gh_file")
3416
def test_get_approved_files_config(download_gh_file):
3517
repo = mock.Mock()
@@ -56,20 +38,26 @@ def test_get_approved_files_config_fails(download_gh_file):
5638
)
5739

5840

59-
def test_get_approved_files():
41+
@mock.patch("repo_policies.check_bot_approved_files.get_approved_files_config")
42+
def test_get_approved_files(get_approved_files_config):
6043
config_file = open(
6144
"reusable_workflows/tests/test_data/BOT_APPROVED_FILES", "r"
6245
).read()
63-
approved_files = get_approved_files(config_file)
46+
get_approved_files_config.return_value = config_file
47+
repo = mock.Mock()
48+
approved_files = get_approved_files(repo)
6449

6550
assert approved_files == ["file1", "file2", "folder/*.txt"]
6651

6752

68-
def get_test_approved_files():
53+
@mock.patch("repo_policies.check_bot_approved_files.get_approved_files_config")
54+
def get_test_approved_files(get_approved_files_config):
6955
config_file = open(
7056
"reusable_workflows/tests/test_data/BOT_APPROVED_FILES", "r"
7157
).read()
72-
approved_files = get_approved_files(config_file)
58+
get_approved_files_config.return_value = config_file
59+
repo = mock.Mock()
60+
approved_files = get_approved_files(repo)
7361
return approved_files
7462

7563

@@ -95,62 +83,36 @@ def test_check_files_in_approved_list_fails():
9583
assert not check_files_in_approved_list(changed_files, approved_files)
9684

9785

98-
@mock.patch("repo_policies.check_bot_approved_files.get_changed_files")
99-
@mock.patch(
100-
"repo_policies.check_bot_approved_files.get_approved_files_config"
101-
)
86+
@mock.patch("repo_policies.check_bot_approved_files.get_approved_files")
10287
@mock.patch("github3.login")
103-
def test_pr_is_blocked_false(gh_login, get_approved_files_config, get_changed_files):
88+
def test_pr_is_blocked_false(gh_login, get_approved_files):
10489
env_vars = {
90+
"CHANGED_FILES": "file1 file2",
10591
"GH_TOKEN": "token",
10692
"GH_ORG": "org",
10793
"REPO": "repo",
108-
"REPO_PATH": "path",
109-
"MERGE_BASE_SHA": "base",
110-
"BRANCH_HEAD_SHA": "head",
11194
}
11295
gh = mock.Mock()
11396
gh_login.return_value = gh
114-
repo = mock.Mock()
115-
gh.repository.return_value = repo
116-
get_changed_files.return_value = ["file1", "file2"]
117-
config_file = open(
118-
"reusable_workflows/tests/test_data/BOT_APPROVED_FILES", "r"
119-
).read()
120-
get_approved_files_config.return_value = config_file
97+
approved_files = get_test_approved_files()
98+
get_approved_files.return_value = approved_files
12199

122100
check_if_pr_is_blocked(env_vars)
123101

124-
get_changed_files.assert_called_once_with("base", "head", "path")
125-
get_approved_files_config.assert_called_once_with(repo)
126102

127-
128-
@mock.patch("repo_policies.check_bot_approved_files.get_changed_files")
129-
@mock.patch(
130-
"repo_policies.check_bot_approved_files.get_approved_files_config"
131-
)
103+
@mock.patch("repo_policies.check_bot_approved_files.get_approved_files")
132104
@mock.patch("github3.login")
133-
def test_pr_is_blocked_true(gh_login, get_approved_files_config, get_changed_files):
105+
def test_pr_is_blocked_true(gh_login, get_approved_files):
134106
env_vars = {
107+
"CHANGED_FILES": "file1 file2 file3",
135108
"GH_TOKEN": "token",
136109
"GH_ORG": "org",
137110
"REPO": "repo",
138-
"REPO_PATH": "path",
139-
"MERGE_BASE_SHA": "base",
140-
"BRANCH_HEAD_SHA": "head",
141111
}
142112
gh = mock.Mock()
143113
gh_login.return_value = gh
144-
repo = mock.Mock()
145-
gh.repository.return_value = repo
146-
get_changed_files.return_value = ["file1", "file2", "file3"]
147-
config_file = open(
148-
"reusable_workflows/tests/test_data/BOT_APPROVED_FILES", "r"
149-
).read()
150-
get_approved_files_config.return_value = config_file
114+
approved_files = get_test_approved_files()
115+
get_approved_files.return_value = approved_files
151116

152117
with pytest.raises(SystemExit):
153118
check_if_pr_is_blocked(env_vars)
154-
155-
get_changed_files.assert_called_once_with("base", "head", "path")
156-
get_approved_files_config.assert_called_once_with(repo)

0 commit comments

Comments
 (0)