allure-report-publisher 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f7267f00f39421165f5b1fb7c4e975cff6e856d7ff321bf5b90ac7ab629fff6a
4
- data.tar.gz: 6189da5c7b8743ee4fb102b01f07e9e419996a249b90a8a43601658f2c1ef6cb
3
+ metadata.gz: 818cdc599b432cb8484e95662881253a9f0df4265eb742f3f56f459ecbe57dfd
4
+ data.tar.gz: a261391b7851b0305883c6cd15a9789854051b30d39184728235641f881fe3fa
5
5
  SHA512:
6
- metadata.gz: 52e25db16ed4068c4556162fd4bfe917b138f2e814dd2d0bf6a019b8dd462014ba7af39cbc6ebe62f0d547bee4ca5ea0813e0719f138fb4cc0890ee2f501ea33
7
- data.tar.gz: 6e1a0903f7923fd2abb0538819f24fce3530827280dd1e568be1dc16942f8bdbdca2c03fe4816e7d3bb00d3b631cfe8a7d4bea0f9af77f3e6c8a9afcfed4ab62
6
+ metadata.gz: 9e8ea6e47d729b8e484c660a5add949546662deb3d339caf6d95b4e45c5970db7137375cf3dd67764f5dcb7e9b78859eb52f8dfc3da0e5dedbcb7d346a5f5e26
7
+ data.tar.gz: dec6eec389f65663b7b6f7531796e0e7c0a39a2137e5f94c0dd881e87284ca31b6a268462a7dca99909250b91b911f250e378319dcb3b97930078eaa22b858c9
data/README.md CHANGED
@@ -30,6 +30,9 @@ allure-report-publisher will automatically detect if used in CI environment and
30
30
 
31
31
  ### AWS S3
32
32
 
