Home > Mobile >  Automate Pull Request in Git
Automate Pull Request in Git

Time:03-21

I have a small script which makes some changes to certain files and push those changes. However after the script runs I get the PR url and I have to manually submit it from the browser. How to automate it so that even the pull request is created automatically.

#!/bin/bash

git clone -b dev [email protected]:userabc/git-automation.git
cd git-automation/

git pull [email protected]:userabc/git-automation.git
git branch release
git checkout release

sleep 5

sed -i -e '/UI:/{n;n;s/\(imageTag\).*/\1: '"$1"' /}' \
       -e '/DB:/{n;n;s/\(imageTag\).*/\1: '"$2"' /}' \
       test.yaml

sleep 10

git add test.yaml

git commit -m "updated tags"

sleep 5

git push [email protected]:userabc/git-automation release

I tried using request-pull by having the below command after git push, but it seems to be not working or justify the automation of creating PR without having to open the url from browser and clicking on submit button

git request-pull dev https://github.com/userabc/git-automation release

CodePudding user response:

To create a pull request, you can use gh pr create, which

Example:

gh pr create --title "The bug is fixed" --body "Everything works again"

Your script will need to authenticate itself first (using gh auth login)

CodePudding user response:

You can use github API for this.

First, create a new personal access token from https://github.com/settings/tokens

Then use curl to call github's API to create new pull request:

curl \
  -u "userabc:{YOUR_PERSONAL_ACCESS_TOKEN}" \
  -X POST \
  -H "Accept: application/vnd.github.v3 json" \
  https://api.github.com/repos/userabc/git-automation/pulls \
  -d '{"head":"YOUR_BRANCH","base":"master","title":"This is an automated PR"}'

Docs:

Result (I replaced my name and repo to match yours):

