diff --git a/README.md b/README.md index b27b1c9..34a55fd 100644 --- a/README.md +++ b/README.md @@ -64,10 +64,10 @@ the commit history. Believe it or not, there are a few more environment variables you can set! These determine the text used by repository-sync when creating commit messages and pull requests. They are also dependent on the name of the destination repository. All of these values are optional. -For the examples below, we'll assuming repository-sync is committing into a repository called `gjtorikian/this-test`. Because environment variables cannot use the `/` or `-` characters, you must substitute it within the destination repository name as `_`: `gjtorikian_this_test`. The default string is `'Sync changes from upstream repository'`. +For the examples below, we'll assuming repository-sync is committing into a repository called `gjtorikian/this-test`. Because environment variables cannot use the `/` or `-` characters, you must substitute those characters in the repository name as `_`. The repository name should also be capitalized. Using our example repository, that would mean a prefix of `GJTORIKIAN_THIS_TEST`. -* `#{safe_destination_repo}_COMMIT_MESSAGE`: This determines the commit message to use when committing into your public repository. Example: `gjtorikian_this_test_COMMIT_MESSAGE`. +* `#{safe_destination_repo}_COMMIT_MESSAGE`: This determines the commit message to use when committing into your public repository. Example: `GJTORIKIAN_THIS_TEST_COMMIT_MESSAGE`. -* `#{safe_destination_repo}_PR_TITLE`: This determines the title of the PR that's opened into either repository. Example: `gjtorikian_this_test_PR_TITLE`. The default string is `'Sync changes from upstream repository'`. +* `#{safe_destination_repo}_PR_TITLE`: This determines the title of the PR that's opened into either repository. Example: `GJTORIKIAN_THIS_TEST_PR_TITLE`. The default string is `'Sync changes from upstream repository'`. -* `#{safe_destination_repo}_PR_BODY`: This determines the body text of the PR that's opened into either repository. Example: `gjtorikian_this_test_PR_BODY`. The default string is a listing of the added, modified, and removed files in the PR. +* `#{safe_destination_repo}_PR_BODY`: This determines the body text of the PR that's opened into either repository. Example: `GJTORIKIAN_THIS_TEST_PR_BODY`. The default string is a listing of the added, modified, and removed files in the PR. diff --git a/lib/cloner.rb b/lib/cloner.rb index 8b29694..2ce0276 100644 --- a/lib/cloner.rb +++ b/lib/cloner.rb @@ -57,11 +57,11 @@ def branch_name end def safe_destination_repo - @safe_destination_repo ||= destination_repo.tr('/-', '_') + @safe_destination_repo ||= destination_repo.tr('/-', '_').upcase end def commit_message - @commit_message ||= ENV["#{safe_destination_repo.upcase}_COMMIT_MESSAGE"] || 'Sync changes from upstream repository' + @commit_message ||= ENV["#{safe_destination_repo}_COMMIT_MESSAGE"] || 'Sync changes from upstream repository' end def files @@ -73,11 +73,15 @@ def clone_url_with_token end def pull_request_title - ENV["#{safe_destination_repo.upcase}_PR_TITLE"] || 'Sync changes from upstream repository' + if files.count == 1 + "#{files.first["status"].capitalize} #{files.first["filename"]}" + else + ENV["#{safe_destination_repo}_PR_TITLE"] || 'Sync changes from upstream repository' + end end def pull_request_body - return ENV["#{safe_destination_repo.upcase}_PR_BODY"] if ENV["#{safe_destination_repo.upcase}_PR_BODY"] + return ENV["#{safe_destination_repo}_PR_BODY"] if ENV["#{safe_destination_repo}_PR_BODY"] body = "" ["added", "removed", "unchanged"].each do |type| filenames = files.select { |f| f['status'] == type }.map { |f| f['filename'] } diff --git a/spec/cloner_spec.rb b/spec/cloner_spec.rb index 79425b7..ab1ebf9 100644 --- a/spec/cloner_spec.rb +++ b/spec/cloner_spec.rb @@ -35,7 +35,7 @@ end it "creates the safe repo name" do - expect(cloner.safe_destination_repo).to eql("gjtorikian_destination_repo") + expect(cloner.safe_destination_repo).to eql("GJTORIKIAN_DESTINATION_REPO") end it "defaults to the default commit message" do @@ -61,15 +61,31 @@ end it "defaults to the default pull request title" do + url = "https://api.github.com/repos/gjtorikian/destination_repo/compare/master...#{cloner.branch_name}" + stub_request(:get, url). + to_return(:status => 200, :body => fixture("compare_some_files.json"), :headers => { 'Content-Type' => 'application/json' }) + expect(cloner.pull_request_title).to eql("Sync changes from upstream repository") end it "respects user supplied pull-request titles" do + url = "https://api.github.com/repos/gjtorikian/destination_repo/compare/master...#{cloner.branch_name}" + stub_request(:get, url). + to_return(:status => 200, :body => fixture("compare_some_files.json"), :headers => { 'Content-Type' => 'application/json' }) + with_env "GJTORIKIAN_DESTINATION_REPO_PR_TITLE", "Some title" do expect(cloner.pull_request_title).to eql("Some title") end end + it "uses descriptive pull request titles when only one files has changed" do + url = "https://api.github.com/repos/gjtorikian/destination_repo/compare/master...#{cloner.branch_name}" + stub_request(:get, url). + to_return(:status => 200, :body => fixture("compare_single_file.json"), :headers => { 'Content-Type' => 'application/json' }) + + expect(cloner.pull_request_title).to eql("Added file1.txt") + end + it "geneates the pull request body" do url = "https://api.github.com/repos/gjtorikian/destination_repo/compare/master...#{cloner.branch_name}" stub_request(:get, url). diff --git a/spec/fixtures/compare_single_file.json b/spec/fixtures/compare_single_file.json new file mode 100644 index 0000000..9edd5c3 --- /dev/null +++ b/spec/fixtures/compare_single_file.json @@ -0,0 +1,234 @@ +{ + "url": "https://api.github.com/repos/octocat/Hello-World/compare/master...topic", + "html_url": "https://github.com/octocat/Hello-World/compare/master...topic", + "permalink_url": "https://github.com/octocat/Hello-World/compare/octocat:bbcd538c8e72b8c175046e27cc8f907076331401...octocat:0328041d1152db8ae77652d1618a02e57f745f17", + "diff_url": "https://github.com/octocat/Hello-World/compare/master...topic.diff", + "patch_url": "https://github.com/octocat/Hello-World/compare/master...topic.patch", + "base_commit": { + "url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", + "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", + "html_url": "https://github.com/octocat/Hello-World/commit/6dcb09b5b57875f334f61aebed695e2e4193db5e", + "comments_url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e/comments", + "commit": { + "url": "https://api.github.com/repos/octocat/Hello-World/git/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", + "author": { + "name": "Monalisa Octocat", + "email": "support@github.com", + "date": "2011-04-14T16:00:49Z" + }, + "committer": { + "name": "Monalisa Octocat", + "email": "support@github.com", + "date": "2011-04-14T16:00:49Z" + }, + "message": "Fix all the bugs", + "tree": { + "url": "https://api.github.com/repos/octocat/Hello-World/tree/6dcb09b5b57875f334f61aebed695e2e4193db5e", + "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e" + }, + "comment_count": 0 + }, + "author": { + "login": "octocat", + "id": 1, + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "committer": { + "login": "octocat", + "id": 1, + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "parents": [ + { + "url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", + "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e" + } + ] + }, + "merge_base_commit": { + "url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", + "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", + "html_url": "https://github.com/octocat/Hello-World/commit/6dcb09b5b57875f334f61aebed695e2e4193db5e", + "comments_url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e/comments", + "commit": { + "url": "https://api.github.com/repos/octocat/Hello-World/git/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", + "author": { + "name": "Monalisa Octocat", + "email": "support@github.com", + "date": "2011-04-14T16:00:49Z" + }, + "committer": { + "name": "Monalisa Octocat", + "email": "support@github.com", + "date": "2011-04-14T16:00:49Z" + }, + "message": "Fix all the bugs", + "tree": { + "url": "https://api.github.com/repos/octocat/Hello-World/tree/6dcb09b5b57875f334f61aebed695e2e4193db5e", + "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e" + }, + "comment_count": 0 + }, + "author": { + "login": "octocat", + "id": 1, + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "committer": { + "login": "octocat", + "id": 1, + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "parents": [ + { + "url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", + "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e" + } + ] + }, + "status": "behind", + "ahead_by": 1, + "behind_by": 2, + "total_commits": 1, + "commits": [ + { + "url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", + "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", + "html_url": "https://github.com/octocat/Hello-World/commit/6dcb09b5b57875f334f61aebed695e2e4193db5e", + "comments_url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e/comments", + "commit": { + "url": "https://api.github.com/repos/octocat/Hello-World/git/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", + "author": { + "name": "Monalisa Octocat", + "email": "support@github.com", + "date": "2011-04-14T16:00:49Z" + }, + "committer": { + "name": "Monalisa Octocat", + "email": "support@github.com", + "date": "2011-04-14T16:00:49Z" + }, + "message": "Fix all the bugs", + "tree": { + "url": "https://api.github.com/repos/octocat/Hello-World/tree/6dcb09b5b57875f334f61aebed695e2e4193db5e", + "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e" + }, + "comment_count": 0 + }, + "author": { + "login": "octocat", + "id": 1, + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "committer": { + "login": "octocat", + "id": 1, + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "parents": [ + { + "url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", + "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e" + } + ] + } + ], + "files": [ + { + "sha": "bbcd538c8e72b8c175046e27cc8f907076331401", + "filename": "file1.txt", + "status": "added", + "additions": 103, + "deletions": 21, + "changes": 124, + "blob_url": "https://github.com/octocat/Hello-World/blob/6dcb09b5b57875f334f61aebed695e2e4193db5e/file1.txt", + "raw_url": "https://github.com/octocat/Hello-World/raw/6dcb09b5b57875f334f61aebed695e2e4193db5e/file1.txt", + "contents_url": "https://api.github.com/repos/octocat/Hello-World/contents/file1.txt?ref=6dcb09b5b57875f334f61aebed695e2e4193db5e", + "patch": "@@ -132,7 +132,7 @@ module Test @@ -1000,7 +1000,7 @@ module Test" + } + ] +}