33
+ - `AWS authentication`: requires environment variables `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` or credentials file `~/.aws/credentials`
34
+ - `Allure report link`: requires `GITHUB_AUTH_TOKEN` or `GITLAB_AUTH_TOKEN` in order to update pull request description with link to latest report
35
+
33
36
  ```shell
34
37
  $ (allure-report-publisher|docker run --rm andrcuns/allure-report-publisher:latest) upload s3 --help
35
38
  Command:
@@ -43,6 +46,7 @@ Description:
43
46
 
44
47
  Options:
45
48
  --[no-]color # Toggle color output
49
+ --[no-]update-pr # Update pull request description with url to allure report, default: false
46
50
  --result-files-glob=VALUE # Allure results files glob. Required: true
47
51
  --bucket=VALUE # Bucket name. Required: true
48
52
  --prefix=VALUE # Optional prefix for report path. Required: false
@@ -6,6 +6,10 @@ module Publisher
6
6
  def self.included(mod)
7
7
  mod.instance_eval do
8
8
  option :color, type: :boolean, desc: "Toggle color output"
9
+ option :update_pr,
10
+ type: :boolean,
11
+ default: false,
12
+ desc: "Update pull request description with url to allure report"
9
13
  end
10
14
  end
11
15
  end
@@ -25,7 +25,7 @@ module Publisher
25
25
  args[:result_files_glob],
26
26
  args[:bucket],
27
27
  args[:prefix]
28
- ).execute
28
+ ).execute(update_pr: args[:update_pr])
29
29
  end
30
30
 
31
31
  private
@@ -48,7 +48,7 @@ module Publisher
48
48
  # @param [String] message
49
49
  # @return [void]
50
50
  def error(message)
51
- puts colorize(message, :red)
51
+ warn colorize(message, :red)
52
52
  exit(1)
53
53
  end
54
54
 
@@ -0,0 +1,111 @@
1
+ module Publisher
2
+ # Namespace for providers executing tests
3
+ #
4
+ module Providers
5
+ # Detect CI provider
6
+ #
7
+ # @return [Publisher::Providers::Base]
8
+ def self.provider
9
+ return Github if ENV["GITHUB_WORKFLOW"]
10
+ return Gitlab if ENV["GITLAB_CI"]
11
+ end
12
+
13
+ # Base class for CI executor info
14
+ #
15
+ class Base
16
+ EXECUTOR_JSON = "executor.json".freeze
17
+ DESCRIPTION_PATTERN = /<!-- allure -->[\s\S]+<!-- allurestop -->/.freeze
18
+
19
+ def initialize(results_path, report_url)
20
+ @results_path = results_path
21
+ @report_url = report_url
22
+ end
23
+
24
+ # :nocov:
25
+
26
+ # Get ci run ID without creating instance of ci provider
27
+ #
28
+ # @return [String]
29
+ def self.run_id
30
+ raise("Not implemented!")
31
+ end
32
+ # :nocov:
33
+
34
+ # Write executor info file
35
+ #
36
+ # @return [void]
37
+ def write_executor_info
38
+ File.open("#{results_path}/#{EXECUTOR_JSON}", "w") do |file|
39
+ file.write(executor_info.to_json)
40
+ end
41
+ end
42
+
43
+ # Add report url to pull request description
44
+ #
45
+ # @return [void]
46
+ def add_report_url
47
+ raise("Not a pull request, skipped!") unless pr?
48
+
49
+ reported = pr_description.match?(DESCRIPTION_PATTERN)
50
+ return update_pr_description(pr_description.gsub(DESCRIPTION_PATTERN, description_template).strip) if reported
51
+
52
+ update_pr_description("#{pr_description}\n\n#{description_template}".strip)
53
+ end
54
+
55
+ private
56
+
57
+ attr_reader :results_path, :report_url
58
+
59
+ # :nocov:
60
+
61
+ # Get executor info
62
+ #
63
+ # @return [Hash]
64
+ def executor_info
65
+ raise("Not implemented!")
66
+ end
67
+
68
+ # Pull request run
69
+ #
70
+ # @return [Boolean]
71
+ def pr?
72
+ raise("Not implemented!")
73
+ end
74
+
75
+ # Current pull request description
76
+ #
77
+ # @return [String]
78
+ def pr_description
79
+ raise("Not implemented!")
80
+ end
81
+
82
+ # Update pull request description
83
+ #
84
+ # @param [String] _desc
85
+ # @return [void]
86
+ def update_pr_description(_desc)
87
+ raise("Not implemented!")
88
+ end
89
+ # :nocov:
90
+
91
+ # CI run id
92
+ #
93
+ # @return [String]
94
+ def run_id
95
+ self.class.run_id
96
+ end
97
+
98
+ # Allure report url pr description
99
+ #
100
+ # @return [String]
101
+ def description_template
102
+ <<~DESC
103
+ <!-- allure -->
104
+ ---
105
+ 📝 [Latest allure report](#{report_url})
106
+ <!-- allurestop -->
107
+ DESC
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,102 @@
1
+ require "octokit"
2
+
3
+ module Publisher
4
+ module Providers
5
+ # Github implementation
6
+ #
7
+ class Github < Base
8
+ # Run id
9
+ #
10
+ # @return [String]
11
+ def self.run_id
12
+ @run_id ||= ENV["GITHUB_RUN_ID"]
13
+ end
14
+
15
+ private
16
+
17
+ # Executor info
18
+ #
19
+ # @return [Hash]
20
+ def executor_info
21
+ {
22
+ name: "Github",
23
+ type: "github",
24
+ reportName: "AllureReport",
25
+ url: server_url,
26
+ reportUrl: report_url,
27
+ buildUrl: build_url,
28
+ buildOrder: run_id,
29
+ buildName: build_name
30
+ }
31
+ end
32
+
33
+ # Github api client
34
+ #
35
+ # @return [Octokit::Client]
36
+ def client
37
+ @client ||= begin
38
+ raise("Missing GITHUB_AUTH_TOKEN environment variable!") unless ENV["GITHUB_AUTH_TOKEN"]
39
+
40
+ Octokit::Client.new(access_token: ENV["GITHUB_AUTH_TOKEN"], api_endpoint: ENV["GITHUB_API_URL"])
41
+ end
42
+ end
43
+
44
+ # Pull request run
45
+ #
46
+ # @return [Boolean]
47
+ def pr?
48
+ ENV["GITHUB_EVENT_NAME"] == "pull_request"
49
+ end
50
+
51
+ # Pull request description
52
+ #
53
+ # @return [String]
54
+ def pr_description
55
+ @pr_description ||= client.pull_request(repository, pr_id)[:body]
56
+ end
57
+
58
+ # Update pull request description
59
+ #
60
+ # @param [String] _desc
61
+ # @return [void]
62
+ def update_pr_description(desc)
63
+ client.update_pull_request(repository, pr_id, body: desc)
64
+ end
65
+
66
+ # Pull request id
67
+ #
68
+ # @return [Integer]
69
+ def pr_id
70
+ @pr_id ||= JSON.parse(File.read(ENV["GITHUB_EVENT_PATH"]))["number"]
71
+ end
72
+
73
+ # Server url
74
+ #
75
+ # @return [String]
76
+ def server_url
77
+ @server_url ||= ENV["GITHUB_SERVER_URL"]
78
+ end
79
+
80
+ # Build url
81
+ #
82
+ # @return [String]
83
+ def build_url
84
+ @build_url ||= "#{server_url}/#{repository}/actions/runs/#{run_id}"
85
+ end
86
+
87
+ # Job name
88
+ #
89
+ # @return [String]
90
+ def build_name
91
+ @build_name ||= ENV["GITHUB_JOB"]
92
+ end
93
+
94
+ # Github repository
95
+ #
96
+ # @return [String]
97
+ def repository
98
+ @repository ||= ENV["GITHUB_REPOSITORY"]
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,103 @@
1
+ require "gitlab"
2
+
3
+ module Publisher
4
+ module Providers
5
+ # Gitlab implementation
6
+ #
7
+ class Gitlab < Base
8
+ # Get ci run ID without creating instance of ci provider
9
+ #
10
+ # @return [String]
11
+ def self.run_id
12
+ @run_id ||= ENV["CI_PIPELINE_ID"]
13
+ end
14
+
15
+ # Get executor info
16
+ #
17
+ # @return [Hash]
18
+ def executor_info
19
+ {
20
+ name: "Gitlab",
21
+ type: "gitlab",
22
+ reportName: "AllureReport",
23
+ url: server_url,
24
+ reportUrl: report_url,
25
+ buildUrl: build_url,
26
+ buildOrder: run_id,
27
+ buildName: build_name
28
+ }
29
+ end
30
+
31
+ # Current pull request description
32
+ #
33
+ # @return [String]
34
+ def pr_description
35
+ @pr_description ||= client.merge_request(project, mr_iid).description
36
+ end
37
+
38
+ # Update pull request description
39
+ #
40
+ # @param [String] desc
41
+ # @return [void]
42
+ def update_pr_description(desc)
43
+ client.update_merge_request(project, mr_iid, description: desc)
44
+ end
45
+
46
+ # Get gitlab client
47
+ #
48
+ # @return [Gitlab::Client]
49
+ def client
50
+ @client ||= begin
51
+ raise("Missing GITLAB_AUTH_TOKEN environment variable!") unless ENV["GITLAB_AUTH_TOKEN"]
52
+
53
+ ::Gitlab::Client.new(
54
+ endpoint: "#{server_url}/api/v4",
55
+ private_token: ENV["GITLAB_AUTH_TOKEN"]
56
+ )
57
+ end
58
+ end
59
+
60
+ # Pull request run
61
+ #
62
+ # @return [Boolean]
63
+ def pr?
64
+ ENV["CI_PIPELINE_SOURCE"] == "merge_request_event"
65
+ end
66
+
67
+ # Merge request iid
68
+ #
69
+ # @return [Integer]
70
+ def mr_iid
71
+ @mr_iid ||= ENV["CI_MERGE_REQUEST_IID"]
72
+ end
73
+
74
+ # Server url
75
+ #
76
+ # @return [String]
77
+ def server_url
78
+ @server_url ||= ENV["CI_SERVER_URL"]
79
+ end
80
+
81
+ # Build url
82
+ #
83
+ # @return [String]
84
+ def build_url
85
+ @build_url ||= ENV["CI_PIPELINE_URL"]
86
+ end
87
+
88
+ # Job name
89
+ #
90
+ # @return [String]
91
+ def build_name
92
+ @build_name ||= ENV["CI_JOB_NAME"]
93
+ end
94
+
95
+ # Github repository
96
+ #
97
+ # @return [String]
98
+ def project
99
+ @project ||= ENV["CI_PROJECT_PATH"]
100
+ end
101
+ end
102
+ end
103
+ end
@@ -24,39 +24,40 @@ module Publisher
24
24
  # Execute allure report generation and upload
25
25
  #
26
26
  # @return [void]
27
- def execute
28
- raise(StandardError, "Not Implemented!")
27
+ def execute(update_pr: false)
28
+ check_client_configured
29
+
30
+ generate_report
31
+ upload_history_and_report
32
+ add_report_url if update_pr
33
+ rescue StandardError => e
34
+ error(e.message)
29
35
  end
30
- # :nocov:
31
36
 
32
37
  private
33
38
 
34
39
  attr_reader :results_glob, :bucket, :prefix
35
40
 
36
- # :nocov:
37
-
38
- # Report url
41
+ # Validate if client is properly configured
42
+ # and raise error if it is not
39
43
  #
40
- # @return [String]
41
- def report_url
42
- raise(StandardError, "Not Implemented!")
44
+ # @return [void]
45
+ def check_client_configured
46
+ raise("Not Implemented!")
43
47
  end
44
- # :nocov:
45
48
 
46
- # Get run id
49
+ # Report url
47
50
  #
48
51
  # @return [String]
49
- def run_id
50
- @run_id ||= CI.provider&.run_id
52
+ def report_url
53
+ raise("Not Implemented!")
51
54
  end
52
55
 
53
- # Get CI provider
56
+ # Upload report to s3
54
57
  #
55
- # @return [Publisher::CI::Base]
56
- def ci_provider
57
- return @ci_provider if defined?(@ci_provider)
58
-
59
- @ci_provider = CI.provider&.new(results_dir, report_url)
58
+ # @return [void]
59
+ def upload_history_and_report
60
+ raise("Not implemented!")
60
61
  end
61
62
 
62
63
  # Add allure history
@@ -77,11 +78,50 @@ module Publisher
77
78
  return unless ci_provider
78
79
 
79
80
  log("Adding executor info")
80
- Helpers::Spinner.spin("adding") do
81
+ Helpers::Spinner.spin("adding executor") do
81
82
  ci_provider.write_executor_info
82
83
  end
83
84
  end
84
85
 
86
+ # Generate allure report
87
+ #
88
+ # @return [void]
89
+ def generate_report
90
+ add_history
91
+ add_executor_info
92
+
93
+ ReportGenerator.new(results_glob, results_dir, report_dir).generate
94
+ end
95
+
96
+ # Add allure report url to pull request description
97
+ #
98
+ # @return [void]
99
+ def add_report_url
100
+ return unless ci_provider
101
+
102
+ log("Adding allure report link to pr description")
103
+ Helpers::Spinner.spin("adding link", exit_on_error: false) do
104
+ ci_provider.add_report_url
105
+ end
106
+ end
107
+ # :nocov:
108
+
109
+ # Get run id
110
+ #
111
+ # @return [String]
112
+ def run_id
113
+ @run_id ||= Providers.provider&.run_id
114
+ end
115
+
116
+ # Get CI provider
117
+ #
118
+ # @return [Publisher::Providers::Base]
119
+ def ci_provider
120
+ return @ci_provider if defined?(@ci_provider)
121
+
122
+ @ci_provider = Providers.provider&.new(results_dir, report_url)
123
+ end
124
+
85
125
  # Fetch allure report history
86
126
  #
87
127
  # @return [void]
@@ -122,16 +162,6 @@ module Publisher
122
162
  .glob("#{report_dir}/**/*")
123
163
  .reject(&:directory?)
124
164
  end
125
-
126
- # Generate allure report
127
- #
128
- # @return [void]
129
- def generate_report
130
- add_history
131
- add_executor_info
132
-
133
- ReportGenerator.new(results_glob, results_dir, report_dir).generate
134
- end
135
165
  end
136
166
  end
137
167
  end
@@ -5,18 +5,14 @@ module Publisher
5
5
  # Report upload to AWS S3 bucket
6
6
  #
7
7
  class S3 < Uploader
8
- def execute
9
- generate_report
10
- upload_history_and_report
11
- end
12
-
13
8
  private
14
9
 
15
- # S3 client
10
+ # Validate if client is properly configured
11
+ # and raise error if it is not
16
12
  #
17
- # @return [Aws::S3::Client]
18
- def s3
19
- @s3 ||= Aws::S3::Client.new(region: ENV["AWS_REGION"] || "us-east-1")
13
+ # @return [void]
14
+ def check_client_configured
15
+ s3
20
16
  end
21
17
 
22
18
  # Report url
@@ -54,6 +50,19 @@ module Publisher
54
50
  end
55
51
  end
56
52
 
53
+ # S3 client
54
+ #
55
+ # @return [Aws::S3::Client]
56
+ def s3
57
+ @s3 ||= Aws::S3::Client.new(region: ENV["AWS_REGION"] || "us-east-1")
58
+ rescue Aws::Sigv4::Errors::MissingCredentialsError
59
+ raise(<<~MSG.strip)
60
+ missing aws credentials, provide credentials with one of the following options:
61
+ - AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables
62
+ - ~/.aws/credentials file
63
+ MSG
64
+ end
65
+
57
66
  # Upload allure history
58
67
  #
59
68
  # @return [void]
@@ -61,6 +70,9 @@ module Publisher
61
70
  upload_to_s3(report_files.select { |file| file.fnmatch?("*/history/*") }, prefix)
62
71
  end
63
72
 
73
+ # Upload allure report
74
+ #
75
+ # @return [void]
64
76
  def upload_report
65
77
  upload_to_s3(report_files)
66
78
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Publisher
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.3"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: allure-report-publisher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrejs Cunskis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-29 00:00:00.000000000 Z
11
+ date: 2021-05-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-s3
@@ -44,6 +44,34 @@ dependencies:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
46
  version: 0.6.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: gitlab
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '4.17'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '4.17'
61
+ - !ruby/object:Gem::Dependency
62
+ name: octokit
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '4.21'
68
+ type: :runtime
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '4.21'
47
75
  - !ruby/object:Gem::Dependency
48
76
  name: parallel
49
77
  requirement: !ruby/object:Gem::Requirement
@@ -114,10 +142,11 @@ files:
114
142
  - lib/allure_report_publisher/commands/base.rb
115
143
  - lib/allure_report_publisher/commands/upload_s3.rb
116
144
  - lib/allure_report_publisher/commands/version.rb
117
- - lib/allure_report_publisher/lib/ci/_base.rb
118
- - lib/allure_report_publisher/lib/ci/github_actions.rb
119
145
  - lib/allure_report_publisher/lib/helpers/helpers.rb
120
146
  - lib/allure_report_publisher/lib/helpers/spinner.rb
147
+ - lib/allure_report_publisher/lib/providers/_base.rb
148
+ - lib/allure_report_publisher/lib/providers/github.rb
149
+ - lib/allure_report_publisher/lib/providers/gitlab.rb
121
150
  - lib/allure_report_publisher/lib/report_generator.rb
122
151
  - lib/allure_report_publisher/lib/uploaders/_uploader.rb
123
152
  - lib/allure_report_publisher/lib/uploaders/s3_uploader.rb
@@ -1,63 +0,0 @@
1
- module Publisher
2
- # CI provider utilities
3
- #
4
- module CI
5
- # Detect CI provider
6
- #
7
- # @return [Publisher::CI::Base]
8
- def self.provider
9
- return GithubActions if ENV["GITHUB_WORKFLOW"]
10
- end
11
-
12
- # Base class for CI executor info
13
- #
14
- class Base
15
- EXECUTOR_JSON = "executor.json".freeze
16
-
17
- def initialize(results_path, report_url)
18
- @results_path = results_path
19
- @report_url = report_url
20
- end
21
-
22
- # :nocov:
23
-
24
- # Get ci run ID without creating instance of ci provider
25
- #
26
- # @return [String]
27
- def self.run_id
28
- raise("Not implemented!")
29
- end
30
- # :nocov:
31
-
32
- # Write executor info file
33
- #
34
- # @return [void]
35
- def write_executor_info
36
- File.open("#{results_path}/#{EXECUTOR_JSON}", "w") do |file|
37
- file.write(executor_info.to_json)
38
- end
39
- end
40
-
41
- private
42
-
43
- attr_reader :results_path, :report_url
44
-
45
- # :nocov:
46
-
47
- # Get executor info
48
- #
49
- # @return [Hash]
50
- def executor_info
51
- raise("Not implemented!")
52
- end
53
- # :nocov:
54
-
55
- # CI run id
56
- #
57
- # @return [String]
58
- def run_id
59
- self.class.run_id
60
- end
61
- end
62
- end
63
- end
@@ -1,50 +0,0 @@
1
- module Publisher
2
- module CI
3
- # Github actions executor info
4
- #
5
- class GithubActions < Base
6
- # Run id
7
- #
8
- # @return [String]
9
- def self.run_id
10
- @run_id ||= ENV["GITHUB_RUN_ID"]
11
- end
12
-
13
- private
14
-
15
- # Executor info
16
- #
17
- # @return [Hash]
18
- def executor_info
19
- {
20
- name: "Github",
21
- type: "github",
22
- reportName: "AllureReport",
23
- url: server_url,
24
- reportUrl: report_url,
25
- buildUrl: build_url,
26
- buildOrder: run_id,
27
- buildName: build_name
28
- }
29
- end
30
-
31
- # Server url
32
- #
33
- # @return [String]
34
- def server_url
35
- @server_url ||= ENV["GITHUB_SERVER_URL"]
36
- end
37
-
38
- # Build url
39
- #
40
- # @return [String]
41
- def build_url
42
- @build_url ||= "#{server_url}/#{ENV['GITHUB_REPOSITORY']}/actions/runs/#{run_id}"
43
- end
44
-
45
- def build_name
46
- @build_name ||= ENV["GITHUB_JOB"]
47
- end
48
- end
49
- end
50
- end