Created
December 19, 2018 10:01
-
-
Save cyrilchampier/09f85618a80d43d797c8e1d3937c55eb to your computer and use it in GitHub Desktop.
to be used in rpellerin PR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # frozen_string_literal: true | |
| # TODO: clean usage of env vars: | |
| # Heroku should fill our variables, and we should have no references to such variable in our code: | |
| # HEROKU_TEST_RUN_COMMIT_VERSION / HEROKU_TEST_RUN_COMMIT_VERSION / HEROKU_TEST_RUN_ID | |
| module TestUtils::Helpers::GitUtils | |
| GITHUB_API = 'https://api.github.com' | |
| GITHUB_DOCTOLIB_REPOSITORY_PATH = '/repos/doctolib/doctolib' | |
| # TODO: nice error if git command not found | |
| def commit_sha1 | |
| # The following constants are for local testing. | |
| # When run on Heroku, HASH is set to HEROKU_TEST_RUN_COMMIT_VERSION in test.sh | |
| @_commit_sha1 ||= ENV['HASH'].presence || execute('git rev-parse HEAD') | |
| end | |
| def ref_name | |
| @_ref_name ||= ENV['HEROKU_TEST_RUN_BRANCH'].presence || execute('git rev-parse --abbrev-ref HEAD') | |
| end | |
| # Returns a hash with filename as key and patch content as value. | |
| def pull_request_diff | |
| @_pull_request_diff ||= | |
| pull_request_json['files'].map { |file_diff| [file_diff['filename'], file_diff['patch']] }.to_h | |
| end | |
| private | |
| # TODO: this is not a "pull_request", it's a diff with current master. | |
| # We should not compare with master, but with branch root. | |
| # Check https://www.git-scm.com/docs/git-rev-parse/1.7.5 | |
| def pull_request_json | |
| comparison_path = '/compare/master...' | |
| url = [GITHUB_API, GITHUB_DOCTOLIB_REPOSITORY_PATH, comparison_path, commit_sha1].join | |
| headers = { | |
| Accept: 'application/json', | |
| Authorization: "token #{github_token}", | |
| 'User-Agent': 'HTTParty' | |
| } | |
| HTTParty.get(url, { headers: headers }) | |
| end | |
| # TODO: nice error if this env var is not defined | |
| def github_token | |
| ENV['GITHUB_ACCESS_TOKEN'] | |
| end | |
| def execute(command) | |
| %x(#{command} 2>/dev/null || true).strip | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment