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
114 changes: 114 additions & 0 deletions .github/workflows/pr-review.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
name: PR Review

on:
pull_request:
branches:
- main

permissions:
contents: read
pull-requests: write

jobs:
review:
name: Code Review
runs-on: ubuntu-latest
timeout-minutes: 15

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: pnpm/action-setup@v3
with:
version: 8.10.0

- uses: actions/setup-node@v4
with:
node-version: 20
cache: pnpm

- run: pnpm install --frozen-lockfile

- id: lint
continue-on-error: true
run: pnpm lint 2>&1 | tee lint_output.txt

- id: build
continue-on-error: true
run: pnpm build 2>&1 | tee build_output.txt

- id: test
continue-on-error: true
run: pnpm test:unit 2>&1 | tee test_output.txt
working-directory: apps/vscode-extension

- id: linelength
continue-on-error: true
shell: bash
run: |
overages=""
while IFS= read -r file; do
if [ -f "$file" ]; then
results=$(node -e "
var fs=require('fs');
var lines=fs.readFileSync('$file','utf-8').split('\n');
var start=-1,name='';
for(var i=0;i<lines.length;i++){
var m=lines[i].match(/^\s*(export\s+)?(async\s+)?function\s+(\w+)/);
if(m){
if(start>=0){
var cnt=i-start;
if(cnt>130)console.log(name+' in $file ('+cnt+' lines, line '+(start+1)+')');
}
start=i;name=m[3];
}
}
")
if [ -n "$results" ]; then overages="$overages"$'\n'"$results"; fi
fi
done < <(find apps packages -name "*.ts" -o -name "*.tsx")
echo "$overages" | tee linelength_output.txt
if [ -z "$overages" ]; then echo "passed=true" >> $GITHUB_OUTPUT; else echo "passed=false" >> $GITHUB_OUTPUT; fi

- env:
GH_TOKEN: ${{ github.token }}
BUILD: ${{ steps.build.outcome }}
LINT: ${{ steps.lint.outcome }}
TEST: ${{ steps.test.outcome }}
LL: ${{ steps.linelength.outputs.passed }}
shell: bash
run: |
echo "## PR 审查报告" > comment.md
echo "" >> comment.md
echo "### 检查结果" >> comment.md
echo "" >> comment.md
echo "| 检查项 | 状态 |" >> comment.md
echo "|--------|------|" >> comment.md

if [ "$BUILD" = "success" ]; then echo "| \`pnpm build\` | :white_check_mark: |" >> comment.md; else echo "| \`pnpm build\` | :x: |" >> comment.md; fi
if [ "$LINT" = "success" ]; then echo "| \`pnpm lint\` | :white_check_mark: |" >> comment.md; else echo "| \`pnpm lint\` | :x: |" >> comment.md; fi
if [ "$TEST" = "success" ]; then echo "| \`pnpm test\` | :white_check_mark: |" >> comment.md; else echo "| \`pnpm test\` | :x: |" >> comment.md; fi
if [ "$LL" = "true" ]; then echo "| 函数 <= 130 行 | :white_check_mark: |" >> comment.md; else echo "| 函数 <= 130 行 | :x: |" >> comment.md; fi

echo "" >> comment.md
echo "### 未通过的检查" >> comment.md
echo "" >> comment.md

any_fail=false
if [ "$BUILD" != "success" ]; then echo "- [ ] \`pnpm build\`" >> comment.md; any_fail=true; fi
if [ "$LINT" != "success" ]; then echo "- [ ] \`pnpm lint\`" >> comment.md; any_fail=true; fi
if [ "$TEST" != "success" ]; then echo "- [ ] \`pnpm test\`" >> comment.md; any_fail=true; fi
if [ "$LL" != "true" ]; then echo "- [ ] 函数长度不超过 130 行" >> comment.md; any_fail=true; fi

if [ "$any_fail" = false ]; then echo "全部通过,无需修改。" >> comment.md; fi

echo "" >> comment.md
echo "---" >> comment.md
echo "" >> comment.md
echo "*自动生成 by PR Review workflow*" >> comment.md

gh pr comment ${{ github.event.pull_request.number }} --body "$(cat comment.md)"

if [ "$any_fail" = true ]; then exit 1; fi
4 changes: 2 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"presentation": { "reveal": "silent", "panel": "dedicated" },
"group": { "kind": "build", "isDefault": true },
"options": {
"cwd": "${workspaceFolder}/packages/vscode-extension"
"cwd": "${workspaceFolder}/apps/vscode-extension"
}
},
{
Expand All @@ -38,7 +38,7 @@
"presentation": { "reveal": "silent", "panel": "dedicated" },
"group": "build",
"options": {
"cwd": "${workspaceFolder}/packages/vscode-extension"
"cwd": "${workspaceFolder}/apps/vscode-extension"
}
}
]
Expand Down
56 changes: 56 additions & 0 deletions apps/vscode-extension/src/SignUpContest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { fetchText, fetchTextPost, CfError, LoginRequiredError, ProxyError } from "./tools/fetch";

