• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

evolvedbinary / prosemirror-lwdita / a894ebc1-2fdf-462b-a7ab-2ed844602ca1

10 Oct 2024 04:53PM UTC coverage: 44.395% (+0.3%) from 44.102%
a894ebc1-2fdf-462b-a7ab-2ed844602ca1

Pull #416

circleci

plutonik-a
[doc] Remove obsolete comment
Pull Request #416: Improve user UX regarding errors in the editor during file & GitHub operations

175 of 365 branches covered (47.95%)

Branch coverage included in aggregate %.

13 of 31 new or added lines in 4 files covered. (41.94%)

2 existing lines in 2 files now uncovered.

324 of 759 relevant lines covered (42.69%)

26.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.36
/packages/prosemirror-lwdita/src/github-integration/github.plugin.ts
1
/*!
2
Copyright (C) 2020 Evolved Binary
3

4
This program is free software: you can redistribute it and/or modify
5
it under the terms of the GNU Affero General Public License as
6
published by the Free Software Foundation, either version 3 of the
7
License, or (at your option) any later version.
8

9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
GNU Affero General Public License for more details.
13

14
You should have received a copy of the GNU Affero General Public License
15
along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
*/
17

18
import { xditaToJdita } from "@evolvedbinary/lwdita-xdita";
1✔
19
import { document as jditaToProsemirrorJson } from "../document";
1✔
20
import { showErrorPage } from "./request";
1✔
21

22
/**
23
 * Fetches the raw content of a document from a GitHub repository.
24
 *
25
 * @param ghrepo - The GitHub repository in the format "owner/repo".
26
 * @param source - The path to the file within the repository.
27
 * @param branch - The branch from which to fetch the document.
28
 * @returns A promise that resolves to the raw content of the document as a string.
29
 *
30
 * @remarks
31
 * This function currently fetches the document from the 'main' branch of the repository.
32
 * should use the GitHub API to dynamically determine the default branch of the repository.
33
 */
34
export const fetchRawDocumentFromGitHub = async (ghrepo: string, source: string, branch: string): Promise<string> => {
2✔
35
  const url = `https://raw.githubusercontent.com/${ghrepo}/${branch}/${source}`;
2✔
36
  const response = await fetch(url);
2✔
37

38
  if (!response.ok) {
2✔
39
    showErrorPage('fileNotFound', '', response.statusText);
1✔
40
  }
41
  //TODO: Handle errors
42
  return response.text();
1✔
43
};
44

45
/**
46
 * Transforms a raw GitHub document into a ProseMirror state save.
47
 *
48
 * @param rawDocument - The raw xdita document as a string.
49
 * @returns A promise that resolves to a record containing the ProseMirror state save.
50
 */
51
// eslint-disable-next-line @typescript-eslint/no-explicit-any
52
export const transformGitHubDocumentToProsemirrorJson = async (rawDocument: string): Promise<Record<string, any>> => {
1✔
53
    // convert the raw xdita document to jdita
54
    const jdita = await xditaToJdita(rawDocument);
1✔
55

56
    // convert the jdita document to prosemirror state save
57
    const prosemirrorJson = await jditaToProsemirrorJson(jdita);
1✔
58

59
    return prosemirrorJson;
1✔
60
};
61

62
/**
63
 * Fetches a raw document from a GitHub repository and transforms it into a ProseMirror JSON document.
64
 *
65
 * @param ghrepo - The GitHub repository from which to fetch the document.
66
 * @param source - The source path of the document within the repository.
67
 * @param branch - The branch from which to fetch the document.
68
 * @returns A promise that resolves to the transformed ProseMirror JSON document.
69
 */
70
export const fetchAndTransform = async (ghrepo: string, source: string, branch: string) => {
1✔
71
  const rawDoc = await fetchRawDocumentFromGitHub(ghrepo, source, branch);
×
72
  const jsonDoc = await transformGitHubDocumentToProsemirrorJson(rawDoc);
×
73
  return jsonDoc;
×
74
};
75

