Skip to content

Commit

Permalink
Merge branch 'features/noagur/ScheduledQueryRuleApi2025-01-01-preview…
Browse files Browse the repository at this point in the history
…' of https://github.com/noaharon206/azure-rest-api-specs into features/noagur/ScheduledQueryRuleApi2025-01-01-preview
  • Loading branch information
Noa Gur committed Feb 26, 2025
2 parents dc41c99 + e3ca7c1 commit d720c66
Show file tree
Hide file tree
Showing 2,090 changed files with 265,330 additions and 6,787 deletions.
6 changes: 5 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ updates:
# Updated manually to align with minimum supported Node version
- dependency-name: "@types/node"
# Updated manually to align with repo microsoft/typespec
- dependency-name: "typescript"
- dependency-name: "@vitest/coverage-v8"
- dependency-name: "prettier"
- dependency-name: "vitest"
- dependency-name: "typescript"
# Updated manually by the Liftr team
- dependency-name: "@azure-tools/typespec-liftr-base"
# minimatch@9 is the last version to support Node 18
Expand Down Expand Up @@ -43,7 +45,9 @@ updates:
# Updated manually to align with minimum supported Node version
- dependency-name: "@types/node"
# Updated manually to align with repo microsoft/typespec
- dependency-name: "@vitest/coverage-v8"
- dependency-name: "prettier"
- dependency-name: "vitest"
# Points to "github:actions/github-script" since package isn't published to npmjs
- dependency-name: "@types/github-script"
# Leave the constraint if the original constraint allows the new version, otherwise, bump the constraint.
Expand Down
125 changes: 0 additions & 125 deletions .github/package-lock.json

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

1 change: 0 additions & 1 deletion .github/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"@types/github-script": "github:actions/github-script",
"@octokit/webhooks-types": "^7.5.1",
"@vitest/coverage-v8": "^3.0.5",
"memfs": "^4.17.0",
"prettier": "^3.3.3",
"vitest": "^3.0.5"
},
Expand Down
113 changes: 88 additions & 25 deletions .github/src/context.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// @ts-check

import { PER_PAGE_MAX } from "./github.js";

/**
* Extracts inputs from context based on event name and properties.
* run_id is only defined for "workflow_run:completed" events.
Expand Down Expand Up @@ -79,37 +81,98 @@ export async function extractInputs(github, context, core) {

let issue_number;

const pull_requests = payload.workflow_run.pull_requests;
if (pull_requests && pull_requests.length > 0) {
// For non-fork PRs, we should be able to extract the PR number from the payload, which avoids an
// unnecessary API call. The listPullRequestsAssociatedWithCommit() API also seems to return
// empty for non-fork PRs.
issue_number = pull_requests[0].number;
} else {
// For fork PRs, we must call an API in the head repository to get the PR number in the base repository
if (payload.workflow_run.event === "pull_request") {
// Extract the issue number from the payload itself, or by passing the head_sha to an API
// Do NOT attempt to extract the issue number from an artifact, since this could be modified
// in a fork PR.

// Owner and repo for the PR head (at least one should differ from base for fork PRs)
const head_owner = payload.workflow_run.head_repository.owner.login;
const head_repo = payload.workflow_run.head_repository.name;
const head_sha = payload.workflow_run.head_sha;
const pull_requests = payload.workflow_run.pull_requests;
if (pull_requests && pull_requests.length > 0) {
// For non-fork PRs, we should be able to extract the PR number from the payload, which avoids an
// unnecessary API call. The listPullRequestsAssociatedWithCommit() API also seems to return
// empty for non-fork PRs.
issue_number = pull_requests[0].number;
} else {
// For fork PRs, we must call an API in the head repository to get the PR number in the base repository

core.info(
`listPullRequestsAssociatedWithCommit(${head_owner}, ${head_repo}, ${head_sha})`,
// Owner and repo for the PR head (at least one should differ from base for fork PRs)
const head_owner = payload.workflow_run.head_repository.owner.login;
const head_repo = payload.workflow_run.head_repository.name;
const head_sha = payload.workflow_run.head_sha;

core.info(
`listPullRequestsAssociatedWithCommit(${head_owner}, ${head_repo}, ${head_sha})`,
);
const pullRequests = await github.paginate(
github.rest.repos.listPullRequestsAssociatedWithCommit,
{
owner: head_owner,
repo: head_repo,
commit_sha: head_sha,
per_page: PER_PAGE_MAX,
},
);

if (pullRequests.length === 1) {
issue_number = pullRequests[0].number;
} else {
throw new Error(
`Unexpected number of pull requests associated with commit '${head_sha}'. Expected: '1'. Actual: '${pullRequests.length}'.`,
);
}
}
} else if (
payload.workflow_run.event === "issue_comment" ||
payload.workflow_run.event == "workflow_run"
) {
// Attempt to extract issue number from artifact. This can be trusted, because it was uploaded from a workflow that is trusted,
// because "issue_comment" and "workflow_run" only trigger on workflows in the default branch.
const artifacts = await github.paginate(
github.rest.actions.listWorkflowRunArtifacts,
{
owner: payload.workflow_run.repository.owner.login,
repo: payload.workflow_run.repository.name,
run_id: payload.workflow_run.id,
per_page: PER_PAGE_MAX,
},
);
const { data: pullRequests } =
await github.rest.repos.listPullRequestsAssociatedWithCommit({
owner: head_owner,
repo: head_repo,
commit_sha: head_sha,
});

if (pullRequests.length === 1) {
issue_number = pullRequests[0].number;
} else {

const artifactNames = artifacts.map((a) => a.name);

core.info(`artifactNames: ${JSON.stringify(artifactNames)}`);

for (const artifactName of artifactNames) {
// If artifactName has format "issue-number=number", set issue_number
// Else, if artifactName has format "issue-number=other-string", throw an error
// Else, if artifactName does not start with "issue-number=", ignore it
const firstEquals = artifactName.indexOf("=");
if (firstEquals !== -1) {
const key = artifactName.substring(0, firstEquals);
const value = artifactName.substring(firstEquals + 1);

if (key === "issue-number") {
const parsedValue = Number.parseInt(value);
if (parsedValue) {
issue_number = parsedValue;
continue;
} else {
throw new Error(
`Invalid issue-number: '${value}' parsed to '${parsedValue}'`,
);
}
}
}
}

if (!issue_number) {
throw new Error(
`Unexpected number of pull requests associated with commit '${head_sha}'. Expected: '1'. Actual: '${pullRequests.length}'.`,
`Could not find 'issue-number' artifact, which is required to associate the triggering workflow run with a PR`,
);
}
} else {
throw new Error(
`Context '${context.eventName}:${context.payload.action}' with 'workflow_run.event=${payload.workflow_run.event} is not yet supported.`,
);
}

inputs = {
Expand Down
3 changes: 3 additions & 0 deletions .github/src/github.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @ts-check

export const PER_PAGE_MAX = 100;
Loading

0 comments on commit d720c66

Please sign in to comment.