[level 3] Title: 있었는데요 없었습니다, Time: 0.00 ms, Memory: 0.0 MB -BaekjoonHub #157
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Convert Commit to Issue | |
on: | |
push: | |
branches: | |
- main | |
paths: | |
- '백준/**' | |
- '프로그래머스/**' | |
workflow_dispatch: | |
jobs: | |
create_issue: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 # 모든 커밋을 가져오기 위해 | |
- name: Create Issue for Commit | |
env: | |
GITHUB_TOKEN: ${{ secrets.ACC_TKN }} | |
run: | | |
git log ${{ github.event.before }}..${{ github.event.after }} --format='%H' | while read commit_hash; do | |
COMMIT_MESSAGE=$(git log -1 --format=%B $commit_hash) | |
COMMIT_TITLE=$(echo "$COMMIT_MESSAGE" | head -n 1) | |
COMMIT_BODY=$(echo "$COMMIT_MESSAGE" | awk '/^$/ {seen_blank=1; next} seen_blank {print}') | |
echo "Commit Title: $COMMIT_TITLE" | |
echo "Commit Body: $COMMIT_BODY" | |
echo "Files changed in this commit: $COMMITTED_FILES" | |
problem_title=$(echo "$COMMIT_TITLE" | awk -F 'Title: |,' '{print $2}') | |
if [[ -z "$problem_title" ]]; then | |
echo "This commit is not by algorithm solving." | |
elif [[ $COMMIT_TITLE == [* ]]; then | |
echo "문제: $problem_title" | |
echo "Creating issue for commit starting with [" | |
# remove double qoute using tr -d '"' | |
COMMITTED_FILES=$(git diff-tree --no-commit-id --name-only -r $commit_hash | tr -d '"') | |
echo $COMMITTED_FILES | |
# Initializing an empty string for the links | |
COMMITTED_FILES_LINKS="" | |
# Looping through each committed file to create a Markdown link | |
for file in $COMMITTED_FILES; do | |
decoded_file=$(printf "%b" "$file") | |
# Replace this with your GitHub repository details | |
file_url="https://github.com/$GITHUB_REPOSITORY/blob/main/$decoded_file" | |
COMMITTED_FILES_LINKS+="- [$decoded_file]($file_url)\n" | |
done | |
for file in $COMMITTED_FILES; do | |
# Check if the file is a README.md in the specific path | |
decoded_file=$(printf "%b" "$file") | |
if [[ "$decoded_file" == 백준/*/*README.md ]]; then | |
# Assuming $decoded_file holds the path to the README.md file | |
README_CONTENT=$(gh api repos/:owner/:repo/contents/$decoded_file --jq '.content' | base64 --decode) | |
echo $README_CONTENT | |
# Now you can process README_CONTENT with awk or other tools as needed | |
LABELS=$(echo $README_CONTENT | awk '{ | |
match($0, /### 분류 ([^#]+) ###/, arr); | |
if (arr[1] != "") print arr[1]; | |
}') | |
echo $LABELS | |
# Convert labels into format suitable for `gh issue create` command | |
GH_LABELS=$(echo $LABELS | tr ',' '\n' | sed 's/ /-/g' | sed 's/^-//;s/-$//' | awk '{printf "--label \"%s\" ", $0}') | |
echo $GH_LABELS | |
break # Assuming only one README.md is relevant per commit | |
fi | |
done | |
# Use gh issue list and grep to find an issue by title | |
existing_issue=$(gh issue list --repo "$GITHUB_REPOSITORY" | grep -w "$problem_title" | awk '{print $1}') | |
if [[ ! -z "$existing_issue" ]]; then | |
echo "Issue already exists with title: $problem_title, Issue Number: $existing_issue" | |
# Create a comment on the existing issue | |
comment_body=$(echo -e "A new commit has been made that references this issue.\n\n**Committed Files:**\n$COMMITTED_FILES_LINKS") | |
gh issue comment $existing_issue --body "$comment_body" --repo "$GITHUB_REPOSITORY" | |
else | |
# 먼저 현재 리포지토리의 모든 라벨을 가져옵니다. | |
existing_labels=$(gh label list --repo "$GITHUB_REPOSITORY" | cut -f1) | |
# 라벨이 존재하는지 확인하고, 없으면 생성합니다. | |
for label in $(echo $LABELS | tr ',' '\n' | sed 's/ /-/g' | sed 's/^-//;s/-$//'); do | |
# echo 명령을 사용하여 현재 처리 중인 라벨을 출력할 수 있습니다. | |
echo "Processing label: $label" | |
# 현재 라벨이 이미 존재하는 라벨 목록에 있는지 확인합니다. | |
if ! grep -q "^$label$" <<< "$existing_labels"; then | |
echo "Creating label: $label" | |
gh label create "$label" --force --description "Automatically created label" --repo "$GITHUB_REPOSITORY" | |
else | |
echo "Label already exists: $label" | |
fi | |
done | |
# No existing issue found, proceed to create a new issue | |
issue_body=$(echo -e "This issue was automatically generated from a commit.\n\n**Committed Files:**\n$COMMITTED_FILES_LINKS") | |
issue_response=$(gh issue create --title "$problem_title" --body "$issue_body" --repo "$GITHUB_REPOSITORY" --assignee @me $GH_LABELS --label "auto-generated") | |
echo "New issue created: $issue_response" | |
fi | |
fi | |
done |