github_commit 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e1aba9e53e7a3cb374904d4ab5df1f56bafbfca2101e4392e66925180b813bb0
4
+ data.tar.gz: a5c9b4c5f323b5053aee094394b96896dd86804690f2758b64e8d3f91d8d9671
5
+ SHA512:
6
+ metadata.gz: b0969e1f6d79c6d44d742d4890372c27bb22251ebfd6869f04c3a3ab440fee9dbabc229e4a7cc3cd79e24028e367cad5789c221f564b86c25edbe4fe2464fbeb
7
+ data.tar.gz: d8ab17c36008d74b5fe9932bc40fbd39e97fece3b05263f4110e9a57805e30da75ded0db5bb72b963485dfb06dbafef6ffb95153933b57c50a27d62b708d6e27
@@ -0,0 +1,15 @@
1
+ on: [push, pull_request]
2
+ jobs:
3
+ test:
4
+ runs-on: ubuntu-latest
5
+ steps:
6
+ - uses: actions/checkout@v2
7
+ - uses: ruby/setup-ruby@v1
8
+ - uses: actions/cache@v1
9
+ with:
10
+ path: vendor/bundle
11
+ key: bundle-${{ hashFiles('**/Gemfile.lock') }}
12
+ restore-keys: bundle
13
+ - run: bundle install --jobs 4 --retry 3 --deployment
14
+ - run: bundle exec rspec
15
+ - run: bundle exec rubocop
@@ -0,0 +1,5 @@
1
+ .bundle
2
+ coverage
3
+ github_commit-*
4
+ tmp
5
+ vendor
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
@@ -0,0 +1,9 @@
1
+ inherit_gem:
2
+ rubocop-govuk:
3
+ - config/default.yml
4
+ - config/rake.yml
5
+ - config/rspec.yml
6
+
7
+ inherit_mode:
8
+ merge:
9
+ - Exclude
@@ -0,0 +1 @@
1
+ 2.7.2
@@ -0,0 +1,3 @@
1
+ # 0.1.0
2
+
3
+ - Initial release
@@ -0,0 +1,16 @@
1
+ FROM ruby:2.7.2
2
+
3
+ RUN apt-get update -qq && apt-get install -y build-essential \
4
+ software-properties-common
5
+
6
+ RUN apt-get clean
7
+
8
+ # Install GitHub CLI
9
+ RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-key C99B11DEB97541F0
10
+ RUN apt-add-repository https://cli.github.com/packages
11
+ RUN apt update
12
+ RUN apt install gh
13
+
14
+ RUN gem install github_commit octokit
15
+ ADD . /
16
+ WORKDIR /opt/resource
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,107 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ github_commit (0.1.0)
5
+ octokit (~> 4.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (6.1.0)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 1.6, < 2)
13
+ minitest (>= 5.1)
14
+ tzinfo (~> 2.0)
15
+ zeitwerk (~> 2.3)
16
+ addressable (2.7.0)
17
+ public_suffix (>= 2.0.2, < 5.0)
18
+ ast (2.4.1)
19
+ brakeman (4.10.0)
20
+ concurrent-ruby (1.1.7)
21
+ diff-lcs (1.4.4)
22
+ docile (1.3.3)
23
+ faraday (1.1.0)
24
+ multipart-post (>= 1.2, < 3)
25
+ ruby2_keywords
26
+ i18n (1.8.5)
27
+ concurrent-ruby (~> 1.0)
28
+ minitest (5.14.2)
29
+ multipart-post (2.1.1)
30
+ octokit (4.19.0)
31
+ faraday (>= 0.9)
32
+ sawyer (~> 0.8.0, >= 0.5.3)
33
+ parallel (1.20.1)
34
+ parser (2.7.2.0)
35
+ ast (~> 2.4.1)
36
+ public_suffix (4.0.6)
37
+ rack (2.2.3)
38
+ rainbow (3.0.0)
39
+ regexp_parser (2.0.1)
40
+ rexml (3.2.4)
41
+ rspec (3.10.0)
42
+ rspec-core (~> 3.10.0)
43
+ rspec-expectations (~> 3.10.0)
44
+ rspec-mocks (~> 3.10.0)
45
+ rspec-core (3.10.0)
46
+ rspec-support (~> 3.10.0)
47
+ rspec-expectations (3.10.0)
48
+ diff-lcs (>= 1.2.0, < 2.0)
49
+ rspec-support (~> 3.10.0)
50
+ rspec-mocks (3.10.0)
51
+ diff-lcs (>= 1.2.0, < 2.0)
52
+ rspec-support (~> 3.10.0)
53
+ rspec-support (3.10.0)
54
+ rubocop (0.87.1)
55
+ parallel (~> 1.10)
56
+ parser (>= 2.7.1.1)
57
+ rainbow (>= 2.2.2, < 4.0)
58
+ regexp_parser (>= 1.7)
59
+ rexml
60
+ rubocop-ast (>= 0.1.0, < 1.0)
61
+ ruby-progressbar (~> 1.7)
62
+ unicode-display_width (>= 1.4.0, < 2.0)
63
+ rubocop-ast (0.8.0)
64
+ parser (>= 2.7.1.5)
65
+ rubocop-govuk (3.17.2)
66
+ rubocop (= 0.87.1)
67
+ rubocop-ast (= 0.8.0)
68
+ rubocop-rails (= 2.8.1)
69
+ rubocop-rake (= 0.5.1)
70
+ rubocop-rspec (= 1.42.0)
71
+ rubocop-rails (2.8.1)
72
+ activesupport (>= 4.2.0)
73
+ rack (>= 1.1)
74
+ rubocop (>= 0.87.0)
75
+ rubocop-rake (0.5.1)
76
+ rubocop
77
+ rubocop-rspec (1.42.0)
78
+ rubocop (>= 0.87.0)
79
+ ruby-progressbar (1.10.1)
80
+ ruby2_keywords (0.0.2)
81
+ sawyer (0.8.2)
82
+ addressable (>= 2.3.5)
83
+ faraday (> 0.8, < 2.0)
84
+ simplecov (0.20.0)
85
+ docile (~> 1.1)
86
+ simplecov-html (~> 0.11)
87
+ simplecov_json_formatter (~> 0.1)
88
+ simplecov-html (0.12.3)
89
+ simplecov_json_formatter (0.1.2)
90
+ tzinfo (2.0.4)
91
+ concurrent-ruby (~> 1.0)
92
+ unicode-display_width (1.7.0)
93
+ zeitwerk (2.4.2)
94
+
95
+ PLATFORMS
96
+ ruby
97
+
98
+ DEPENDENCIES
99
+ brakeman (~> 4.10)
100
+ github_commit!
101
+ rspec (~> 3.0)
102
+ rubocop (~> 0.87)
103
+ rubocop-govuk (~> 3.17)
104
+ simplecov (~> 0.20)
105
+
106
+ BUNDLED WITH
107
+ 2.1.4
@@ -0,0 +1,71 @@
1
+ # Concourse GitHub Commit Resource
2
+
3
+ A Concourse resource type for GitHub commits.
4
+
5
+ The `in` step creates a directory `.github` containing several files:
6
+
7
+ * `ref` contains the sha of the commit
8
+ * `author` contains the name of the committer from GitHub's API
9
+ * `message` contains the commit message from GitHub's API
10
+ * `state` contains the current combined status of the commit, if any
11
+ * `repo` contains the pulled repository checked out at the specified commit
12
+ * `combined_status` contains the full `combined_status` JSON from GitHub's API
13
+ * `commit` contains the full `commit` JSON from GitHub's API
14
+
15
+ ## Usage
16
+
17
+ The following example runs tests for every new commit, and updates the status
18
+ for a check on a GitHub commit (pending, and then success/failure/error).
19
+
20
+ ```yaml
21
+ resource_types:
22
+ - name: github-commit
23
+ type: registry-image
24
+ source:
25
+ repository: govuk/github-commit
26
+
27
+ resources:
28
+ - name: commit
29
+ type: github-commit
30
+ source:
31
+ repository: alphagov/frontend
32
+ access_token: ((github_access_token))
33
+ path: commit # relative path to the commit, must match the resource name.
34
+
35
+ jobs:
36
+ - name: run-tests
37
+ plan:
38
+ - get: github-commit
39
+ trigger: true
40
+ - put: github-commit
41
+ params:
42
+ context: run-tests # corresponds to GitHub Status API context field
43
+ status: pending # puts a pending status on the GitHub commit
44
+ - task: run-tests
45
+ file: repo/run-tests.yml
46
+ on_success:
47
+ put: commit
48
+ params: {status: success, context: run-tests} # puts a success status on the GitHub commit
49
+ on_failure:
50
+ put: commit
51
+ params: {status: failure, context: run-tests}
52
+ on_abort:
53
+ put: commit
54
+ params: {status: error, context: run-tests}
55
+ on_error:
56
+ put: commit
57
+ params: {status: error, context: run-tests}
58
+ ```
59
+
60
+ ## `get`
61
+
62
+ Options:
63
+
64
+ ```
65
+ collaborators_only: Only `verified` commits from collaborators
66
+ ```
67
+
68
+ ## Development
69
+
70
+ The output of this project is a container image pushed to DockerHub, for use
71
+ in Concourse pipelines as a Resource Type.
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'github_commit'
4
+ require 'json'
5
+ require 'octokit'
6
+
7
+ input = JSON.load($stdin.read, nil, { symbolize_names: true, create_additions: false })
8
+
9
+ repo = input.dig(:source, :repository)
10
+ access_token = input.dig(:source, :access_token)
11
+ sha = input.dig(:version, :ref)
12
+
13
+ service = GithubCommit::CommitsFetcher.new(
14
+ client: Octokit::Client.new(access_token: access_token),
15
+ repo: repo,
16
+ )
17
+
18
+ puts service.commits(sha: sha).to_json
data/bin/in ADDED
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'github_commit'
4
+ require 'json'
5
+ require 'octokit'
6
+
7
+ dir, *_ = ARGV
8
+
9
+ Dir.mkdir(dir) unless File.exists?(dir)
10
+
11
+ input = JSON.load($stdin.read, nil, { symbolize_names: true, create_additions: false })
12
+ repo = input.dig(:source, :repository)
13
+ access_token = input.dig(:source, :access_token)
14
+ sha = input.dig(:version, :ref)
15
+
16
+ service = GithubCommit::CommitFetcher.new(
17
+ client: Octokit::Client.new(access_token: access_token),
18
+ repo: repo,
19
+ )
20
+
21
+ writer = GithubCommit::CommitWriter.new(
22
+ commit: service.fetch_commit(sha: sha),
23
+ status: service.fetch_status(sha: sha),
24
+ dir: dir
25
+ )
26
+
27
+ writer.write_commit!
28
+
29
+ `git clone https://github.com/#{repo}.git #{dir}/repo`
30
+ `cd #{dir}/repo && git checkout #{sha}`
31
+
32
+ puts writer.metadata.to_json
data/bin/out ADDED
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'github_commit'
4
+ require 'json'
5
+ require 'octokit'
6
+
7
+ $stdout = STDERR
8
+
9
+ dir, *_ = ARGV
10
+
11
+ input = JSON.load($stdin.read, nil, { symbolize_names: true, create_additions: false })
12
+
13
+ path = input.dig(:source, :path)
14
+ unless path
15
+ puts "[Error] path must be provided on Concourse Resource Type source"
16
+ exit 1
17
+ end
18
+
19
+ sha_file = "#{dir}/#{path}/ref"
20
+ access_token = input.dig(:source, :access_token)
21
+ state = input.dig(:params, :status)
22
+ repository = input.dig(:source, :repository)
23
+
24
+ unless access_token && File.exists?(sha_file) && state && repository
25
+ missing = []
26
+ missing << :access_token unless access_token && !access_token.empty?
27
+ missing << :sha unless File.exists?(sha_file)
28
+ missing << :state unless state && !state.empty?
29
+ missing << :repository unless repository && !repository.empty?
30
+
31
+ puts("[Error] Missing params: #{ missing.join(', ') }")
32
+
33
+ exit 1
34
+ end
35
+
36
+ sha = File.read(sha_file)
37
+ author = File.read("#{dir}/#{path}/author")
38
+ message = File.read("#{dir}/#{path}/message")
39
+
40
+ updater = GithubCommit::StatusUpdater.new(
41
+ client: Octokit::Client.new(access_token: access_token),
42
+ repo: repository,
43
+ sha: sha,
44
+ )
45
+
46
+ def parse_target_url(provided_target)
47
+ return provided_target if provided_target
48
+ if ENV.key?("ATC_EXTERNAL_URL")
49
+ return "#{ENV["ATC_EXTERNAL_URL"]}/teams/#{ENV["BUILD_TEAM_NAME"]}/pipelines/#{ENV["BUILD_PIPELINE_NAME"]}/jobs/#{ENV["BUILD_JOB_NAME"]}/builds/#{ENV["BUILD_NAME"]}"
50
+ end
51
+ nil
52
+ end
53
+
54
+ updater.update_status(
55
+ status: state,
56
+ context: input.dig(:params, :context) || "Concourse",
57
+ target_url: parse_target_url(input.dig(:params, :target_url)),
58
+ description: input.dig(:params, :description),
59
+ )
60
+
61
+ output = {
62
+ version: { ref: sha },
63
+ metadata: [
64
+ { name: "author", value: author },
65
+ { name: "message", value: message },
66
+ { name: "status", value: state },
67
+ { name: "commit", value: sha },
68
+ ]
69
+ }
70
+
71
+ $stdout = STDOUT
72
+
73
+ puts output.to_json
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "github_commit"
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "github_commit"
7
+ gem.version = GithubCommit::VERSION
8
+ gem.authors = ["GOV.UK Dev"]
9
+ gem.email = ["govuk-dev@digital.cabinet-office.gov.uk"]
10
+ gem.summary = "Gem for GitHub commits as a Concourse Resource Type"
11
+ gem.description = "Gem for GitHub commits as a Concourse Resource Type"
12
+ gem.homepage = "https://github.com/alphagov/github-commit"
13
+ gem.license = "OGL-UK-3.0"
14
+
15
+ gem.files = `git ls-files -z`.split("\x0")
16
+ gem.executables = `git ls-files -z -- bin/*`.split("\x0").map { |f| File.basename(f) }
17
+ gem.test_files = `git ls-files -z -- {spec}/*`.split("\x0")
18
+ gem.require_paths = %w[lib]
19
+
20
+ gem.add_runtime_dependency "octokit", "~> 4.2"
21
+
22
+ gem.add_development_dependency "brakeman", "~> 4.10"
23
+ gem.add_development_dependency "rspec", "~> 3.0"
24
+ gem.add_development_dependency "rubocop", "~> 0.87"
25
+ gem.add_development_dependency "rubocop-govuk", "~> 3.17"
26
+ gem.add_development_dependency "simplecov", "~> 0.20"
27
+ end
@@ -0,0 +1,8 @@
1
+ require_relative "./github_commit/commit_fetcher"
2
+ require_relative "./github_commit/commits_fetcher"
3
+ require_relative "./github_commit/commit_writer"
4
+ require_relative "./github_commit/status_updater"
5
+
6
+ module GithubCommit
7
+ VERSION = "0.1.0".freeze
8
+ end
@@ -0,0 +1,20 @@
1
+ module GithubCommit
2
+ class CommitFetcher
3
+ def initialize(repo:, client: Octokit::Client.new)
4
+ @client = client
5
+ @repo = repo
6
+ end
7
+
8
+ def fetch_commit(sha:)
9
+ client.commit(repo, sha)
10
+ end
11
+
12
+ def fetch_status(sha:)
13
+ client.status(repo, sha)
14
+ end
15
+
16
+ private
17
+
18
+ attr_reader :client, :repo
19
+ end
20
+ end
@@ -0,0 +1,34 @@
1
+ module GithubCommit
2
+ class CommitWriter
3
+ def initialize(commit:, status:, dir:)
4
+ @commit = commit
5
+ @status = status
6
+ @dir = dir
7
+ end
8
+
9
+ def write_commit!
10
+ File.open("#{dir}/ref", "w") { |f| f.write commit.sha }
11
+ File.open("#{dir}/state", "w") { |f| f.write status.state }
12
+ File.open("#{dir}/combined_status", "w") { |f| f.write status.to_h.to_json }
13
+ File.open("#{dir}/commit", "w") { |f| f.write commit.to_h.to_json }
14
+ File.open("#{dir}/author", "w") { |f| f.write commit.commit.author.name }
15
+ File.open("#{dir}/message", "w") { |f| f.write commit.commit.message }
16
+ end
17
+
18
+ def metadata
19
+ {
20
+ version: { ref: commit.sha },
21
+ metadata: [
22
+ { name: "author", value: commit.commit.author.name },
23
+ { name: "message", value: commit.commit.message },
24
+ { name: "status", value: status.state },
25
+ { name: "commit", value: commit.sha },
26
+ ],
27
+ }
28
+ end
29
+
30
+ private
31
+
32
+ attr_reader :commit, :status, :dir
33
+ end
34
+ end
@@ -0,0 +1,37 @@
1
+ module GithubCommit
2
+ class CommitsFetcher
3
+ def initialize(repo:, client: Octokit::Client.new)
4
+ @client = client
5
+ @repo = repo
6
+ end
7
+
8
+ def commits(sha: nil)
9
+ branch_commits = client.branches(repo).map do |branch|
10
+ client.commits(repo, { sha: branch.commit.sha, per_page: 15 })
11
+ .map do |c|
12
+ {
13
+ date: c.commit.committer.date,
14
+ sha: c.sha,
15
+ }
16
+ end
17
+ end
18
+
19
+ commits = branch_commits.flatten
20
+ .uniq { |c| c.dig(:sha) }
21
+ .sort { |a, b| b.dig(:date) <=> a.dig(:date) }
22
+ .map { |s| { ref: s.dig(:sha) } }
23
+
24
+ return [commits.first] unless sha
25
+
26
+ index = commits.index({ ref: sha })
27
+ index = 1 if index == commits.count - 1
28
+ output = commits[0..index]
29
+
30
+ output.reverse
31
+ end
32
+
33
+ private
34
+
35
+ attr_reader :client, :repo, :sha
36
+ end
37
+ end
@@ -0,0 +1,23 @@
1
+ module GithubCommit
2
+ class StatusUpdater
3
+ def initialize(repo:, sha:, client: Octokit::Client.new)
4
+ @client = client
5
+ @repo = repo
6
+ @sha = sha
7
+ end
8
+
9
+ def update_status(status:, context: nil, target_url: nil, description: nil)
10
+ options = {
11
+ context: context,
12
+ target_url: target_url,
13
+ description: description,
14
+ }.compact
15
+
16
+ client.create_status(repo, sha, status, options)
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :client, :repo, :sha
22
+ end
23
+ end
File without changes
File without changes
File without changes
@@ -0,0 +1,33 @@
1
+ require "github_commit/commit_fetcher"
2
+
3
+ describe GithubCommit::CommitFetcher do
4
+ it "can be initialized" do
5
+ expect { described_class.new(client: nil, repo: nil) }.not_to raise_error
6
+ end
7
+
8
+ context "with authorized client" do
9
+ subject(:fetcher) { described_class.new(client: client, repo: repo) }
10
+
11
+ let(:repo) { "alphagov/example" }
12
+ let(:sha) { "123" }
13
+ let(:client) { instance_double("Octokit::Client") }
14
+
15
+ describe "#fetch_commit" do
16
+ let(:commit) { { sha: sha } }
17
+
18
+ it "fetches a commit" do
19
+ expect(client).to receive(:commit).with(repo, sha).and_return(commit)
20
+ expect(fetcher.fetch_commit(sha: sha)).to eq(commit)
21
+ end
22
+ end
23
+
24
+ describe "#fetch_status" do
25
+ let(:status) { { sha: sha, state: "success" } }
26
+
27
+ it "fetches a status for a commit" do
28
+ expect(client).to receive(:status).with(repo, sha).and_return(status)
29
+ expect(fetcher.fetch_status(sha: sha)).to eq(status)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,74 @@
1
+ require "github_commit/commit_writer"
2
+ require "tmpdir"
3
+
4
+ describe GithubCommit::CommitWriter do
5
+ attr_reader :temp_dir
6
+
7
+ around do |example|
8
+ dir = Dir.mktmpdir
9
+ begin
10
+ @temp_dir = dir
11
+ example.run
12
+ ensure
13
+ FileUtils.remove_entry dir
14
+ end
15
+ end
16
+
17
+ # rubocop:disable RSpec/VerifiedDoubles
18
+ let(:status) do
19
+ double("Sawyer::Resource",
20
+ state: commit_state,
21
+ to_h: { state: commit_state })
22
+ end
23
+ let(:commit) do
24
+ double("Sawyer::Resource",
25
+ sha: sha,
26
+ to_h: { sha: sha },
27
+ commit: double("Sawyer::Resource",
28
+ author: double("Sawyer::Resource",
29
+ name: commit_author),
30
+ message: commit_msg))
31
+ end
32
+ # rubocop:enable RSpec/VerifiedDoubles
33
+ let(:commit_state) { "pending" }
34
+ let(:commit_msg) { "Add tests" }
35
+ let(:commit_author) { "Harry Potter" }
36
+ let(:sha) { "123" }
37
+
38
+ it "can be initialized" do
39
+ expect { described_class.new(commit: nil, status: nil, dir: nil) }.not_to raise_error
40
+ end
41
+
42
+ describe "#write_commit!" do
43
+ subject(:writer) { described_class.new(commit: commit, status: status, dir: dir) }
44
+
45
+ let(:dir) { temp_dir }
46
+
47
+ it "writes out files to the directory" do
48
+ expect(Dir.exist?(temp_dir)).to be true
49
+ writer.write_commit!
50
+ expect(File.read("#{dir}/ref")).to eq(sha)
51
+ expect(File.read("#{dir}/state")).to eq(commit_state)
52
+ expect(File.read("#{dir}/combined_status")).to eq("{\"state\":\"pending\"}")
53
+ expect(File.read("#{dir}/commit")).to eq("{\"sha\":\"123\"}")
54
+ expect(File.read("#{dir}/author")).to eq(commit_author)
55
+ expect(File.read("#{dir}/message")).to eq(commit_msg)
56
+ end
57
+ end
58
+
59
+ describe "#metadata" do
60
+ subject(:writer) { described_class.new(commit: commit, status: status, dir: "dir") }
61
+
62
+ it "returns a hash" do
63
+ expect(writer.metadata).to eq({
64
+ version: { ref: sha },
65
+ metadata: [
66
+ { name: "author", value: commit_author },
67
+ { name: "message", value: commit_msg },
68
+ { name: "status", value: commit_state },
69
+ { name: "commit", value: sha },
70
+ ],
71
+ })
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,75 @@
1
+ require "github_commit/commits_fetcher"
2
+
3
+ describe GithubCommit::CommitsFetcher do
4
+ it "can be initialized" do
5
+ expect { described_class.new(client: nil, repo: nil) }.not_to raise_error
6
+ end
7
+
8
+ context "with authorized client" do
9
+ let(:fetcher) { described_class.new(client: client, repo: repo) }
10
+
11
+ let(:repo) { "alphagov/example" }
12
+ let(:sha1) { "123" }
13
+ let(:sha2) { "456" }
14
+ let(:sha3) { "789" }
15
+
16
+ let(:client) { instance_double("Octokit::Client") }
17
+
18
+ describe "#commits" do
19
+ let(:commit1) do
20
+ # rubocop:disable RSpec/VerifiedDoubles
21
+ double("Sawyer::Resource",
22
+ sha: sha1,
23
+ commit: double("Sawyer::Resource",
24
+ committer: double("Sawyer::Resource", date: Time.parse("2018-12-25"))))
25
+ end
26
+ let(:commit2) do
27
+ double("Sawyer::Resource",
28
+ sha: sha2,
29
+ commit: double("Sawyer::Resource",
30
+ committer: double("Sawyer::Resource", date: Time.parse("2020-12-25"))))
31
+ end
32
+ let(:commit3) do
33
+ double("Sawyer::Resource",
34
+ sha: sha3,
35
+ commit: double("Sawyer::Resource",
36
+ committer: double("Sawyer::Resource", date: Time.parse("2019-12-25"))))
37
+ end
38
+ let(:branches) do
39
+ [
40
+ double("Sawyer::Resource",
41
+ commit: double("Sawyer::Resource",
42
+ sha: sha1)),
43
+ double("Sawyer::Resource",
44
+ commit: double("Sawyer::Resource",
45
+ sha: sha2)),
46
+ double("Sawyer::Resource",
47
+ commit: double("Sawyer::Resource",
48
+ sha: sha3)),
49
+ # rubocop:enable RSpec/VerifiedDoubles
50
+ ]
51
+ end
52
+
53
+ context "when a commit hash is provided" do
54
+ it "fetches the succeeding commits in reverse chronological order" do
55
+ expect_calls
56
+ expect(fetcher.commits(sha: sha1)).to eq([{ ref: sha3 }, { ref: sha2 }])
57
+ end
58
+ end
59
+
60
+ context "when a commit hash is not provided" do
61
+ it "returns the most recent commit" do
62
+ expect_calls
63
+ expect(fetcher.commits).to eq([{ ref: sha2 }])
64
+ end
65
+ end
66
+
67
+ def expect_calls
68
+ expect(client).to receive(:branches).with(repo).and_return(branches)
69
+ expect(client).to receive(:commits).with(repo, { sha: sha1, per_page: 15 }).and_return([commit1])
70
+ expect(client).to receive(:commits).with(repo, { sha: sha2, per_page: 15 }).and_return([commit2])
71
+ expect(client).to receive(:commits).with(repo, { sha: sha3, per_page: 15 }).and_return([commit3])
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,39 @@
1
+ require "github_commit/status_updater"
2
+
3
+ describe GithubCommit::StatusUpdater do
4
+ it "can be initialized" do
5
+ expect { described_class.new(client: nil, repo: nil, sha: nil) }.not_to raise_error
6
+ end
7
+
8
+ describe "#update_status" do
9
+ subject(:updater) { described_class.new(client: client, repo: repo, sha: sha) }
10
+
11
+ let(:client) { instance_double("Octokit::Client") }
12
+ let(:repo) { "alphagov/example" }
13
+ let(:sha) { "123" }
14
+ let(:title) { "Concourse" }
15
+ let(:target_url) { "https://example.org" }
16
+ let(:description) { "The Concourse pipeline is running..." }
17
+ let(:status) { "pending" }
18
+
19
+ it "updates the status for a commit" do
20
+ expect(client).to receive(:create_status).with(
21
+ repo,
22
+ sha,
23
+ status,
24
+ {
25
+ context: title,
26
+ target_url: target_url,
27
+ description: description,
28
+ },
29
+ )
30
+
31
+ updater.update_status(
32
+ status: status,
33
+ context: title,
34
+ target_url: target_url,
35
+ description: description,
36
+ )
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,104 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+
15
+ require "simplecov"
16
+ SimpleCov.start { add_filter "/spec/" }
17
+
18
+ $LOAD_PATH << File.expand_path("..", __dir__)
19
+ $LOAD_PATH << File.expand_path("../lib", __dir__)
20
+
21
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
22
+ RSpec.configure do |config|
23
+ # rspec-expectations config goes here. You can use an alternate
24
+ # assertion/expectation library such as wrong or the stdlib/minitest
25
+ # assertions if you prefer.
26
+ config.expect_with :rspec do |expectations|
27
+ # This option will default to `true` in RSpec 4. It makes the `description`
28
+ # and `failure_message` of custom matchers include text for helper methods
29
+ # defined using `chain`, e.g.:
30
+ # be_bigger_than(2).and_smaller_than(4).description
31
+ # # => "be bigger than 2 and smaller than 4"
32
+ # ...rather than:
33
+ # # => "be bigger than 2"
34
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
35
+ end
36
+
37
+ # rspec-mocks config goes here. You can use an alternate test double
38
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
39
+ config.mock_with :rspec do |mocks|
40
+ # Prevents you from mocking or stubbing a method that does not exist on
41
+ # a real object. This is generally recommended, and will default to
42
+ # `true` in RSpec 4.
43
+ mocks.verify_partial_doubles = true
44
+ end
45
+
46
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
47
+ # have no way to turn it off -- the option exists only for backwards
48
+ # compatibility in RSpec 3). It causes shared context metadata to be
49
+ # inherited by the metadata hash of host groups and examples, rather than
50
+ # triggering implicit auto-inclusion in groups with matching metadata.
51
+ config.shared_context_metadata_behavior = :apply_to_host_groups
52
+
53
+ # The settings below are suggested to provide a good initial experience
54
+ # with RSpec, but feel free to customize to your heart's content.
55
+ # # This allows you to limit a spec run to individual examples or groups
56
+ # # you care about by tagging them with `:focus` metadata. When nothing
57
+ # # is tagged with `:focus`, all examples get run. RSpec also provides
58
+ # # aliases for `it`, `describe`, and `context` that include `:focus`
59
+ # # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
60
+ # config.filter_run_when_matching :focus
61
+ #
62
+ # # Allows RSpec to persist some state between runs in order to support
63
+ # # the `--only-failures` and `--next-failure` CLI options. We recommend
64
+ # # you configure your source control system to ignore this file.
65
+ # config.example_status_persistence_file_path = "spec/examples.txt"
66
+ #
67
+ # # Limits the available syntax to the non-monkey patched syntax that is
68
+ # # recommended. For more details, see:
69
+ # # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
70
+ # # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
71
+ # # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
72
+ # config.disable_monkey_patching!
73
+ #
74
+ # # This setting enables warnings. It's recommended, but in some cases may
75
+ # # be too noisy due to issues in dependencies.
76
+ # config.warnings = true
77
+ #
78
+ # # Many RSpec users commonly either run the entire suite or an individual
79
+ # # file, and it's useful to allow more verbose output when running an
80
+ # # individual spec file.
81
+ # if config.files_to_run.one?
82
+ # # Use the documentation formatter for detailed output,
83
+ # # unless a formatter has already been configured
84
+ # # (e.g. via a command-line flag).
85
+ # config.default_formatter = "doc"
86
+ # end
87
+ #
88
+ # # Print the 10 slowest examples and example groups at the
89
+ # # end of the spec run, to help surface which specs are running
90
+ # # particularly slow.
91
+ # config.profile_examples = 10
92
+ #
93
+ # # Run specs in random order to surface order dependencies. If you find an
94
+ # # order dependency and want to debug it, you can fix the order by providing
95
+ # # the seed, which is printed after each run.
96
+ # # --seed 1234
97
+ # config.order = :random
98
+ #
99
+ # # Seed global randomization in this process using the `--seed` CLI option.
100
+ # # Setting this allows you to use `--seed` to deterministically reproduce
101
+ # # test failures related to randomization by passing the same `--seed` value
102
+ # # as the one that triggered the failure.
103
+ # Kernel.srand config.seed
104
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: github_commit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - GOV.UK Dev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-12-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: octokit
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: brakeman
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.87'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.87'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-govuk
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.17'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.17'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.20'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.20'
97
+ description: Gem for GitHub commits as a Concourse Resource Type
98
+ email:
99
+ - govuk-dev@digital.cabinet-office.gov.uk
100
+ executables:
101
+ - check
102
+ - in
103
+ - out
104
+ extensions: []
105
+ extra_rdoc_files: []
106
+ files:
107
+ - ".github/workflows/ci.yml"
108
+ - ".gitignore"
109
+ - ".rspec"
110
+ - ".rubocop.yml"
111
+ - ".ruby-version"
112
+ - CHANGELOG.md
113
+ - Dockerfile
114
+ - Gemfile
115
+ - Gemfile.lock
116
+ - README.md
117
+ - bin/check
118
+ - bin/in
119
+ - bin/out
120
+ - github_commit.gemspec
121
+ - lib/github_commit.rb
122
+ - lib/github_commit/commit_fetcher.rb
123
+ - lib/github_commit/commit_writer.rb
124
+ - lib/github_commit/commits_fetcher.rb
125
+ - lib/github_commit/status_updater.rb
126
+ - spec/bin/check_spec.rb
127
+ - spec/bin/in_spec.rb
128
+ - spec/bin/out_spec.rb
129
+ - spec/lib/github_commit/commit_fetcher_spec.rb
130
+ - spec/lib/github_commit/commit_writer_spec.rb
131
+ - spec/lib/github_commit/commits_fetcher_spec.rb
132
+ - spec/lib/github_commit/status_updater_spec.rb
133
+ - spec/spec_helper.rb
134
+ homepage: https://github.com/alphagov/github-commit
135
+ licenses:
136
+ - OGL-UK-3.0
137
+ metadata: {}
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubygems_version: 3.1.4
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Gem for GitHub commits as a Concourse Resource Type
157
+ test_files: []