Skip to content

Commit

Permalink
[HUD] Upgrade typescript 4.6.3 -> 5.7.2 (#6100)
Browse files Browse the repository at this point in the history
This results in a lot of "union too complex to represent" errors, which
I solved by separating the context, which was previously typed
`Context<webhook type>` to just `Context`, and the payload (if it is
used), which is now typed `Context<webhook type>["payload"]`

so functions taking in
`context: Context<"pull_request">`
now take in
`context: Context, payload: Context<"pull_request">["payload"]`
and anything that previously used `context.payload` now just uses
`payload`

Some disccusion on this can be found here:
probot/probot#1966
https://togithub.com/ubiquity-os/ubiquity-os-kernel/issues/31
https://togithub.com/probot/probot/issues/1968

I'm not sure why the import order formatter changed it's mind about what
order things should be in, but it seems to still have opinions which I
think is the main thing to check
  • Loading branch information
clee2000 authored Jan 6, 2025
1 parent 1b23394 commit 0914fa3
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 52 deletions.
1 change: 0 additions & 1 deletion torchci/components/LoadingPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { CircularProgress, styled } from "@mui/material";
import React from "react";

const LoadingContainer = styled("div")(({}) => ({
display: "flex",
Expand Down
80 changes: 44 additions & 36 deletions torchci/lib/bot/ciflowPushTrigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ function labelToTag(label: string, prNum: number): string {
}

function getAllPRTags(
context: Context<"pull_request"> | Context<"pull_request.closed">
context: Context,
payload: Context<"pull_request" | "pull_request.closed">["payload"]
) {
const prNum = context.payload.pull_request.number;
const labels = context.payload.pull_request.labels
const prNum = payload.pull_request.number;
const labels = payload.pull_request.labels
.map((label) => label.name)
.filter(isCIFlowLabel);

Expand All @@ -31,11 +32,7 @@ function getAllPRTags(
* @param tag looks like "ciflow/trunk/12345", where 12345 is the PR number.
* @param headSha
*/
async function syncTag(
context: Context<"pull_request"> | Context<"pull_request.labeled">,
tag: string,
headSha: string
) {
async function syncTag(context: Context, tag: string, headSha: string) {
context.log.info(`Synchronizing tag ${tag} to head sha ${headSha}`);
const matchingTags = await context.octokit.git.listMatchingRefs(
context.repo({ ref: `tags/${tag}` })
Expand Down Expand Up @@ -67,10 +64,7 @@ async function syncTag(
* Remove a tag from the repo if necessary.
* @param tag looks like "ciflow/trunk/12345", where 12345 is the PR number.
*/
async function rmTag(
context: Context<"pull_request.closed"> | Context<"pull_request.unlabeled">,
tag: string
) {
async function rmTag(context: Context, tag: string) {
context.log.info(`Cleaning up tag ${tag}`);
const matchingTags = await context.octokit.git.listMatchingRefs(
context.repo({ ref: `tags/${tag}` })
Expand All @@ -89,11 +83,14 @@ async function rmTag(
* We check all the CIFlow labels on the PR and make sure the corresponding tags
* are pointing to the PR's head SHA.
*/
async function handleSyncEvent(context: Context<"pull_request">) {
async function handleSyncEvent(
context: Context,
payload: Context<"pull_request">["payload"]
) {
context.log.debug("START Processing sync event");

const headSha = context.payload.pull_request.head.sha;
const tags = getAllPRTags(context);
const headSha = payload.pull_request.head.sha;
const tags = getAllPRTags(context, payload);
const promises = tags.map(
async (tag) => await syncTag(context, tag, headSha)
);
Expand All @@ -103,41 +100,46 @@ async function handleSyncEvent(context: Context<"pull_request">) {

// Remove the tag corresponding to the removed label.
async function handleUnlabeledEvent(
context: Context<"pull_request.unlabeled">
context: Context,
payload: Context<"pull_request.unlabeled">["payload"]
) {
context.log.debug("START Processing unlabeled event");

const label = context.payload.label.name;
const label = payload.label.name;
if (!isCIFlowLabel(label)) {
return;
}
const prNum = context.payload.pull_request.number;
const tag = labelToTag(context.payload.label.name, prNum);
const prNum = payload.pull_request.number;
const tag = labelToTag(payload.label.name, prNum);
await rmTag(context, tag);
}

// Remove all tags as this PR is closed.
async function handleClosedEvent(context: Context<"pull_request.closed">) {
async function handleClosedEvent(
context: Context,
payload: Context<"pull_request.closed">["payload"]
) {
context.log.debug("START Processing rm event");

const tags = getAllPRTags(context);
const tags = getAllPRTags(context, payload);
const promises = tags.map(async (tag) => await rmTag(context, tag));
await Promise.all(promises);
}

// Add the tag corresponding to the new label.
async function handleLabelEvent(
context: Context<"pull_request.labeled">,
context: Context,
payload: Context<"pull_request.labeled">["payload"],
tracker: CachedConfigTracker
) {
context.log.debug("START Processing label event");
if (context.payload.pull_request.state === "closed") {
if (payload.pull_request.state === "closed") {
// Ignore closed PRs. If this PR is reopened, the tags will get pushed as
// part of the sync event handling.
return;
}

const label = context.payload.label.name;
const label = payload.label.name;
if (!isCIFlowLabel(label)) {
return;
}
Expand All @@ -151,25 +153,25 @@ async function handleLabelEvent(
"No ciflow labels are configured for this repo.\n" +
"For information on how to enable CIFlow bot see " +
"this [wiki]( https://github.com/pytorch/test-infra/wiki/PyTorch-bot#ciflow-bot)",
issue_number: context.payload.pull_request.number,
issue_number: payload.pull_request.number,
})
);
return;
}
const prNum = context.payload.pull_request.number;
const owner = context.payload.repository.owner.login;
const repo = context.payload.repository.name;
const prNum = payload.pull_request.number;
const owner = payload.repository.owner.login;
const repo = payload.repository.name;
const has_write_permissions = await hasWritePermissions(
context,
context.payload.pull_request.user.login
payload.pull_request.user.login
);
const has_ci_approved = has_write_permissions
? true
: await hasApprovedPullRuns(
context.octokit,
owner,
repo,
context.payload.pull_request.head.sha
payload.pull_request.head.sha
);
if (!valid_labels.includes(label)) {
let body = `Unknown label \`${label}\`.\n Currently recognized labels are\n`;
Expand Down Expand Up @@ -220,23 +222,29 @@ async function handleLabelEvent(
if (prNum == 26921 && isPyTorchPyTorch(owner, repo)) {
return;
}
const tag = labelToTag(context.payload.label.name, prNum);
await syncTag(context, tag, context.payload.pull_request.head.sha);
const tag = labelToTag(payload.label.name, prNum);
await syncTag(context, tag, payload.pull_request.head.sha);
}

export default function ciflowPushTrigger(app: Probot) {
const tracker = new CachedConfigTracker(app);
app.on("pull_request.labeled", async (context) => {
await handleLabelEvent(context, tracker);
await handleLabelEvent(context, context.payload, tracker);
});
app.on(
[
"pull_request.synchronize",
"pull_request.opened",
"pull_request.reopened",
],
handleSyncEvent
async (context) => {
await handleSyncEvent(context, context.payload);
}
);
app.on("pull_request.closed", handleClosedEvent);
app.on("pull_request.unlabeled", handleUnlabeledEvent);
app.on("pull_request.closed", async (context) => {
await handleClosedEvent(context, context.payload);
});
app.on("pull_request.unlabeled", async (context) => {
await handleUnlabeledEvent(context, context.payload);
});
}
2 changes: 1 addition & 1 deletion torchci/lib/bot/pytorchBotHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { getHelp, getParser } from "./cliParser";
import { cherryPickClassifications } from "./Constants";
import PytorchBotLogger from "./pytorchbotLogger";
import {
hasWritePermissions as _hasWP,
addLabels,
CachedConfigTracker,
hasApprovedPullRuns,
hasWritePermissions as _hasWP,
isFirstTimeContributor,
isPyTorchOrg,
isPyTorchPyTorch,
Expand Down
9 changes: 2 additions & 7 deletions torchci/lib/bot/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ export function isTime0(time: string): boolean {

export const TIME_0 = "1970-01-01 00:00:00.000000000";

export function repoKey(
context: Context | Context<"pull_request.labeled">
): string {
export function repoKey(context: Context): string {
const repo = context.repo();
return `${repo.owner}/${repo.repo}`;
}
Expand Down Expand Up @@ -59,10 +57,7 @@ export class CachedConfigTracker {
});
}

async loadConfig(
context: Context | Context<"pull_request.labeled">,
force = false
): Promise<object> {
async loadConfig(context: Context, force = false): Promise<object> {
const key = repoKey(context);
if (!(key in this.repoConfigs) || force) {
context.log({ key }, "loadConfig");
Expand Down
2 changes: 1 addition & 1 deletion torchci/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"prettier": "2.6.2",
"prettier-plugin-organize-imports": "^3.2.4",
"ts-jest": "^29.2.5",
"typescript": "4.6.3"
"typescript": "^5.7.2"
},
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
2 changes: 1 addition & 1 deletion torchci/pages/[repoOwner]/[repoName]/commit/[sha].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import CommitStatus from "components/CommitStatus";
import { useSetTitle } from "components/DynamicTitle";
import { fetcher } from "lib/GeneralUtils";
import { useRouter } from "next/router";
import { IssueLabelApiResponse } from "pages/api/issue/[label]";
import { CommitApiResponse } from "pages/api/[repoOwner]/[repoName]/commit/[sha]";
import { IssueLabelApiResponse } from "pages/api/issue/[label]";
import useSWR from "swr";

export function CommitInfo({
Expand Down
2 changes: 1 addition & 1 deletion torchci/pages/[repoOwner]/[repoName]/pull/[prNumber].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { useSetTitle } from "components/DynamicTitle";
import ErrorBoundary from "components/ErrorBoundary";
import { PRData } from "lib/types";
import { useRouter } from "next/router";
import { IssueLabelApiResponse } from "pages/api/issue/[label]";
import { CommitApiResponse } from "pages/api/[repoOwner]/[repoName]/commit/[sha]";
import { IssueLabelApiResponse } from "pages/api/issue/[label]";
import { useEffect, useState } from "react";
import useSWR from "swr";

Expand Down
8 changes: 4 additions & 4 deletions torchci/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8222,10 +8222,10 @@ typed-rest-client@^1.8.9:
tunnel "0.0.6"
underscore "^1.12.1"

typescript@4.6.3:
version "4.6.3"
resolved "https://registry.npmjs.org/typescript/-/typescript-4.6.3.tgz"
integrity sha512-yNIatDa5iaofVozS/uQJEl3JRWLKKGJKh6Yaiv0GLGSuhpFJe7P3SbHZ8/yjAHRQwKRoA6YZqlfjXWmVzoVSMw==
typescript@^5.7.2:
version "5.7.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.7.2.tgz#3169cf8c4c8a828cde53ba9ecb3d2b1d5dd67be6"
integrity sha512-i5t66RHxDvVN40HfDd1PsEThGNnlMCMT3jMUuoh9/0TaqWevNontacunWyN02LA9/fIbEWlcHZcgTKb9QoaLfg==

uglify-js@^3.1.4:
version "3.17.3"
Expand Down

0 comments on commit 0914fa3

Please sign in to comment.