gitea backend returning proper values
This commit is contained in:
71
src/lib/server/gitea.ts
Normal file
71
src/lib/server/gitea.ts
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
export interface GitCommit {
|
||||||
|
message: string;
|
||||||
|
hash: string;
|
||||||
|
shortHash: string;
|
||||||
|
url: string;
|
||||||
|
timestamp: Date;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getGiteaRepos(fetch: typeof globalThis.fetch) {
|
||||||
|
let response = await fetch(
|
||||||
|
'https://git.voidarc.co.uk/api/v1/repos/search?sort=updated&order=desc&limit=10'
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Could not fetch repo information: ' + response.status);
|
||||||
|
}
|
||||||
|
|
||||||
|
let data = await response.json();
|
||||||
|
|
||||||
|
return data.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getRepoCommits(fetch: typeof globalThis.fetch, url: string) {
|
||||||
|
let response = await fetch(url + '/commits?page=1&limit=15');
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Could not fetch commit information: ' + response.status);
|
||||||
|
}
|
||||||
|
|
||||||
|
let data = await response.json();
|
||||||
|
|
||||||
|
let finalData = [];
|
||||||
|
|
||||||
|
for (let commit of data) {
|
||||||
|
// Filter out other users
|
||||||
|
if (commit.author.login == 'voidarc') {
|
||||||
|
finalData.push(commit);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return finalData;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getLatestCommits(fetch: typeof globalThis.fetch, count: number): GitCommit[] {
|
||||||
|
const repos = await getGiteaRepos(fetch);
|
||||||
|
|
||||||
|
let commitList = [];
|
||||||
|
for (let repo of repos) {
|
||||||
|
console.log(repo.name);
|
||||||
|
let commits = await getRepoCommits(fetch, repo.url);
|
||||||
|
for (let commit of commits) {
|
||||||
|
commitList.push(commit);
|
||||||
|
console.log('pushed');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
commitList.sort((a, b) => new Date(b.created).getTime() - new Date(a.created).getTime());
|
||||||
|
|
||||||
|
let interfaceCommitList: GitCommit[] = [];
|
||||||
|
for (let commit of commitList) {
|
||||||
|
let commitObject: GitCommit = {
|
||||||
|
message: commit.commit.message,
|
||||||
|
hash: commit.sha,
|
||||||
|
shortHash: commit.sha.substring(0, 8),
|
||||||
|
timestamp: new Date(commit.created),
|
||||||
|
url: commit.html_url
|
||||||
|
};
|
||||||
|
interfaceCommitList.push(commitObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
return interfaceCommitList.slice(0, count);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user