{
  "url": "https://api.github.com/repos/userabc/git-automation/pulls/1",
  "id": 884514144,
  "node_id": "PR_kwDOHCWx0s40uJ1g",
  "html_url": "https://github.com/userabc/git-automation/pull/1",
  "diff_url": "https://github.com/userabc/git-automation/pull/1.diff",
  "patch_url": "https://github.com/userabc/git-automation/pull/1.patch",
  "issue_url": "https://api.github.com/repos/userabc/git-automation/issues/1",
  "number": 1,
  "state": "open",
  "locked": false,
  "title": "hello",
  "user": {
    "login": "userabc",
    "id": 18624029,
    "node_id": "MDQ6VXNlcjE4NjI0MDI5",
    "avatar_url": "https://avatars.githubusercontent.com/u/18624029?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/userabc",
    "html_url": "https://github.com/userabc",
    "followers_url": "https://api.github.com/users/userabc/followers",
    "following_url": "https://api.github.com/users/userabc/following{/other_user}",
    "gists_url": "https://api.github.com/users/userabc/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/userabc/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/userabc/subscriptions",
    "organizations_url": "https://api.github.com/users/userabc/orgs",
    "repos_url": "https://api.github.com/users/userabc/repos",
    "events_url": "https://api.github.com/users/userabc/events{/privacy}",
    "received_events_url": "https://api.github.com/users/userabc/received_events",
    "type": "User",
    "site_admin": false
  },
  "body": null,
  "created_at": "2022-03-21T07:57:15Z",
  "updated_at": "2022-03-21T07:57:15Z",
  "closed_at": null,
  "merged_at": null,
  "merge_commit_sha": null,
  "assignee": null,
  "assignees": [

  ],
  "requested_reviewers": [

  ],
  "requested_teams": [

  ],
  "labels": [

  ],
  "milestone": null,
  "draft": false,
  "commits_url": "https://api.github.com/repos/userabc/git-automation/pulls/1/commits",
  "review_comments_url": "https://api.github.com/repos/userabc/git-automation/pulls/1/comments",
  "review_comment_url": "https://api.github.com/repos/userabc/git-automation/pulls/comments{/number}",
  "comments_url": "https://api.github.com/repos/userabc/git-automation/issues/1/comments",
  "statuses_url": "https://api.github.com/repos/userabc/git-automation/statuses/70b778341b5e158193a2536ff7634314f8a3dc6f",
  "head": {
    "label": "userabc:e",
    "ref": "e",
    "sha": "70b778341b5e158193a2536ff7634314f8a3dc6f",
    "user": {
      "login": "userabc",
      "id": 18624029,
      "node_id": "MDQ6VXNlcjE4NjI0MDI5",
      "avatar_url": "https://avatars.githubusercontent.com/u/18624029?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/userabc",
      "html_url": "https://github.com/userabc",
      "followers_url": "https://api.github.com/users/userabc/followers",
      "following_url": "https://api.github.com/users/userabc/following{/other_user}",
      "gists_url": "https://api.github.com/users/userabc/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/userabc/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/userabc/subscriptions",
      "organizations_url": "https://api.github.com/users/userabc/orgs",
      "repos_url": "https://api.github.com/users/userabc/repos",
      "events_url": "https://api.github.com/users/userabc/events{/privacy}",
      "received_events_url": "https://api.github.com/users/userabc/received_events",
      "type": "User",
      "site_admin": false
    },
    "repo": {
      "id": 472232402,
      "node_id": "R_kgDOHCWx0g",
      "name": "git-automation",
      "full_name": "userabc/git-automation",
      "private": true,
      "owner": {
        "login": "userabc",
        "id": 18624029,
        "node_id": "MDQ6VXNlcjE4NjI0MDI5",
        "avatar_url": "https://avatars.githubusercontent.com/u/18624029?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/userabc",
        "html_url": "https://github.com/userabc",
        "followers_url": "https://api.github.com/users/userabc/followers",
        "following_url": "https://api.github.com/users/userabc/following{/other_user}",
        "gists_url": "https://api.github.com/users/userabc/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/userabc/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/userabc/subscriptions",
        "organizations_url": "https://api.github.com/users/userabc/orgs",
        "repos_url": "https://api.github.com/users/userabc/repos",
        "events_url": "https://api.github.com/users/userabc/events{/privacy}",
        "received_events_url": "https://api.github.com/users/userabc/received_events",
        "type": "User",
        "site_admin": false
      },
      "html_url": "https://github.com/userabc/git-automation",
      "description": null,
      "fork": false,
      "url": "https://api.github.com/repos/userabc/git-automation",
      "forks_url": "https://api.github.com/repos/userabc/git-automation/forks",
      "keys_url": "https://api.github.com/repos/userabc/git-automation/keys{/key_id}",
      "collaborators_url": "https://api.github.com/repos/userabc/git-automation/collaborators{/collaborator}",
      "teams_url": "https://api.github.com/repos/userabc/git-automation/teams",
      "hooks_url": "https://api.github.com/repos/userabc/git-automation/hooks",
      "issue_events_url": "https://api.github.com/repos/userabc/git-automation/issues/events{/number}",
      "events_url": "https://api.github.com/repos/userabc/git-automation/events",
      "assignees_url": "https://api.github.com/repos/userabc/git-automation/assignees{/user}",
      "branches_url": "https://api.github.com/repos/userabc/git-automation/branches{/branch}",
      "tags_url": "https://api.github.com/repos/userabc/git-automation/tags",
      "blobs_url": "https://api.github.com/repos/userabc/git-automation/git/blobs{/sha}",
      "git_tags_url": "https://api.github.com/repos/userabc/git-automation/git/tags{/sha}",
      "git_refs_url": "https://api.github.com/repos/userabc/git-automation/git/refs{/sha}",
      "trees_url": "https://api.github.com/repos/userabc/git-automation/git/trees{/sha}",
      "statuses_url": "https://api.github.com/repos/userabc/git-automation/statuses/{sha}",
      "languages_url": "https://api.github.com/repos/userabc/git-automation/languages",
      "stargazers_url": "https://api.github.com/repos/userabc/git-automation/stargazers",
      "contributors_url": "https://api.github.com/repos/userabc/git-automation/contributors",
      "subscribers_url": "https://api.github.com/repos/userabc/git-automation/subscribers",
      "subscription_url": "https://api.github.com/repos/userabc/git-automation/subscription",
      "commits_url": "https://api.github.com/repos/userabc/git-automation/commits{/sha}",
      "git_commits_url": "https://api.github.com/repos/userabc/git-automation/git/commits{/sha}",
      "comments_url": "https://api.github.com/repos/userabc/git-automation/comments{/number}",
      "issue_comment_url": "https://api.github.com/repos/userabc/git-automation/issues/comments{/number}",
      "contents_url": "https://api.github.com/repos/userabc/git-automation/contents/{ path}",
      "compare_url": "https://api.github.com/repos/userabc/git-automation/compare/{base}...{head}",
      "merges_url": "https://api.github.com/repos/userabc/git-automation/merges",
      "archive_url": "https://api.github.com/repos/userabc/git-automation/{archive_format}{/ref}",
      "downloads_url": "https://api.github.com/repos/userabc/git-automation/downloads",
      "issues_url": "https://api.github.com/repos/userabc/git-automation/issues{/number}",
      "pulls_url": "https://api.github.com/repos/userabc/git-automation/pulls{/number}",
      "milestones_url": "https://api.github.com/repos/userabc/git-automation/milestones{/number}",
      "notifications_url": "https://api.github.com/repos/userabc/git-automation/notifications{?since,all,participating}",
      "labels_url": "https://api.github.com/repos/userabc/git-automation/labels{/name}",
      "releases_url": "https://api.github.com/repos/userabc/git-automation/releases{/id}",
      "deployments_url": "https://api.github.com/repos/userabc/git-automation/deployments",
      "created_at": "2022-03-21T07:39:41Z",
      "updated_at": "2022-03-21T07:39:41Z",
      "pushed_at": "2022-03-21T07:47:06Z",
      "git_url": "git://github.com/userabc/git-automation.git",
      "ssh_url": "[email protected]:userabc/git-automation.git",
      "clone_url": "https://github.com/userabc/git-automation.git",
      "svn_url": "https://github.com/userabc/git-automation",
      "homepage": null,
      "size": 0,
      "stargazers_count": 0,
      "watchers_count": 0,
      "language": null,
      "has_issues": true,
      "has_projects": true,
      "has_downloads": true,
      "has_wiki": true,
      "has_pages": false,
      "forks_count": 0,
      "mirror_url": null,
      "archived": false,
      "disabled": false,
      "open_issues_count": 1,
      "license": null,
      "allow_forking": true,
      "is_template": false,
      "topics": [

      ],
      "visibility": "private",
      "forks": 0,
      "open_issues": 1,
      "watchers": 0,
      "default_branch": "master"
    }
  },
  "base": {
    "label": "userabc:master",
    "ref": "master",
    "sha": "d7579f8d65e0524c7d11b6b093914a7c4bba6c92",
    "user": {
      "login": "userabc",
      "id": 18624029,
      "node_id": "MDQ6VXNlcjE4NjI0MDI5",
      "avatar_url": "https://avatars.githubusercontent.com/u/18624029?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/userabc",
      "html_url": "https://github.com/userabc",
      "followers_url": "https://api.github.com/users/userabc/followers",
      "following_url": "https://api.github.com/users/userabc/following{/other_user}",
      "gists_url": "https://api.github.com/users/userabc/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/userabc/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/userabc/subscriptions",
      "organizations_url": "https://api.github.com/users/userabc/orgs",
      "repos_url": "https://api.github.com/users/userabc/repos",
      "events_url": "https://api.github.com/users/userabc/events{/privacy}",
      "received_events_url": "https://api.github.com/users/userabc/received_events",
      "type": "User",
      "site_admin": false
    },
    "repo": {
      "id": 472232402,
      "node_id": "R_kgDOHCWx0g",
      "name": "git-automation",
      "full_name": "userabc/git-automation",
      "private": true,
      "owner": {
        "login": "userabc",
        "id": 18624029,
        "node_id": "MDQ6VXNlcjE4NjI0MDI5",
        "avatar_url": "https://avatars.githubusercontent.com/u/18624029?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/userabc",
        "html_url": "https://github.com/userabc",
        "followers_url": "https://api.github.com/users/userabc/followers",
        "following_url": "https://api.github.com/users/userabc/following{/other_user}",
        "gists_url": "https://api.github.com/users/userabc/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/userabc/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/userabc/subscriptions",
        "organizations_url": "https://api.github.com/users/userabc/orgs",
        "repos_url": "https://api.github.com/users/userabc/repos",
        "events_url": "https://api.github.com/users/userabc/events{/privacy}",
        "received_events_url": "https://api.github.com/users/userabc/received_events",
        "type": "User",
        "site_admin": false
      },
      "html_url": "https://github.com/userabc/git-automation",
      "description": null,
      "fork": false,
      "url": "https://api.github.com/repos/userabc/git-automation",
      "forks_url": "https://api.github.com/repos/userabc/git-automation/forks",
      "keys_url": "https://api.github.com/repos/userabc/git-automation/keys{/key_id}",
      "collaborators_url": "https://api.github.com/repos/userabc/git-automation/collaborators{/collaborator}",
      "teams_url": "https://api.github.com/repos/userabc/git-automation/teams",
      "hooks_url": "https://api.github.com/repos/userabc/git-automation/hooks",
      "issue_events_url": "https://api.github.com/repos/userabc/git-automation/issues/events{/number}",
      "events_url": "https://api.github.com/repos/userabc/git-automation/events",
      "assignees_url": "https://api.github.com/repos/userabc/git-automation/assignees{/user}",
      "branches_url": "https://api.github.com/repos/userabc/git-automation/branches{/branch}",
      "tags_url": "https://api.github.com/repos/userabc/git-automation/tags",
      "blobs_url": "https://api.github.com/repos/userabc/git-automation/git/blobs{/sha}",
      "git_tags_url": "https://api.github.com/repos/userabc/git-automation/git/tags{/sha}",
      "git_refs_url": "https://api.github.com/repos/userabc/git-automation/git/refs{/sha}",
      "trees_url": "https://api.github.com/repos/userabc/git-automation/git/trees{/sha}",
      "statuses_url": "https://api.github.com/repos/userabc/git-automation/statuses/{sha}",
      "languages_url": "https://api.github.com/repos/userabc/git-automation/languages",
      "stargazers_url": "https://api.github.com/repos/userabc/git-automation/stargazers",
      "contributors_url": "https://api.github.com/repos/userabc/git-automation/contributors",
      "subscribers_url": "https://api.github.com/repos/userabc/git-automation/subscribers",
      "subscription_url": "https://api.github.com/repos/userabc/git-automation/subscription",
      "commits_url": "https://api.github.com/repos/userabc/git-automation/commits{/sha}",
      "git_commits_url": "https://api.github.com/repos/userabc/git-automation/git/commits{/sha}",
      "comments_url": "https://api.github.com/repos/userabc/git-automation/comments{/number}",
      "issue_comment_url": "https://api.github.com/repos/userabc/git-automation/issues/comments{/number}",
      "contents_url": "https://api.github.com/repos/userabc/git-automation/contents/{ path}",
      "compare_url": "https://api.github.com/repos/userabc/git-automation/compare/{base}...{head}",
      "merges_url": "https://api.github.com/repos/userabc/git-automation/merges",
      "archive_url": "https://api.github.com/repos/userabc/git-automation/{archive_format}{/ref}",
      "downloads_url": "https://api.github.com/repos/userabc/git-automation/downloads",
      "issues_url": "https://api.github.com/repos/userabc/git-automation/issues{/number}",
      "pulls_url": "https://api.github.com/repos/userabc/git-automation/pulls{/number}",
      "milestones_url": "https://api.github.com/repos/userabc/git-automation/milestones{/number}",
      "notifications_url": "https://api.github.com/repos/userabc/git-automation/notifications{?since,all,participating}",
      "labels_url": "https://api.github.com/repos/userabc/git-automation/labels{/name}",
      "releases_url": "https://api.github.com/repos/userabc/git-automation/releases{/id}",
      "deployments_url": "https://api.github.com/repos/userabc/git-automation/deployments",
      "created_at": "2022-03-21T07:39:41Z",
      "updated_at": "2022-03-21T07:39:41Z",
      "pushed_at": "2022-03-21T07:47:06Z",
      "git_url": "git://github.com/userabc/git-automation.git",
      "ssh_url": "[email protected]:userabc/git-automation.git",
      "clone_url": "https://github.com/userabc/git-automation.git",
      "svn_url": "https://github.com/userabc/git-automation",
      "homepage": null,
      "size": 0,
      "stargazers_count": 0,
      "watchers_count": 0,
      "language": null,
      "has_issues": true,
      "has_projects": true,
      "has_downloads": true,
      "has_wiki": true,
      "has_pages": false,
      "forks_count": 0,
      "mirror_url": null,
      "archived": false,
      "disabled": false,
      "open_issues_count": 1,
      "license": null,
      "allow_forking": true,
      "is_template": false,
      "topics": [

      ],
      "visibility": "private",
      "forks": 0,
      "open_issues": 1,
      "watchers": 0,
      "default_branch": "master"
    }
  },
  "_links": {
    "self": {
      "href": "https://api.github.com/repos/userabc/git-automation/pulls/1"
    },
    "html": {
      "href": "https://github.com/userabc/git-automation/pull/1"
    },
    "issue": {
      "href": "https://api.github.com/repos/userabc/git-automation/issues/1"
    },
    "comments": {
      "href": "https://api.github.com/repos/userabc/git-automation/issues/1/comments"
    },
    "review_comments": {
      "href": "https://api.github.com/repos/userabc/git-automation/pulls/1/comments"
    },
    "review_comment": {
      "href": "https://api.github.com/repos/userabc/git-automation/pulls/comments{/number}"
    },
    "commits": {
      "href": "https://api.github.com/repos/userabc/git-automation/pulls/1/commits"
    },
    "statuses": {
      "href": "https://api.github.com/repos/userabc/git-automation/statuses/70b778341b5e158193a2536ff7634314f8a3dc6f"
    }
  },
  "author_association": "OWNER",
  "auto_merge": null,
  "active_lock_reason": null,
  "merged": false,
  "mergeable": null,
  "rebaseable": null,
  "mergeable_state": "unknown",
  "merged_by": null,
  "comments": 0,
  "review_comments": 0,
  "maintainer_can_modify": false,
  "commits": 2,
  "additions": 0,
  "deletions": 0,
  "changed_files": 2
}
  • Related