Skip to content

Commit d2a5f63

Browse files
authored
refactor: unify the repository structure (#789)
* refactor: we only take repoId as entry point * refactor: adapt Rest schema to match GQL, merge catalogs * refactor: Make Repository screen only accept a repoId parameter * revert: remove unwanted commit * refactor: simplify navigate to repo * fix: fix repository screen params * fix: use correct attribute
1 parent 03cf43a commit d2a5f63

File tree

10 files changed

+86
-40
lines changed

10 files changed

+86
-40
lines changed

src/api/schemas/events.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import { schema } from 'normalizr';
2+
import { repoSchema } from './repos';
23

34
export const eventSchema = new schema.Entity(
45
'events',
5-
{},
6+
{
7+
repo: repoSchema,
8+
payload: {
9+
forkee: repoSchema,
10+
},
11+
},
612
{
713
idAttribute: event => event.id,
814
}

src/api/schemas/gql-repos.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { schema } from 'normalizr';
22

33
export const gqlRepoSchema = new schema.Entity(
4-
'gqlRepos',
4+
'repos',
55
{},
66
{
77
idAttribute: ({ repository }) => {

src/api/schemas/repos.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,62 @@
11
import { schema } from 'normalizr';
2+
import { languageColors } from 'config';
3+
4+
const minV3 = repo => ({
5+
id: repo.id,
6+
name: repo.name.split('/')[0],
7+
nameWithOwner: repo.name,
8+
// TODO: Remove me once the transition is done
9+
'----------------': '-------------------',
10+
full_name: repo.name,
11+
url: `https://api.github.com/repos/${repo.name}`,
12+
});
13+
14+
const fullV3 = repo => {
15+
return {
16+
id: repo.id,
17+
name: repo.name,
18+
nameWithOwner: repo.full_name,
19+
isFork: repo.fork,
20+
description: repo.description,
21+
forkCount: repo.forks_count,
22+
isPrivate: repo.private,
23+
primaryLanguage: repo.language
24+
? {
25+
name: repo.language,
26+
color: languageColors[repo.language],
27+
}
28+
: null,
29+
stargazers: {
30+
totalCount: repo.stargazers_count,
31+
},
32+
defaultBranchRef: {
33+
name: repo.default_branch,
34+
},
35+
// TODO: Remove me once the transition is done
36+
'----------------': '-------------------',
37+
full_name: repo.full_name,
38+
fork: repo.fork,
39+
private: repo.private,
40+
forks_count: repo.forks_count,
41+
language: repo.language,
42+
stargazers_count: repo.stargazers_count,
43+
url: `https://api.github.com/repos/${repo.full_name}`,
44+
};
45+
};
246

347
export const repoSchema = new schema.Entity(
448
'repos',
549
{},
650
{
7-
idAttribute: repo => repo.full_name.toLowerCase(),
51+
idAttribute: repo =>
52+
(repo.full_name ? repo.full_name : repo.name).toLowerCase(),
53+
processStrategy: repo => {
54+
if (repo.full_name) {
55+
// full v3
56+
return fullV3(repo);
57+
}
58+
59+
return minV3(repo);
60+
},
861
}
962
);

src/auth/screens/events.screen.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const mapStateToProps = state => {
1515
const {
1616
auth: { user, locale },
1717
pagination: { ACTIVITY_GET_EVENTS_RECEIVED },
18-
entities: { events },
18+
entities: { events, repos },
1919
} = state;
2020

2121
const userEventsPagination = ACTIVITY_GET_EVENTS_RECEIVED[user.login] || {
@@ -25,6 +25,7 @@ const mapStateToProps = state => {
2525

2626
return {
2727
user,
28+
repos,
2829
userEventsPagination,
2930
userEvents,
3031
locale,
@@ -126,10 +127,12 @@ class Events extends Component {
126127
);
127128
}
128129

129-
getRepoLink(userEvent) {
130+
getRepoLink(userEvent, isFork = false) {
131+
const repoId = isFork ? userEvent.payload.forkee : userEvent.repo;
132+
130133
return (
131-
<LinkDescription onPress={() => this.navigateToRepository(userEvent)}>
132-
{userEvent.repo.name}
134+
<LinkDescription onPress={() => this.navigateToRepository(repoId)}>
135+
{this.props.repos[repoId].nameWithOwner}
133136
</LinkDescription>
134137
);
135138
}
@@ -256,13 +259,7 @@ class Events extends Component {
256259
handleForkEvent(userEvent) {
257260
const actor = this.getActorLink(userEvent);
258261
const repo = this.getRepoLink(userEvent);
259-
const fork = (
260-
<LinkDescription
261-
onPress={() => this.navigateToRepository(userEvent, true)}
262-
>
263-
{userEvent.payload.forkee.full_name}
264-
</LinkDescription>
265-
);
262+
const fork = this.getRepoLink(userEvent, true);
266263

267264
return t('{actor} forked {repo} at {fork}', this.props.locale, {
268265
actor,
@@ -540,16 +537,9 @@ class Events extends Component {
540537
},
541538
});
542539

543-
navigateToRepository = (userEvent, isForkEvent) => {
540+
navigateToRepository = repoId => {
544541
this.props.navigation.navigate('Repository', {
545-
repository: !isForkEvent
546-
? {
547-
...userEvent.repo,
548-
name: userEvent.repo.name.substring(
549-
userEvent.repo.name.indexOf('/') + 1
550-
),
551-
}
552-
: userEvent.payload.forkee,
542+
repoId,
553543
});
554544
};
555545

src/auth/screens/privacy-policy.screen.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { ScrollView } from 'react-native';
55
import { ViewContainer } from 'components';
66
import { t } from 'utils';
77
import { colors, fonts, normalize } from 'config';
8-
import { v3 } from 'api';
98

109
const Container = styled.View`
1110
flex: 1;
@@ -139,7 +138,7 @@ export class PrivacyPolicyScreen extends Component {
139138
<Link
140139
onPress={() =>
141140
navigation.navigate('Repository', {
142-
repositoryUrl: `${v3.root}/repos/gitpoint/git-point`,
141+
repoId: 'gitpoint/git-point',
143142
})
144143
}
145144
>

src/components/repository-list-item.component.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ export const RepositoryListItem = ({
139139
color: colors.grey,
140140
type: 'octicon',
141141
}}
142-
onPress={() => navigation.navigate('Repository', { repository })}
142+
onPress={() =>
143+
navigation.navigate('Repository', { repoId: repository.full_name })
144+
}
143145
/>
144146
);
145147

src/issue/screens/issue.screen.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
IssueEventListItem,
2222
} from 'components';
2323
import { v3 } from 'api';
24-
import { t, formatEventsToRender, openURLInView } from 'utils';
24+
import { t, formatEventsToRender, openURLInView, getRepoIdFromUrl } from 'utils';
2525
import { colors } from 'config';
2626
import { getRepository, getContributors } from 'repository';
2727
import {
@@ -179,7 +179,7 @@ class Issue extends Component {
179179
const { navigation } = this.props;
180180

181181
navigation.navigate('Repository', {
182-
repositoryUrl: url,
182+
repoId: getRepoIdFromUrl(url),
183183
});
184184
};
185185

src/notifications/screens/notifications.screen.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { bindActionCreators } from 'redux';
77
import { FlatList, View, ScrollView, Platform } from 'react-native';
88
import { ButtonGroup, Card, Icon } from 'react-native-elements';
99

10-
import { v3 } from 'api';
1110
import {
1211
Button,
1312
ViewContainer,
@@ -264,7 +263,7 @@ class Notifications extends Component {
264263
const { navigation } = this.props;
265264

266265
navigation.navigate('Repository', {
267-
repositoryUrl: `${v3.root}/repos/${fullName}`,
266+
repoId: fullName,
268267
});
269268
};
270269

@@ -361,7 +360,7 @@ class Notifications extends Component {
361360
const { navigation } = this.props;
362361

363362
navigation.navigate('Repository', {
364-
repositoryUrl: `${v3.root}/repos/${fullName}`,
363+
repoId: fullName,
365364
});
366365
};
367366

src/repository/screens/repository.screen.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,13 @@ import { colors, fonts } from 'config';
3030
const mapStateToProps = (state, ownProps) => {
3131
const {
3232
auth: { user, locale },
33-
entities: { gqlRepos, users, repos },
33+
entities: { users, repos },
3434
pagination: { REPOS_GET_CONTRIBUTORS },
3535
} = state;
3636

37-
const params = ownProps.navigation.state.params;
37+
const repoId = ownProps.navigation.state.params.repoId;
3838

39-
const repoId =
40-
params.repoId ||
41-
params.repository.url
42-
.replace('https://api.github.com/repos/', '')
43-
.toLowerCase();
44-
45-
const repository = gqlRepos[repoId] || repos[repoId] || params.repository;
39+
const repository = repos[repoId];
4640

4741
const contributorsPagination = REPOS_GET_CONTRIBUTORS[repoId] || {
4842
ids: [],

src/utils/migration-helper.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ export const toOldIssueFormat = (issue, repoId, isPr = false) => {
1717
url: `https://api.github.com/repos/${repoId}/issues/${issue.number}`,
1818
};
1919
};
20+
21+
export const getRepoIdFromUrl = url =>
22+
url.replace(/https:\/\/api.github.com\/repos\/(.+)\/(.+)\/?/, '$1/$2');

0 commit comments

Comments
 (0)