Skip to content

Commit

Permalink
fixing ci scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
tenmoves committed Mar 3, 2023
1 parent dba09ee commit f0f0c39
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 16 deletions.
54 changes: 50 additions & 4 deletions lib/archethic/governance/code.ex
Original file line number Diff line number Diff line change
Expand Up @@ -153,28 +153,74 @@ defmodule Archethic.Governance.Code do
"""
@spec applicable_proposal?(Proposal.t()) :: boolean()
def applicable_proposal?(
%Proposal{changes: changes, address: address},
proposal,
src_dir \\ @src_dir
) do
res = apply_diff(proposal, src_dir, false)
match?({_, 0}, res)
end

# @spec apply_proposal(Proposal.t()) :: :ok
# def apply_proposal(
# proposal = %Proposal{description: description, address: address},
# src_dir \\ @src_dir
# ) do
# random = :crypto.strong_rand_bytes(4) |> Base.encode16()
# branch_name = "prop_#{random}_#{Base.encode16(address)}"

# cmd_options = [stderr_to_stdout: true, cd: src_dir]
# git_fn = fn args -> System.cmd("git", args, cmd_options) end

# with {:apply_diff, {_, 0}} <- {:apply_diff, apply_diff(proposal, src_dir, true)},
# {:git_checkout, {_, 0}} <- {:git_checkout, git_fn.(["checkout", "-b", branch_name])},
# {:git_add, {_, 0}} <- {:git_add, git_fn.(["add", "--all"])},
# {:git_commit, {_, 0}} <- {:git_commit, git_fn.(["commit", "-m", description])} do
# :ok
# else
# {:apply_diff, {_, error_code}} ->
# raise "apply_diff_to_current_branch failed with error code #{error_code}"

# {:git_checkout, {_, error_code}} ->
# raise "git_checkout failed with error code #{error_code}"

# {:git_add, {_, error_code}} ->
# raise "git_add failed with error code #{error_code}"

# {:git_commit, {_, error_code}} ->
# raise "git_commit failed with error code #{error_code}"
# end
# end

defp apply_diff(
%Proposal{changes: changes, address: address},
src_dir,
persist?
) do
random = :crypto.strong_rand_bytes(4) |> Base.encode16()
prop_file = Path.join(System.tmp_dir!(), "prop_#{random}_#{Base.encode16(address)}")
File.write!(prop_file, changes)

cmd_options = [stderr_to_stdout: true, cd: src_dir]
git = fn args -> System.cmd("git", args, cmd_options) end

git_args =
if persist? do
["apply", prop_file]
else
["apply", "--check", prop_file]
end

res =
case status() do
{:clean, _} ->
git.(["apply", "--check", prop_file])
git.(git_args)

otherwise ->
{:error, otherwise}
end

File.rm(prop_file)

match?({_, 0}, res)
res
end

@doc """
Expand Down
21 changes: 18 additions & 3 deletions lib/archethic/governance/code/cicd/docker/cicd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ defmodule Archethic.Governance.Code.CICD.Docker do
end

@impl CICD
def run_ci!(prop = %Proposal{}) do
def run_ci!(prop = %Proposal{changes: changes}) do
File.write!("./proposal.diff", changes)
run!(prop, @ci_image, @ci_conductor, &do_run_docker_ci/1, "CI failed")
File.rm!("./proposal.diff")
end

@impl CICD
Expand Down Expand Up @@ -127,10 +129,23 @@ defmodule Archethic.Governance.Code.CICD.Docker do

@ci_script "/opt/code/scripts/governance/proposal_ci_job.sh"

defp do_run_docker_ci(%Proposal{address: address, changes: changes}) do
defp do_run_docker_ci(%Proposal{address: address, changes: changes, description: description}) do
Logger.info("Verify proposal", address: Base.encode16(address))
name = container_name(address)
args = ["run", "--entrypoint", @ci_script, "-i", "--name", name, "archethic-ci", name]

args = [
"run",
"--entrypoint",
@ci_script,
"-i",
"--name",
name,
"archethic-ci",
name,
description,
address
]

port = Port.open({:spawn_executable, System.find_executable("docker")}, [:binary, args: args])

# wait 250 ms or fail sooner
Expand Down
16 changes: 9 additions & 7 deletions scripts/governance/mk-code-proposal.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
@@ -1,35 +1,38 @@
#!/bin/bash

# Assuming that the current branch is a feature branch and the most recent
# version of the source code is on the master branch, this script creates
# code-proposal.patch file.
# version of the source code is on the default_main_branch branch,
# this script creates code-proposal.patch file.

default_main_branch="fixing_governance_ci"
status=$(git status --porcelain)

if [ -n "$status" ]; then
Expand All @@ -12,7 +14,7 @@ if [ -n "$status" ]; then
fi

feature_branch=$(git rev-parse --abbrev-ref HEAD)
feature_log=$(git log --format=" * %B" master..)
feature_log=$(git log --format=" * %B" ${default_main_branch}..)
summary="📦 ${feature_branch}\n\n${feature_log}"

cleanup() { git checkout $feature_branch; }
Expand All @@ -21,15 +23,15 @@ trap cleanup ERR

set -e

git checkout master
git checkout ${default_main_branch}

git merge --squash $feature_branch

if (echo -e "$summary" |\
git commit --author="archethic <dev@archethic.net>" --no-gpg-sign --edit -F-)
then
git format-patch --stdout master^ > code-proposal.patch
git reset --hard master^
git format-patch --stdout ${default_main_branch}^ > code-proposal.patch
git reset --hard ${default_main_branch}^
fi

git checkout $feature_branch
git checkout $feature_branch
11 changes: 9 additions & 2 deletions scripts/governance/proposal_ci_job.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
set -e

PROPOSAL_ADDRESS=$1
PROPOSAL_FILENAME=/tmp/proposal
PROPOSAL_DESCRIPTION=$2
PROPOSAL_FILENAME=./proposal.diff

echo "=== Test proposal ${PROPOSAL_ADDRESS}"
tee ${PROPOSAL_FILENAME}
Expand All @@ -12,7 +13,13 @@ echo "=== Create branch ${PROPOSAL_ADDRESS}"
git checkout -b "prop_${PROPOSAL_ADDRESS}"

echo "=== Apply patch ${PROPOSAL_FILENAME}"
git am ${PROPOSAL_FILENAME} --committer-date-is-author-date --no-gpg-sign
git apply ${PROPOSAL_FILENAME}

echo "=== git add files"
git add --all

echo "=== git commit "
git commit -m "${PROPOSAL_DESCRIPTION}"

echo "=== Run CI"
mix git_hooks.run pre_push
Expand Down

0 comments on commit f0f0c39

Please sign in to comment.