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

evolvedbinary / prosemirror-lwdita / 658fc1bf-163a-43c6-acfa-bd5557e320a8

15 Oct 2024 03:21PM UTC coverage: 44.494% (+0.3%) from 44.19%
658fc1bf-163a-43c6-acfa-bd5557e320a8

Pull #439

circleci

marmoure
[doc] how to deploy on production
Pull Request #439: How to deploy on production

175 of 365 branches covered (47.95%)

Branch coverage included in aggregate %.

326 of 761 relevant lines covered (42.84%)

26.25 hits per line

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

77.78
/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
import { showToast } from "../toast";
1✔
22

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

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

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

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

60
    return prosemirrorJson;
1✔
61
};
62

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

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

90
  // TODO (AvC): Depending on further error handling, this eror might be redirected to the error page
91
  if (!response.ok) {
×
92
    showToast('Sorry, an error occured while publishing the document. Please try again. The error: ' + response.statusText, 'error');
×
93
  }
94

95
  const json = await response.json();
×
96
  //TODO: Handle errors
97
  return json.token;
×
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): Depending on further error handling, this eror might be redirected to the error page
115
  if (!response.ok) {
3✔
116
    showToast('Sorry, an error occured while publishing the document. Please try again. The error: ' + response.statusText, 'error');
1✔
117
  }
118
  const json = await response.json();
2✔
119
  return json;
2✔
120
};
121

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

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

172
  // TODO (AvC): Depending on further error handling, this eror might be redirected to the error page
173
  if (!response.ok) {
1!
174
    showToast('Sorry, an error occured while publishing the document. Please try again. The error: ' + response.statusText , 'error');
×
175
  }
176

177
  const json = await response.json();
1✔
178
  return json.url;
1✔
179
};
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