Desarrollo y Mantenimiento de Sistemas Informáticos

4º. 1er cuatrimestre. Itinerario de Sistemas de la Información. Grado en Ingeniería Informática. ULL


Organization DMSI org   Github Classroom DMSI class   Campus Virtual DMSI campus   Profesor Casiano   Chat Chat

Clase del Lunes 2021/11/29

Práctica gh-cli-graphql

Estamos estudiando las extensiones en GitHub Cli:

Objetivos

  1. Intentaremos publicar la extensión para que sea utilizable por otros usuarios gh extension install ...
    1. Haremos un fork al espacio del usuario y renombraremos los ficheros
    2. Documentación y mejora de los mensajes
    3. Automatizaremos en lo posible las comprobaciones de error

      ➜  gh-cli-graphql-casiano-rodriguez-leon-alumnoudv5 git:(main) ✗ ./gh-repo-rename-aluXXXX -o ULL-ESIT-DMSI-1922 -r prueba-funciona  -n prueba
      gh: Could not resolve to a Repository with the name 'ULL-ESIT-DMSI-1922/prueba-funciona'.
      ➜  gh-cli-graphql-casiano-rodriguez-leon-alumnoudv5 git:(main) ✗ ./gh-repo-rename-aluXXXX -o ULL-ESIT-DMSI-1920 -r prueba  -n prueba-antonella
      gh: Name already exists on this account
      
    4. Publicaremos la extensión
  2. Publicaremos un módulo npm con el código js y el ejecutable en el ámbito del alumno
  3. Modificaremos la extensión para que use el módulo npm

gh-repo-rename-aluXXXX.js

#! /usr/bin/env node
const ins = require("util").inspect;

const shell = require('shelljs');
const { Command } = require('commander');
const program = new Command();
const { version } = require("./package.json")

program
  .version(version)
  .option('-r, --repo <repo>', 'repository')
  .option('-o, --org <org>', 'org')
  .option('-n, --name <name>', 'name');

program.parse(process.argv);

let args = program.args;
debugger;

const getRepoId = (owner, name) => `
query {
  repository(owner: "${owner}", name: "${name}") {
    id
  }
}
`;

const renameRepo = (id, newName) => `
mutation {
  updateRepository(input: {name: "${newName}", repositoryId: "${id}"}) {
    repository {
      name
    }
  }
}
`;

let { org, repo, name } = program.opts();

if (!org || ! repo || !name) program.help();

if (!shell.which('git')) shell.echo("git not installed")
if (!shell.which('gh')) shell.echo("gh not installed");

let r = shell.exec(`gh api  graphql -f query='${getRepoId(org, repo)}' --jq '.data.repository.id'`, 
                   {silent: true});
if (r.code !== 0) {
  console.error(r.stderr);
  process.exit(r.code);
}

const Id = r.stdout;

//  stdout: '{"data":{"updateRepository":{"repository":{"name":"prueba"}}}}'
r  = shell.exec(
  `gh api graphql -f query='${renameRepo(Id, name)}'  --jq '.data.updateRepository.repository.name'`,
  {silent:true})

if (r.code !== 0) {
    console.error(r.stderr);
    process.exit(r.code);
}

console.log(r.stdout)
➜  gh-cli-graphql-casiano-rodriguez-leon-alumnoudv5 git:(main) ✗ ./gh-repo-rename-aluXXXX -o ULL-ESIT-DMSI-1922 -r prueba-funciona  -n prueba
➜  gh-cli-graphql-casiano-rodriguez-leon-alumnoudv5 git:(main) ✗ ./gh-repo-rename-aluXXXX -o ULL-ESIT-DMSI-1922 -r prueba-funciona  -n prueba
gh: Could not resolve to a Repository with the name 'ULL-ESIT-DMSI-1922/prueba-funciona'.
➜  gh-cli-graphql-casiano-rodriguez-leon-alumnoudv5 git:(main) ✗ ./gh-repo-rename-aluXXXX -o ULL-ESIT-DMSI-1920 -r prueba  -n prueba-antonella
gh: Name already exists on this account

Package.json

{
  "dependencies": {
    "comander": "^0.0.1-security",
    "commander": "^8.3.0",
    "shelljs": "^0.8.4"
  },
  "name": "gh-repo-rename-aluXXXX",
  "description": "",
  "version": "1.1.0",
  "main": "gh-repo-rename-aluXXXX.js",
  "scripts": {
    "debug": "node --inspect-brk gh-repo-rename-aluXXXX.js -o ULL-ESIT-DMSI-1920 -r prueba-funciona  -n prueba",
    "create-prueba": "gh create-repo prueba ULL-ESIT-DMSI-1920",
    "list-prueba": "gh submodule-add -s prueba -o ULL-ESIT-DMSI-1920 -n",
    "test": "./gh-repo-rename-aluXXXX -o ULL-ESIT-DMSI-1920 -r prueba  -n prueba-funciona; npm run list-prueba",
    "back": "./gh-repo-rename-aluXXXX -o ULL-ESIT-DMSI-1920 -r prueba-funciona  -n prueba; npm run list-prueba",
    "start": "node gh-repo-rename-aluXXXX.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/aluXXXX/gh-repo-rename-aluXXXX.git"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/aluXXXX/gh-repo-rename-aluXXXX/issues"
  },
  "homepage": "https://github.com/aluXXXX/gh-repo-rename-aluXXXX#readme"
}

bash script

#!/usr/bin/env bash

# Determine if an executable is in the PATH
if ! type -p node >/dev/null; then
   echo "Node not found on the system. Can't work" >&2
   exit 1
fi

# ${BASH_SOURCE[0]} (or, more simply, $BASH_SOURCE[1] ) contains the (potentially relative) path of the containing 
# script in all invocation scenarios, notably also when the script is sourced, which is not true for $0.
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

node ${SCRIPT_DIR}/gh-repo-rename-aluXXXX.js "$@"

Entendiendo el script:

#SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
SCRIPT_DIR="$( dirname -- "${BASH_SOURCE[0]}" )"
SCRIPT_DIR="$( dirname -- "${BASH_SOURCE[0]}" )"
WITH0="$( dirname -- "$0")"
echo $WITH0 $SCRIPT_DIR

Ejecuciones:

[~/campus-virtual/1920/dmsi1920/apuntes(master)]$ bash /tmp/bash_source 
/tmp/bash_source /tmp
[~/campus-virtual/1920/dmsi1920/apuntes(master)]$ bash /tmp/bash_source 
/tmp /tmp
[~/campus-virtual/1920/dmsi1920/apuntes(master)]$ . /tmp/bash_source 
. /tmp

GitHub GraphQL Explorer

Ejemplos

Referencias