Skip to content

Instantly share code, notes, and snippets.

@codermarcos
Last active September 22, 2023 14:31
Show Gist options
  • Save codermarcos/3f2faaeb5a3b383705a376a9f62e8f22 to your computer and use it in GitHub Desktop.
Save codermarcos/3f2faaeb5a3b383705a376a9f62e8f22 to your computer and use it in GitHub Desktop.
Bookmark run Javascript

Pull request template to use as bookmark!

This bookmark run a javascript to apply a simple template at textbox in Github!

If you need use to Gitlab or Bitbucket you need change the css selector!

Motivation: I don't recommend use this if you own the repository because you could create the template directly on Github.

How to use

  1. Create a new book mark. To this example i will use chrome.

    1. Access chrome://bookmarks/
    2. Right click and click on Add new bookmark
    3. Choose a good name i recommend Pull request template

If you need create to other browser you can read: firefox, edge

  1. To change the textbox use this function or if you want a complex template you can create ownself function:
function() {
    document.querySelector("#pull_request_body").value = "# πŸ“ Description";
}
  1. To use it you need use a snnipet to allow execute Javascript at URL
javascript:(YOUR FUNCTION CODE HERE)();

Example: javascript:(function() { alert('OlΓ‘') })();

If you want use my template you can copy here:

(function(){const[url]=location.href.split('?');const[,branch]=url.split(location.href.includes('...')?'...':'compare/');const[,unformatedID]=branch.split('/');const id=(unformatedID??'').toUpperCase();const commits = document.querySelectorAll('li.js-commits-list-item > .js-details-container > .Details-content--hidden > pre');const changes = new Set();for (let i = 0; i < commits.length;i++) {const[,change]=commits?.item(i)?.textContent?.split('Changes to be committed:\n\t')??[];if (change)changes.add(...change.split('\n\t'));}document.querySelector('#pull_request_body').value = ['# πŸ“ Description','','<DESCRICAO AQUI>','','## πŸ”Ž Evidence that this works','','<UPLOAD IMAGES>','','## πŸ§ͺ How to validate','','Run the application locally:','','- [ ] Should something','','','## 🏷️ Related tasks','','- '+id,'','','## πŸ”ƒ Pull request related','','- #000','','','### πŸ—ƒοΈ Changes','','On branch:','','> **' + branch + '**','','','Changes to be committed:','','> '+[...changes].sort().join('\n')].join('\n')})()

This function works getting all file changes and the branch (should be feat/ID/name, or anything/ID/name) name to complete task id as can you see below:

function() {
      const[url] = location.href.split('?');
      const[,branch] = url.split(location.href.includes('...') ? '...' : 'compare/');
      const[,unformatedID] = branch.split('/');
      const id = (unformatedID ?? '').toUpperCase();
      const commits = document.querySelectorAll('li.js-commits-list-item > .js-details-container > .Details-content--hidden > pre');
      const changes = new Set();
      for (let i = 0; i < commits.length;i++) {
            const[,change] = commits?.item(i)?.textContent?.split('Changes to be committed:\n\t') ?? [];
            if (change) changes.add(...change.split('\n\t'));
      }
      document.querySelector('#pull_request_body').value = [
            '# πŸ“ Description',
            '',
            '<DESCRICAO AQUI>',
            '',
            '## πŸ”Ž Evidence that this works',
            '',
            '<UPLOAD IMAGES>',
            '',
            '## πŸ§ͺ How to validate',
            '',
            'Run the application locally:',
            '',
            '- [ ] Should something',
            '',
            '',
            '## 🏷️ Related tasks',
            '',
            '- '+id,
            '',
            '',
            '## πŸ”ƒ Pull request related',
            '',
            '- #000',
            '',
            '',
            '### πŸ—ƒοΈ Changes',
            '',
            'On branch:',
            '',
            '> **' + branch + '**',
            '',
            '',
            'Changes to be committed:',
            '',
            '> '+[...changes].sort().join('\n')
      ].join('\n')
}
  1. Add the copied code to Bookmark URL

⚠️ Attention: The function code needs be encoded to use in URI. You can use a online encoder or use this Javacript function BEFORE add to Bookmark URL.

function(){
    const [url]=location.href.split('?');
    const [,branch]=url.split(location.href.includes('...') ? '...' : 'compare/');
    const [,unformatedID]=branch.split('/');
    const id=unformatedID.toUpperCase();
    const commits=document.querySelectorAll('li.js-commits-list-item > .js-details-container > .Details-content--hidden > pre');
    const changes = new Set();
    for(let i=0;i<commits.length;i++){
        const [,change]=commits.item(i).textContent.split('Changes to be committed:\n\t');
        if(change)changes.add(...change.split('\n\t'));
    }
    document.querySelector('#pull_request_body').value=[
        '# πŸ“ Description',
        '',
        '<DESCRICAO AQUI>',
        '',
        '## πŸ”Ž Evidence that this works',
        '',
        '## πŸ§ͺ How to validate',
        '',
        'Run the application using [vercel preview](https://dashboard-git-'+ branch.replace('/', '-').replace('/', '') +'-arena-im.vercel.app):',
        '',
        '- [ ] Should something',
        '',
        '',
        '## 🏷️ Related tasks',
        '',
        '- ['+id+'- <TITULO DA TASK AQUI>](https://arenaim.atlassian.net/browse/'+id+')',
        '',
        '',
        '## πŸ”ƒ Pull request related',
        '',
        '- #000',
        '',
        '',
        '### πŸ’ͺ Authored by',
        '', 
        '- @codermarcos',
        '',
        '',
        '### πŸ—ƒοΈ Changes',
        '',
        'On branch:',
        '',
        '> **'+branch+'**',
        '',
        '',
        'Changes to be committed:',
        '',
        '> '+[...changes].sort().join('\n ')
    ].join('\n');
}
javascript:(function(){const [url] = location.href.split('?'); const [, branch] = url.split(location.href.includes('...') ? '...' : 'compare/'); const [, unformatedID] = branch.split('/'); const id = unformatedID.toUpperCase(); const commits = document.querySelectorAll('li.js-commits-list-item > .js-details-container > .Details-content--hidden > pre'); const changes = new Set(); for (let i = 0; i < commits.length; i++) { const [, change] = commits.item(i).textContent.split('Changes to be committed:\n\t'); if (change) changes.add(...change.split('\n\t')); } document.querySelector('#pull_request_body').value = ['# πŸ“ Description', '', '<DESCRICAO AQUI>', '', '## πŸ”Ž Evidence that this works', '', '## πŸ§ͺ How to validate', '', 'Run the application using [vercel preview](https://dashboard-git-' + branch.replace('/', '-').replace('/', '') + '-arena-im.vercel.app):', '', '- [ ] Should something', '', '', '## 🏷️ Related tasks', '', '- [' + id + '- <TITULO DA TASK AQUI>](https://arenaim.atlassian.net/browse/' + id + ')', '', '', '## πŸ”ƒ Pull request related', '', '- #000', '', '', '### πŸ’ͺ Authored by', '', '- @codermarcos', '', '', '### πŸ—ƒοΈ Changes', '', 'On branch:', '', '> **' + branch + '**', '', '', 'Changes to be committed:', '', '> ' + [...changes].sort()?.join('\n ')].join('\n');})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment