Skip to content
Merged
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
19 changes: 12 additions & 7 deletions .github/copilot-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,11 @@ The agent is driven by a workflow in the target repo
credentials. No App webhook or corporate FIC is used.
The ingress allowlist enrolls numeric repository IDs for manual and
automatic issue-loop jobs from their protected default branch, and
same-repository team-memory via the fixed coordinator on protected default
`main`. Source, coordinator and validated wiki destination must match.
team-memory via the fixed coordinator on protected default `main`
(same-repository only for `microsoft/IssueLens`; same-repository or enrolled
non-pilot public cross-repository sources on their verified protected
default branch for non-pilot coordinators, where coordinator and validated
wiki destination match and share `repo-<coordinator_id>-wiki`).
IssueLens keeps its stricter built-in workflow rules. The worker revalidates
created comments using the repository stored at admission. The ZIP does not
load the example allowlist or enroll Java Pack in code.
Expand Down Expand Up @@ -407,11 +410,13 @@ incomplete discovery rather than silently omitting sources. IssueLens automatic
and manual requests share `issuelens-team-memory-wiki-microsoft-IssueLens`
with `queue: max` and no in-progress cancellation: one complete coordinator
run and at most 100 pending runs. Queue admission and dispatch acknowledgement
are not maintenance success. Only `microsoft/IssueLens` sources and its validated
`microsoft/IssueLens` wiki destination are accepted in this first rollout;
policy is never overridden to force a destination. Java tooling callers remain
unchanged and will need authenticated cross-repository dispatch and a separate
queue for their shared `microsoft/vscode-java-pack` wiki. Direct external action
are not maintenance success. In this repository's workflow, only
`microsoft/IssueLens` sources and its validated `microsoft/IssueLens` wiki
destination are accepted; policy is never overridden to force a destination.
Enrolled non-pilot coordinators such as `microsoft/vscode-java-pack` serialize
same-repository and enrolled Java tooling cross-repository sources under their
shared coordinator queue and durable `repo-104967329-wiki` bridge reservation.
Direct external action
callers retain their existing event behavior and per-push concurrency.
The pilot does not serialize chat, issue-loop work, or external direct requests.
The manual pilot uses `ISSUELENS_TEAM_MEMORY_MANUAL_PILOT=true` after bridge
Expand Down
154 changes: 126 additions & 28 deletions automation_bridge/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def workflow_claims(token: str, audience: str, pilot_repository_id: int, allowli
require(claims["event_name"] in ISSUE_EVENT_ACTIONS, "Unsupported workflow event")
elif claims["workflow_ref"] == workflow_ref(action.COORDINATOR_WORKFLOW):
require(claims["event_name"] == "workflow_dispatch", "Unsupported workflow event")
return _validated_claims(claims, "team-memory", repository_id)
return _validated_claims(claims, "team-memory", repository_id, pilot_repository_id, allowlist)
else:
raise ValueError("Workflow is not enabled for this bridge")
else:
Expand All @@ -110,14 +110,18 @@ def workflow_claims(token: str, audience: str, pilot_repository_id: int, allowli
if path.casefold() == action.COORDINATOR_WORKFLOW.casefold():
require(workflow == workflow_ref(action.COORDINATOR_WORKFLOW, repository)
and claims["event_name"] == "workflow_dispatch", "Unsupported team-memory workflow or event")
return _validated_claims(claims, "team-memory", repository_id)
return _validated_claims(claims, "team-memory", repository_id, pilot_repository_id, allowlist)
require(claims["event_name"] in ISSUE_EVENT_ACTIONS, "Unsupported workflow event")
return _validated_claims(claims, "issue-loop", repository_id)
return _validated_claims(claims, "issue-loop", repository_id, pilot_repository_id, allowlist)


def _validated_claims(claims: dict, kind: str, repository_id: int) -> dict:
def _validated_claims(
claims: dict, kind: str, repository_id: int,
pilot_repository_id: int, allowlist: frozenset[int],
) -> dict:
return {
**claims, "kind": kind, "repository_id": repository_id,
"pilot_repository_id": pilot_repository_id, "allowlist": allowlist,
"run_id": action.positive(claims["run_id"]), "run_attempt": action.positive(claims["run_attempt"]),
"workflow_sha": action.full_sha(claims["workflow_sha"]),
}
Expand All @@ -127,27 +131,59 @@ def workflow_ref(path: str, repository: str = PILOT_REPOSITORY, branch: str = PI
return f"{repository}/{path}@refs/heads/{branch}"


def default_branch_protected(claims: dict) -> bool:
repository = claims["repository"]
project = action.github_read(f"/repos/{repository}", token="", timeout=5)
branch_name = project.get("default_branch")
def _github_public_read(path: str) -> dict | None:
try:
return action.github_read(path, token="", timeout=5)
except urllib.error.HTTPError as error:
if error.code == 404:
return None
raise


def _branch_protected(
repository: str, repository_id: int, branch_name: str, *, skip_branch_read: bool = False,
) -> bool:
project = _github_public_read(f"/repos/{repository}")
if project is None:
return False
default_branch = project.get("default_branch")
require(isinstance(project.get("full_name"), str) and type(project.get("id")) is int
and type(project.get("private")) is bool and isinstance(project.get("visibility"), str)
and isinstance(branch_name, str) and branch_name,
and isinstance(default_branch, str) and default_branch,
"Invalid GitHub repository response")
if (project["full_name"].casefold() != repository.casefold()
or project["id"] != claims["repository_id"] or project["private"]
or project["visibility"] != "public" or claims["ref"] != f"refs/heads/{branch_name}"):
or project["id"] != repository_id or project["private"]
or project["visibility"] != "public" or default_branch != branch_name):
return False
if "ref_protected" in claims:
if skip_branch_read:
return True
branch_path = urllib.parse.quote(branch_name, safe="")
branch = action.github_read(f"/repos/{repository}/branches/{branch_path}", token="", timeout=5)
require(branch.get("name") == branch_name and isinstance(branch.get("protected"), bool),
branch_path = urllib.parse.quote(default_branch, safe="")
branch = _github_public_read(f"/repos/{repository}/branches/{branch_path}")
if branch is None:
return False
require(branch.get("name") == default_branch and isinstance(branch.get("protected"), bool),
"Invalid GitHub branch protection response")
return branch["protected"]


def default_branch_protected(claims: dict, data: dict | None = None) -> bool:
ref = claims.get("ref", "")
if not isinstance(ref, str) or not ref.startswith("refs/heads/"):
return False
if not _branch_protected(
claims["repository"], claims["repository_id"], ref[len("refs/heads/"):],
skip_branch_read="ref_protected" in claims,
):
return False
if data is not None and claims.get("kind") == "team-memory":
metadata = data["metadata"]
if metadata["repository_id"] != claims["repository_id"]:
return _branch_protected(
metadata["repository"], metadata["repository_id"], metadata["base_ref"],
)
return True


def job_id(claims: dict) -> str:
return hashlib.sha256(
f"v1:{claims['repository_id']}:{claims['kind']}:{claims['run_id']}".encode("ascii")
Expand All @@ -169,7 +205,20 @@ def _positive_int(value):
return action.positive(value)


def _valid_branch_name(value) -> bool:
return (
isinstance(value, str)
and bool(re.fullmatch(r"[A-Za-z0-9._/-]{1,255}", value))
and not value.startswith(("/", "."))
and not value.endswith(("/", ".", ".lock"))
and ".." not in value
and "//" not in value
and "/." not in value
)


def _issue_job(metadata: dict, claims: dict) -> str:
require(metadata.get("repository") == claims["repository"], "Invalid workflow job")
event = claims["event_name"]
operation = metadata.get("event_action")
require(metadata.get("event_name") == event and operation in ISSUE_EVENT_ACTIONS[event],
Expand Down Expand Up @@ -217,16 +266,46 @@ def _validate_merge(merge: dict, repository: str):


def _memory_job(metadata: dict, claims: dict) -> str:
repository = claims["repository"]
require(metadata.get("repository_id") == claims["repository_id"]
and metadata.get("base_ref") == PILOT_BRANCH
and metadata.get("coordinator_repository") == repository
and metadata.get("required_wiki_repository") == repository
coordinator = claims["repository"]
coordinator_id = _positive_int(claims["repository_id"])
source_repository = metadata.get("repository")
source_repository_id = metadata.get("repository_id")
base_ref = metadata.get("base_ref")
require(metadata.get("coordinator_repository") == coordinator
and metadata.get("required_wiki_repository") == coordinator
and metadata.get("coordinator_workflow_ref") == claims["workflow_ref"]
and metadata.get("coordinator_workflow_sha") == claims["workflow_sha"]
and metadata.get("coordinator_run_id") == claims["run_id"]
and metadata.get("coordinator_run_attempt") == claims["run_attempt"],
"Team-memory coordinator identity mismatch")
require(isinstance(source_repository, str)
and re.fullmatch(action.SOURCE_REPOSITORY_PATTERN, source_repository)
and ".." not in source_repository
and type(source_repository_id) is int and source_repository_id > 0
and _valid_branch_name(base_ref),
"Invalid team-memory source repository")
pilot_id = claims.get("pilot_repository_id")
if coordinator == PILOT_REPOSITORY or (type(pilot_id) is int and coordinator_id == pilot_id):
require(coordinator == PILOT_REPOSITORY
and source_repository == PILOT_REPOSITORY
and source_repository_id == coordinator_id
and base_ref == PILOT_BRANCH
and claims.get("ref", f"refs/heads/{PILOT_BRANCH}") == f"refs/heads/{PILOT_BRANCH}",
"Team-memory coordinator identity mismatch")
elif source_repository_id == coordinator_id:
require(source_repository == coordinator
and source_repository.casefold() != PILOT_REPOSITORY.casefold()
and claims.get("ref") == f"refs/heads/{base_ref}",
"Team-memory coordinator identity mismatch")
else:
allowlist = claims.get("allowlist")
require(isinstance(allowlist, frozenset)
and type(pilot_id) is int and pilot_id in allowlist
and coordinator_id in allowlist and coordinator_id != pilot_id
and source_repository_id in allowlist and source_repository_id != pilot_id
and source_repository.casefold() != PILOT_REPOSITORY.casefold()
and source_repository.casefold() != coordinator.casefold(),
"Team-memory source repository is not enrolled")
fields = {
"repository", "repository_id", "base_ref", "event_name", "event_action",
"actor_login", "triggering_actor", "workflow_ref", "workflow_sha", "run_id", "run_attempt",
Expand All @@ -243,9 +322,9 @@ def _memory_job(metadata: dict, claims: dict) -> str:
before, after = (action.full_sha(metadata[field]) for field in ("push_before", "push_after"))
action.full_sha(metadata["source_tip_sha"])
require(before != after and metadata["range_origin"] == "authorized-reconciliation"
and metadata["source_identity"] == f"{repository}@{before}..{after}"
and metadata["source_identity"] == f"{source_repository}@{before}..{after}"
and metadata["event_action"] == "push" and metadata["workflow_sha"] == after
and metadata["workflow_ref"] == workflow_ref(action.DISPATCH_WORKFLOW, repository)
and metadata["workflow_ref"] == workflow_ref(action.DISPATCH_WORKFLOW, source_repository, base_ref)
and _positive_int(metadata["commit_count"]) <= action.MAX_PUSH_COMMITS,
"Invalid reconciliation range metadata")
_positive_int(metadata["run_id"])
Expand All @@ -254,22 +333,23 @@ def _memory_job(metadata: dict, claims: dict) -> str:
require(isinstance(pulls, list) and 0 < len(pulls) <= action.MAX_BATCH_PRS
and all(isinstance(pull, dict) and set(pull) == _MERGE_FIELDS for pull in pulls),
"Invalid reconciliation PR inventory")
require(len({_validate_merge(pull, repository) for pull in pulls}) == len(pulls), "Duplicate merged PRs")
require(len({_validate_merge(pull, source_repository) for pull in pulls}) == len(pulls),
"Duplicate merged PRs")
else:
require(set(metadata) == fields | _MERGE_FIELDS and metadata.get("event_name") == "workflow_dispatch"
and metadata.get("event_action") == "workflow_dispatch"
and metadata.get("actor_login") == claims["actor"]
and all(metadata.get(field) == claims[field] for field in (
"workflow_ref", "workflow_sha", "run_id", "run_attempt",
)), "Invalid manual team-memory metadata")
_validate_merge(metadata, repository)
_validate_merge(metadata, source_repository)
return "wiki"


def validate_request(data: dict, claims: dict) -> str:
require(isinstance(data, dict) and set(data) == {"request_type", "metadata"}
and data["request_type"] == claims["kind"] and isinstance(data["metadata"], dict)
and data["metadata"].get("repository") == claims["repository"], "Invalid workflow job")
and data["request_type"] == claims["kind"] and isinstance(data["metadata"], dict),
"Invalid workflow job")
validator = _issue_job if claims["kind"] == "issue-loop" else _memory_job
target = validator(data["metadata"], claims)
return target if claims["repository"] == PILOT_REPOSITORY else f"repo-{claims['repository_id']}-{target}"
Expand Down Expand Up @@ -374,9 +454,10 @@ def prepare(self, job) -> urllib.request.Request:
payload = json.loads(job["payload"])
kind, metadata = payload["request_type"], payload["metadata"]
repository = job.get("repository")
if "repository" in job:
require(repository == metadata.get("repository"), "Bridge job repository mismatch")
if kind == "issue-loop":
if "repository" in job:
require(isinstance(repository, str) and repository == metadata.get("repository"),
"Bridge job repository mismatch")
digest = metadata.pop("comment_sha256", None)
if metadata["comment_added"]:
require(isinstance(digest, str) and JOB_ID.fullmatch(digest), "Created comment requires its revision digest")
Expand All @@ -389,6 +470,23 @@ def prepare(self, job) -> urllib.request.Request:
_verify_created_comment(repository, metadata, digest)
invocation = action.build_issue_loop_request(metadata)
elif kind == "team-memory":
if "repository" in job:
if "coordinator_repository" in metadata:
require(isinstance(repository, str)
and repository == metadata.get("coordinator_repository")
and repository == metadata.get("required_wiki_repository"),
"Bridge job repository mismatch")
else:
require(isinstance(repository, str)
and repository == metadata.get("repository")
and repository == metadata.get("required_wiki_repository", repository),
"Bridge job repository mismatch")
if repository == PILOT_REPOSITORY:
require(metadata.get("repository") == PILOT_REPOSITORY, "Bridge job repository mismatch")
else:
require(isinstance(metadata.get("repository"), str)
and metadata["repository"].casefold() != PILOT_REPOSITORY.casefold(),
"Bridge job repository mismatch")
invocation = action.build_team_memory_request(metadata)
else:
raise ValueError("Unsupported bridge job type")
Expand Down
2 changes: 1 addition & 1 deletion automation_bridge/function_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def submit(request: func.HttpRequest) -> func.HttpResponse:
except (ValueError, TypeError):
return respond({"error": "invalid_job"}, 400)
try:
protected = bridge.default_branch_protected(claims)
protected = bridge.default_branch_protected(claims, data)
except (OSError, http.client.HTTPException, ValueError):
logger.warning("GitHub repository or branch validation unavailable")
return respond({"error": "branch_protection_unavailable"}, 503)
Expand Down
9 changes: 8 additions & 1 deletion automation_bridge/repository_allowlist.example.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
[
1303440932,
104967329
104967329,
102584737,
102583752,
110522074,
216314492,
626812412,
129053101,
116921700
]
Loading