repo_commit_section.vue 5.5 KB
Newer Older
J
Jacob Schatz 已提交
1
<script>
P
Phil Hughes 已提交
2
import Flash from '../../flash';
3 4
import Store from '../stores/repo_store';
import RepoMixin from '../mixins/repo_mixin';
J
Jacob Schatz 已提交
5
import Service from '../services/repo_service';
J
Jacob Schatz 已提交
6 7
import PopupDialog from '../../vue_shared/components/popup_dialog.vue';
import { visitUrl } from '../../lib/utils/url_utility';
J
Jacob Schatz 已提交
8

B
Bryce Johnson 已提交
9
export default {
J
Jacob Schatz 已提交
10 11
  mixins: [RepoMixin],

12 13 14
  data() {
    return Store;
  },
15

J
Jacob Schatz 已提交
16 17 18
  components: {
    PopupDialog,
  },
L
Luke "Jared" Bennett 已提交
19

20
  computed: {
J
Jacob Schatz 已提交
21 22 23 24
    showCommitable() {
      return this.isCommitable && this.changedFiles.length;
    },

25
    branchPaths() {
J
Jacob Schatz 已提交
26
      return this.changedFiles.map(f => f.path);
J
Jacob Schatz 已提交
27 28
    },

J
Jacob Schatz 已提交
29
    cantCommitYet() {
J
Jacob Schatz 已提交
30
      return !this.commitMessage || this.submitCommitsLoading;
J
Jacob Schatz 已提交
31 32
    },

J
Jacob Schatz 已提交
33
    filePluralize() {
L
Luke "Jared" Bennett 已提交
34
      return this.changedFiles.length > 1 ? 'files' : 'file';
J
Jacob Schatz 已提交
35
    },
36
  },
37

J
Jacob Schatz 已提交
38
  methods: {
J
Jacob Schatz 已提交
39 40 41 42 43 44 45 46 47 48
    commitToNewBranch(status) {
      if (status) {
        this.showNewBranchDialog = false;
        this.tryCommit(null, true, true);
      } else {
        // reset the state
      }
    },

    makeCommit(newBranch) {
J
Jacob Schatz 已提交
49 50
      // see https://docs.gitlab.com/ce/api/commits.html#create-a-commit-with-multiple-files-and-actions
      const commitMessage = this.commitMessage;
L
Luke "Jared" Bennett 已提交
51
      const actions = this.changedFiles.map(f => ({
52
        action: f.tempFile ? 'create' : 'update',
J
Jacob Schatz 已提交
53
        file_path: f.path,
L
Luke "Jared" Bennett 已提交
54 55
        content: f.newContent,
      }));
J
Jacob Schatz 已提交
56
      const branch = newBranch ? `${this.currentBranch}-${this.currentShortHash}` : this.currentBranch;
57
      const payload = {
J
Jacob Schatz 已提交
58
        branch,
59
        commit_message: commitMessage,
L
Luke "Jared" Bennett 已提交
60 61
        actions,
      };
J
Jacob Schatz 已提交
62 63 64 65
      if (newBranch) {
        payload.start_branch = this.currentBranch;
      }
      this.submitCommitsLoading = true;
J
Jacob Schatz 已提交
66
      Service.commitFiles(payload)
J
Jacob Schatz 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
        .then(() => {
          this.resetCommitState();
          if (this.startNewMR) {
            this.redirectToNewMr(branch);
          } else {
            this.redirectToBranch(branch);
          }
        })
        .catch(() => {
          Flash('An error occurred while committing your changes');
        });
    },

    tryCommit(e, skipBranchCheck = false, newBranch = false) {
      if (skipBranchCheck) {
        this.makeCommit(newBranch);
      } else {
        Store.setBranchHash()
          .then(() => {
            if (Store.branchChanged) {
              Store.showNewBranchDialog = true;
              return;
            }
            this.makeCommit(newBranch);
          })
          .catch(() => {
            Flash('An error occurred while committing your changes');
          });
      }
    },

    redirectToNewMr(branch) {
      visitUrl(this.newMrTemplateUrl.replace('{{source_branch}}', branch));
    },

    redirectToBranch(branch) {
      visitUrl(this.customBranchURL.replace('{{branch}}', branch));
104 105
    },

106
    resetCommitState() {
107
      this.submitCommitsLoading = false;
J
Jacob Schatz 已提交
108 109 110 111 112
      this.openedFiles = this.openedFiles.map((file) => {
        const f = file;
        f.changed = false;
        return f;
      });
113 114 115
      this.changedFiles = [];
      this.commitMessage = '';
      this.editMode = false;
J
Jacob Schatz 已提交
116
      window.scrollTo(0, 0);
L
Luke "Jared" Bennett 已提交
117
    },
J
Jacob Schatz 已提交
118
  },
L
Luke "Jared" Bennett 已提交
119
};
J
Jacob Schatz 已提交
120 121 122
</script>

