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

evolvedbinary / prosemirror-lwdita / 94c3fee4-61bb-4638-8fbe-6ee589605475

04 Oct 2024 03:56PM UTC coverage: 44.102%. Remained the same
94c3fee4-61bb-4638-8fbe-6ee589605475

Pull #412

circleci

marmoure
[bugfix] Address PR comments from Adam
Pull Request #412: Cypress tests for GitHub integration

170 of 357 branches covered (47.62%)

Branch coverage included in aggregate %.

1 of 3 new or added lines in 2 files covered. (33.33%)

316 of 745 relevant lines covered (42.42%)

26.8 hits per line

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

82.5
/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

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

37
  //TODO: Handle errors
38
  return response.text();
2✔
39
};
40

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

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

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

89
/**
90
 * Fetches user information from the backend API.
91
 *
92
 * @param token - The authorization token to access the GitHub API.
93
 * @returns A promise that resolves to a record containing user information.
94
 */
95
export const getUserInfo = async (token: string): Promise<Record<string, string>> => {
3✔
96
  const url = `http://localhost:3000/api/github/user`;
3✔
97
  const response = await fetch(url, {
3✔
98
    headers: {
99
      'authorization': `Bearer ${token}`
100
    }
101
  });
102
  const json = await response.json();
3✔
103
  return json;
2✔
104
};
105

106
/**
107
 * Publishes a document to a specified GitHub repository.
108
 * Makes a POST request to the `/api/github/integration` endpoint with the necessary details to create a pull request.
109
 * 
110
 * @param ghrepo - The GitHub repository in the format "owner/repo".
111
 * @param source - The path to the source document.
112
 * @param branch - The branch used as base for the PR.
113
 * @param title - The title of the pull request and the commit message.
114
 * @param desc - The description of the pull request.
115
 * @param changedDocument - The content of the changed document.
116
 * @returns A promise that resolves when the document has been published.
117
 */
118
export const createPrFromContribution = async (ghrepo: string, source: string, branch: string, changedDocument: string, title: string, desc: string): Promise<string> => {
1✔
119
  const authenticatedUserInfo = await getUserInfo(localStorage.getItem('token') as string);
1✔
120
  
121
  const owner = ghrepo.split('/')[0];
1✔
122
  const repo = ghrepo.split('/')[1];
1✔
123
  const newOwner = authenticatedUserInfo.login;
1✔
124
  const date = new Date();
1✔
125
  const newBranch = `doc/petal-${date.getFullYear()}${date.getMonth()}${date.getDate()}${date.getHours()}${date.getMinutes()}${date.getSeconds()}`;
1✔
126
  const commitMessage = title;
1✔
127
  const path = source;
1✔
128
  const content = changedDocument;
1✔
129
  const change = {
1✔
130
    path,
131
    content
132
  };
133
  const body = `${desc} \n ------------------\n This is an automated PR made by the prosemirror-lwdita demo`; 
1✔
134
  // get the token from the local storage
135
  const token = localStorage.getItem('token');
1✔
136
  // make a post request to  /api/github/integration
137
  const response = await fetch('http://localhost:3000/api/github/integration', {
1✔
138
    method: 'POST',
139
    headers: {
140
      'Content-Type': 'application/json',
141
      'Authorization': `Bearer ${token}`
142
    },
143
    body: JSON.stringify({
144
      owner,
145
      repo,
146
      newOwner,
147
      branch,
148
      newBranch,
149
      commitMessage,
150
      change,
151
      title,
152
      body
153
    })
154
  });
155

156

157
  const json = await response.json();
1✔
158
  return json.url;
1✔
159
};
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