diff --git a/.github/workflows/notion-study-sync.yml b/.github/workflows/notion-study-sync.yml new file mode 100644 index 0000000..dd2cc14 --- /dev/null +++ b/.github/workflows/notion-study-sync.yml @@ -0,0 +1,61 @@ +name: Notion study sync + +on: + push: + branches: [main] + pull_request: + branches: [main] + paths: ['notion-sync/**', '.github/workflows/notion-study-sync.yml'] + workflow_dispatch: + inputs: + dry_run: + description: 'Preview only; do not write to Notion' + type: boolean + default: true + +permissions: + contents: read + +# A replaced pending run is safe: every run scans all reachable submissions. +concurrency: + group: notion-study-sync-${{ github.event_name == 'pull_request' && github.event.pull_request.number || 'main' }} + cancel-in-progress: false + +jobs: + sync: + runs-on: ubuntu-latest + timeout-minutes: 20 + steps: + - uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5 + with: + fetch-depth: 0 + persist-credentials: false + ref: ${{ github.event_name == 'pull_request' && github.ref || 'main' }} + - uses: actions/setup-java@b6effb05e454b25005698d916606bdc6ffcbf961 # v5 + with: + distribution: temurin + java-version: '21' + - name: Test and package + run: ./notion-sync/mvnw -B -f notion-sync/pom.xml verify + - name: Preview recorded submissions + run: java -jar notion-sync/target/algo-study-sync.jar --dry-run --output notion-sync/target/preview.json + - name: Check Notion connection + id: connection + if: github.event_name != 'pull_request' + env: + NOTION_TOKEN: ${{ secrets.NOTION_TOKEN }} + run: | + if [ -n "$NOTION_TOKEN" ]; then + echo 'ready=true' >> "$GITHUB_OUTPUT" + else + echo 'ready=false' >> "$GITHUB_OUTPUT" + echo '::notice::Notion sync is pending: add the NOTION_TOKEN repository secret.' + echo 'Notion 자동 기록은 아직 꺼져 있습니다. NOTION_TOKEN 등록 후 Run workflow에서 dry_run을 해제하세요.' >> "$GITHUB_STEP_SUMMARY" + fi + - name: Create new study records + if: github.event_name != 'pull_request' && steps.connection.outputs.ready == 'true' && (github.event_name != 'workflow_dispatch' || inputs.dry_run == false) + env: + NOTION_TOKEN: ${{ secrets.NOTION_TOKEN }} + NOTION_PROBLEMS_DATA_SOURCE_ID: ${{ vars.NOTION_PROBLEMS_DATA_SOURCE_ID || '41bda392-82f1-8130-9edd-000b3999b05b' }} + NOTION_ATTEMPTS_DATA_SOURCE_ID: ${{ vars.NOTION_ATTEMPTS_DATA_SOURCE_ID || '41bda392-82f1-819c-b4d6-000b0f4efcf8' }} + run: java -jar notion-sync/target/algo-study-sync.jar diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..af3ecbd --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +**/.gradle/ +**/build/ +**/target/ +.idea/ +*.iml +.env +.env.* +!.env.example diff --git a/notion-sync/.mvn/wrapper/maven-wrapper.properties b/notion-sync/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 0000000..c0bcafe --- /dev/null +++ b/notion-sync/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,3 @@ +wrapperVersion=3.3.4 +distributionType=only-script +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.11/apache-maven-3.9.11-bin.zip diff --git a/notion-sync/README.md b/notion-sync/README.md new file mode 100644 index 0000000..6e4432d --- /dev/null +++ b/notion-sync/README.md @@ -0,0 +1,86 @@ +# 코딩테스트 학습 기록 자동화 + +코드트리의 GitHub 제출 기록을 읽어 Notion의 **문제 하나 → 1차·2차 시도**로 보관합니다. 매 시도의 코드는 그 커밋에서 읽고, 코멘트는 사용자가 직접 작성합니다. 기존 페이지를 수정·삭제하지 않습니다. + +## 사용 흐름 + +1. 코드트리에 제출하면 연결된 저장소에 커밋이 생깁니다. +2. `main`의 push가 GitHub Actions를 실행합니다. +3. Java 프로그램이 전체 커밋 이력을 부모 순서로 읽고 문제별 시도 번호를 붙입니다. +4. Notion에 없는 문제와 시도만 생성합니다. 문제의 `시도 이력`에서 각 시도를 열 수 있습니다. +5. 사용자는 시도 페이지의 `내 코멘트`, 문제 페이지의 `문제 전체 정리`에 메모합니다. + +이 프로그램은 제출 코드를 실행하거나 정답을 판단하지 않습니다. GitHub에 기록된 판정만 옮깁니다. LLM 호출과 자동 해설은 현재 버전에 없습니다. + +## 최초 연결 + +기록 대상: [코딩테스트](https://app.notion.com/p/41bda39282f1802bb138c9a48633354e) + +1. Notion의 Developer portal에서 이 자동화용 내부 연결을 만들고, 콘텐츠 읽기·추가 권한을 허용합니다. `코딩테스트` 페이지에 해당 연결을 추가하여 두 데이터 소스에 접근할 수 있게 합니다. 개인용 PAT도 API에서 지원하지만, 페이지 접근을 제한하기 쉬운 내부 연결을 권장합니다. +2. GitHub 저장소의 **Settings → Secrets and variables → Actions → New repository secret**에 `NOTION_TOKEN`이라는 이름으로 토큰을 저장합니다. 채팅·소스·README에는 토큰을 넣지 않습니다. +3. **Actions → Notion study sync → Run workflow**에서 `dry_run`을 해제하고 실행합니다. 첫 실행은 GitHub에 남아 있는 과거 제출도 가져옵니다. +4. 동일한 workflow를 다시 실행하여 새 시도 0개인지 확인합니다. 노션 메모를 작성한 뒤 재실행해 메모가 남는지도 확인합니다. +5. 이후 실제 코드트리 제출 한 건으로 push 트리거와 저장까지 확인합니다. 커밋 인증 방식에 따라 push 후 실행이 생기지 않을 수 있으므로 이 검증은 필요합니다. + +토큰이 없으면 검사·미리보기만 수행하며 자동 기록은 꺼져 있다는 안내를 Actions summary에 남깁니다. ChatGPT의 Notion 연결과 GitHub Actions가 사용할 토큰은 별개입니다. + +다른 노션 대상으로 옮길 때 Repository variables를 설정하면 됩니다. + +| 이름 | 현재 기본 데이터 소스 | +|---|---| +| `NOTION_PROBLEMS_DATA_SOURCE_ID` | `41bda392-82f1-8130-9edd-000b3999b05b` | +| `NOTION_ATTEMPTS_DATA_SOURCE_ID` | `41bda392-82f1-819c-b4d6-000b0f4efcf8` | + +데이터베이스 페이지 ID와 데이터 소스 ID는 다릅니다. 두 ID는 비밀 토큰이 아니며 접근 권한을 대신하지 않습니다. + +## 설계 결정 + +| 항목 | 규칙 | +|---|---| +| 문제 식별 | `codetree:` + 문제 URL의 마지막 경로. 폴더명·표시 제목과 분리 | +| 시도 식별 | 저장소 + 문제 키 + 전체 커밋 SHA | +| 차수 | Git 부모 순서로 문제별 1, 2, 3… 부여. 날짜 역전에도 순서 유지 | +| 동일 코드 재제출 | 별도 제출 커밋이 있다면 별도 시도로 저장 | +| 재실행 | 같은 시도 키가 있으면 본문을 포함해 그대로 보존 | +| 코드가 없는 이론 문제 | 코드 없음으로 기록. 답안이나 오답 이유를 만들어내지 않음 | +| 시간 | 실제 제출 시각을 추측하지 않고 Git 커밋의 기록 시각 사용 | +| 상태 저장 | Notion 데이터 소스에 동기화 키를 저장. 일회성 Actions 캐시에 의존하지 않음 | +| 일부 실패 | 이미 생성된 기록을 남기고 다음 실행에서 빠진 기록 처리 | +| 불확실한 쓰기 응답 | 키로 재조회. 확인되지 않으면 중단하며 같은 POST를 즉시 반복하지 않음 | +| 동시 실행 | GitHub Actions concurrency로 단일 쓰기 실행. 별도 로컬 쓰기를 동시에 실행하지 않음 | + +### 한계 + +- **차수는 GitHub에 남아 있는 제출 기준**입니다. 코드트리가 GitHub에 보내지 않은 시도는 복원할 수 없습니다. 실제 사이트의 총 시도 횟수와 일치한다고 보장하지 않습니다. +- 지원 입력은 코드트리의 `[Passed]`, `[Wrong Answer]` 등 판정 커밋과 문제 README입니다. 일반 문서·코드 편집은 시도로 세지 않습니다. 프로그래머스 파서는 다음 단계입니다. +- `main`의 first-parent 이력이 기준입니다. 이력 재작성·다른 브랜치의 제출을 합치는 방식은 지원하지 않습니다. 기존 노션 기록과 이력이 어긋나면 자동 번호 변경 대신 중단합니다. +- 생성한 시도를 삭제하면 다음 실행에서 다시 생성됩니다. 보존하고 싶다면 삭제 대신 메모로 표시하세요. +- 자동화용 `문제 키`, `시도 키`, `차수`는 변경하지 마세요. 기본 뷰에서는 키를 숨겼습니다. +- Notion API는 고유 제약/분산 트랜잭션을 제공하는 DB가 아니므로 엄격한 exactly-once 보장은 아닙니다. 단일 실행, 저장된 키, 오류 후 재조회로 중복 위험을 줄입니다. 여러 프로세스의 동시 수동 실행은 지원하지 않습니다. +- 매우 큰 코드는 Notion 요청 제한을 넘기면 일부를 잘라 저장하지 않고 명시적으로 실패합니다. + +## 실행과 검증 + +필요 환경: Java 21, Git. Maven Wrapper가 Maven을 내려받습니다. 저장소 최상위에서 실행합니다. + +```sh +./notion-sync/mvnw -B -f notion-sync/pom.xml verify +java -jar notion-sync/target/algo-study-sync.jar --dry-run --output notion-sync/target/preview.json +``` + +실제 저장에는 `NOTION_TOKEN`, `NOTION_PROBLEMS_DATA_SOURCE_ID`, `NOTION_ATTEMPTS_DATA_SOURCE_ID`, `GITHUB_REPOSITORY` 환경변수가 필요합니다. dry-run에는 인증이 필요하지 않습니다. + +테스트는 임시 Git 저장소와 가짜 Notion 응답으로 과거 코드 보존, 재제출, 중복 방지, 메모 보존, 쓰기 응답 유실, API 제한과 페이지네이션을 확인합니다. 실제 인증·권한·노션 렌더링은 최초 연결 후 별도로 확인해야 합니다. + +## 다음 단계: 변경사항 요약 + +사용자 요청: AI가 이전 시도와 현재 시도의 코드를 비교하여 **확인되는 변경사항만** 작성합니다. 해설, 정답 제안, 사용자의 의도 추측은 포함하지 않습니다. + +확장 시 입력은 동일 문제의 인접한 두 시도 스냅샷, 출력은 별도의 `변경사항 요약` 영역입니다. 기존 `내 코멘트`와 분리하고, 모델·프롬프트 버전과 대상 시도 키를 기록합니다. 이는 우선 고정된 AI 워크플로로 구현할 수 있으며, 에이전트 구조를 요구하지 않습니다. + +## 근거 문서 + +- https://docs.github.com/en/actions/how-tos/write-workflows/choose-when-workflows-run/trigger-a-workflow +- https://developers.notion.com/guides/data-apis/working-with-page-content +- https://developers.notion.com/reference/query-a-data-source +- https://developers.notion.com/reference/request-limits diff --git a/notion-sync/mvnw b/notion-sync/mvnw new file mode 100755 index 0000000..bd8896b --- /dev/null +++ b/notion-sync/mvnw @@ -0,0 +1,295 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Apache Maven Wrapper startup batch script, version 3.3.4 +# +# Optional ENV vars +# ----------------- +# JAVA_HOME - location of a JDK home dir, required when download maven via java source +# MVNW_REPOURL - repo url base for downloading maven distribution +# MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven +# MVNW_VERBOSE - true: enable verbose log; debug: trace the mvnw script; others: silence the output +# ---------------------------------------------------------------------------- + +set -euf +[ "${MVNW_VERBOSE-}" != debug ] || set -x + +# OS specific support. +native_path() { printf %s\\n "$1"; } +case "$(uname)" in +CYGWIN* | MINGW*) + [ -z "${JAVA_HOME-}" ] || JAVA_HOME="$(cygpath --unix "$JAVA_HOME")" + native_path() { cygpath --path --windows "$1"; } + ;; +esac + +# set JAVACMD and JAVACCMD +set_java_home() { + # For Cygwin and MinGW, ensure paths are in Unix format before anything is touched + if [ -n "${JAVA_HOME-}" ]; then + if [ -x "$JAVA_HOME/jre/sh/java" ]; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + JAVACCMD="$JAVA_HOME/jre/sh/javac" + else + JAVACMD="$JAVA_HOME/bin/java" + JAVACCMD="$JAVA_HOME/bin/javac" + + if [ ! -x "$JAVACMD" ] || [ ! -x "$JAVACCMD" ]; then + echo "The JAVA_HOME environment variable is not defined correctly, so mvnw cannot run." >&2 + echo "JAVA_HOME is set to \"$JAVA_HOME\", but \"\$JAVA_HOME/bin/java\" or \"\$JAVA_HOME/bin/javac\" does not exist." >&2 + return 1 + fi + fi + else + JAVACMD="$( + 'set' +e + 'unset' -f command 2>/dev/null + 'command' -v java + )" || : + JAVACCMD="$( + 'set' +e + 'unset' -f command 2>/dev/null + 'command' -v javac + )" || : + + if [ ! -x "${JAVACMD-}" ] || [ ! -x "${JAVACCMD-}" ]; then + echo "The java/javac command does not exist in PATH nor is JAVA_HOME set, so mvnw cannot run." >&2 + return 1 + fi + fi +} + +# hash string like Java String::hashCode +hash_string() { + str="${1:-}" h=0 + while [ -n "$str" ]; do + char="${str%"${str#?}"}" + h=$(((h * 31 + $(LC_CTYPE=C printf %d "'$char")) % 4294967296)) + str="${str#?}" + done + printf %x\\n $h +} + +verbose() { :; } +[ "${MVNW_VERBOSE-}" != true ] || verbose() { printf %s\\n "${1-}"; } + +die() { + printf %s\\n "$1" >&2 + exit 1 +} + +trim() { + # MWRAPPER-139: + # Trims trailing and leading whitespace, carriage returns, tabs, and linefeeds. + # Needed for removing poorly interpreted newline sequences when running in more + # exotic environments such as mingw bash on Windows. + printf "%s" "${1}" | tr -d '[:space:]' +} + +scriptDir="$(dirname "$0")" +scriptName="$(basename "$0")" + +# parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties +while IFS="=" read -r key value; do + case "${key-}" in + distributionUrl) distributionUrl=$(trim "${value-}") ;; + distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;; + esac +done <"$scriptDir/.mvn/wrapper/maven-wrapper.properties" +[ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties" + +case "${distributionUrl##*/}" in +maven-mvnd-*bin.*) + MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ + case "${PROCESSOR_ARCHITECTURE-}${PROCESSOR_ARCHITEW6432-}:$(uname -a)" in + *AMD64:CYGWIN* | *AMD64:MINGW*) distributionPlatform=windows-amd64 ;; + :Darwin*x86_64) distributionPlatform=darwin-amd64 ;; + :Darwin*arm64) distributionPlatform=darwin-aarch64 ;; + :Linux*x86_64*) distributionPlatform=linux-amd64 ;; + *) + echo "Cannot detect native platform for mvnd on $(uname)-$(uname -m), use pure java version" >&2 + distributionPlatform=linux-amd64 + ;; + esac + distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip" + ;; +maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;; +*) MVN_CMD="mvn${scriptName#mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;; +esac + +# apply MVNW_REPOURL and calculate MAVEN_HOME +# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ +[ -z "${MVNW_REPOURL-}" ] || distributionUrl="$MVNW_REPOURL$_MVNW_REPO_PATTERN${distributionUrl#*"$_MVNW_REPO_PATTERN"}" +distributionUrlName="${distributionUrl##*/}" +distributionUrlNameMain="${distributionUrlName%.*}" +distributionUrlNameMain="${distributionUrlNameMain%-bin}" +MAVEN_USER_HOME="${MAVEN_USER_HOME:-${HOME}/.m2}" +MAVEN_HOME="${MAVEN_USER_HOME}/wrapper/dists/${distributionUrlNameMain-}/$(hash_string "$distributionUrl")" + +exec_maven() { + unset MVNW_VERBOSE MVNW_USERNAME MVNW_PASSWORD MVNW_REPOURL || : + exec "$MAVEN_HOME/bin/$MVN_CMD" "$@" || die "cannot exec $MAVEN_HOME/bin/$MVN_CMD" +} + +if [ -d "$MAVEN_HOME" ]; then + verbose "found existing MAVEN_HOME at $MAVEN_HOME" + exec_maven "$@" +fi + +case "${distributionUrl-}" in +*?-bin.zip | *?maven-mvnd-?*-?*.zip) ;; +*) die "distributionUrl is not valid, must match *-bin.zip or maven-mvnd-*.zip, but found '${distributionUrl-}'" ;; +esac + +# prepare tmp dir +if TMP_DOWNLOAD_DIR="$(mktemp -d)" && [ -d "$TMP_DOWNLOAD_DIR" ]; then + clean() { rm -rf -- "$TMP_DOWNLOAD_DIR"; } + trap clean HUP INT TERM EXIT +else + die "cannot create temp dir" +fi + +mkdir -p -- "${MAVEN_HOME%/*}" + +# Download and Install Apache Maven +verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." +verbose "Downloading from: $distributionUrl" +verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" + +# select .zip or .tar.gz +if ! command -v unzip >/dev/null; then + distributionUrl="${distributionUrl%.zip}.tar.gz" + distributionUrlName="${distributionUrl##*/}" +fi + +# verbose opt +__MVNW_QUIET_WGET=--quiet __MVNW_QUIET_CURL=--silent __MVNW_QUIET_UNZIP=-q __MVNW_QUIET_TAR='' +[ "${MVNW_VERBOSE-}" != true ] || __MVNW_QUIET_WGET='' __MVNW_QUIET_CURL='' __MVNW_QUIET_UNZIP='' __MVNW_QUIET_TAR=v + +# normalize http auth +case "${MVNW_PASSWORD:+has-password}" in +'') MVNW_USERNAME='' MVNW_PASSWORD='' ;; +has-password) [ -n "${MVNW_USERNAME-}" ] || MVNW_USERNAME='' MVNW_PASSWORD='' ;; +esac + +if [ -z "${MVNW_USERNAME-}" ] && command -v wget >/dev/null; then + verbose "Found wget ... using wget" + wget ${__MVNW_QUIET_WGET:+"$__MVNW_QUIET_WGET"} "$distributionUrl" -O "$TMP_DOWNLOAD_DIR/$distributionUrlName" || die "wget: Failed to fetch $distributionUrl" +elif [ -z "${MVNW_USERNAME-}" ] && command -v curl >/dev/null; then + verbose "Found curl ... using curl" + curl ${__MVNW_QUIET_CURL:+"$__MVNW_QUIET_CURL"} -f -L -o "$TMP_DOWNLOAD_DIR/$distributionUrlName" "$distributionUrl" || die "curl: Failed to fetch $distributionUrl" +elif set_java_home; then + verbose "Falling back to use Java to download" + javaSource="$TMP_DOWNLOAD_DIR/Downloader.java" + targetZip="$TMP_DOWNLOAD_DIR/$distributionUrlName" + cat >"$javaSource" <<-END + public class Downloader extends java.net.Authenticator + { + protected java.net.PasswordAuthentication getPasswordAuthentication() + { + return new java.net.PasswordAuthentication( System.getenv( "MVNW_USERNAME" ), System.getenv( "MVNW_PASSWORD" ).toCharArray() ); + } + public static void main( String[] args ) throws Exception + { + setDefault( new Downloader() ); + java.nio.file.Files.copy( java.net.URI.create( args[0] ).toURL().openStream(), java.nio.file.Paths.get( args[1] ).toAbsolutePath().normalize() ); + } + } + END + # For Cygwin/MinGW, switch paths to Windows format before running javac and java + verbose " - Compiling Downloader.java ..." + "$(native_path "$JAVACCMD")" "$(native_path "$javaSource")" || die "Failed to compile Downloader.java" + verbose " - Running Downloader.java ..." + "$(native_path "$JAVACMD")" -cp "$(native_path "$TMP_DOWNLOAD_DIR")" Downloader "$distributionUrl" "$(native_path "$targetZip")" +fi + +# If specified, validate the SHA-256 sum of the Maven distribution zip file +if [ -n "${distributionSha256Sum-}" ]; then + distributionSha256Result=false + if [ "$MVN_CMD" = mvnd.sh ]; then + echo "Checksum validation is not supported for maven-mvnd." >&2 + echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 + exit 1 + elif command -v sha256sum >/dev/null; then + if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c - >/dev/null 2>&1; then + distributionSha256Result=true + fi + elif command -v shasum >/dev/null; then + if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | shasum -a 256 -c >/dev/null 2>&1; then + distributionSha256Result=true + fi + else + echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." >&2 + echo "Please install either command, or disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 + exit 1 + fi + if [ $distributionSha256Result = false ]; then + echo "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised." >&2 + echo "If you updated your Maven version, you need to update the specified distributionSha256Sum property." >&2 + exit 1 + fi +fi + +# unzip and move +if command -v unzip >/dev/null; then + unzip ${__MVNW_QUIET_UNZIP:+"$__MVNW_QUIET_UNZIP"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -d "$TMP_DOWNLOAD_DIR" || die "failed to unzip" +else + tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR" || die "failed to untar" +fi + +# Find the actual extracted directory name (handles snapshots where filename != directory name) +actualDistributionDir="" + +# First try the expected directory name (for regular distributions) +if [ -d "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" ]; then + if [ -f "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/bin/$MVN_CMD" ]; then + actualDistributionDir="$distributionUrlNameMain" + fi +fi + +# If not found, search for any directory with the Maven executable (for snapshots) +if [ -z "$actualDistributionDir" ]; then + # enable globbing to iterate over items + set +f + for dir in "$TMP_DOWNLOAD_DIR"/*; do + if [ -d "$dir" ]; then + if [ -f "$dir/bin/$MVN_CMD" ]; then + actualDistributionDir="$(basename "$dir")" + break + fi + fi + done + set -f +fi + +if [ -z "$actualDistributionDir" ]; then + verbose "Contents of $TMP_DOWNLOAD_DIR:" + verbose "$(ls -la "$TMP_DOWNLOAD_DIR")" + die "Could not find Maven distribution directory in extracted archive" +fi + +verbose "Found extracted Maven distribution directory: $actualDistributionDir" +printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$actualDistributionDir/mvnw.url" +mv -- "$TMP_DOWNLOAD_DIR/$actualDistributionDir" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME" + +clean || : +exec_maven "$@" diff --git a/notion-sync/mvnw.cmd b/notion-sync/mvnw.cmd new file mode 100644 index 0000000..92450f9 --- /dev/null +++ b/notion-sync/mvnw.cmd @@ -0,0 +1,189 @@ +<# : batch portion +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM http://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Apache Maven Wrapper startup batch script, version 3.3.4 +@REM +@REM Optional ENV vars +@REM MVNW_REPOURL - repo url base for downloading maven distribution +@REM MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven +@REM MVNW_VERBOSE - true: enable verbose log; others: silence the output +@REM ---------------------------------------------------------------------------- + +@IF "%__MVNW_ARG0_NAME__%"=="" (SET __MVNW_ARG0_NAME__=%~nx0) +@SET __MVNW_CMD__= +@SET __MVNW_ERROR__= +@SET __MVNW_PSMODULEP_SAVE=%PSModulePath% +@SET PSModulePath= +@FOR /F "usebackq tokens=1* delims==" %%A IN (`powershell -noprofile "& {$scriptDir='%~dp0'; $script='%__MVNW_ARG0_NAME__%'; icm -ScriptBlock ([Scriptblock]::Create((Get-Content -Raw '%~f0'))) -NoNewScope}"`) DO @( + IF "%%A"=="MVN_CMD" (set __MVNW_CMD__=%%B) ELSE IF "%%B"=="" (echo %%A) ELSE (echo %%A=%%B) +) +@SET PSModulePath=%__MVNW_PSMODULEP_SAVE% +@SET __MVNW_PSMODULEP_SAVE= +@SET __MVNW_ARG0_NAME__= +@SET MVNW_USERNAME= +@SET MVNW_PASSWORD= +@IF NOT "%__MVNW_CMD__%"=="" ("%__MVNW_CMD__%" %*) +@echo Cannot start maven from wrapper >&2 && exit /b 1 +@GOTO :EOF +: end batch / begin powershell #> + +$ErrorActionPreference = "Stop" +if ($env:MVNW_VERBOSE -eq "true") { + $VerbosePreference = "Continue" +} + +# calculate distributionUrl, requires .mvn/wrapper/maven-wrapper.properties +$distributionUrl = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionUrl +if (!$distributionUrl) { + Write-Error "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties" +} + +switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) { + "maven-mvnd-*" { + $USE_MVND = $true + $distributionUrl = $distributionUrl -replace '-bin\.[^.]*$',"-windows-amd64.zip" + $MVN_CMD = "mvnd.cmd" + break + } + default { + $USE_MVND = $false + $MVN_CMD = $script -replace '^mvnw','mvn' + break + } +} + +# apply MVNW_REPOURL and calculate MAVEN_HOME +# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ +if ($env:MVNW_REPOURL) { + $MVNW_REPO_PATTERN = if ($USE_MVND -eq $False) { "/org/apache/maven/" } else { "/maven/mvnd/" } + $distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace "^.*$MVNW_REPO_PATTERN",'')" +} +$distributionUrlName = $distributionUrl -replace '^.*/','' +$distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$','' + +$MAVEN_M2_PATH = "$HOME/.m2" +if ($env:MAVEN_USER_HOME) { + $MAVEN_M2_PATH = "$env:MAVEN_USER_HOME" +} + +if (-not (Test-Path -Path $MAVEN_M2_PATH)) { + New-Item -Path $MAVEN_M2_PATH -ItemType Directory | Out-Null +} + +$MAVEN_WRAPPER_DISTS = $null +if ((Get-Item $MAVEN_M2_PATH).Target[0] -eq $null) { + $MAVEN_WRAPPER_DISTS = "$MAVEN_M2_PATH/wrapper/dists" +} else { + $MAVEN_WRAPPER_DISTS = (Get-Item $MAVEN_M2_PATH).Target[0] + "/wrapper/dists" +} + +$MAVEN_HOME_PARENT = "$MAVEN_WRAPPER_DISTS/$distributionUrlNameMain" +$MAVEN_HOME_NAME = ([System.Security.Cryptography.SHA256]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join '' +$MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME" + +if (Test-Path -Path "$MAVEN_HOME" -PathType Container) { + Write-Verbose "found existing MAVEN_HOME at $MAVEN_HOME" + Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" + exit $? +} + +if (! $distributionUrlNameMain -or ($distributionUrlName -eq $distributionUrlNameMain)) { + Write-Error "distributionUrl is not valid, must end with *-bin.zip, but found $distributionUrl" +} + +# prepare tmp dir +$TMP_DOWNLOAD_DIR_HOLDER = New-TemporaryFile +$TMP_DOWNLOAD_DIR = New-Item -Itemtype Directory -Path "$TMP_DOWNLOAD_DIR_HOLDER.dir" +$TMP_DOWNLOAD_DIR_HOLDER.Delete() | Out-Null +trap { + if ($TMP_DOWNLOAD_DIR.Exists) { + try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } + catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } + } +} + +New-Item -Itemtype Directory -Path "$MAVEN_HOME_PARENT" -Force | Out-Null + +# Download and Install Apache Maven +Write-Verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." +Write-Verbose "Downloading from: $distributionUrl" +Write-Verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" + +$webclient = New-Object System.Net.WebClient +if ($env:MVNW_USERNAME -and $env:MVNW_PASSWORD) { + $webclient.Credentials = New-Object System.Net.NetworkCredential($env:MVNW_USERNAME, $env:MVNW_PASSWORD) +} +[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 +$webclient.DownloadFile($distributionUrl, "$TMP_DOWNLOAD_DIR/$distributionUrlName") | Out-Null + +# If specified, validate the SHA-256 sum of the Maven distribution zip file +$distributionSha256Sum = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionSha256Sum +if ($distributionSha256Sum) { + if ($USE_MVND) { + Write-Error "Checksum validation is not supported for maven-mvnd. `nPlease disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." + } + Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash + if ((Get-FileHash "$TMP_DOWNLOAD_DIR/$distributionUrlName" -Algorithm SHA256).Hash.ToLower() -ne $distributionSha256Sum) { + Write-Error "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised. If you updated your Maven version, you need to update the specified distributionSha256Sum property." + } +} + +# unzip and move +Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null + +# Find the actual extracted directory name (handles snapshots where filename != directory name) +$actualDistributionDir = "" + +# First try the expected directory name (for regular distributions) +$expectedPath = Join-Path "$TMP_DOWNLOAD_DIR" "$distributionUrlNameMain" +$expectedMvnPath = Join-Path "$expectedPath" "bin/$MVN_CMD" +if ((Test-Path -Path $expectedPath -PathType Container) -and (Test-Path -Path $expectedMvnPath -PathType Leaf)) { + $actualDistributionDir = $distributionUrlNameMain +} + +# If not found, search for any directory with the Maven executable (for snapshots) +if (!$actualDistributionDir) { + Get-ChildItem -Path "$TMP_DOWNLOAD_DIR" -Directory | ForEach-Object { + $testPath = Join-Path $_.FullName "bin/$MVN_CMD" + if (Test-Path -Path $testPath -PathType Leaf) { + $actualDistributionDir = $_.Name + } + } +} + +if (!$actualDistributionDir) { + Write-Error "Could not find Maven distribution directory in extracted archive" +} + +Write-Verbose "Found extracted Maven distribution directory: $actualDistributionDir" +Rename-Item -Path "$TMP_DOWNLOAD_DIR/$actualDistributionDir" -NewName $MAVEN_HOME_NAME | Out-Null +try { + Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null +} catch { + if (! (Test-Path -Path "$MAVEN_HOME" -PathType Container)) { + Write-Error "fail to move MAVEN_HOME" + } +} finally { + try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } + catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } +} + +Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" diff --git a/notion-sync/mvnwDebug b/notion-sync/mvnwDebug new file mode 100755 index 0000000..a703427 --- /dev/null +++ b/notion-sync/mvnwDebug @@ -0,0 +1,24 @@ +#!/bin/sh + +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# ----------------------------------------------------------------------------- +# Apache Maven Wrapper debug script, version 3.3.4 +# ----------------------------------------------------------------------------- + +. "$(dirname "$0")"/mvnw diff --git a/notion-sync/mvnwDebug.cmd b/notion-sync/mvnwDebug.cmd new file mode 100644 index 0000000..8e6d7bd --- /dev/null +++ b/notion-sync/mvnwDebug.cmd @@ -0,0 +1,27 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM http://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Apache Maven Wrapper debug script, version 3.3.4 +@REM ---------------------------------------------------------------------------- + +@SET __MVNW_ARG0_NAME__=%~nx0 +@%~dp0mvnw.cmd %* +@echo "Cannot find %~dp0mvnw.cmd" >&2 +@cmd /c exit /b 2 diff --git a/notion-sync/pom.xml b/notion-sync/pom.xml new file mode 100644 index 0000000..8799b0f --- /dev/null +++ b/notion-sync/pom.xml @@ -0,0 +1,18 @@ + + + 4.0.0 + dev.minukalgo-study-sync1.0.0 + 21UTF-8 + + com.google.code.gsongson2.13.2 + org.junit.jupiterjunit-jupiter5.13.4test + + algo-study-sync + org.apache.maven.pluginsmaven-compiler-plugin3.14.0 + org.apache.maven.pluginsmaven-surefire-plugin3.5.4 + org.apache.maven.pluginsmaven-shade-plugin3.6.1packageshade + false + dev.minuk.algosync.Main + + + diff --git a/notion-sync/src/main/java/dev/minuk/algosync/GitHistory.java b/notion-sync/src/main/java/dev/minuk/algosync/GitHistory.java new file mode 100644 index 0000000..f306b40 --- /dev/null +++ b/notion-sync/src/main/java/dev/minuk/algosync/GitHistory.java @@ -0,0 +1,107 @@ +package dev.minuk.algosync; + +import java.io.IOException; +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.util.*; +import java.util.regex.Pattern; + +/** Reads immutable snapshots. Solution files are data; never compile or execute them. */ +public final class GitHistory { + private static final Pattern SUBJECT = Pattern.compile("^\\[([^]]+)] (.+)$"); + private static final Pattern LINK = Pattern.compile("^# +\\[(.+)]\\((https://(?:www\\.)?codetree\\.ai/[^\\s)]+)\\)", Pattern.MULTILINE); + private static final Map RESULTS = Map.ofEntries( + Map.entry("Passed", "정답"), Map.entry("Wrong Answer", "오답"), + Map.entry("Time Limit Exceeded", "시간 초과"), Map.entry("Memory Limit Exceeded", "메모리 초과"), + Map.entry("Runtime Error", "런타임 에러"), Map.entry("Compile Error", "컴파일 에러"), + Map.entry("Compilation Error", "컴파일 에러")); + private static final Map LANGUAGES = Map.ofEntries( + Map.entry("java", "java"), Map.entry("py", "python"), Map.entry("cpp", "c++"), + Map.entry("cc", "c++"), Map.entry("c", "c"), Map.entry("js", "javascript"), + Map.entry("ts", "typescript"), Map.entry("kt", "kotlin"), Map.entry("sql", "sql"), + Map.entry("go", "go"), Map.entry("rs", "rust"), Map.entry("cs", "c#"), Map.entry("swift", "swift")); + private final Path root; + public GitHistory(Path root) { this.root = root; } + + public List read() throws IOException, InterruptedException { + if (!git("rev-parse", "--is-shallow-repository").strip().equals("false")) + throw new IOException("Full Git history required: checkout fetch-depth: 0."); + List found = new ArrayList<>(); + Map numbers = new HashMap<>(); + // Parent order, not author timestamps: a clock change must not renumber attempts. + for (String line : git("log", "--reverse", "--first-parent", "--format=%H%x09%cI%x09%s", "HEAD").lines().toList()) { + String[] fields = line.split("\t", 3); + var subject = SUBJECT.matcher(fields[2]); + if (!subject.matches() || !RESULTS.containsKey(subject.group(1))) continue; + String sha = fields[0]; + String titleHint = subject.group(2).replaceFirst(" \\([^()]*ms, [^()]*[KMG]?B\\)$", ""); + List snapshot = nulLines(git("ls-tree", "-rz", "--name-only", sha)); + Set paths = new HashSet<>(snapshot); + Set readmes = new TreeSet<>(); + for (String changed : nulLines(git("diff-tree", "--root", "--no-commit-id", "--name-only", "--no-renames", "-r", "-z", sha))) { + String directory = parent(changed); + while (!directory.isEmpty()) { + String candidate = directory + "/README.md"; + if (paths.contains(candidate)) { readmes.add(candidate); break; } + directory = parent(directory); + } + } + // A real empty commit can still represent a distinct identical-code submission. + if (readmes.isEmpty()) { + for (String path : snapshot) { + if (!path.endsWith("/README.md")) continue; + Metadata metadata = metadata(git("show", sha + ":" + path)); + if (metadata != null && metadata.title.equals(titleHint)) readmes.add(path); + } + if (readmes.size() != 1) throw new IOException("Cannot uniquely resolve submission " + sha); + } + Set seenInCommit = new HashSet<>(); + for (String readme : readmes) { + Metadata data = metadata(git("show", sha + ":" + readme)); + if (data == null) continue; + if (!seenInCommit.add(data.key)) throw new IOException("Ambiguous duplicate problem in " + sha); + String directory = parent(readme); + List files = new ArrayList<>(); + for (String path : snapshot) { + if (!parent(path).equals(directory)) continue; + String extension = path.substring(path.lastIndexOf('.') + 1).toLowerCase(Locale.ROOT); + if (LANGUAGES.containsKey(extension)) + files.add(new Submission.SourceFile(path, LANGUAGES.get(extension), git("show", sha + ":" + path))); + } + int attempt = numbers.merge(data.key, 1, Integer::sum); + found.add(new Submission(data.key, data.title, data.url, data.category, data.difficulty, + sha, fields[1], RESULTS.get(subject.group(1)), attempt, List.copyOf(files))); + } + if (seenInCommit.isEmpty()) throw new IOException("Submission has no supported problem metadata: " + sha); + } + return List.copyOf(found); + } + + record Metadata(String key, String title, String url, String category, String difficulty) {} + static Metadata metadata(String text) { + var link = LINK.matcher(text); + if (!link.find()) return null; + URI uri = URI.create(link.group(2)); + String path = uri.getPath().replaceAll("/+$", ""); + String key = "codetree:" + path.substring(path.lastIndexOf('/') + 1); + String title = link.group(1).replaceFirst("^\\[[^]]+]\\s*", ""); + return new Metadata(key, title, "https://www.codetree.ai" + uri.getRawPath(), + cell(text, "커리큘럼"), cell(text, "난이도")); + } + static String cell(String text, String name) { + var match = Pattern.compile("(?m)^\\|\\s*" + Pattern.quote(name) + "\\s*\\|\\s*(.*?)\\s*\\|\\s*$").matcher(text); + if (!match.find()) return ""; + return match.group(1).replaceAll("\\[([^]]+)]\\([^)]+\\)", "$1"); + } + static String parent(String path) { int index = path.lastIndexOf('/'); return index < 0 ? "" : path.substring(0, index); } + static List nulLines(String value) { return Arrays.stream(value.split("\u0000")).filter(s -> !s.isEmpty()).toList(); } + private String git(String... args) throws IOException, InterruptedException { + List command = new ArrayList<>(List.of("git", "-C", root.toString(), "-c", "core.quotepath=false")); + command.addAll(List.of(args)); + Process process = new ProcessBuilder(command).redirectErrorStream(true).start(); + byte[] output = process.getInputStream().readAllBytes(); + if (process.waitFor() != 0) throw new IOException("Git read failed: " + String.join(" ", args)); + return new String(output, StandardCharsets.UTF_8); + } +} diff --git a/notion-sync/src/main/java/dev/minuk/algosync/Main.java b/notion-sync/src/main/java/dev/minuk/algosync/Main.java new file mode 100644 index 0000000..90cca86 --- /dev/null +++ b/notion-sync/src/main/java/dev/minuk/algosync/Main.java @@ -0,0 +1,44 @@ +package dev.minuk.algosync; + +import com.google.gson.*; +import java.nio.file.*; +import java.util.*; + +public final class Main { + public static void main(String[] args) throws Exception { + boolean dryRun = false; + Path root = Path.of("."); + Path output = null; + for (int i = 0; i < args.length; i++) { + switch (args[i]) { + case "--dry-run" -> dryRun = true; + case "--repo-dir" -> root = Path.of(args[++i]); + case "--output" -> output = Path.of(args[++i]); + default -> throw new IllegalArgumentException("Unknown argument: " + args[i]); + } + } + List submissions = new GitHistory(root).read(); + long problemCount = submissions.stream().map(Submission::problemKey).distinct().count(); + System.out.printf("Read %d problems, %d GitHub-recorded submissions.%n", problemCount, submissions.size()); + if (output != null) { + if (output.toAbsolutePath().getParent() != null) Files.createDirectories(output.toAbsolutePath().getParent()); + Files.writeString(output, new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create().toJson(submissions)); + } + if (dryRun) { + for (Submission s : submissions) System.out.printf("%s / %d차 / %s / %d code files%n", s.title(), s.attempt(), s.result(), s.files().size()); + return; + } + String repository = required("GITHUB_REPOSITORY"); + if (!repository.matches("[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+")) throw new IllegalArgumentException("Invalid GITHUB_REPOSITORY."); + String problems = uuid("NOTION_PROBLEMS_DATA_SOURCE_ID"), attempts = uuid("NOTION_ATTEMPTS_DATA_SOURCE_ID"); + var result = new SyncService(new NotionClient(required("NOTION_TOKEN")), problems, attempts, repository).sync(submissions); + System.out.printf("Created %d problems / %d attempts; preserved %d existing attempts.%n", + result.problemsCreated(), result.attemptsCreated(), result.attemptsSkipped()); + } + static String required(String key) { + String value = System.getenv(key); + if (value == null || value.isBlank()) throw new IllegalArgumentException("Missing environment variable: " + key); + return value; + } + static String uuid(String key) { return UUID.fromString(required(key)).toString(); } +} diff --git a/notion-sync/src/main/java/dev/minuk/algosync/NotionClient.java b/notion-sync/src/main/java/dev/minuk/algosync/NotionClient.java new file mode 100644 index 0000000..b9481b8 --- /dev/null +++ b/notion-sync/src/main/java/dev/minuk/algosync/NotionClient.java @@ -0,0 +1,91 @@ +package dev.minuk.algosync; + +import com.google.gson.*; +import java.io.IOException; +import java.net.URI; +import java.net.http.*; +import java.time.Duration; +import java.util.*; + +public final class NotionClient { + record Response(int status, JsonObject body, String retryAfter) {} + @FunctionalInterface interface Transport { Response call(String method, String path, JsonObject body) throws IOException, InterruptedException; } + @FunctionalInterface interface Sleeper { void sleep(long millis) throws InterruptedException; } + private final Transport transport; + private final Sleeper sleeper; + + public NotionClient(String token) { + HttpClient client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(20)).build(); + this.transport = (method, path, body) -> { + HttpRequest.Builder request = HttpRequest.newBuilder(URI.create("https://api.notion.com/v1/" + path)) + .timeout(Duration.ofSeconds(60)).header("Authorization", "Bearer " + token) + .header("Notion-Version", "2025-09-03").header("Content-Type", "application/json"); + request.method(method, body == null ? HttpRequest.BodyPublishers.noBody() : HttpRequest.BodyPublishers.ofString(body.toString())); + HttpResponse reply = client.send(request.build(), HttpResponse.BodyHandlers.ofString()); + JsonObject parsed; + try { parsed = JsonParser.parseString(reply.body()).getAsJsonObject(); } + catch (RuntimeException e) { throw new IOException("Notion returned a non-JSON response (HTTP " + reply.statusCode() + ")."); } + return new Response(reply.statusCode(), parsed, reply.headers().firstValue("Retry-After").orElse("")); + }; + this.sleeper = Thread::sleep; + } + NotionClient(Transport transport, Sleeper sleeper) { this.transport = transport; this.sleeper = sleeper; } + + public List query(String source, JsonObject filter) throws IOException, InterruptedException { + List result = new ArrayList<>(); + String cursor = null; + Set cursors = new HashSet<>(); + do { + JsonObject body = new JsonObject(); body.addProperty("page_size", 100); + if (filter != null) body.add("filter", filter); + if (cursor != null) body.addProperty("start_cursor", cursor); + JsonObject page = request("POST", "data_sources/" + source + "/query", body, true); + if (page.has("request_status") && "incomplete".equals(string(page.getAsJsonObject("request_status"), "type"))) + throw new IOException("Notion query was incomplete; refusing writes based on a partial index."); + for (JsonElement row : page.getAsJsonArray("results")) result.add(row.getAsJsonObject()); + cursor = page.get("has_more").getAsBoolean() ? string(page, "next_cursor") : null; + if (cursor != null && (cursor.isBlank() || !cursors.add(cursor))) throw new IOException("Invalid pagination cursor."); + } while (cursor != null); + return result; + } + public JsonObject createUnique(String source, String property, String key, JsonObject payload) throws IOException, InterruptedException { + // Never blindly repeat a create: an error response may follow a successful write. + try { return request("POST", "pages", payload, false); } + catch (UncertainWrite failure) { + JsonObject filter = new JsonObject(); filter.addProperty("property", property); + JsonObject equals = new JsonObject(); equals.addProperty("equals", key); filter.add("rich_text", equals); + for (int i = 0; i < 3; i++) { + sleeper.sleep(1000L << i); + List matches = query(source, filter); + if (matches.size() == 1) return matches.getFirst(); + if (matches.size() > 1) throw new IOException("Duplicate key already exists. Resolve duplicates before retrying."); + } + throw new IOException("Write outcome remains uncertain. Check Notion, then rerun; no duplicate create was sent.", failure); + } + } + private JsonObject request(String method, String path, JsonObject body, boolean readOnly) throws IOException, InterruptedException { + for (int attempt = 0; attempt < 5; attempt++) { + Response response; + try { response = transport.call(method, path, body); } + catch (IOException e) { + if (!readOnly) throw new UncertainWrite(e); + if (attempt == 4) throw e; + sleeper.sleep(1000L << attempt); continue; + } + if (response.status >= 200 && response.status < 300) return response.body; + if (!readOnly && response.status >= 500) throw new UncertainWrite(new IOException("HTTP " + response.status)); + if ((response.status == 429 || readOnly && response.status >= 500) && attempt < 4) { + long wait = 1000L << attempt; + try { if (!response.retryAfter.isBlank()) wait = Math.max(0, Long.parseLong(response.retryAfter)) * 1000L; } + catch (NumberFormatException ignored) { /* bounded exponential fallback */ } + // If the server needs a long delay, stop instead of retrying earlier than requested. + if (wait > 60_000) throw new IOException("Notion asks for a longer cooldown; rerun after Retry-After."); + sleeper.sleep(wait); continue; + } + throw new IOException("Notion HTTP " + response.status + " (" + string(response.body, "code") + "). Check access, schema, and request limits."); + } + throw new IOException("Notion retry budget exhausted."); + } + static String string(JsonObject value, String key) { return value.has(key) && !value.get(key).isJsonNull() ? value.get(key).getAsString() : ""; } + static final class UncertainWrite extends IOException { UncertainWrite(IOException cause) { super("Creation may have succeeded.", cause); } } +} diff --git a/notion-sync/src/main/java/dev/minuk/algosync/NotionPages.java b/notion-sync/src/main/java/dev/minuk/algosync/NotionPages.java new file mode 100644 index 0000000..ff4dacc --- /dev/null +++ b/notion-sync/src/main/java/dev/minuk/algosync/NotionPages.java @@ -0,0 +1,67 @@ +package dev.minuk.algosync; + +import com.google.gson.*; +import java.util.*; + +/** Payload construction is separate from IO so the exact content can be previewed and tested. */ +public final class NotionPages { + private static final Gson JSON = new Gson(); + private NotionPages() {} + public static JsonObject problem(String source, Submission s) { + Map properties = new LinkedHashMap<>(); + properties.put("문제", title(s.title())); + properties.put("플랫폼", Map.of("select", Map.of("name", "코드트리"))); + properties.put("분류", text(s.category())); + properties.put("난이도", text(s.difficulty())); + properties.put("문제 링크", Map.of("url", s.url())); + properties.put("복습 상태", Map.of("select", Map.of("name", "미정"))); + properties.put("문제 키", text(s.problemKey())); + return page(source, properties, List.of( + block("paragraph", "위의 ‘시도 이력’에서 1차·2차 기록을 열 수 있습니다."), + block("heading_2", "문제 전체 정리"), + block("paragraph", "이곳에 배운 점과 다시 풀 때 기억할 내용을 직접 적어주세요."))); + } + public static JsonObject attempt(String source, String problemId, String repository, Submission s) { + Map properties = new LinkedHashMap<>(); + properties.put("시도", title(s.attempt() + "차 시도 · " + s.title())); + properties.put("문제", Map.of("relation", List.of(Map.of("id", problemId)))); + properties.put("차수", Map.of("number", s.attempt())); + properties.put("결과", Map.of("select", Map.of("name", s.result()))); + properties.put("기록 시각", Map.of("date", Map.of("start", s.recordedAt()))); + properties.put("커밋", Map.of("url", s.commitUrl(repository))); + properties.put("시도 키", text(s.key(repository))); + List blocks = new ArrayList<>(); + blocks.add(block("heading_2", "내 코멘트")); + blocks.add(block("paragraph", "접근 방법, 막힌 부분, 수정한 이유를 직접 적어주세요.")); + blocks.add(block("heading_2", "제출 코드")); + if (s.files().isEmpty()) blocks.add(block("paragraph", "이 커밋에는 코드 파일이 없습니다. 이론 문제의 답안이나 오답 이유는 GitHub 기록만으로 확인할 수 없습니다.")); + for (var file : s.files()) { + blocks.add(block("paragraph", file.path().substring(file.path().lastIndexOf('/') + 1))); + for (String part : chunks(file.content(), 6000)) blocks.add(Map.of("object", "block", "type", "code", + "code", Map.of("language", file.language(), "rich_text", rich(part)))); + } + blocks.add(block("paragraph", "차수는 GitHub에 기록된 순서이며, 기록 시각은 커밋 시각입니다.")); + return page(source, properties, blocks); + } + static JsonObject page(String source, Map properties, List blocks) { + if (blocks.size() > 95) throw new IllegalArgumentException("Page too large; refusing to truncate code."); + JsonObject result = JSON.toJsonTree(Map.of("parent", Map.of("type", "data_source_id", "data_source_id", source), + "properties", properties, "children", blocks)).getAsJsonObject(); + if (result.toString().getBytes(java.nio.charset.StandardCharsets.UTF_8).length > 450_000) + throw new IllegalArgumentException("Page payload exceeds safe Notion size; refusing to truncate."); + return result; + } + static Object block(String type, String content) { return Map.of("object", "block", "type", type, type, Map.of("rich_text", rich(content))); } + static Object title(String value) { return Map.of("title", rich(value)); } + static Object text(String value) { return Map.of("rich_text", rich(value)); } + static List rich(String value) { return chunks(value, 1900).stream().map(s -> (Object) Map.of("type", "text", "text", Map.of("content", s))).toList(); } + static List chunks(String value, int size) { + List out = new ArrayList<>(); + for (int start = 0; start < value.length();) { + int end = Math.min(start + size, value.length()); + if (end < value.length() && Character.isHighSurrogate(value.charAt(end - 1))) end--; + out.add(value.substring(start, end)); start = end; + } + return out.isEmpty() ? List.of("") : out; + } +} diff --git a/notion-sync/src/main/java/dev/minuk/algosync/Submission.java b/notion-sync/src/main/java/dev/minuk/algosync/Submission.java new file mode 100644 index 0000000..1d019c2 --- /dev/null +++ b/notion-sync/src/main/java/dev/minuk/algosync/Submission.java @@ -0,0 +1,11 @@ +package dev.minuk.algosync; + +import java.util.List; + +public record Submission(String problemKey, String title, String url, String category, + String difficulty, String sha, String recordedAt, String result, + int attempt, List files) { + public record SourceFile(String path, String language, String content) {} + public String key(String repository) { return repository + ":" + problemKey + ":" + sha; } + public String commitUrl(String repository) { return "https://github.com/" + repository + "/commit/" + sha; } +} diff --git a/notion-sync/src/main/java/dev/minuk/algosync/SyncService.java b/notion-sync/src/main/java/dev/minuk/algosync/SyncService.java new file mode 100644 index 0000000..e473cd0 --- /dev/null +++ b/notion-sync/src/main/java/dev/minuk/algosync/SyncService.java @@ -0,0 +1,57 @@ +package dev.minuk.algosync; + +import com.google.gson.*; +import java.io.IOException; +import java.util.*; + +/** Create-only sync: no PATCH/DELETE operations, so existing notes and snapshots are preserved. */ +public final class SyncService { + private final NotionClient notion; + private final String problems, attempts, repository; + public SyncService(NotionClient notion, String problems, String attempts, String repository) { + this.notion = notion; this.problems = problems; this.attempts = attempts; this.repository = repository; + } + public record Result(int problemsCreated, int attemptsCreated, int attemptsSkipped) {} + public Result sync(List submissions) throws IOException, InterruptedException { + Map problemIndex = index(notion.query(problems, null), "문제 키"); + Map attemptIndex = index(notion.query(attempts, null), "시도 키"); + Set expected = new HashSet<>(); + for (Submission s : submissions) { + if (!expected.add(s.key(repository))) throw new IOException("Duplicate input submission."); + JsonObject saved = attemptIndex.get(s.key(repository)); + if (saved != null && saved.getAsJsonObject("properties").getAsJsonObject("차수").get("number").getAsInt() != s.attempt()) + throw new IOException("Attempt numbering differs from saved history; refusing to renumber existing notes."); + } + for (String key : attemptIndex.keySet()) + if (key.startsWith(repository + ":") && !expected.contains(key)) + throw new IOException("Saved submission missing from Git history. Check branch, shallow clone or force push."); + int addedProblems = 0, addedAttempts = 0, skipped = 0; + for (Submission s : submissions) { + if (attemptIndex.containsKey(s.key(repository))) { skipped++; continue; } + JsonObject problem = problemIndex.get(s.problemKey()); + if (problem == null) { + problem = notion.createUnique(problems, "문제 키", s.problemKey(), NotionPages.problem(problems, s)); + problemIndex.put(s.problemKey(), problem); addedProblems++; + } + JsonObject created = notion.createUnique(attempts, "시도 키", s.key(repository), + NotionPages.attempt(attempts, problem.get("id").getAsString(), repository, s)); + attemptIndex.put(s.key(repository), created); addedAttempts++; + } + return new Result(addedProblems, addedAttempts, skipped); + } + static Map index(List pages, String property) throws IOException { + Map result = new HashMap<>(); + for (JsonObject page : pages) { + JsonObject props = page.getAsJsonObject("properties"); + if (props == null || !props.has(property)) throw new IOException("Missing property: " + property); + StringBuilder key = new StringBuilder(); + for (JsonElement rich : props.getAsJsonObject(property).getAsJsonArray("rich_text")) { + JsonObject item = rich.getAsJsonObject(); + key.append(item.has("plain_text") ? item.get("plain_text").getAsString() : item.getAsJsonObject("text").get("content").getAsString()); + } + if (key.isEmpty()) continue; // Manually created rows do not claim a sync key. + if (result.put(key.toString(), page) != null) throw new IOException("Duplicate " + property + "; refusing to add more records."); + } + return result; + } +} diff --git a/notion-sync/src/test/java/dev/minuk/algosync/WorkflowTest.java b/notion-sync/src/test/java/dev/minuk/algosync/WorkflowTest.java new file mode 100644 index 0000000..e1e5d84 --- /dev/null +++ b/notion-sync/src/test/java/dev/minuk/algosync/WorkflowTest.java @@ -0,0 +1,159 @@ +package dev.minuk.algosync; + +import com.google.gson.*; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import java.io.IOException; +import java.nio.file.*; +import java.util.*; +import static org.junit.jupiter.api.Assertions.*; + +class WorkflowTest { + @TempDir Path dir; + private void git(String... args) throws Exception { + var cmd = new ArrayList<>(List.of("git", "-C", dir.toString())); cmd.addAll(List.of(args)); + var process = new ProcessBuilder(cmd).redirectErrorStream(true).start(); + String output = new String(process.getInputStream().readAllBytes(), java.nio.charset.StandardCharsets.UTF_8); + assertEquals(0, process.waitFor(), output); + } + private void init() throws Exception { + git("init"); git("config", "user.name", "Fixture"); git("config", "user.email", "fixture@example.com"); + } + private void write(String path, String content) throws Exception { + Path file = dir.resolve(path); Files.createDirectories(file.getParent()); Files.writeString(file, content); + } + private void commit(String subject) throws Exception { git("add", "."); git("commit", "--allow-empty", "-m", subject); } + private String readme(String name, String slug) { + return "# [[연습]" + name + "](https://www.codetree.ai/trails/complete/curated-cards/" + slug + ")\n\n" + + "| 항목 | 내용 |\n|---|---|\n| 커리큘럼 | [Trail 1 / 조건문](https://www.codetree.ai/trail-info/) |\n| 난이도 | 쉬움 |\n"; + } + @Test void immutableSnapshotsEmptySubmissionsAndSlashInTitle() throws Exception { + init(); String base = "trail1/A/B 출력/"; + write(base + "README.md", readme("A/B 출력", "a-divide-b")); + write(base + "solution.java", "class Main { int value = 1; }\n"); + commit("[Wrong Answer] A/B 출력 (127ms, 10MB)"); + write(base + "solution.java", "class Main { int value = 2; }\n"); + commit("[Passed] A/B 출력 (125ms, 10MB)"); + commit("[Passed] A/B 출력 (126ms, 10MB)"); + write("README.md", "ordinary documentation"); commit("docs: change README"); + List data = new GitHistory(dir).read(); + assertEquals(3, data.size()); + assertEquals(List.of(1, 2, 3), data.stream().map(Submission::attempt).toList()); + assertTrue(data.get(0).files().getFirst().content().contains("value = 1")); + assertTrue(data.get(1).files().getFirst().content().contains("value = 2")); + assertEquals(data.get(1).files(), data.get(2).files()); + assertNotEquals(data.get(1).sha(), data.get(2).sha()); + assertEquals("Trail 1 / 조건문", data.get(0).category()); + assertEquals("A/B 출력", data.get(0).title()); + } + @Test void theoryProblemDoesNotInventSourceOrExplanation() throws Exception { + init(); write("trail3/시간복잡도/README.md", readme("시간복잡도", "complexity")); + commit("[Wrong Answer] 시간복잡도"); + var data = new GitHistory(dir).read(); + assertEquals(1, data.size()); assertTrue(data.getFirst().files().isEmpty()); + String payload = NotionPages.attempt("attempts", "problem", "test/repo", data.getFirst()).toString(); + assertTrue(payload.contains("코드 파일이 없습니다")); + assertFalse(payload.contains("AI")); + } + @Test void folderRenameKeepsProblemIdentity() throws Exception { + init(); write("trail1/old/README.md", readme("이름", "same-id")); + commit("[Wrong Answer] 이름"); + git("mv", "trail1/old", "trail1/new"); commit("[Passed] 이름"); + var data = new GitHistory(dir).read(); + assertEquals(2, data.size()); assertEquals(data.get(0).problemKey(), data.get(1).problemKey()); + assertEquals(2, data.get(1).attempt()); + } + @Test void sourceChunksPreserveUnicodeExactly() { + String source = "a".repeat(1899) + "😀" + "한글\n".repeat(2500); + assertEquals(source, String.join("", NotionPages.chunks(source, 1900))); + for (String chunk : NotionPages.chunks(source, 1900)) { + assertTrue(chunk.length() <= 1900); + assertFalse(Character.isHighSurrogate(chunk.charAt(chunk.length() - 1))); + } + } + private Submission submission(int n) { + return new Submission("codetree:problem", "문제", "https://www.codetree.ai/problems/problem", "조건문", "쉬움", + Integer.toString(n).repeat(40), "2026-11-17T07:00:00Z", n == 1 ? "오답" : "정답", n, + List.of(new Submission.SourceFile("trail1/문제/main.java", "java", "int n = " + n + ";"))); + } + static class FakeNotion implements NotionClient.Transport { + final Map> rows = new HashMap<>(); + int creates, queries, failCreateAt = -1; + FakeNotion() { rows.put("problems", new ArrayList<>()); rows.put("attempts", new ArrayList<>()); } + public NotionClient.Response call(String method, String path, JsonObject body) { + assertEquals("POST", method, "Sync must never PATCH or DELETE existing user notes."); + if (path.equals("pages")) { + JsonObject row = body.deepCopy(); row.addProperty("id", "page-" + ++creates); + rows.get(body.getAsJsonObject("parent").get("data_source_id").getAsString()).add(row); + if (creates == failCreateAt) return new NotionClient.Response(503, new JsonObject(), ""); + return new NotionClient.Response(200, row, ""); + } + queries++; + JsonArray result = new JsonArray(); + for (JsonObject row : rows.get(path.split("/")[1])) { + if (body.has("filter")) { + JsonObject f = body.getAsJsonObject("filter"); + String prop = f.get("property").getAsString(); + String wanted = f.getAsJsonObject("rich_text").get("equals").getAsString(); + String key = row.getAsJsonObject("properties").getAsJsonObject(prop).getAsJsonArray("rich_text") + .get(0).getAsJsonObject().getAsJsonObject("text").get("content").getAsString(); + if (!wanted.equals(key)) continue; + } + result.add(row); + } + JsonObject out = new JsonObject(); out.add("results", result); out.addProperty("has_more", false); + return new NotionClient.Response(200, out, ""); + } + } + @Test void rerunPreservesCommentsAndDoesNotCreateDuplicates() throws Exception { + FakeNotion fake = new FakeNotion(); + var service = new SyncService(new NotionClient(fake, ms -> {}), "problems", "attempts", "test/repo"); + var data = List.of(submission(1), submission(2)); + assertEquals(new SyncService.Result(1, 2, 0), service.sync(data)); + JsonObject first = fake.rows.get("attempts").getFirst(); + first.addProperty("user_written_comment", "내가 직접 쓴 메모"); + String saved = first.toString(); + assertEquals(new SyncService.Result(0, 0, 2), service.sync(data)); + assertEquals(3, fake.creates); assertEquals(saved, first.toString()); + } + @Test void lostWriteResponseReconcilesWithoutRepeatingCreation() throws Exception { + FakeNotion fake = new FakeNotion(); fake.failCreateAt = 2; + var service = new SyncService(new NotionClient(fake, ms -> {}), "problems", "attempts", "test/repo"); + assertEquals(1, service.sync(List.of(submission(1))).attemptsCreated()); + assertEquals(2, fake.creates); assertEquals(1, fake.rows.get("attempts").size()); + } + @Test void unknownWriteOutcomeStopsInsteadOfBlindRetry() { + int[] writes = {0}; + NotionClient.Transport transport = (method, path, body) -> { + if (path.equals("pages")) { writes[0]++; throw new IOException("lost connection"); } + return new NotionClient.Response(200, JsonParser.parseString("{\"results\":[],\"has_more\":false}").getAsJsonObject(), ""); + }; + var client = new NotionClient(transport, ms -> {}); + assertThrows(IOException.class, () -> client.createUnique("attempts", "시도 키", "key", new JsonObject())); + assertEquals(1, writes[0]); + } + @Test void detectsMissingHistoryBeforeWriting() throws Exception { + FakeNotion fake = new FakeNotion(); + var service = new SyncService(new NotionClient(fake, ms -> {}), "problems", "attempts", "test/repo"); + service.sync(List.of(submission(1), submission(2))); + assertThrows(IOException.class, () -> service.sync(List.of(submission(1)))); + assertEquals(3, fake.creates); + } + @Test void paginatesAndHonorsRateLimit() throws Exception { + int[] requests = {0}; List sleeps = new ArrayList<>(); + var client = new NotionClient((method, path, body) -> { + int n = ++requests[0]; + if (n == 1) return new NotionClient.Response(429, new JsonObject(), "2"); + if (n == 2) return new NotionClient.Response(200, JsonParser.parseString("{\"results\":[{\"id\":\"one\"}],\"has_more\":true,\"next_cursor\":\"next\"}").getAsJsonObject(), ""); + assertEquals("next", body.get("start_cursor").getAsString()); + return new NotionClient.Response(200, JsonParser.parseString("{\"results\":[{\"id\":\"two\"}],\"has_more\":false}").getAsJsonObject(), ""); + }, sleeps::add); + assertEquals(2, client.query("problems", null).size()); + assertEquals(List.of(2000L), sleeps); + } + @Test void incompleteIndexPreventsWrites() { + var client = new NotionClient((method, path, body) -> new NotionClient.Response(200, + JsonParser.parseString("{\"results\":[],\"has_more\":false,\"request_status\":{\"type\":\"incomplete\"}}").getAsJsonObject(), ""), ms -> {}); + assertThrows(IOException.class, () -> client.query("problems", null)); + } +}