jekyll-github-pages-gem 1.0.0 → 1.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +3 -1
- data/Gemfile.lock +32 -25
- data/README.md +10 -1
- data/Rakefile +5 -0
- data/jekyll_github_pages.gemspec +5 -1
- data/lib/factories/base_factory.rb +40 -0
- data/lib/factories/page_factory.rb +67 -0
- data/lib/factories/post_factory.rb +55 -1
- data/lib/jekyll_github_pages.rb +9 -7
- data/lib/models/page.rb +13 -0
- data/lib/models/post_image_manager.rb +17 -15
- data/lib/services/{post_services/base_post_service.rb → base_editing_service.rb} +2 -2
- data/lib/services/github_service.rb +69 -122
- data/lib/services/kramdown_service.rb +1 -80
- data/lib/services/page_service.rb +85 -0
- data/lib/services/post_service.rb +184 -0
- data/lib/uploaders/post_image_uploader.rb +1 -0
- metadata +58 -8
- data/lib/services/post_services/post_creation_service.rb +0 -55
- data/lib/services/post_services/post_editing_service.rb +0 -46
- data/lib/services/post_services/post_pull_request_editing_service.rb +0 -33
@@ -0,0 +1,184 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../factories/post_factory'
|
4
|
+
|
5
|
+
module Services
|
6
|
+
##
|
7
|
+
# This class contains all operations related to posts on a Jekyll website
|
8
|
+
class PostService < BaseEditingService
|
9
|
+
def initialize(repo_name, access_token)
|
10
|
+
super(repo_name, access_token)
|
11
|
+
@post_factory = Factories::PostFactory.new
|
12
|
+
@kramdown_service = Services::KramdownService.new
|
13
|
+
end
|
14
|
+
|
15
|
+
##
|
16
|
+
# This method fetches all the markdown contents of all the posts on a Jekyll website
|
17
|
+
# that have been written and returns a list of models representing a Post.
|
18
|
+
def get_all_posts
|
19
|
+
result = []
|
20
|
+
api_posts = @github_service.get_contents_from_path('_posts')
|
21
|
+
api_posts.each do |api_post|
|
22
|
+
post_text_contents = @github_service.get_text_contents_from_file(api_post.path)
|
23
|
+
post_model = @post_factory.create_post(post_text_contents, api_post.path, nil)
|
24
|
+
image_paths = @kramdown_service.get_all_image_paths(post_model.contents)
|
25
|
+
|
26
|
+
images = []
|
27
|
+
image_paths.each do |image_path|
|
28
|
+
image_content = @github_service.get_contents_from_path(image_path)
|
29
|
+
images << create_post_image(image_path, image_content.content)
|
30
|
+
end
|
31
|
+
|
32
|
+
post_model.images = images
|
33
|
+
|
34
|
+
result << post_model
|
35
|
+
end
|
36
|
+
result
|
37
|
+
end
|
38
|
+
|
39
|
+
##
|
40
|
+
# This method fetches all of the posts that have been written but have not been merged into master yet
|
41
|
+
#
|
42
|
+
# Params
|
43
|
+
# +pr_body+::the pr body for the posts in PR
|
44
|
+
def get_all_posts_in_pr(pr_body)
|
45
|
+
result = []
|
46
|
+
pull_requests = @github_service.get_open_pull_requests_with_body(pr_body)
|
47
|
+
pull_requests.each do |pull_request|
|
48
|
+
pull_request_files = @github_service.get_pr_files(pull_request[:number])
|
49
|
+
|
50
|
+
post = nil
|
51
|
+
images = []
|
52
|
+
pull_request_files.each do |pull_request_file|
|
53
|
+
ref = @github_service.get_ref_from_contents_url(pull_request_file[:contents_url])
|
54
|
+
pr_file_contents = @github_service.get_contents_from_path(pull_request_file[:filename], ref)
|
55
|
+
|
56
|
+
if pull_request_file[:filename].end_with?('.md')
|
57
|
+
post_text_contents = @github_service.get_text_content_from_file(pr_file_contents.path, ref)
|
58
|
+
post = @post_factory.create_post(post_text_contents, pr_file_contents.path, ref)
|
59
|
+
result << post
|
60
|
+
else
|
61
|
+
images << create_post_image(pr_file_contents.path, pr_file_contents.content)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
post.images = images
|
66
|
+
end
|
67
|
+
result
|
68
|
+
end
|
69
|
+
|
70
|
+
##
|
71
|
+
# This method fetches a single post from a Jekyll website given a post title
|
72
|
+
# and returns a Post model
|
73
|
+
#
|
74
|
+
# Params:
|
75
|
+
# +title+:: A title of a Jekyll website post
|
76
|
+
# +ref+::a sha for a ref indicating the head of a branch a post is pushed to on the GitHub server
|
77
|
+
def get_post_by_title(title, ref)
|
78
|
+
result = nil
|
79
|
+
result = get_all_posts_in_pr.find { |x| x.title == title } if ref
|
80
|
+
result = get_all_posts.find { |x| x.title == title } unless ref
|
81
|
+
result&.images&.each { |x| PostImageManager.instance.add_downloaded_image(x) }
|
82
|
+
result
|
83
|
+
end
|
84
|
+
|
85
|
+
##
|
86
|
+
# This method submits a new post to GitHub by checking out a new branch for the post,
|
87
|
+
# if the branch already doesn't exist. Commiting and pushing the markdown and any photos
|
88
|
+
# attached to the post to the branch. And then finally opening a pull request into master
|
89
|
+
# for the new post.
|
90
|
+
#
|
91
|
+
# Params
|
92
|
+
# +oauth_token+::a user's oauth access token
|
93
|
+
# +post_markdown+:: the markdown contents of a post
|
94
|
+
# +pull_request_body+::an optional pull request body for the post, it will be blank if nothing is provided
|
95
|
+
# +reviewers+:: an optional list of reviewers for the post PR
|
96
|
+
def create_post(post_markdown, post_title, pull_request_body = '', reviewers = [])
|
97
|
+
# This ref_name variable represents the branch name
|
98
|
+
# for creating a post. At the end we strip out all of the whitespace in
|
99
|
+
# the post_title to create a valid branch name
|
100
|
+
branch_name = "createPost#{post_title.gsub(/\s+/, '')}"
|
101
|
+
ref_name = "heads/#{branch_name}"
|
102
|
+
|
103
|
+
master_head_sha = @github_service.get_master_head_sha
|
104
|
+
sha_base_tree = @github_service.get_base_tree_for_branch(master_head_sha)
|
105
|
+
|
106
|
+
@github_service.create_ref_if_necessary(ref_name, master_head_sha)
|
107
|
+
|
108
|
+
new_post_path = create_new_filepath_for_post(post_title)
|
109
|
+
new_tree_sha = create_new_tree(post_markdown, post_title, new_post_path, sha_base_tree)
|
110
|
+
|
111
|
+
@github_service.commit_and_push_to_repo("Created post #{post_title}",
|
112
|
+
new_tree_sha, master_head_sha, ref_name)
|
113
|
+
@github_service.create_pull_request(branch_name, 'master', "Created Post #{post_title}",
|
114
|
+
pull_request_body,
|
115
|
+
reviewers)
|
116
|
+
|
117
|
+
PostImageManager.instance.clear
|
118
|
+
end
|
119
|
+
|
120
|
+
##
|
121
|
+
# This method submits changes to an existing post to GitHub by checking out a new branch for the post,
|
122
|
+
# if the branch already doesn't exist. Commiting and pushing the markdown changes and any added photos
|
123
|
+
# for the existing post to the branch. And the finally opening a pull request into master for the new post.
|
124
|
+
#
|
125
|
+
# Params
|
126
|
+
# +post_markdown+::the modified markdown to submit
|
127
|
+
# +post_title+::the title for the existing post
|
128
|
+
# +existing_post_file_path+::the file path to the existing post on GitHub
|
129
|
+
# +pull_request_body+::an optional pull request body for the post, it will be blank if nothing is provided
|
130
|
+
# +reviewers+:: an optional list of reviewers for the post PR
|
131
|
+
def edit_post(post_markdown, post_title, existing_post_file_path, pull_request_body = '', reviewers = [])
|
132
|
+
# This ref_name variable represents the branch name
|
133
|
+
# for editing a post. At the end we strip out all of the whitespace in
|
134
|
+
# the post_title to create a valid branch name
|
135
|
+
branch_name = "editPost#{post_title.gsub(/\s+/, '')}"
|
136
|
+
ref_name = "heads/#{branch_name}"
|
137
|
+
|
138
|
+
master_head_sha = @github_service.get_master_head_sha
|
139
|
+
sha_base_tree = @github_service.get_base_tree_for_branch(master_head_sha)
|
140
|
+
|
141
|
+
@github_service.create_ref_if_necessary(ref_name, master_head_sha)
|
142
|
+
new_tree_sha = create_new_tree(post_markdown, post_title, existing_post_file_path, sha_base_tree)
|
143
|
+
|
144
|
+
@github_service.commit_and_push_to_repo("Edited post #{post_title}", new_tree_sha, master_head_sha, ref_name)
|
145
|
+
@github_service.create_pull_request(branch_name, 'master', "Edited Post #{post_title}",
|
146
|
+
pull_request_body,
|
147
|
+
reviewers)
|
148
|
+
|
149
|
+
PostImageManager.instance.clear
|
150
|
+
end
|
151
|
+
|
152
|
+
##
|
153
|
+
# This method submits changes to a post that is already in PR, commiting and pushing the markdown changes
|
154
|
+
# and any added photos to the branch. Since the post is in PR these changes will be a PR updated to the given branch
|
155
|
+
#
|
156
|
+
# Params:
|
157
|
+
# +post_markdown+::the modified markdown to submit
|
158
|
+
# +post_title+::the title for the existing post
|
159
|
+
# +existing_post_file_path+::the file path to the existing post on GitHub
|
160
|
+
# +ref+::the ref to update
|
161
|
+
def edit_post_in_pr(post_markdown, post_title, existing_post_file_path, ref)
|
162
|
+
ref_name = @github_service.get_ref_name_by_sha(ref)
|
163
|
+
sha_base_tree = @github_service.get_base_tree_for_branch(ref)
|
164
|
+
|
165
|
+
new_tree_sha = create_new_tree(post_markdown, post_title, existing_post_file_path, sha_base_tree)
|
166
|
+
@github_service.commit_and_push_to_repo("Edited post #{post_title}", new_tree_sha, ref, ref_name)
|
167
|
+
|
168
|
+
PostImageManager.instance.clear
|
169
|
+
end
|
170
|
+
|
171
|
+
private
|
172
|
+
|
173
|
+
def create_new_filepath_for_post(post_title)
|
174
|
+
"_posts/#{DateTime.now.strftime('%Y-%m-%d')}-#{post_title.gsub(/\s+/, '')}.md"
|
175
|
+
end
|
176
|
+
|
177
|
+
def create_post_image(filename, contents)
|
178
|
+
result = PostImage.new
|
179
|
+
result.filename = filename
|
180
|
+
result.contents = contents
|
181
|
+
result
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,63 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jekyll-github-pages-gem
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- MSOE SSE Web Team
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
12
|
-
dependencies:
|
11
|
+
date: 2020-09-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: carrierwave
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.0.0.rc
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '3.0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 2.0.0.rc
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '3.0'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: kramdown
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 2.3.0
|
40
|
+
type: :runtime
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 2.3.0
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: octokit
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '4.18'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '4.18'
|
13
61
|
description:
|
14
62
|
email:
|
15
63
|
executables: []
|
@@ -21,16 +69,18 @@ files:
|
|
21
69
|
- README.md
|
22
70
|
- Rakefile
|
23
71
|
- jekyll_github_pages.gemspec
|
72
|
+
- lib/factories/base_factory.rb
|
73
|
+
- lib/factories/page_factory.rb
|
24
74
|
- lib/factories/post_factory.rb
|
25
75
|
- lib/jekyll_github_pages.rb
|
76
|
+
- lib/models/page.rb
|
26
77
|
- lib/models/post.rb
|
27
78
|
- lib/models/post_image_manager.rb
|
79
|
+
- lib/services/base_editing_service.rb
|
28
80
|
- lib/services/github_service.rb
|
29
81
|
- lib/services/kramdown_service.rb
|
30
|
-
- lib/services/
|
31
|
-
- lib/services/
|
32
|
-
- lib/services/post_services/post_editing_service.rb
|
33
|
-
- lib/services/post_services/post_pull_request_editing_service.rb
|
82
|
+
- lib/services/page_service.rb
|
83
|
+
- lib/services/post_service.rb
|
34
84
|
- lib/uploaders/post_image_uploader.rb
|
35
85
|
homepage:
|
36
86
|
licenses:
|
@@ -44,7 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
44
94
|
requirements:
|
45
95
|
- - ">="
|
46
96
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
97
|
+
version: 2.5.1
|
48
98
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
99
|
requirements:
|
50
100
|
- - ">="
|
@@ -1,55 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative './base_post_service'
|
4
|
-
require_relative '../github_service'
|
5
|
-
|
6
|
-
module Services
|
7
|
-
##
|
8
|
-
# This class is responsible for creating posts on a Jekyll website
|
9
|
-
class PostCreationService < BasePostService
|
10
|
-
def initialize(repo_name, access_token)
|
11
|
-
super(repo_name, access_token)
|
12
|
-
end
|
13
|
-
|
14
|
-
##
|
15
|
-
# This method submits a new post to GitHub by checking out a new branch for the post,
|
16
|
-
# if the branch already doesn't exist. Commiting and pushing the markdown and any photos
|
17
|
-
# attached to the post to the branch. And then finally opening a pull request into master
|
18
|
-
# for the new post.
|
19
|
-
#
|
20
|
-
# Params
|
21
|
-
# +oauth_token+::a user's oauth access token
|
22
|
-
# +post_markdown+:: the markdown contents of a post
|
23
|
-
# +pull_request_body+::an optional pull request body for the post, it will be blank if nothing is provided
|
24
|
-
# +reviewers+:: an optional list of reviewers for the post PR
|
25
|
-
def create_post(post_markdown, post_title, pull_request_body = '', reviewers = [])
|
26
|
-
# This ref_name variable represents the branch name
|
27
|
-
# for creating a post. At the end we strip out all of the whitespace in
|
28
|
-
# the post_title to create a valid branch name
|
29
|
-
branch_name = "createPost#{post_title.gsub(/\s+/, '')}"
|
30
|
-
ref_name = "heads/#{branch_name}"
|
31
|
-
|
32
|
-
master_head_sha = @github_service.get_master_head_sha
|
33
|
-
sha_base_tree = @github_service.get_base_tree_for_branch(master_head_sha)
|
34
|
-
|
35
|
-
@github_service.create_ref_if_necessary(ref_name, master_head_sha)
|
36
|
-
|
37
|
-
new_post_path = create_new_filepath_for_post(post_title)
|
38
|
-
new_tree_sha = create_new_tree(post_markdown, post_title, new_post_path, sha_base_tree)
|
39
|
-
|
40
|
-
@github_service.commit_and_push_to_repo("Created post #{post_title}",
|
41
|
-
new_tree_sha, master_head_sha, ref_name)
|
42
|
-
@github_service.create_pull_request(branch_name, 'master', "Created Post #{post_title}",
|
43
|
-
pull_request_body,
|
44
|
-
reviewers)
|
45
|
-
|
46
|
-
PostImageManager.instance.clear
|
47
|
-
end
|
48
|
-
|
49
|
-
private
|
50
|
-
|
51
|
-
def create_new_filepath_for_post(post_title)
|
52
|
-
"_posts/#{DateTime.now.strftime('%Y-%m-%d')}-#{post_title.gsub(/\s+/, '')}.md"
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
@@ -1,46 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative './base_post_service'
|
4
|
-
require_relative '../github_service'
|
5
|
-
|
6
|
-
module Services
|
7
|
-
##
|
8
|
-
# This class is responsible for editing posts on a Jekyll website
|
9
|
-
class PostEditingService < BasePostService
|
10
|
-
def initialize(repo_name, access_token)
|
11
|
-
super(repo_name, access_token)
|
12
|
-
end
|
13
|
-
|
14
|
-
##
|
15
|
-
# This method submits changes to an existing post to GitHub by checking out a new branch for the post,
|
16
|
-
# if the branch already doesn't exist. Commiting and pushing the markdown changes and any added photos
|
17
|
-
# for the existing post to the branch. And the finally opening a pull request into master for the new post.
|
18
|
-
#
|
19
|
-
# Params
|
20
|
-
# +post_markdown+::the modified markdown to submit
|
21
|
-
# +post_title+::the title for the existing post
|
22
|
-
# +existing_post_file_path+::the file path to the existing post on GitHub
|
23
|
-
# +pull_request_body+::an optional pull request body for the post, it will be blank if nothing is provided
|
24
|
-
# +reviewers+:: an optional list of reviewers for the post PR
|
25
|
-
def edit_post(post_markdown, post_title, existing_post_file_path, pull_request_body = '', reviewers = [])
|
26
|
-
# This ref_name variable represents the branch name
|
27
|
-
# for editing a post. At the end we strip out all of the whitespace in
|
28
|
-
# the post_title to create a valid branch name
|
29
|
-
branch_name = "editPost#{post_title.gsub(/\s+/, '')}"
|
30
|
-
ref_name = "heads/#{branch_name}"
|
31
|
-
|
32
|
-
master_head_sha = @github_service.get_master_head_sha
|
33
|
-
sha_base_tree = @github_service.get_base_tree_for_branch(master_head_sha)
|
34
|
-
|
35
|
-
@github_service.create_ref_if_necessary(ref_name, master_head_sha)
|
36
|
-
new_tree_sha = create_new_tree(post_markdown, post_title, existing_post_file_path, sha_base_tree)
|
37
|
-
|
38
|
-
@github_service.commit_and_push_to_repo("Edited post #{post_title}", new_tree_sha, master_head_sha, ref_name)
|
39
|
-
@github_service.create_pull_request(branch_name, 'master', "Edited Post #{post_title}",
|
40
|
-
pull_request_body,
|
41
|
-
reviewers)
|
42
|
-
|
43
|
-
PostImageManager.instance.clear
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative './base_post_service'
|
4
|
-
require_relative '../github_service'
|
5
|
-
|
6
|
-
module Services
|
7
|
-
##
|
8
|
-
# This class is responsible for editing posts that are in PR on a Jekyll website
|
9
|
-
class PostPullRequestEditingService < BasePostService
|
10
|
-
def initialize(repo_name, access_token)
|
11
|
-
super(repo_name, access_token)
|
12
|
-
end
|
13
|
-
|
14
|
-
##
|
15
|
-
# This method submits changes to a post that is already in PR, commiting and pushing the markdown changes
|
16
|
-
# and any added photos to the branch. Since the post is in PR these changes will be a PR updated to the given branch
|
17
|
-
#
|
18
|
-
# Params:
|
19
|
-
# +post_markdown+::the modified markdown to submit
|
20
|
-
# +post_title+::the title for the existing post
|
21
|
-
# +existing_post_file_path+::the file path to the existing post on GitHub
|
22
|
-
# +ref+::the ref to update
|
23
|
-
def edit_post_in_pr(post_markdown, post_title, existing_post_file_path, ref)
|
24
|
-
ref_name = @github_service.get_ref_name_by_sha(ref)
|
25
|
-
sha_base_tree = @github_service.get_base_tree_for_branch(ref)
|
26
|
-
|
27
|
-
new_tree_sha = create_new_tree(post_markdown, post_title, existing_post_file_path, sha_base_tree)
|
28
|
-
@github_service.commit_and_push_to_repo("Edited post #{post_title}", new_tree_sha, ref, ref_name)
|
29
|
-
|
30
|
-
PostImageManager.instance.clear
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|