feat: fetch commits from Vervis

I had to hardcode the path to the commits because it is not a property
on the repository object. There are empty repositories, which I handle
as well.

Next step is displaying them in the Activities.

Signed-off-by: André Jaenisch <andre.jaenisch@posteo.de>
This commit is contained in:
André Jaenisch 2024-06-21 12:24:47 +02:00
parent 288ad752ad
commit d3fc06a239
No known key found for this signature in database
GPG key ID: 5A668E771F1ED854

View file

@ -47,18 +47,86 @@ export async function getHomepage({ account, passphrase, server }) {
const dom = cheerio.load(response.data);
const inbox = getInboxLinkFromResponse(response);
const peopleId = getPeopleIdFromInboxLink(inbox);
const r = await fetchPersonData(loginFormData, getCookie(response), peopleId);
const person = getPersonFromResponse(r);
const s = await fetchFollowings(loginFormData, getCookie(r), person);
const followings = getFollowingsFromResponse(s);
const t = await fetchFollowingsMap(loginFormData, getCookie(s), followings);
const followingsMap = getFollowingsMapFromResponses(t);
const u = await fetchFollowingsCollaborators(
const a = await fetchPersonData(loginFormData, getCookie(response), peopleId);
const person = getPersonFromResponse(a);
const b = await fetchFollowings(loginFormData, getCookie(a), person);
const followings = getFollowingsFromResponse(b);
const c = await fetchFollowingsMap(loginFormData, getCookie(b), followings);
const followingsMap = getFollowingsMapFromResponses(c);
const d = await fetchFollowingsCollaborators(
loginFormData,
getCookie(t[t.length - 1]),
getCookie(c[c.length - 1]),
followingsMap
);
const collaboratorsMap = getFollowingsCollabotorsFromResponses(u);
const collaboratorsMap = getFollowingsCollabotorsFromResponses(d);
// Have to update cookies from one request to the next
let temp = d[d.length - 1];
const e = await Promise.all(
followingsMap
.filter((following) => following.type === 'Repository')
.map(async (repository) => {
const resp = await fetchCommitsForRepository(
loginFormData,
getCookie(temp),
repository.id,
'main'
);
if (resp.headers) {
temp = resp;
} else {
temp = {
...temp,
...resp
};
}
return resp;
})
);
const commits = getCommitsFromResponses(e);
const f = await fetchCommitsMap(loginFormData, getCookie(temp), commits);
const commitsMap = getCommitsMapFromResponses(f);
// fr33domlover = qn870 on fig.fr33domlover.site
const g = await fetchPersonData(loginFormData, getCookie(temp), 'qn870');
const otherPerson = getPersonFromResponse(g);
const h = await fetchFollowings(loginFormData, getCookie(g), otherPerson);
const otherFollowings = getFollowingsFromResponse(h);
const i = await fetchFollowingsMap(loginFormData, getCookie(h), otherFollowings);
const otherFollowingsMap = getFollowingsMapFromResponses(i);
const j = await fetchFollowingsCollaborators(
loginFormData,
getCookie(i[i.length - 1]),
otherFollowingsMap
);
const otherCollaboratorsMap = getFollowingsCollabotorsFromResponses(j);
temp = i[i.length - 1];
const k = await Promise.all(
otherFollowingsMap
.filter((following) => following.type === 'Repository')
.map(async (repository) => {
const resp = await fetchCommitsForRepository(
loginFormData,
getCookie(temp),
repository.id,
'main'
);
if (resp.headers) {
temp = resp;
} else {
temp = {
...temp,
...resp
};
}
return resp;
})
);
const otherCommits = getCommitsFromResponses(k);
const l = await fetchCommitsMap(loginFormData, getCookie(temp), otherCommits);
const otherCommitsMap = getCommitsMapFromResponses(l);
if (dom('h2:contains("Your teams") + p').text().includes("aren't a member")) {
console.log('No teams');
@ -87,11 +155,15 @@ export async function getHomepage({ account, passphrase, server }) {
}
return {
collaboratorsMap,
cookies: getCookie(u[u.length - 1]),
collaboratorsMap: [...collaboratorsMap, ...otherCollaboratorsMap],
commits: [...commits, ...otherCommits],
commitsMap: [...commitsMap, ...otherCommitsMap],
cookies: getCookie(temp),
followings,
followingsMap,
otherFollowings,
followingsMap: [...followingsMap, ...otherFollowingsMap],
person,
otherPerson,
patches,
projects,
repos,
@ -133,6 +205,14 @@ function getFollowingsCollabotorsFromResponses(responses) {
return responses.map((response) => response.data);
}
function getCommitsFromResponses(responses) {
return responses.map((response) => response.data);
}
function getCommitsMapFromResponses(responses) {
return responses.map((response) => response.data);
}
function getPeopleIdFromInboxLink(inboxLink) {
// The id is part of the pathname as /people/:peopleId/inbox
const url = new URL(inboxLink);
@ -197,3 +277,55 @@ async function fetchFollowingsCollaborators(loginFormData, cookie, followingsMap
return Promise.all(mappedCollaborators);
}
async function fetchCommitsForRepository(loginFormData, cookie, repository, branch) {
const headers = new AxiosHeaders();
headers.set({ Cookie: cookie });
const { pathname } = new URL(repository);
let response = null;
try {
response = await loginFormData.instance.get(`${pathname}/commits-by/${branch}?page=1`, {
headers
});
} catch (exc) {
// Can turn out to be 404 Not Found
response = {
...exc.response,
data: {
orderedItems: [],
details: exc.response.data.message
}
};
}
return response;
}
async function fetchCommitsMap(loginFormData, cookie, commits) {
const headers = new AxiosHeaders();
headers.set({ Cookie: cookie });
const mappedCommits = commits
.map((commit) => {
return commit.orderedItems.map(async (c) => {
const { pathname } = new URL(c);
let response = null;
try {
response = await loginFormData.instance.get(pathname, { headers });
} catch (exc) {
// Can turn out to be 500 Internal Server Error
response = {
...exc.response,
data: {
details: exc.response.data.error
}
};
}
return response;
});
})
.flat(1);
return Promise.all(mappedCommits);
}