Why does an api request to a local instance of Gitea timeout

123 views Asked by At

I am trying to migrate a private github repository to a local instance of gitea I've installed in a Docker container on a NAS in my home. I tried the built in migration but after running for about 5 minutes it failed on some obscure reference in a comment and left the only option to delete the repository and started again. So I manually created a Repository Called "alan/PAS".

I've been using a nodejs application to read github issues and milestones for years as part of a report I write about activity on the repository. I decided I would try and use that to migrate, writing my own nodejs script. I only wanted to migrate labels, milestones issues and comments. I ran the whole script with the writing to gitea part commented out and it worked perfectly, fetching all the information I need. However when it came to writing to gitea I couldn't make it work. I'll concentrate just on the labels section as that is where its initially failing.

Here is my code for that bit.

const labels = await readGithub(`labels`);
const labellist = new Map();
for(const label of labels) {
  console.log('label received', label);
  const l = await writeGitea('labels','POST',{color:'#'+label.color,description:label.description,name: label.name, exclusive: false});
  labellist.set(label.id, l.id)
}

Nothing too complicated I read from github a list of labels and the iterate over them one by one and attempt to post a slightly altered version to gitea. I add some info to a map to refer to it later.

The code in writeGitea in as follows

const options = {
  hostname: 'gitea.local',
  port: 3443,
  headers : {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    'Authorisation' : 'c9454acadb6b68c2897a9abfbef56d5ec3dfe433'
  }
}
async function writeGitea(url, method, body) {
  console.log('writeGitea to url',url, 'method', method, 'body', body)
  options.path = `/api/v1/repos/alan/PAS/${url}?token=c9454acadb6b68c2897a9abfbef56d5ec3dfe433`;
  options.method = method;
  return new Promise((resolve,reject) => {
    const request = https.request(options, (res) => {
      let body = '';
      res.on('data', (chunk) => {
        body += chunk
      });
      res.on('end', () => {
        console.log('Gitea Request Complete, Status Code', res.statusCode, 'URL', options.path, 'response body', body)
        if (res.statusCode > 299) {
          reject(res.statusCode);
        } else {
          resolve(body)
        }

      });
      res.on('error',() => reject(Error('Gitea Request Failed')));
    })
    request.write(JSON.stringify(body));
  });
}

Again standard nodejs use of https

When I run this, nothing happens, no chunks received (I did have console.log in there to check), until eventually the request times out and throws a "socket hang up" error.

HOWEVER, I had previously used the toolkit that comes with the repository to see what authorisations work. Only two did and I used one of them... and then tried to run the same request there https://gitea.local:3443/api/v1/repos/alan/PASv5/labels?token=c9454acadb6b68c2897a9abfbef56d5ec3dfe433 (the token will be changed so is I don't mind exposing it)

This worked and created the label correctly. Here is the curl request it used.

curl -X 'POST' \
  'https://gitea.local:3443/api/v1/repos/alan/PAS/labels?token=c9454acadb6b68c2897a9abfbef56d5ec3dfe433' \
  -H 'accept: application/json' \
  -H 'Authorization: c9454acadb6b68c2897a9abfbef56d5ec3dfe433' \
  -H 'Content-Type: application/json' \
  -d '{
  "color": "#38774E",
  "description": "",
  "exclusive": false,
  "name": "appointments"
}'

And here is the response (status 201)

{
  "id": 112,
  "name": "appointments",
  "exclusive": false,
  "color": "38774e",
  "description": "",
  "url": "https://192.168.0.190:3433/api/v1/repos/alan/PAS/labels/112"
}

I am at a total loss why curl works and my nodejs request doesn't. Has anyone any ideas?

1

There are 1 answers

0
akc42 On BEST ANSWER

Solved. I missed a request.end() off the end of the writeToGitea function.

I would like to give a shoutout to https://curlconverter.com/node-http/ which solved it for me.