forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-pull-request-tile.js
More file actions
165 lines (144 loc) · 5.48 KB
/
create-pull-request-tile.js
File metadata and controls
165 lines (144 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import React from 'react';
import PropTypes from 'prop-types';
import Octicon from '../atom/octicon';
import {RemotePropType, BranchSetPropType} from '../prop-types';
export default class CreatePullRequestTile extends React.Component {
static propTypes = {
repository: PropTypes.shape({
defaultBranchRef: PropTypes.shape({
prefix: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
}),
}),
remote: RemotePropType.isRequired,
branches: BranchSetPropType.isRequired,
aheadCount: PropTypes.number,
pushInProgress: PropTypes.bool.isRequired,
onCreatePr: PropTypes.func.isRequired,
}
render() {
if (this.isRepositoryNotFound()) {
return (
<div className="github-CreatePullRequestTile-message">
<strong>Repository not found</strong> for the remote <code>{this.props.remote.getName()}</code>.
<hr className="github-CreatePullRequestTile-divider" />
<Octicon icon="link" />
Do you need to update your <strong>remote URL</strong>?
</div>
);
}
if (this.isDetachedHead()) {
return (
<div className="github-CreatePullRequestTile-message">
You are not currently on <strong>any branch</strong>.
<hr className="github-CreatePullRequestTile-divider" />
<Octicon icon="git-branch" />
<strong>Create a new branch</strong>
to share your work with a pull request.
</div>
);
}
if (this.hasNoDefaultRef()) {
return (
<div className="github-CreatePullRequestTile-message">
The repository at remote <code>{this.props.remote.getName()}</code> is <strong>empty</strong>.
<hr className="github-CreatePullRequestTile-divider" />
<Octicon icon="arrow-up" />
<strong>Push a main branch</strong> to begin sharing your work.
</div>
);
}
if (this.isOnDefaultRef()) {
return (
<div className="github-CreatePullRequestTile-message">
You are currently on your repository's <strong>default branch</strong>.
<hr className="github-CreatePullRequestTile-divider" />
<Octicon icon="git-branch" />
<strong>Checkout or create a new branch</strong>
to share your work with a pull request.
</div>
);
}
if (this.isSameAsDefaultRef()) {
return (
<div className="github-CreatePullRequestTile-message">
Your current branch <strong>has not moved</strong> from the repository's default branch.
<hr className="github-CreatePullRequestTile-divider" />
<Octicon icon="git-commit" />
<strong>Make some commits</strong>
to share your work with a pull request.
</div>
);
}
let message = 'Open new pull request';
let disable = false;
const differentRemote = this.pushesToDifferentRemote();
if (this.props.pushInProgress) {
message = 'Pushing...';
disable = true;
} else if (!this.hasUpstreamBranch() || differentRemote) {
message = 'Publish + open new pull request';
} else if (this.props.aheadCount > 0) {
message = 'Push + open new pull request';
}
return (
<div>
{differentRemote &&
<div className="github-CreatePullRequestTile-message">
Your current branch is <strong>configured</strong> to push to the
remote <code>{this.props.branches.getHeadBranch().getPush().getRemoteName()}</code>.
<hr className="github-CreatePullRequestTile-divider" />
<Octicon icon="cloud-upload" />
<strong>Publish</strong> it to <code>{this.props.remote.getName()}</code> instead?
</div>
}
<div className="github-CreatePullRequestTile-controls">
<button
className="github-CreatePullRequestTile-createPr btn btn-primary"
onClick={this.props.onCreatePr}
disabled={disable}>
{message}
</button>
</div>
</div>
);
}
isRepositoryNotFound() {
return !this.props.repository;
}
isDetachedHead() {
return !this.props.branches.getHeadBranch().isPresent();
}
hasNoDefaultRef() {
return !this.props.repository.defaultBranchRef;
}
isOnDefaultRef() {
/* istanbul ignore if */
if (!this.props.repository) { return false; }
const defaultRef = this.props.repository.defaultBranchRef;
/* istanbul ignore if */
if (!defaultRef) { return false; }
const currentBranch = this.props.branches.getHeadBranch();
return currentBranch.getPush().getRemoteRef() === `${defaultRef.prefix}${defaultRef.name}`;
}
isSameAsDefaultRef() {
/* istanbul ignore if */
if (!this.props.repository) { return false; }
const defaultRef = this.props.repository.defaultBranchRef;
/* istanbul ignore if */
if (!defaultRef) { return false; }
const currentBranch = this.props.branches.getHeadBranch();
const mainBranches = this.props.branches.getPushSources(
this.props.remote.getName(), `${defaultRef.prefix}${defaultRef.name}`);
return mainBranches.some(branch => branch.getSha() === currentBranch.getSha());
}
pushesToDifferentRemote() {
const p = this.props.branches.getHeadBranch().getPush();
if (!p.isRemoteTracking()) { return false; }
const pushRemoteName = p.getRemoteName();
return pushRemoteName !== this.props.remote.getName();
}
hasUpstreamBranch() {
return this.props.branches.getHeadBranch().getUpstream().isPresent();
}
}