-
Notifications
You must be signed in to change notification settings - Fork 20
150 lines (130 loc) · 5.45 KB
/
copy_parse_markdown.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
name: Copy Parse Markdown and Generate JSON from Source Repo
on:
workflow_dispatch:
inputs:
source_repo:
description: 'Source repository name'
required: true
default: 'DmitryRyumin/NewEraAI-Papers'
source_file_path:
description: 'Path to the file in the source repository'
required: true
default: 'code/markdown_to_json_parser.py'
code_directory:
description: 'Directory where code is stored'
required: true
default: 'code'
display_file_contents:
description: 'Whether or not to display the contents of the doanload file and the destination file'
required: true
default: 'false'
type: choice
options:
- true
- false
schedule:
- cron: '30 23 * * *'
jobs:
copy-code:
runs-on: ubuntu-latest
permissions:
contents: write
env:
INPUT_SOURCE_REPO: ${{ github.event.inputs.source_repo || 'DmitryRyumin/NewEraAI-Papers' }}
INPUT_SOURCE_FILE_PATH: ${{ github.event.inputs.source_file_path || 'code/markdown_to_json_parser.py' }}
INPUT_CODE_DIRECTORY: ${{ github.event.inputs.code_directory || 'code' }}
INPUT_DISPLAY_FILE_CONTENTS: ${{ github.event.inputs.display_file_contents || 'false' }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
continue-on-error: true # Continue with the workflow even if the checkout fails
with:
ref: ${{ github.event.before || 'main' }}
- name: Set SOURCE_FILE_PATH
run: |
echo "SOURCE_FILE_PATH=${{ github.workspace }}/${{ env.INPUT_CODE_DIRECTORY }}/markdown_to_json_parser_new.py" >> $GITHUB_ENV
- name: Set SOURCE_DESTINATION_FILE_PATH
run: |
echo "SOURCE_DESTINATION_FILE_PATH=${{ github.workspace }}/${{ env.INPUT_SOURCE_FILE_PATH }}" >> $GITHUB_ENV
- name: Define show_file_content function
run: echo 'source ${{ github.workspace }}/scripts/show_file_content.sh' > show_file_content_step.sh
- name: Download source file
run: |
if mkdir -p "${{ github.workspace }}/${{ env.INPUT_CODE_DIRECTORY }}"; then
echo "Directory created successfully or already existed."
else
echo "Failed to create directory."
exit 1
fi
source_url="https://raw.githubusercontent.com/${{ env.INPUT_SOURCE_REPO }}/main/${{ env.INPUT_SOURCE_FILE_PATH }}"
if curl -o "${{ env.SOURCE_FILE_PATH }}" "$source_url"; then
echo "Source file downloaded successfully."
else
echo "Failed to download source file."
exit 1
fi
- name: Display content of the downloaded source file
run: |
set -e
source show_file_content_step.sh
show_file_content "${{ env.SOURCE_FILE_PATH }}" "${{ env.INPUT_DISPLAY_FILE_CONTENTS }}"
- name: Display content of destination file from target repository
run: |
set -e
source show_file_content_step.sh
show_file_content "${{ env.SOURCE_DESTINATION_FILE_PATH }}" "${{ env.INPUT_DISPLAY_FILE_CONTENTS }}"
- name: Compare and handle files
run: |
set -e
handle_equal_files() {
echo "Files are equal. Deleting SOURCE_FILE_PATH."
if rm "${{ env.SOURCE_FILE_PATH }}"; then
echo "SOURCE_FILE_PATH deleted successfully."
else
echo "Failed to delete SOURCE_FILE_PATH."
exit 1
fi
}
handle_unequal_files() {
echo "Files are not equal. Replacing SOURCE_DESTINATION_FILE_PATH with content from SOURCE_FILE_PATH."
if cat "${{ env.SOURCE_FILE_PATH }}" > "${{ env.SOURCE_DESTINATION_FILE_PATH }}"; then
echo "SOURCE_DESTINATION_FILE_PATH replaced successfully."
rm "${{ env.SOURCE_FILE_PATH }}"
else
echo "Failed to replace SOURCE_DESTINATION_FILE_PATH."
exit 1
fi
}
handle_missing_destination() {
echo "SOURCE_DESTINATION_FILE_PATH does not exist. Renaming SOURCE_FILE_PATH to SOURCE_DESTINATION_FILE_PATH."
if [ -f "${{ env.SOURCE_FILE_PATH }}" ] && [ -f "${{ env.SOURCE_DESTINATION_FILE_PATH }}" ]; then
if mv "${{ env.SOURCE_FILE_PATH }}" "${{ env.SOURCE_DESTINATION_FILE_PATH }}"; then
echo "Files renamed successfully."
else
echo "Failed to rename files."
exit 1
fi
else
echo "One or both of the files do not exist."
fi
}
if [ -f "${{ env.SOURCE_DESTINATION_FILE_PATH }}" ]; then
if cmp -s "${{ env.SOURCE_DESTINATION_FILE_PATH }}" "${{ env.SOURCE_FILE_PATH }}"; then
handle_equal_files
else
handle_unequal_files
fi
else
handle_missing_destination
fi
- name: Display working code directory content
run: |
ls -al "${{ github.workspace }}/${{ env.INPUT_CODE_DIRECTORY }}"
- name: Auto commit changes
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: 'Copy Parse Markdown and Generate JSON from Source Repo'
env:
PAPER_TOKEN: ${{ secrets.PAPER_TOKEN }}
- name: Set output status
run: echo "status=${{ steps.parse.outcome }}" >> $GITHUB_ENV