Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: multi_scm_type parameter #571

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions autorelease/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def main():
parser.add_argument("--lang", default=None)
parser.add_argument("command")
parser.add_argument("--multi-scm-name")
parser.add_argument("--multi-scm-type", default="github")

args = parser.parse_args()

Expand Down Expand Up @@ -86,6 +87,7 @@ def main():
args.release,
trigger.to_pysafe_language_name(args.lang),
args.multi_scm_name,
args.multi_scm_type,
)
if not args.pull:
raise Exception("missing required arg --pull")
Expand All @@ -95,6 +97,7 @@ def main():
args.kokoro_credentials,
args.pull,
multi_scm_name=args.multi_scm_name,
multi_scm_type=args.multi_scm_type,
)

if args.report:
Expand Down
19 changes: 14 additions & 5 deletions autorelease/kokoro.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ def _send_pubsub_message(

publish_request = {"messages": [{"data": encoded_data.decode("utf-8")}]}

print(f"Sending message: {data}")
resp = session.post(url, json=publish_request)
resp.raise_for_status()

return resp


def _make_build_request(
job_name: str, sha: str, env_vars: dict = None, multi_scm_name: str = ""
job_name: str, sha: str, env_vars: dict = None, multi_scm_name: str = "",
multi_scm_type: str = "github"
) -> str:
request = kokoro_api_pb2.BuildRequest(
full_job_name=job_name,
Expand All @@ -52,9 +54,14 @@ def _make_build_request(
# If the job is configured to use multiple SCMs, then we need to send
# the scm_name field as part of the request
if multi_scm_name:
request.multi_scm_revision.github_scm_revision.add(
name=multi_scm_name, commit_sha=sha
)
if multi_scm_type == 'git-on-borg':
request.multi_scm_revision.git_on_borg_scm_revision.add(
name=multi_scm_name, sha1=sha
)
else:
request.multi_scm_revision.github_scm_revision.add(
name=multi_scm_name, commit_sha=sha
)
else:
request.scm_revision.github_scm_revision.commit_sha = sha

Expand Down Expand Up @@ -105,8 +112,10 @@ def trigger_build(
sha: str,
env_vars: dict = None,
multi_scm_name: str = "",
multi_scm_type: str = "github"
):
build_request = _make_build_request(
job_name, sha, env_vars=env_vars, multi_scm_name=multi_scm_name
job_name, sha, env_vars=env_vars, multi_scm_name=multi_scm_name,
multi_scm_type=multi_scm_type
)
_send_pubsub_message(session, _DEVREL_PROD_KOKORO_TOPIC, build_request)
10 changes: 9 additions & 1 deletion autorelease/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import importlib
import re
import traceback
from typing import Tuple

from autorelease import common, github, kokoro, reporter
Expand Down Expand Up @@ -59,6 +60,7 @@ def trigger_kokoro_build_for_pull_request(
update_labels: bool = True,
use_allowlist: bool = True,
multi_scm_name: str = "",
multi_scm_type: str = "github",
) -> None:
"""Triggers the Kokoro job for a given pull request if possible.

Expand Down Expand Up @@ -87,6 +89,7 @@ def trigger_kokoro_build_for_pull_request(
"name" in label and label["name"] == "autorelease: triggered"
for label in pull["labels"]
):
result.print("The labels include autorelease: triggered'")
return

# Determine language.
Expand Down Expand Up @@ -125,6 +128,7 @@ def trigger_kokoro_build_for_pull_request(
sha=sha,
env_vars={"AUTORELEASE_PR": pull_request_url},
multi_scm_name=multi_scm_name,
multi_scm_type=multi_scm_type,
)
if update_labels:
gh.update_pull_labels(pull, add=["autorelease: triggered"])
Expand All @@ -143,6 +147,7 @@ def trigger_for_release(
release_url: str,
pysafe_lang: str,
multi_scm_name: str = "",
multi_scm_type: str = "github",
) -> reporter.Reporter:
"""Trigger a Kokoro job based on a release PR URL.

Expand Down Expand Up @@ -187,17 +192,18 @@ def trigger_for_release(
sha=sha,
env_vars={},
multi_scm_name=multi_scm_name,
multi_scm_type=multi_scm_type,
)

report.add(result)
return report


def trigger_single(
github_token: str,
kokoro_credentials: str,
pull_request_url: str,
multi_scm_name: str = "",
multi_scm_type: str = "github",
) -> reporter.Reporter:
"""Trigger a Kokoro job based on a release PR URL.

Expand Down Expand Up @@ -241,11 +247,13 @@ def trigger_single(
False,
False,
multi_scm_name=multi_scm_name,
multi_scm_type=multi_scm_type,
)
# Failing any one PR is fine, just record it in the log and continue.
except Exception as exc:
result.error = True
result.print(f"{exc!r}")
traceback.print_exc()

return report

Expand Down
31 changes: 31 additions & 0 deletions protos/kokoro_api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
// This has been heavily modified from the upstream source to just contain
// the fields we need.
// Source: https://cs.corp.google.com/piper///depot/google3/devtools/kokoro/api/proto/kokoro_api.proto
// How to generate protos/kokoro_api_pb2.py:
// 1. Download appropriate protoc from https://github.com/protocolbuffers/protobuf/releases
// 2. Run `/tmp/protoc-21.6/bin/protoc --python_out=. protos/kokoro_api.proto`

syntax = "proto2";

Expand Down Expand Up @@ -66,6 +69,7 @@ message ScmRevision {

// Represents a specific revision for multi-scm.
message MultiScmRevision {
repeated GitOnBorgScmRevision git_on_borg_scm_revision = 2;
repeated GithubScmRevision github_scm_revision = 4;
}

Expand Down Expand Up @@ -95,3 +99,30 @@ message GithubScmRevision {

// next index: 7
}

// Represents a specific revision in a Git-on-Borg repository.
message GitOnBorgScmRevision {
// Name of the scm that this revision belongs to. This name should be the same
// as multi_scm.git_on_borg_scm.name
// If name is provided, repo_url is ignored.
optional string name = 3;

// The SHA1 hash identifying a revision. In the build request from user, this
// can be a commit or a branch (e.g. 'master'). In the build status response,
// this will be a commit sha.
optional string sha1 = 1;

// The URL for the change in Git on borg repository. This field will be
// populated in the build result, and will be ignored if it is specified in
// the query. Previously called repository_url.
optional string revision_url = 5;

// This is optional for builds manually triggered by the users. For the builds
// automatically triggered by Kokoro, it should be the branch to which the
// sha1 is belongs to.
optional string branch = 4;

reserved 2;

// next index: 8
}
69 changes: 10 additions & 59 deletions protos/kokoro_api_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/test_autorelease_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ def test_trigger_single(
"AUTORELEASE_PR": "https://github.com/googleapis/php-trace/pull/1234"
},
multi_scm_name="",
multi_scm_type="github",
)
update_pull_labels.assert_not_called()

Expand Down Expand Up @@ -250,6 +251,7 @@ def test_trigger_single_default_multi_scm(
"AUTORELEASE_PR": "https://github.com/googleapis/java-trace/pull/1234"
},
multi_scm_name="java-trace",
multi_scm_type="github",
)
update_pull_labels.assert_not_called()

Expand Down Expand Up @@ -278,6 +280,7 @@ def test_trigger_for_release(trigger_build, get_url, make_authorized_session):
sha="abcd1234",
env_vars={},
multi_scm_name="",
multi_scm_type="github",
)


Expand Down Expand Up @@ -324,6 +327,7 @@ def test_trigger_single_multi_scm(
"AUTORELEASE_PR": "https://github.com/googleapis/java-trace/pull/1234"
},
multi_scm_name="java-trace",
multi_scm_type="github",
)
update_pull_labels.assert_not_called()

Expand Down Expand Up @@ -368,6 +372,7 @@ def test_trigger_package(
"AUTORELEASE_PR": "https://github.com/GoogleCloudPlatform/functions-framework-java/pull/111"
},
multi_scm_name="functions-framework-java",
multi_scm_type="github",
)


Expand Down Expand Up @@ -465,4 +470,5 @@ def test_trigger_multi_scm(
"AUTORELEASE_PR": "https://github.com/GoogleCloudPlatform/functions-framework-java/pull/111"
},
multi_scm_name="functions-framework-java",
multi_scm_type="github",
)