forked from xuanxu/upload-files-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_branch.rb
99 lines (84 loc) · 4.03 KB
/
create_branch.rb
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
require "octokit"
require "pathname"
def gh_token
gh_token_from_env = ENV['BOT_TOKEN'].to_s.strip
gh_token_from_env = ENV['GH_ACCESS_TOKEN'].to_s.strip if gh_token_from_env.empty?
raise "!! ERROR: Invalid GitHub Token" if gh_token_from_env.empty?
gh_token_from_env
end
def github_client
@github_client ||= Octokit::Client.new(access_token: gh_token, auto_paginate: true)
end
def get_main_ref(repo)
main_ref = github_client.refs(repo).select { |r| r[:ref] == "refs/heads/main" }.first
if main_ref.nil?
main_ref = github_client.refs(repo).select { |r| r[:ref] == "refs/heads/master" }.first
end
main_ref.object.sha
end
issue_id = ENV["ISSUE_ID"]
pdf_path = ENV["PDF_PATH"].to_s.strip
jats_path = ENV["JATS_PATH"].to_s.strip
crossref_path = ENV["CROSSREF_PATH"].to_s.strip
papers_repo = ENV["PAPERS_REPO"]
branch_prefix = ENV["BRANCH_PREFIX"]
id = "%05d" % issue_id
branch = branch_prefix.empty? ? id.to_s : "#{branch_prefix}.#{id}"
ref = "heads/#{branch}"
begin
# Check branch existence
github_client.ref(papers_repo, ref)
# Delete old branch and create it again
github_client.delete_ref(papers_repo, ref)
github_client.create_ref(papers_repo, ref, get_main_ref(papers_repo))
rescue Octokit::NotFound
# Create branch if it doesn't exist
github_client.create_ref(papers_repo, ref, get_main_ref(papers_repo))
end
pdf_uploaded_path = "#{branch}/10.55458.#{branch}.pdf"
jats_uploaded_path = "#{branch}/10.55458.#{branch}.jats"
crossref_uploaded_path = "#{branch}/10.55458.#{branch}.crossref.xml"
# Add PDF file if present
if !pdf_path.empty? && File.exist?(pdf_path)
gh_response = github_client.create_contents(papers_repo,
pdf_uploaded_path,
"Creating 10.55458.#{branch}.pdf",
File.open("#{pdf_path.strip}").read,
branch: branch)
system("echo 'pdf_html_url=#{gh_response.content.html_url}' >> $GITHUB_OUTPUT")
system("echo 'pdf_download_url=#{gh_response.content.download_url}' >> $GITHUB_OUTPUT")
end
# Add Crossref XML file if present
if !crossref_path.empty? && File.exist?(crossref_path)
crossref_gh_response = github_client.create_contents(papers_repo,
crossref_uploaded_path,
"Creating 10.55458.#{branch}.crossref.xml",
File.open("#{crossref_path.strip}").read,
branch: branch)
system("echo 'crossref_html_url=#{crossref_gh_response.content.html_url}' >> $GITHUB_OUTPUT")
system("echo 'crossref_download_url=#{crossref_gh_response.content.download_url}' >> $GITHUB_OUTPUT")
end
# Add JATS file if present
if !jats_path.empty? && File.exist?(jats_path)
jats_gh_response = github_client.create_contents(papers_repo,
jats_uploaded_path,
"Creating 10.55458.#{branch}.jats",
File.open("#{jats_path.strip}").read,
branch: branch)
system("echo 'jats_html_url=#{jats_gh_response.content.html_url}' >> $GITHUB_OUTPUT")
system("echo 'jats_download_url=#{jats_gh_response.content.download_url}' >> $GITHUB_OUTPUT")
# Add JATS' media files if present
media_folder = File.join(File.dirname(jats_path), "media")
if Dir.exist?(media_folder)
media_files = Dir[File.join(media_folder, "**/*.*")]
media_files.each do |media_file|
media_file_name = Pathname(media_file).relative_path_from(media_folder).to_s
media_file_uploaded_path = "#{branch}/media/#{media_file_name}"
github_client.create_contents(papers_repo,
media_file_uploaded_path,
"Adding media file: #{media_file_name}",
File.open(media_file).read,
branch: branch)
end
end
end