<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jonathan Wilson</title>
    <description>The latest articles on DEV Community by Jonathan Wilson (@johnyjwilson).</description>
    <link>https://dev.to/johnyjwilson</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F92204%2Fdc859b09-5f11-4647-983b-66504c36af31.jpg</url>
      <title>DEV Community: Jonathan Wilson</title>
      <link>https://dev.to/johnyjwilson</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/johnyjwilson"/>
    <language>en</language>
    <item>
      <title>Version bump your Xcode project with Github's API</title>
      <dc:creator>Jonathan Wilson</dc:creator>
      <pubDate>Thu, 07 Nov 2019 21:28:22 +0000</pubDate>
      <link>https://dev.to/johnyjwilson/version-bump-your-xcode-project-with-github-s-api-gif</link>
      <guid>https://dev.to/johnyjwilson/version-bump-your-xcode-project-with-github-s-api-gif</guid>
      <description>&lt;p&gt;This post is going to describe the process of updating the version&lt;br&gt;
numbers of your Xcode project using the &lt;a href="https://developer.github.com/v3/"&gt;Github Rest API&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this approach I am creating an automated pull request (PR) with the changes. This guide expects the project to be utilising the plist file&lt;br&gt;
to maintain the version and build numbers.&lt;/p&gt;

&lt;p&gt;There are five steps to complete this task:&lt;br&gt;
 • Get the &lt;a href="https://help.github.com/en/github/getting-started-with-github/github-glossary#commit"&gt;SHA&lt;/a&gt; for the head of the branch you would like to apply the changes&lt;br&gt;
 • Create a new branch from that SHA&lt;br&gt;
 • Retrieve the current contents of the plist file&lt;br&gt;
 • Update that file with the new version strings&lt;br&gt;
 • Create a PR with these changes&lt;/p&gt;

&lt;p&gt;I am not going to cover the authentication with Github, it is very well &lt;a href="https://developer.github.com/v3/#authentication"&gt;documented&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 1: Get the SHA identifier for a branch
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://developer.github.com/v3/git/refs/#get-a-reference"&gt;API Reference&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Perform a get request to:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;https://api.github.com/repos/:owner/:repo/git/refs/heads/:branchName&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Substituting variables for your project. Then pull out the SHA variable from the JSON decoded response:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;json.object.sha&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is basically just a reference to the last commit made in the branch you selected. We can let the API know that our new branch should originate from here.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 2: Create a new branch to contain the updates
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://developer.github.com/v3/git/refs/#create-a-reference"&gt;API Reference&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Post a JSON object containing the name of the new branch in the format below, and supply the SHA retrieved in Step 1. The format of the &lt;code&gt;ref&lt;/code&gt; content is important, if it doesn't start with &lt;code&gt;refs&lt;/code&gt; and have at least two slashes, it will be rejected.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;body: {
  ref: "refs/heads/" + branchName,
  sha: sourceSha
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The endpoint in this case is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;https://api.github.com/repos/:owner/:repo/git/refs/&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Get the original contents of the Plist file
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://developer.github.com/v3/repos/contents/#get-contents"&gt;API Reference&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We need to get the original contents of the plist so that we can apply the specific changes.&lt;/p&gt;

