<commit_before>Feature: hub fork
  Background:
    Given I am in "dotfiles" git repo
    And the "origin" remote has url "git://github.com/evilchelu/dotfiles.git"
    And I am "mislav" on github.com with OAuth token "<PASSWORD>"

  Scenario: Fork the repository
    Given the GitHub API server:
      """
      before {
        halt 400 unless request.env['HTTP_X_ORIGINAL_SCHEME'] == 'https'
        halt 401 unless request.env['HTTP_AUTHORIZATION'] == 'token OTOKEN'
      }
      get('/repos/mislav/dotfiles', :host_name => 'api.github.com') { 404 }
      post('/repos/evilchelu/dotfiles/forks', :host_name => 'api.github.com') {
        assert :organization => nil
        status 202
        json :name => 'dotfiles', :owner => { :login => 'mislav' }
      }
      """
    When I successfully run `hub fork`
    Then the output should contain exactly "new remote: mislav\n"
    And "git remote add -f mislav git://github.com/evilchelu/dotfiles.git" should be run
    And "git remote set-url mislav <EMAIL>:mislav/dotfiles.git" should be run
    And the url for "mislav" should be "<EMAIL>:mislav/dotfiles.git"

  Scenario: Fork the repository with new remote name specified
    Given the GitHub API server:
      """
      get('/repos/mislav/dotfiles') { 404 }
      post('/repos/evilchelu/dotfiles/forks') {
        assert :organization => nil
        status 202
        json :name => 'dotfiles', :owner => { :login => 'mislav' }
      }
      """
    And I successfully run `git remote rename origin upstream`
    When I successfully run `hub fork --remote-name=origin`
    Then the output should contain exactly "new remote: origin\n"
    And "git remote add -f origin git://github.com/evilchelu/dotfiles.git" should be run
    And "git remote set-url origin <EMAIL>:mislav/dotfiles.git" should be run
    And the url for "origin" should be "<EMAIL>:mislav/dotfiles.git"

  Scenario: Fork the repository with redirect
    Given the GitHub API server:
      """
      before {
        halt 400 unless request.env['HTTP_X_ORIGINAL_SCHEME'] == 'https'
        halt 401 unless request.env['HTTP_AUTHORIZATION'] == 'token OTOKEN'
      }
      get('/repos/mislav/dotfiles', :host_name => 'api.github.com') { 404 }
      post('/repos/evilchelu/dotfiles/forks', :host_name => 'api.github.com') {
        redirect 'https://api.github.com/repositories/1234/forks', 307
      }
      post('/repositories/1234/forks', :host_name => 'api.github.com') {
        status 202
        json :name => 'my-dotfiles', :owner => { :login => 'MiSlAv' }
      }
      """
    When I successfully run `hub fork`
    Then the output should contain exactly "new remote: mislav\n"
    And "git remote add -f mislav git://github.com/evilchelu/dotfiles.git" should be run
    And "git remote set-url mislav git@github.com:MiSlAv/my-dotfiles.git" should be run
    And the url for "mislav" should be "git@github.<EMAIL>:MiSlAv/my-dotfiles.git"

  Scenario: Fork the repository when origin URL is private
    Given the "origin" remote has url "<EMAIL>:evilchelu/dotfiles.git"
    Given the GitHub API server:
      """
      before { halt 401 unless request.env['HTTP_AUTHORIZATION'] == 'token OTOKEN' }
      get('/repos/mislav/dotfiles', :host_name => 'api.github.com') { 404 }
      post('/repos/evilchelu/dotfiles/forks', :host_name => 'api.github.com') {
        status 202
        json :name => 'dotfiles', :owner => { :login => 'mislav' }
      }
      """
    When I successfully run `hub fork`
    Then the output should contain exactly "new remote: mislav\n"
    And "git remote add -f mislav ssh://git@github.com/evilchelu/dotfiles.git" should be run
    And "git remote set-url mislav <EMAIL>:mislav/dotfiles.git" should be run
    And the url for "mislav" should be "<EMAIL>:mislav/dotfiles.git"

  Scenario: --no-remote
    Given the GitHub API server:
      """
      post('/repos/evilchelu/dotfiles/forks') {
        status 202
        json :name => 'dotfiles', :owner => { :login => 'mislav' }
      }
      """
    When I successfully run `hub fork --no-remote`
    Then there should be no output
    And there should be no "mislav" remote

  Scenario: Fork failed
    Given the GitHub API server:
      """
      post('/repos/evilchelu/dotfiles/forks') { halt 500 }
      """
    When I run `hub fork`
    Then the exit status should be 1
    And the stderr should contain exactly:
      """
      Error creating fork: Internal Server Error (HTTP 500)\n
      """
    And there should be no "mislav" remote

  Scenario: Unrelated fork already exists
    Given the GitHub API server:
      """
      get('/repos/mislav/dotfiles') {
        halt 406 unless request.env['HTTP_ACCEPT'] == 'application/vnd.github.v3+json;charset=utf-8'
        json :parent => { :html_url => 'https://github.com/unrelated/dotfiles' }
      }
      """
    When I run `hub fork`
    Then the exit status should be 1
    And the stderr should contain exactly:
      """
      Error creating fork: mislav/dotfiles already exists on github.com\n
      """
    And there should be no "mislav" remote

  Scenario: Related fork already exists
    Given the GitHub API server:
      """
      get('/repos/mislav/dotfiles') {
        json :parent => { :html_url => 'https://github.com/EvilChelu/Dotfiles' }
      }
      """
    When I run `hub fork`
    Then the exit status should be 0
    And the url for "mislav" should be "<EMAIL>:mislav/dotfiles.git"


  Scenario: Unrelated remote already exists
    Given the "mislav" remote has url "<EMAIL>:mislav/unrelated.git"
    Given the GitHub API server:
      """
      get('/repos/mislav/dotfiles', :host_name => 'api.github.com') { 404 }
      post('/repos/evilchelu/dotfiles/forks', :host_name => 'api.github.com') {
        assert :organization => nil
        status 202
        json :name => 'dotfiles', :owner => { :login => 'mislav' }
      }
      """
    When I run `hub fork`
    Then the exit status should be 128
    And the stderr should contain exactly:
      """
      fatal: remote mislav already exists.\n
      """
    And the url for "mislav" should be "<EMAIL>:mislav/unrelated.git"

  Scenario: Related fork and related remote already exist
    Given the "mislav" remote has url "<EMAIL>:mislav/dotfiles.git"
    Given the GitHub API server:
      """
      get('/repos/mislav/dotfiles') {
        json :parent => { :html_url => 'https://github.com/EvilChelu/Dotfiles' }
      }
      """
    When I run `hub fork`
    Then the exit status should be 0
    And the stdout should contain exactly:
      """
      existing remote: mislav\n
      """
    And the url for "mislav" should be "<EMAIL>:mislav/dotfiles.git"

  Scenario: Related fork and related remote, but with differing protocol, already exist
    Given the "mislav" remote has url "https://github.com/mislav/dotfiles.git"
    Given the GitHub API server:
      """
	  get('/repos/mislav/dotfiles') {
	    json :parent => { :html_url => 'https://github.com/EvilChelu/Dotfiles' }
	  }
      """
    When I run `hub fork`
    Then the exit status should be 0

  Scenario: Invalid OAuth token<commit_msg>Assert exact stdout for different `hub fork` scenarios<commit_after>Feature: hub fork
  Background:
    Given I am in "dotfiles" git repo
    And the "origin" remote has url "git://github.com/evilchelu/dotfiles.git"
    And I am "mislav" on github.com with OAuth token "<PASSWORD>"

  Scenario: Fork the repository
    Given the GitHub API server:
      """
      before {
        halt 400 unless request.env['HTTP_X_ORIGINAL_SCHEME'] == 'https'
        halt 401 unless request.env['HTTP_AUTHORIZATION'] == 'token OTOKEN'
      }
      get('/repos/mislav/dotfiles', :host_name => 'api.github.com') { 404 }
      post('/repos/evilchelu/dotfiles/forks', :host_name => 'api.github.com') {
        assert :organization => nil
        status 202
        json :name => 'dotfiles', :owner => { :login => 'mislav' }
      }
      """
    When I successfully run `hub fork`
    Then the output should contain exactly "new remote: mislav\n"
    And "git remote add -f mislav git://github.com/evilchelu/dotfiles.git" should be run
    And "git remote set-url mislav git@github.com:mislav/dotfiles.git" should be run
    And the url for "mislav" should be "git@github.com:mislav/dotfiles.git"

  Scenario: Fork the repository with new remote name specified
    Given the GitHub API server:
      """
      get('/repos/mislav/dotfiles') { 404 }
      post('/repos/evilchelu/dotfiles/forks') {
        assert :organization => nil
        status 202
        json :name => 'dotfiles', :owner => { :login => 'mislav' }
      }
      """
    And I successfully run `git remote rename origin upstream`
    When I successfully run `hub fork --remote-name=origin`
    Then the output should contain exactly "new remote: origin\n"
    And "git remote add -f origin git://github.com/evilchelu/dotfiles.git" should be run
    And "git remote set-url origin <EMAIL>:mislav/dotfiles.git" should be run
    And the url for "origin" should be "<EMAIL>:mislav/dotfiles.git"

  Scenario: Fork the repository with redirect
    Given the GitHub API server:
      """
      before {
        halt 400 unless request.env['HTTP_X_ORIGINAL_SCHEME'] == 'https'
        halt 401 unless request.env['HTTP_AUTHORIZATION'] == 'token OTOKEN'
      }
      get('/repos/mislav/dotfiles', :host_name => 'api.github.com') { 404 }
      post('/repos/evilchelu/dotfiles/forks', :host_name => 'api.github.com') {
        redirect 'https://api.github.com/repositories/1234/forks', 307
      }
      post('/repositories/1234/forks', :host_name => 'api.github.com') {
        status 202
        json :name => 'my-dotfiles', :owner => { :login => 'MiSlAv' }
      }
      """
    When I successfully run `hub fork`
    Then the output should contain exactly "new remote: mislav\n"
    And "git remote add -f mislav git://github.com/evilchelu/dotfiles.git" should be run
    And "git remote set-url mislav git@github.com:MiSlAv/my-dotfiles.git" should be run
    And the url for "mislav" should be "git@github.com:MiSlAv/my-dotfiles.git"

  Scenario: Fork the repository when origin URL is private
    Given the "origin" remote has url "<EMAIL>:evilchelu/dotfiles.git"
    Given the GitHub API server:
      """
      before { halt 401 unless request.env['HTTP_AUTHORIZATION'] == 'token OTOKEN' }
      get('/repos/mislav/dotfiles', :host_name => 'api.github.com') { 404 }
      post('/repos/evilchelu/dotfiles/forks', :host_name => 'api.github.com') {
        status 202
        json :name => 'dotfiles', :owner => { :login => 'mislav' }
      }
      """
    When I successfully run `hub fork`
    Then the output should contain exactly "new remote: mislav\n"
    And "git remote add -f mislav ssh://git@github.com/evilchelu/dotfiles.git" should be run
    And "git remote set-url mislav <EMAIL>:mislav/dotfiles.git" should be run
    And the url for "mislav" should be "git<EMAIL>:mislav/dotfiles.git"

  Scenario: --no-remote
    Given the GitHub API server:
      """
      post('/repos/evilchelu/dotfiles/forks') {
        status 202
        json :name => 'dotfiles', :owner => { :login => 'mislav' }
      }
      """
    When I successfully run `hub fork --no-remote`
    Then there should be no output
    And there should be no "mislav" remote

  Scenario: Fork failed
    Given the GitHub API server:
      """
      post('/repos/evilchelu/dotfiles/forks') { halt 500 }
      """
    When I run `hub fork`
    Then the exit status should be 1
    And the stderr should contain exactly:
      """
      Error creating fork: Internal Server Error (HTTP 500)\n
      """
    And there should be no "mislav" remote

  Scenario: Unrelated fork already exists
    Given the GitHub API server:
      """
      get('/repos/mislav/dotfiles') {
        halt 406 unless request.env['HTTP_ACCEPT'] == 'application/vnd.github.v3+json;charset=utf-8'
        json :parent => { :html_url => 'https://github.com/unrelated/dotfiles' }
      }
      """
    When I run `hub fork`
    Then the exit status should be 1
    And the stderr should contain exactly:
      """
      Error creating fork: mislav/dotfiles already exists on github.com\n
      """
    And there should be no "mislav" remote

  Scenario: Related fork already exists
    Given the GitHub API server:
      """
      get('/repos/mislav/dotfiles') {
        json :parent => { :html_url => 'https://github.com/EvilChelu/Dotfiles' }
      }
      """
    When I run `hub fork`
    Then the exit status should be 0
    Then the stdout should contain exactly:
      """
      new remote: mislav\n
      """
    And the url for "mislav" should be "<EMAIL>:mislav/dotfiles.git"


  Scenario: Unrelated remote already exists
    Given the "mislav" remote has url "<EMAIL>:mislav/unrelated.git"
    Given the GitHub API server:
      """
      get('/repos/mislav/dotfiles', :host_name => 'api.github.com') { 404 }
      post('/repos/evilchelu/dotfiles/forks', :host_name => 'api.github.com') {
        assert :organization => nil
        status 202
        json :name => 'dotfiles', :owner => { :login => 'mislav' }
      }
      """
    When I run `hub fork`
    Then the exit status should be 128
    And the stderr should contain exactly:
      """
      fatal: remote mislav already exists.\n
      """
    And the url for "mislav" should be "<EMAIL>:mislav/unrelated.git"

  Scenario: Related fork and related remote already exist
    Given the "mislav" remote has url "<EMAIL>:mislav/dotfiles.git"
    Given the GitHub API server:
      """
      get('/repos/mislav/dotfiles') {
        json :parent => { :html_url => 'https://github.com/EvilChelu/Dotfiles' }
      }
      """
    When I run `hub fork`
    Then the exit status should be 0
    And the stdout should contain exactly:
      """
      existing remote: mislav\n
      """
    And the url for "mislav" should be "<EMAIL>:mislav/dotfiles.git"

  Scenario: Related fork and related remote, but with differing protocol, already exist
    Given the "mislav" remote has url "https://github.com/mislav/dotfiles.git"
    Given the GitHub API server:
      """
      get('/repos/mislav/dotfiles') {
        json :parent => { :html_url => 'https://github.com/EvilChelu/Dotfiles' }
      }
      """
    When I run `hub fork`
    Then the exit status should be 0
    And the stdout should contain exactly:
      """
      existing remote: mislav\n
      """
    And the url for "mislav" should be "https://github.com/mislav/dotfiles.git"

  Scenario: Invalid OAuth token
{'fraction_non_alphanumeric': 0.12192771084337349, 'fraction_numerical': 0.010740103270223752, 'mean_word_length': 2.680263491259184, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 104, 'https://': 13, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 7, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5733132', 'n_tokens_mistral': 4781, 'n_tokens_neox': 4479, 'n_words': 1418}