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

evolvedbinary / prosemirror-lwdita / 675f8f46-f650-4f01-8fca-5d3358824f2d

24 Sep 2024 06:59PM UTC coverage: 43.219%. Remained the same
675f8f46-f650-4f01-8fca-5d3358824f2d

Pull #398

circleci

marmoure
[bugfix] update the function call in the tests
Pull Request #398: Feature/branch name

170 of 372 branches covered (45.7%)

Branch coverage included in aggregate %.

7 of 9 new or added lines in 3 files covered. (77.78%)

174 existing lines in 3 files now uncovered.

308 of 734 relevant lines covered (41.96%)

27.18 hits per line

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

82.05
/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
 * @returns A promise that resolves to the raw content of the document as a string.
27
 *
28
 * @remarks
29
 * This function currently fetches the document from the 'main' branch of the repository.
30
 * should use the GitHub API to dynamically determine the default branch of the repository.
31
 */
32
export const fetchRawDocumentFromGitHub = async (ghrepo: string, source: string, branch: string): Promise<string> => {
2✔
33
  //TODO(YB): the branch should be passed as a parameter
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
 * @returns A promise that resolves to the transformed ProseMirror JSON document.
64
 */
65
export const fetchAndTransform = async (ghrepo: string, source: string, branch: string) => {
1✔
NEW
66
  const rawDoc = await fetchRawDocumentFromGitHub(ghrepo, source, branch);
×
67
  const jsonDoc = await transformGitHubDocumentToProsemirrorJson(rawDoc);
×
68
  return jsonDoc;
×
69
};
70

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

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

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

153

154
  const json = await response.json();
1✔
155
  return json;
1✔
156
};
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