&lt;p&gt;Get the contents from this URL: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;https://api.github.com/repos/:owner/:repo/contents/:pathToPlist&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;pathToPlist&lt;/code&gt; parameter is specific to your project it can be found on the Github website. In my case it was:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;https://api.github.com/repos/:owner/:repo/contents/SupportingFiles/Project-Info.plist&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You may need to pass in a parameter if you need to get the contents from a branch other than your main one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ref: "develop"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The contents of the response are &lt;code&gt;base64&lt;/code&gt; encoded, most languages have a standard function for decrypting this. In my case I am using Javascript and it looked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Buffer.from(json.content, "base64").toString()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You also need to retrieve the blob SHA that is returned here, my understanding is this is basically just a hash of the file used for integrity checks rather than the one used earlier as a reference to a commit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Apply the changes to the plist file and Put to API
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://developer.github.com/v3/repos/contents/#create-or-update-a-file"&gt;API Reference&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First of all we need to update the file contents, this is not going to be the best function you have seen but it gets the job done (Also written in Javascript... by a Swift dev):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const updatePlistContentVersions = function(content, shortVersionString) {
  const splitContent = content.split(/\n/);

  splitContent.forEach(function(value, i) {
    if (value === "\t&amp;lt;key&amp;gt;CFBundleShortVersionString&amp;lt;/key&amp;gt;") {
      splitContent[i + 1] = "\t&amp;lt;string&amp;gt;" + shortVersionString + "&amp;lt;/string&amp;gt;";
    }

    if (value === "\t&amp;lt;key&amp;gt;CFBundleVersion&amp;lt;/key&amp;gt;") {
      splitContent[i + 1] =
        "\t&amp;lt;string&amp;gt;" + shortVersionString + ".$(BUILD_NUMBER)&amp;lt;/string&amp;gt;";
    }
  });

  return splitContent.join("\n");
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The structure of a plist file is described &lt;a href="https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/AboutInformationPropertyListFiles.html"&gt;here&lt;/a&gt; It is XML based and there are plenty of guides on parsing it, In my case I went for the simple approach of splitting by new lines, then looking for the keys I wanted to change.&lt;/p&gt;

&lt;p&gt;There are two keys we are updating here:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;CFBundleShortVersionString&lt;/code&gt;: Which is the main version indicator.&lt;br&gt;
&lt;code&gt;CFBundleVersion&lt;/code&gt;: Describes the build of a given version.&lt;/p&gt;

&lt;p&gt;After finding them I literally just replace the entire contents of the next line with a new one containing the updated values. Following that join the array of lines back into a single string.&lt;/p&gt;

&lt;p&gt;Finally you will need to re-encode the string back into &lt;code&gt;base64&lt;/code&gt;. In Javascript it looks like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Buffer.from(updatedPlistContent).toString("base64")&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To apply the changes to the branch created earlier we will create a Put request to API URL of the plist, which is the same as Step 3. The body of this request should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;body: {
  content: updatedPlistContent,
  sha: blobSha,
  branch: branchName,
  message: "Update version to " + shortVersionString
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;• &lt;code&gt;content&lt;/code&gt; Which is the &lt;code&gt;base64&lt;/code&gt; encoded version of the contents retrieved earlier containing our amendments.&lt;br&gt;
• &lt;code&gt;sha&lt;/code&gt; which is the blob SHA from Step 3.&lt;br&gt;
• &lt;code&gt;branch&lt;/code&gt; The branch created in Step 2, which is where these changes will be applied.&lt;br&gt;
• &lt;code&gt;message&lt;/code&gt; The commit message for these changes. This is basically just a simple commit but through an API.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 5: Create a Pull Request with all the changes
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://developer.github.com/v3/pulls/#create-a-pull-request"&gt;API Reference&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another Post request, now to:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;https://api.github.com/repos/:owner/:repo/pulls&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The contents of the body of this request is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;body: {
  title: title,
  body: description,
  head: sourceBranch,
  base: baseBranch
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;• &lt;code&gt;title&lt;/code&gt; The headline of the PR, best to give a clear description here, maybe even call out a bot created it.&lt;br&gt;
• &lt;code&gt;body&lt;/code&gt; The description of the PR.&lt;br&gt;
• &lt;code&gt;head&lt;/code&gt; The branch the changes are coming from, in our case its Step 2.&lt;br&gt;
• &lt;code&gt;base&lt;/code&gt; The destination of these changes, probably your main development branch.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;We have created a script that will apply a small change to a Github project. The use cases for this are pretty vast. In my own case I attached them to a single Slack bot command: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;@Bot Update the version number to x.x.x&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This command is actualy apart of a set of similar scripts that enable the automation of a release. The Github API is huge and very well documented, you can do anything you can think of so I encourage you to have a look. &lt;/p&gt;

&lt;p&gt;Have a think about automating: tagging a release, creating branches, and highlighting changes in branches. Slack is a great way to&lt;br&gt;
externalise them to the greater team too. Let the product team do all the releasing etc... if you dare.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>ci</category>
      <category>git</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