76
/**
77
 * Exchanges an OAuth code for an access token.
78
 *
79
 * @param code - The OAuth code to exchange for an access token.
80
 * @returns A promise that resolves to the access token as a string.
81
 * @throws Will throw an error if the fetch request fails or if the response is not in the expected format.
82
 */
83
export const exchangeOAuthCodeForAccessToken = async (code: string): Promise<string> => {
1✔
84
  // build the URL to exchange the code for an access token
85
  const url = `http://localhost:3000/api/github/token?code=${code}`;
×
86
  // fetch the access token
87
  const response = await fetch(url);
×
88

89
  // TODO (AvC): This error type might be changed to be more specific depending on
90
  // further error handling
NEW
91
  if (!response.ok) {
×
NEW
92
    showErrorPage('unknownError', '', response.statusText);
×
93
  }
94

UNCOV
95
  const json = await response.json();
×
96
  //TODO: Handle errors
97
  return json;
×
98
};
99

100
/**
101
 * Fetches user information from the backend API.
102
 *
103
 * @param token - The authorization token to access the GitHub API.
104
 * @returns A promise that resolves to a record containing user information.
105
 */
106
export const getUserInfo = async (token: string): Promise<Record<string, string>> => {
3✔
107
  const url = `http://localhost:3000/api/github/user`;
3✔
108
  const response = await fetch(url, {
3✔
109
    headers: {
110
      'authorization': `Bearer ${token}`
111
    }
112
  });
113

114
  // TODO (AvC): This error type might be changed to be more specific depending on
115
  // further error handling
116
  if (!response.ok) {
3✔
117
    showErrorPage('unknownError', '', response.statusText);
1✔
118
  }
119
  const json = await response.json();
2✔
120
  return json;
2✔
121
};
122

123
/**
124
 * Publishes a document to a specified GitHub repository.
125
 * Makes a POST request to the `/api/github/integration` endpoint with the necessary details to create a pull request.
126
 *
127
 * @param ghrepo - The GitHub repository in the format "owner/repo".
128
 * @param source - The path to the source document.
129
 * @param branch - The branch used as base for the PR.
130
 * @param title - The title of the pull request and the commit message.
131
 * @param desc - The description of the pull request.
132
 * @param changedDocument - The content of the changed document.
133
 * @returns A promise that resolves when the document has been published.
134
 */
135
export const createPrFromContribution = async (ghrepo: string, source: string, branch: string, changedDocument: string, title: string, desc: string): Promise<string> => {
1✔
136
  const authenticatedUserInfo = await getUserInfo(localStorage.getItem('token') as string);
1✔
137

138
  const owner = ghrepo.split('/')[0];
1✔
139
  const repo = ghrepo.split('/')[1];
1✔
140
  const newOwner = authenticatedUserInfo.login;
1✔
141
  const date = new Date();
1✔
142
  const newBranch = `doc/petal-${date.getFullYear()}${date.getMonth()}${date.getDate()}${date.getHours()}${date.getMinutes()}${date.getSeconds()}`;
1✔
143
  const commitMessage = title;
1✔
144
  const path = source;
1✔
145
  const content = changedDocument;
1✔
146
  const change = {
1✔
147
    path,
148
    content
149
  };
150
  const body = `${desc} \n ------------------\n This is an automated PR made by the prosemirror-lwdita demo`;
1✔
151
  // get the token from the local storage
152
  const token = localStorage.getItem('token');
1✔
153
  // make a post request to  /api/github/integration
154
  const response = await fetch('http://localhost:3000/api/github/integration', {
1✔
155
    method: 'POST',
156
    headers: {
157
      'Content-Type': 'application/json',
158
      'Authorization': `Bearer ${token}`
159
    },
160
    body: JSON.stringify({
161
      owner,
162
      repo,
163
      newOwner,
164
      branch,
165
      newBranch,
166
      commitMessage,
167
      change,
168
      title,
169
      body
170
    })
171
  });
172

173
  // TODO (AvC): This error type might be changed to be more specific depending on
174
  // further error handling
175
  if (!response.ok) {
1!
NEW
176
    showErrorPage('unknownError', '', response.statusText);
×
177
  }
178

179
  const json = await response.json();
1✔
180
  return json;
1✔
181
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc