Skip to content

Commit

Permalink
Merge pull request #27 from depends-on/golang-native-tools
Browse files Browse the repository at this point in the history
use go mod edit for Go lang changes
  • Loading branch information
fredericlepied authored Oct 9, 2023
2 parents 9bb754f + b122bec commit bbcd939
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
52 changes: 26 additions & 26 deletions golang.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit bbcd939

Please sign in to comment.