Connect to Gitea API #14

Merged
snedadah merged 7 commits from a34sun/connect-to-gitea-api into master 2023-04-04 20:21:22 -04:00
Member

Function that creates new PR

Function that creates new PR
a34sun added 3 commits 2023-03-21 20:27:43 -04:00
snedadah was assigned by a34sun 2023-03-21 20:28:10 -04:00
e26chiu was assigned by a34sun 2023-03-21 20:28:10 -04:00
snedadah reviewed 2023-03-22 20:56:54 -04:00
snedadah left a comment
Owner

Nice work!! Left some quick comments. Also, you should assign as as 'Reviewers" not assignees on gitea :)

Nice work!! Left some quick comments. Also, you should assign as as 'Reviewers" not assignees on gitea :)
@ -9,0 +4,4 @@
export const MAIN_BRANCH = "main"
export const GITEA_API_URL = "https://git.csclub.uwaterloo.ca/api/v1/repos"
export const makePullRequest = async (owner: string, repoName: string, title: string, body:string, branchName: string, assignee: string) => {
Owner

Nice work! Instead of assignee, we should use reviwers. So please add "reviewers: Array" as a paramater. To add reviewers, it seems like we need to make another API call, here is a starting point I got from playing around. Note that you can get the PR number by calling content.number from the PR response:

async function addReviewers(
  reviewers: Array<String>,
  owner: string,
  repoName: string,
  prNumber: number
) {
  const PULL_URL = `${GITEA_API_URL}/${owner}/${repoName}/pulls/${prNumber}/requested_reviewers`;
  const response = await fetch(PULL_URL, {
    method: "POST",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: `Bearer ${process.env.GITEA_API_KEY}`,
    },
    body: JSON.stringify({
      reviewers: reviewers,
    }),
  });
  const content = await response.json();

  if (response.status != 201) {
    throw Error("Error from Gitea when adding reviewers " + content.errors);
  }
}

Furthermore, lets make the assignee param optional by adding a default empty string:

  assignee: string = "",
Nice work! Instead of assignee, we should use reviwers. So please add "reviewers: Array<String>" as a paramater. To add reviewers, it seems like we need to make another API call, here is a starting point I got from playing around. Note that you can get the PR number by calling `content.number` from the PR response: ```ts async function addReviewers( reviewers: Array<String>, owner: string, repoName: string, prNumber: number ) { const PULL_URL = `${GITEA_API_URL}/${owner}/${repoName}/pulls/${prNumber}/requested_reviewers`; const response = await fetch(PULL_URL, { method: "POST", headers: { Accept: "application/json", "Content-Type": "application/json", Authorization: `Bearer ${process.env.GITEA_API_KEY}`, }, body: JSON.stringify({ reviewers: reviewers, }), }); const content = await response.json(); if (response.status != 201) { throw Error("Error from Gitea when adding reviewers " + content.errors); } } ``` Furthermore, lets make the assignee param optional by adding a default empty string: assignee: string = "",
snedadah marked this conversation as resolved
@ -9,0 +6,4 @@
export const makePullRequest = async (owner: string, repoName: string, title: string, body:string, branchName: string, assignee: string) => {
const PULL_URL = `${GITEA_API_URL}/${owner}/${repoName}/pulls`
console.log(PULL_URL)
Owner

You can delete this log, doesn't really add much value IMO to stick around long term.

You can delete this log, doesn't really add much value IMO to stick around long term.
a34sun marked this conversation as resolved
@ -9,0 +23,4 @@
head: branchName,
})
});
const content = await response.json();
Owner

Here, lets check whether the call succeeded or not, and throw an error if it didn't, and return the pr url if it did:

  const content = await response.json();
  if (response.status != 201) {
    console.log(content);
    throw Error("Error from Gitea when making PR: " + content.errors);
  }

  //(after adding the add reviewers method)
  //await addReviewers(reviewers, owner, repoName, content.number);

  return content.url;
Here, lets check whether the call succeeded or not, and throw an error if it didn't, and return the pr url if it did: ```ts const content = await response.json(); if (response.status != 201) { console.log(content); throw Error("Error from Gitea when making PR: " + content.errors); } //(after adding the add reviewers method) //await addReviewers(reviewers, owner, repoName, content.number); return content.url; ```
a34sun marked this conversation as resolved
src/server.ts Outdated
@ -84,3 +84,2 @@
// TODO: Remove this, only for demo purposes
makePullRequest();
// TODO: Implement this for www
Owner

You can just delete these lines.

You can just delete these lines.
a34sun added 2 commits 2023-03-30 16:19:56 -04:00
Author
Member

Right now the function returns the PR is successful but doesn't know if add reviewers was successful, is this how it should work?

Right now the function returns the PR is successful but doesn't know if add reviewers was successful, is this how it should work?
snedadah added 1 commit 2023-04-02 14:00:52 -04:00
snedadah approved these changes 2023-04-02 14:02:06 -04:00
snedadah left a comment
Owner

LGTM! Yep, what you had was fine,

I just did add await addReviewers(reviewers, owner, repoName, content.number); an "await"
so that it does wait for the add reviews to finish.

LGTM! Yep, what you had was fine, I just did add ` await addReviewers(reviewers, owner, repoName, content.number);` an "await" so that it does wait for the add reviews to finish.
snedadah added 1 commit 2023-04-02 14:03:22 -04:00
snedadah merged commit ac5a0a7024 into master 2023-04-04 20:21:22 -04:00
snedadah referenced this issue from a commit 2023-04-04 20:21:22 -04:00
Sign in to join this conversation.
No reviewers
No Label
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: www/Eventer#14
No description provided.