diff --git a/README.md b/README.md index 40b0778..8caf31c 100644 --- a/README.md +++ b/README.md @@ -23,9 +23,8 @@ It then injects the needed changes in the code to use the other Pull Requests. ### Go lang For a go lang change, the action is adding replace directives for the -dependencies inside the `go.mod` file. It is not running `go mod tidy` -and it is expecting it to be run by your build automation in a later -stage. +dependencies inside the `go.mod` file. This action needs to be placed +after the installation of the Go lang toolchain. ### Python @@ -58,6 +57,8 @@ jobs: - name: Checkout code uses: actions/checkout@master + # install the toolchain for your language + - name: Extract dependent Pull Requests uses: depends-on/depends-on-action@main with: diff --git a/golang.py b/golang.py index 657b579..0c4094c 100644 --- a/golang.py +++ b/golang.py @@ -19,32 +19,32 @@ def process_golang(main_dir, dirs, container_mode): github_mods.append(match.group(2)) # add the replace directives to go.mod for the local dependencies nb_replace = 0 - with open(go_mod, "a", encoding="UTF-8") as out_stream: - for mod in github_mods: - if mod in dirs: - if container_mode: - # remove https:// at the beginning of the url and .git at the end - fork_url = ( - dirs[mod]["fork_url"] - .replace("https://", "", 1) - .replace(".git", "", 1) - ) - print( - f'Adding replace directive in go.mod for {mod} => {fork_url} {dirs[mod]["branch"]}', - file=sys.stderr, - ) - # do not use "go mod edit -replace" as go could be not installed at this stage - out_stream.write( - f'replace {mod} => {fork_url} {dirs[mod]["branch"]}\n' - ) - else: - print( - f'Adding replace directive in go.mod for {mod} => {dirs[mod]["path"]}', - file=sys.stderr, - ) - # do not use "go mod edit -replace" as go could be not installed at this stage - out_stream.write(f'replace {mod} => {dirs[mod]["path"]}\n') - nb_replace += 1 + for mod in github_mods: + if mod in dirs: + if container_mode: + # remove https:// at the beginning of the url and .git at the end + fork_url = ( + dirs[mod]["fork_url"] + .replace("https://", "", 1) + .replace(".git", "", 1) + ) + print( + f'Adding replace directive in go.mod for {mod} => {fork_url} {dirs[mod]["branch"]}', + file=sys.stderr, + ) + os.system( + f"set -x; go mod edit -replace {mod}={fork_url}@{dirs[mod]['branch']}" + ) + else: + print( + f'Adding replace directive in go.mod for {mod} => {dirs[mod]["path"]}', + file=sys.stderr, + ) + os.system(f"set -x; go mod edit -replace {mod}={dirs[mod]['path']}") + nb_replace += 1 + # if there is any change to go.mod, `go mod tidy` needs to be called to have a correct go.sums + if nb_replace > 0: + os.system("set -x; go mod tidy") return nb_replace > 0