export interface ContestPage {
contest: string;
title: string;
url: string;
signed: boolean;
csrfToken: string;
Rated: boolean;
}
Comment thread
Transparent-fish marked this conversation as resolved.

export async function fetchContest(contest: string): Promise<ContestPage> {
const url = `https://atcoder.jp/contests/${contest}`;
const html = await fetchText(url);
const titleMatch = html.match(/<title>([^<]+)<\/title>/i);
const title = titleMatch ? titleMatch[1].trim() : "Untitled";
const formRegex = new RegExp(
`<form[^>]*action="[^"]*${contest}\/register"[^>]*>([\\s\\S]*?)<\\/form>`,
"i"
);
const from = html.match(formRegex)?.[1];
let csrfToken = "", signed = false, Rated = false;
if (from) {
const csrfMatch = from.match(/name="csrf_token"[^>]*value="([^"]*)"/i);
csrfToken = csrfMatch ? (csrfMatch[1] ?? "") : "";
signed = /<button[^>]*>Unregister<\/button>/i.test(from);
Rated = /<input[^>]*name\s*=\s*"rated"/i.test(from)
}
return { contest, title, url, signed, csrfToken, Rated };
Comment thread
Transparent-fish marked this conversation as resolved.
}

export async function signedUpContest(contest: string, csrfToken: string, rated?: boolean): Promise<{ success: boolean; message: string }> {
const url = `https://atcoder.jp/contests/${contest}/register`;
let body = `csrf_token=${encodeURIComponent(csrfToken)}&terms=on`;

if (rated) body += `&rated=on`;

try {
const html = await fetchTextPost(url, body);

const isSigned = /Unregister|registered/i.test(html);

if (isSigned) {
return { success: true, message: "报名成功!" };
}

const errMatch = html.match(/<div[^>]*class="[^"]*(?:alert-danger|alert-warning|alert-error)[^"]*"[^>]*>([\s\S]*?)<\/div>/i);
const errMsg = errMatch ? errMatch[1].replace(/<[^>]+>/g, "").trim() : "报名失败,请检查 Cookie 是否有效";
return { success: false, message: errMsg };
} catch (error) {
if (error instanceof CfError || error instanceof LoginRequiredError || error instanceof ProxyError) {
throw error;
}
return { success: false, message: error instanceof Error ? error.message : "报名请求失败" };
}
}
2 changes: 1 addition & 1 deletion apps/vscode-extension/src/atcoder.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as assert from "assert";
import { parseProblemPage } from "./atcoder";
import { setSessionCookie, getSessionCookie } from "./tools/fetch";

const sampleHtml = `
<html>
Expand Down Expand Up @@ -44,7 +45,6 @@ assert.ok(mathResult.constraints.includes('class="katex"'), "should have math re
console.log("HTML/LaTeX rendering test passed");

// Cookie tests
import { setSessionCookie, getSessionCookie } from "./atcoder";
assert.strictEqual(getSessionCookie(), "", "default cookie should be empty");
setSessionCookie("REVEL_SESSION=test123");
assert.strictEqual(getSessionCookie(), "REVEL_SESSION=test123", "cookie should be set");
Expand Down
Loading
Loading