<template>
J
Jacob Schatz 已提交
123 124 125
<div
  v-if="showCommitable"
  id="commit-area">
J
Jacob Schatz 已提交
126 127 128 129 130 131 132 133
  <popup-dialog
    v-if="showNewBranchDialog"
    :primary-button-label="__('Create new branch')"
    kind="primary"
    :title="__('Branch has changed')"
    :text="__('This branch has changed since you started editing. Would you like to create a new branch?')"
    @submit="commitToNewBranch"
  />
J
Jacob Schatz 已提交
134 135
  <form
    class="form-horizontal"
J
Jacob Schatz 已提交
136
    @submit.prevent="tryCommit">
J
Jacob Schatz 已提交
137 138
    <fieldset>
      <div class="form-group">
J
Jacob Schatz 已提交
139 140 141 142
        <label class="col-md-4 control-label staged-files">
          Staged files ({{changedFiles.length}})
        </label>
        <div class="col-md-6">
143
          <ul class="list-unstyled changed-files">
J
Jacob Schatz 已提交
144 145 146 147 148 149
            <li
              v-for="branchPath in branchPaths"
              :key="branchPath">
              <span class="help-block">
                {{branchPath}}
              </span>
J
Jacob Schatz 已提交
150 151 152 153 154
            </li>
          </ul>
        </div>
      </div>
      <div class="form-group">
J
Jacob Schatz 已提交
155 156 157 158 159 160 161 162 163 164 165 166
        <label
          class="col-md-4 control-label"
          for="commit-message">
          Commit message
        </label>
        <div class="col-md-6">
          <textarea
            id="commit-message"
            class="form-control"
            name="commit-message"
            v-model="commitMessage">
          </textarea>
J
Jacob Schatz 已提交
167 168
        </div>
      </div>
169
      <div class="form-group target-branch">
J
Jacob Schatz 已提交
170 171 172 173 174 175 176
        <label
          class="col-md-4 control-label"
          for="target-branch">
          Target branch
        </label>
        <div class="col-md-6">
          <span class="help-block">
177
            {{currentBranch}}
J
Jacob Schatz 已提交
178
          </span>
J
Jacob Schatz 已提交
179 180
        </div>
      </div>
J
Jacob Schatz 已提交
181 182 183 184 185 186 187 188
      <div class="col-md-offset-4 col-md-6">
        <button
          ref="submitCommit"
          type="submit"
          :disabled="cantCommitYet"
          class="btn btn-success">
          <i
            v-if="submitCommitsLoading"
J
Jacob Schatz 已提交
189
            class="js-commit-loading-icon fa fa-spinner fa-spin"
J
Jacob Schatz 已提交
190 191 192 193 194 195
            aria-hidden="true"
            aria-label="loading">
          </i>
          <span class="commit-summary">
            Commit {{changedFiles.length}} {{filePluralize}}
          </span>
J
Jacob Schatz 已提交
196
        </button>
J
Jacob Schatz 已提交
197
      </div>
J
Jacob Schatz 已提交
198 199 200 201 202 203 204 205
      <div class="col-md-offset-4 col-md-6">
        <div class="checkbox">
          <label>
            <input type="checkbox" v-model="startNewMR">
            <span>Start a <strong>new merge request</strong> with these changes</span>
          </label>
        </div>
      </div>
J
Jacob Schatz 已提交
206 207 208
    </fieldset>
  </form>
</div>
209
</template>